stubs.c 4.7 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
 * 2013-11-24     aozima       fixed _sys_read()/_sys_write() issues.
16 17 18 19 20 21 22
 */

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

#include "rtthread.h"

23 24 25 26
#ifdef RT_USING_DFS
#include "dfs_posix.h"
#endif

27 28
#pragma import(__use_no_semihosting_swi)

29
/* TODO: Standard IO device handles. */
30 31 32 33 34 35 36 37 38
#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";

39 40 41 42 43 44 45 46
/**
 * 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.
 */
47 48
FILEHANDLE _sys_open(const char *name, int openmode)
{
49
#ifdef RT_USING_DFS    
50
    int fd;
51 52
#endif
    
53 54 55 56 57 58 59 60 61
    /* 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
62
    return -1;
63 64
#else
    /* TODO: adjust open file mode */
65 66 67 68 69
    fd = open(name, openmode, 0);
    if(fd < 0)
        return -1;
    else
        return fd + STDERR + 1;
70 71 72 73 74 75 76 77
#endif
}

int _sys_close(FILEHANDLE fh)
{
#ifndef RT_USING_DFS
    return 0;
#else
78
    if (fh < STDERR)
79
        return 0;
80 81

    return close(fh - STDERR - 1);
82 83 84
#endif
}

85 86 87 88 89 90 91
/**
 * 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
92
 * @return The number of bytes not read.
93
 */
94 95
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
96
#ifdef RT_USING_DFS    
97
    int size;
98 99
#endif
    
100 101 102 103 104
    if (fh == STDIN)
    {
        /* TODO */
        return 0;
    }
105 106 107 108

    if ((fh == STDOUT) || (fh == STDERR))
        return -1;

109 110 111
#ifndef RT_USING_DFS
    return 0;
#else
112 113 114 115 116
    size = read(fh - STDERR - 1, buf, len);
    if(size >= 0)
        return len - size;
    else
        return -1;
117 118 119
#endif
}

120 121 122 123 124 125 126
/**
 * write data
 *
 * @param fh - file handle
 * @param buf - data buffer
 * @param len - buffer length
 * @param mode - useless, for historical reasons
127
 * @return a positive number representing the number of characters not written.
128
 */
129 130
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
131
#ifdef RT_USING_DFS
132
    int size;
133 134
#endif
    
135 136 137 138 139 140 141 142 143 144 145 146 147
    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
    }
148 149 150 151

    if(fh == STDIN)
        return -1;

152 153 154
#ifndef RT_USING_DFS
    return 0;
#else
155 156 157 158 159
    size = write(fh - STDERR - 1, buf, len);
    if(size >= 0)
        return len - size;
    else
        return -1;
160 161 162
#endif
}

163 164 165 166 167 168
/**
 * 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
 */
169 170
int _sys_seek(FILEHANDLE fh, long pos)
{
171 172 173
    if (fh < STDERR)
        return -1;

174
#ifndef RT_USING_DFS
175
    return -1;
176
#else
177

178
    /* position is relative to the start of file fh */
179
    return lseek(fh - STDERR - 1, pos, 0);
180 181 182
#endif
}

183 184 185
/**
 * used by tmpnam() or tmpfile()
 */
186
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
187
{
188
    return -1;
189 190 191 192
}

char *_sys_command_string(char *cmd, int len)
{
193
    /* no support */
194 195 196 197 198
    return cmd;
}

void _ttywrch(int ch)
{
199
   /* TODO */
200 201 202 203
}

void _sys_exit(int return_code)
{
204
    /* TODO: perhaps exit the thread which is invoking this function */
205 206 207
    while (1);
}

208
/**
209
 * return current length of file.
210 211 212 213
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
214 215
long _sys_flen(FILEHANDLE fh)
{
216
    return -1;
217 218 219 220 221 222 223 224
}

int _sys_istty(FILEHANDLE fh)
{
    return 0;
}


225 226
int remove(const char *filename)
{
227 228 229
#ifndef RT_USING_DFS
    return -1;
#else
230
    return unlink(filename);
231
#endif
232 233
}

234 235 236 237 238 239 240
/* rename() is defined in dfs_posix.c instead */
#if 0
int rename(const char *old, const char *new)
{
    return -1;
}
#endif
241 242 243 244

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

248