stubs.c 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * File     : stubs.c
 * Brief    : reimplement some basic functions of arm standard c library
 *
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-11-23     Yihui        The first version
15 16 17 18 19 20 21 22 23
 */

#include <string.h>
#include <rt_sys.h>

#include "rtthread.h"

#pragma import(__use_no_semihosting_swi)

24
/* TODO: Standard IO device handles. */
25 26 27 28 29 30 31 32 33
#define STDIN       1
#define STDOUT      2
#define STDERR      3

/* Standard IO device name defines. */
const char __stdin_name[]  = "STDIN";
const char __stdout_name[] = "STDOUT";
const char __stderr_name[] = "STDERR";

34 35 36 37 38 39 40 41
/**
 * required by fopen() and freopen().
 *
 * @param name - file name with path.
 * @param openmode - a bitmap hose bits mostly correspond directly to
 *                     the ISO mode specification.
 * @return  -1 if an error occurs.
 */
42 43 44 45 46 47 48 49 50 51 52
FILEHANDLE _sys_open(const char *name, int openmode)
{
    /* Register standard Input Output devices. */
    if (strcmp(name, __stdin_name) == 0)
        return (STDIN);
    if (strcmp(name, __stdout_name) == 0)
        return (STDOUT);
    if (strcmp(name, __stderr_name) == 0)
        return (STDERR);

#ifndef RT_USING_DFS
53
    return -1;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#else
    /* TODO: adjust open file mode */
    return open(name, openmode, 0);
#endif
}

int _sys_close(FILEHANDLE fh)
{
#ifndef RT_USING_DFS
    return 0;
#else
    if (fh < 3)
        return 0;
    
    return close(fh);
#endif
}

72 73 74 75 76 77 78 79 80
/**
 * read data
 *
 * @param fh - file handle
 * @param buf - buffer to save read data
 * @param len - max length of data buffer
 * @param mode - useless, for historical reasons
 * @return actual read data length
 */
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
    if (fh == STDIN)
    {
        /* TODO */
        
        return 0;
    }
    
#ifndef RT_USING_DFS
    return 0;
#else
    return read(fh, buf, len);
#endif
}

97 98 99 100 101 102 103 104 105
/**
 * write data
 *
 * @param fh - file handle
 * @param buf - data buffer
 * @param len - buffer length
 * @param mode - useless, for historical reasons
 * @return actual written data length
 */
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
    if ((fh == STDOUT) || (fh == STDERR))
    {
#ifndef RT_USING_CONSOLE
        return 0;
#else
        rt_device_t console_device;
        extern rt_device_t rt_console_get_device(void);

        console_device = rt_console_get_device();
        if (console_device != 0) rt_device_write(console_device, 0, buf, len);
        return len;
#endif
    }
    
#ifndef RT_USING_DFS
    return 0;
#else
    return write(fh, buf, len);
#endif
}

129 130 131 132 133 134
/**
 * put he file pointer at offset pos from the beginning of the file.
 *
 * @param pos - offset
 * @return the current file position, or -1 on failed
 */
135 136 137
int _sys_seek(FILEHANDLE fh, long pos)
{
#ifndef RT_USING_DFS
138
    return -1;
139
#else
140
    /* position is relative to the start of file fh */
141 142 143 144
    return lseek(fh, pos, 0);
#endif
}

145 146 147 148
/**
 * used by tmpnam() or tmpfile()
 */
void_sys_tmpnam(char *name, int fileno, unsigned maxlength)
149 150 151 152 153
{
}

char *_sys_command_string(char *cmd, int len)
{
154
    /* no support */
155 156 157 158 159
    return cmd;
}

void _ttywrch(int ch)
{
160
   /* TODO */ 
161 162 163 164
}

void _sys_exit(int return_code)
{
165
    /* TODO: perhaps exit the thread which is invoking this function */
166 167 168
    while (1);
}

169 170 171 172 173 174
/**
 * return current length of file. 
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
175 176
long _sys_flen(FILEHANDLE fh)
{
177
    return -1;
178 179 180 181 182 183 184 185
}

int _sys_istty(FILEHANDLE fh)
{
    return 0;
}


186 187 188 189 190 191 192 193 194 195 196 197 198
int remove(const char *filename)
{
    return unlink(filename);
}

/* rename() */

int system(const char *string)
{
    RT_ASSERT(0);
    for(;;);	
}

199