syscalls.c 7.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-11-23     Yihui        The first version
9
 * 2013-11-24     aozima       fixed _sys_read()/_sys_write() issues.
B
Bernard Xiong 已提交
10
 * 2014-08-03     bernard      If using msh, use system() implementation
11
 *                             in msh.
12
 * 2020-08-05     Meco Man     fixed _sys_flen() compiling-warning when
13
 *                             RT_USING_DFS is not defined
mysterywolf's avatar
update  
mysterywolf 已提交
14
 * 2020-02-13     Meco Man     re-implement exit() and abort()
mysterywolf's avatar
mysterywolf 已提交
15
 * 2020-02-14     Meco Man     implement _sys_tmpnam()
16 17
 */

18
#include <rt_sys.h>
mysterywolf's avatar
update  
mysterywolf 已提交
19
#include <rtthread.h>
20
#include <string.h>
21 22 23
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
mysterywolf's avatar
mysterywolf 已提交
24
#ifdef RT_USING_POSIX_STDIO
25 26
#include "libc.h"
#endif
27

28
#define DBG_TAG    "armlibc.syscalls"
29 30 31
#define DBG_LVL    DBG_INFO
#include <rtdbg.h>

B
Bernard Xiong 已提交
32 33 34
#ifdef __CLANG_ARM
__asm(".global __use_no_semihosting\n\t");
#else
35
#pragma import(__use_no_semihosting_swi)
B
Bernard Xiong 已提交
36
#endif
37

B
bernard 已提交
38 39 40 41
/* Standard IO device handles. */
#define STDIN       0
#define STDOUT      1
#define STDERR      2
42 43 44 45 46 47

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

48 49 50 51 52 53 54 55
/**
 * 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.
 */
56 57
FILEHANDLE _sys_open(const char *name, int openmode)
{
58
#ifdef RT_USING_POSIX
59
    int fd;
60
    int mode = O_RDONLY;
B
bernard 已提交
61
#endif
B
Bernard Xiong 已提交
62

63 64 65 66 67 68 69 70
    /* 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);

71
#ifndef RT_USING_POSIX
72
    return 0; /* error */
73
#else
B
Bernard Xiong 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /* Correct openmode from fopen to open */
    if (openmode & OPEN_PLUS)
    {
        if (openmode & OPEN_W)
        {
            mode |= (O_RDWR | O_TRUNC | O_CREAT);
        }
        else if (openmode & OPEN_A)
        {
            mode |= (O_RDWR | O_APPEND | O_CREAT);
        }
        else
            mode |= O_RDWR;
    }
    else
    {
        if (openmode & OPEN_W)
        {
            mode |= (O_WRONLY | O_TRUNC | O_CREAT);
        }
        else if (openmode & OPEN_A)
        {
96
            mode |= (O_WRONLY | O_APPEND | O_CREAT);
B
Bernard Xiong 已提交
97 98
        }
    }
99 100

    fd = open(name, mode, 0);
B
Bernard Xiong 已提交
101
    if (fd < 0)
102
        return 0; /* error */
103
    else
B
bernard 已提交
104
        return fd;
105
#endif /* RT_USING_POSIX */
106 107 108 109
}

int _sys_close(FILEHANDLE fh)
{
110
#ifdef RT_USING_POSIX
111 112
    if (fh <= STDERR)
        return 0; /* error */
113

B
bernard 已提交
114
    return close(fh);
115 116
#else
    return 0;
117
#endif /* RT_USING_POSIX */
118 119
}

B
bernard 已提交
120 121 122 123 124 125 126
/*
 * Read from a file. Can return:
 *  - zero if the read was completely successful
 *  - the number of bytes _not_ read, if the read was partially successful
 *  - the number of bytes not read, plus the top bit set (0x80000000), if
 *    the read was partially successful due to end of file
 *  - -1 if some error other than EOF occurred
127
 *
B
bernard 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 * It is also legal to signal EOF by returning no data but
 * signalling no error (i.e. the top-bit-set mechanism need never
 * be used).
 *
 * So if (for example) the user is trying to read 8 bytes at a time
 * from a file in which only 5 remain, this routine can do three
 * equally valid things:
 *
 *  - it can return 0x80000003 (3 bytes not read due to EOF)
 *  - OR it can return 3 (3 bytes not read), and then return
 *    0x80000008 (8 bytes not read due to EOF) on the next attempt
 *  - OR it can return 3 (3 bytes not read), and then return
 *    8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
 *    attempt
 *
 * `mode' exists for historical reasons and must be ignored.
144
 */
145 146
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
147
#ifdef RT_USING_POSIX
148
    int size;
B
Bernard Xiong 已提交
149

150 151
    if (fh == STDIN)
    {
152
#ifdef RT_USING_POSIX_STDIO
153 154
        if (libc_stdio_get_console() < 0)
        {
155
            LOG_W("Do not invoke standard output before initializing libc");
156
            return 0; /* error, but keep going */
157
        }
158
        size = read(STDIN_FILENO, buf, len);
159 160 161 162
        return 0; /* success */
#else
        return 0; /* error */
#endif
163
    }
164
    else if (fh == STDOUT || fh == STDERR)
165
    {
166
        return 0; /* error */
167
    }
168
    else
169 170 171 172 173 174 175
    {
        size = read(fh, buf, len);
        if (size >= 0)
            return len - size; /* success */
        else
            return 0; /* error */
    }
