stubs.c 4.6 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 50
    int fd;

51 52 53 54 55 56 57 58 59
    /* 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
60
    return -1;
61 62
#else
    /* TODO: adjust open file mode */
63 64 65 66 67
    fd = open(name, openmode, 0);
    if(fd < 0)
        return -1;
    else
        return fd + STDERR + 1;
68 69 70 71 72 73 74 75
#endif
}

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

    return close(fh - STDERR - 1);
80 81 82
#endif
}

83 84 85 86 87 88 89
/**
 * 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
90
 * @return The number of bytes not read.
91
 */
92 93
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
94 95
    int size;

96 97 98
    if (fh == STDIN)
    {
        /* TODO */
99

100 101
        return 0;
    }
102 103 104 105

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

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

117 118 119 120 121 122 123
/**
 * write data
 *
 * @param fh - file handle
 * @param buf - data buffer
 * @param len - buffer length
 * @param mode - useless, for historical reasons
124
 * @return a positive number representing the number of characters not written.
125
 */
126 127
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
128 129
    int size;

130 131 132 133 134 135 136 137 138 139 140 141 142
    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
    }
143 144 145 146

    if(fh == STDIN)
        return -1;

147 148 149
#ifndef RT_USING_DFS
    return 0;
#else
150 151 152 153 154
    size = write(fh - STDERR - 1, buf, len);
    if(size >= 0)
        return len - size;
    else
        return -1;
155 156 157
#endif
}

158 159 160 161 162 163
/**
 * 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
 */
164 165
int _sys_seek(FILEHANDLE fh, long pos)
{
166 167 168
    if (fh < STDERR)
        return -1;

169
#ifndef RT_USING_DFS
170
    return -1;
171
#else
172

173
    /* position is relative to the start of file fh */
174
    return lseek(fh - STDERR - 1, pos, 0);
175 176 177
#endif
}

178 179 180
/**
 * used by tmpnam() or tmpfile()
 */
181
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
182
{
183
    return -1;
184 185 186 187
}

char *_sys_command_string(char *cmd, int len)
{
188
    /* no support */
189 190 191 192 193
    return cmd;
}

void _ttywrch(int ch)
{
194
   /* TODO */
195 196 197 198
}

void _sys_exit(int return_code)
{
199
    /* TODO: perhaps exit the thread which is invoking this function */
200 201 202
    while (1);
}

203
/**
204
 * return current length of file.
205 206 207 208
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
209 210
long _sys_flen(FILEHANDLE fh)
{
211
    return -1;
212 213 214 215 216 217 218 219
}

int _sys_istty(FILEHANDLE fh)
{
    return 0;
}


220 221
int remove(const char *filename)
{
222 223 224
#ifndef RT_USING_DFS
    return -1;
#else
225
    return unlink(filename);
226
#endif
227 228
}

229 230 231 232 233 234 235
/* rename() is defined in dfs_posix.c instead */
#if 0
int rename(const char *old, const char *new)
{
    return -1;
}
#endif
236 237 238 239

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

243