176 177
#else
    return 0; /* error */
178
#endif /* RT_USING_POSIX */
179 180
}

B
bernard 已提交
181 182 183 184
/*
 * Write to a file. Returns 0 on success, negative on error, and
 * the number of characters _not_ written on partial success.
 * `mode' exists for historical reasons and must be ignored.
185 186 187 188
 * The return value is either:
 * A positive number representing the number of characters not written
 * (so any nonzero return value denotes a failure of some sort).
 * A negative number indicating an error.
189
 */
190 191
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
192
#ifdef RT_USING_POSIX
193
    int size;
194
#endif /* RT_USING_POSIX */
B
Bernard Xiong 已提交
195

196
    if (fh == STDOUT || fh == STDERR)
197
    {
198 199 200 201
#ifdef RT_USING_CONSOLE
        rt_device_t console;
        console = rt_console_get_device();
        if (console)
202
        {
203
            rt_device_write(console, -1, buf, len);
204
        }
205 206
        return 0; /* success */
#else
207
        return 0; /* error */
208
#endif /* RT_USING_CONSOLE */
209
    }
210 211
    else if (fh == STDIN)
    {
212
        return 0; /* error */
213
    }
214
    else
215 216 217 218 219 220 221
    {
#ifdef RT_USING_POSIX
        size = write(fh, buf, len);
        if (size >= 0)
            return 0; /* success */
        else
            return 0; /* error */
222
#else
223
        return 0; /* error */
224
#endif /* RT_USING_POSIX */
225
    }
226 227
}

B
bernard 已提交
228 229 230
/*
 * Move the file position to a given offset from the file start.
 * Returns >=0 on success, <0 on failure.
231
 */
232 233
int _sys_seek(FILEHANDLE fh, long pos)
{
234
#ifdef RT_USING_POSIX
235
    if (fh < STDERR)
236
        return 0; /* error */
237

238
    /* position is relative to the start of file fh */
B
bernard 已提交
239
    return lseek(fh, pos, 0);
240 241
#else
    return 0; /* error */
242
#endif /* RT_USING_POSIX */
243 244
}

245 246 247
/**
 * used by tmpnam() or tmpfile()
 */
248
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
249
{
mysterywolf's avatar
mysterywolf 已提交
250 251
    rt_snprintf(name, maxlength, "tem%03d", fileno);
    return 1;
252 253 254 255
}

char *_sys_command_string(char *cmd, int len)
{
256
    /* no support */
257
    return RT_NULL;
258 259
}

260
/* This function writes a character to the console. */
261 262
void _ttywrch(int ch)
{
263
#ifdef RT_USING_CONSOLE
264 265 266 267
    char c;

    c = (char)ch;
    rt_kprintf(&c);
268
#endif /* RT_USING_CONSOLE */
269 270
}

mysterywolf's avatar
mysterywolf 已提交
271
/* for exit() and abort() */
272
RT_WEAK void _sys_exit(int return_code)
273
{
mysterywolf's avatar
update  
mysterywolf 已提交
274 275
    extern void __rt_libc_exit(int status);
    __rt_libc_exit(return_code);
mysterywolf's avatar
update  
mysterywolf 已提交
276
    while(1);
277 278
}

279
/**
280
 * return current length of file.
281 282 283 284
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
285 286
long _sys_flen(FILEHANDLE fh)
{
287
#ifdef RT_USING_POSIX
G
gztss 已提交
288
    struct stat stat;
mysterywolf's avatar
mysterywolf 已提交
289

G
gztss 已提交
290
    if (fh < STDERR)
291
        return 0; /* error */
G
gztss 已提交
292 293 294

    fstat(fh, &stat);
    return stat.st_size;
mysterywolf's avatar
mysterywolf 已提交
295
#else
296
    return 0;
297
#endif /* RT_USING_POSIX */
298 299 300 301
}

int _sys_istty(FILEHANDLE fh)
{
302 303 304 305
    if((STDIN <= fh) && (fh <= STDERR))
        return 1;
    else
        return 0;
306 307
}

308 309
int remove(const char *filename)
{
310
#ifdef RT_USING_POSIX
311
    return unlink(filename);
312 313
#else
    return 0; /* error */
314
#endif /* RT_USING_POSIX */
315 316
}

317 318 319
#ifdef __MICROLIB
#include <stdio.h>

320
int fputc(int c, FILE *f)
321
{
322
#ifdef RT_USING_CONSOLE
323
    char ch[2] = {0};
324

325
    ch[0] = c;
326
    rt_kprintf(&ch[0]);
327
    return 1;
328 329 330
#else
    return 0; /* error */
#endif /* RT_USING_CONSOLE */
331 332
}

333
int fgetc(FILE *f)
B
bernard 已提交
334
{
mysterywolf's avatar
mysterywolf 已提交
335
#ifdef RT_USING_POSIX_STDIO
B
bernard 已提交
336 337
    char ch;

338 339 340
    if (libc_stdio_get_console() < 0)
    {
        LOG_W("Do not invoke standard output before initializing libc");
341
        return 0;
342 343 344
    }

    if(read(STDIN_FILENO, &ch, 1) == 1)
B
bernard 已提交
345
        return ch;
mysterywolf's avatar
mysterywolf 已提交
346
#endif /* RT_USING_POSIX_STDIO */
347
    return 0; /* error */
348
}
349 350

#endif /* __MICROLIB */