syscalls.c 7.4 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 19 20
 */

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

mysterywolf's avatar
update  
mysterywolf 已提交
21
#include <rtthread.h>
B
bernard 已提交
22
#include "libc.h"
23

24
#ifdef RT_USING_DFS
mysterywolf's avatar
update  
mysterywolf 已提交
25
#include <dfs_posix.h>
26 27
#endif

28 29 30 31
#define DBG_TAG    "armlibc.syscalls"
#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)
{
B
Bernard Xiong 已提交
58
#ifdef RT_USING_DFS
59
    int fd;
60
    int mode = O_RDONLY;
B
bernard 已提交
61
#endif
B
Bernard Xiong 已提交
62

63 64 65 66 67 68 69 70 71
    /* 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
72
    return -1;
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 103
        return -1;
    else
B
bernard 已提交
104
        return fd;
105 106 107 108 109 110 111 112
#endif
}

int _sys_close(FILEHANDLE fh)
{
#ifndef RT_USING_DFS
    return 0;
#else
B
bernard 已提交
113
    if (fh <= STDERR) return 0;
114

B
bernard 已提交
115
    return close(fh);
116 117 118
#endif
}

B
bernard 已提交
119 120 121 122 123 124 125
/*
 * 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
126
 *
B
bernard 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
 * 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.
143
 */
144 145
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
B
Bernard Xiong 已提交
146
#ifdef RT_USING_DFS
147
    int size;
B
bernard 已提交
148
#endif
B
Bernard Xiong 已提交
149

150 151
    if (fh == STDIN)
    {
B
bernard 已提交
152
#ifdef RT_USING_POSIX
153 154 155 156 157
        if (libc_stdio_get_console() < 0)
        {
            LOG_E("invoke standard output before initializing libc");
            return -1;
        }
158
        size = read(STDIN_FILENO, buf, len);
B
bernard 已提交
159 160 161 162 163
        return len - size;
#else
        /* no stdin */
        return -1;
#endif
164
    }
165 166
    else if ((fh == STDOUT) || (fh == STDERR))
    {
167
        return -1;
168
    }
169

170 171 172
#ifndef RT_USING_DFS
    return 0;
#else
B
bernard 已提交
173
    size = read(fh, buf, len);
B
Bernard Xiong 已提交
174
    if (size >= 0)
175 176 177
        return len - size;
    else
        return -1;
178 179 180
#endif
}

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
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
B
bernard 已提交
188
#ifdef RT_USING_DFS
189
    int size;
B
bernard 已提交
190
#endif
B
Bernard Xiong 已提交
191

192 193
    if ((fh == STDOUT) || (fh == STDERR))
    {
194
#if !defined(RT_USING_CONSOLE) || !defined(RT_USING_DEVICE)
195 196
        return 0;
#else
B
bernard 已提交
197
#ifdef RT_USING_POSIX
198 199 200 201 202
        if (libc_stdio_get_console() < 0)
        {
            LOG_E("invoke standard input before initializing libc");
            return -1;
        }
203
        size = write(STDOUT_FILENO, buf, len);
B
bernard 已提交
204 205 206 207 208 209 210
        return len - size;
#else
        if (rt_console_get_device())
        {
            rt_device_write(rt_console_get_device(), -1, buf, len);
            return 0;
        }
211

B
bernard 已提交
212 213
        return -1;
#endif
214 215
#endif
    }
216 217 218 219
    else if (fh == STDIN)
    {
        return -1;
    }
220

221 222 223
#ifndef RT_USING_DFS
    return 0;
#else
B
bernard 已提交
224
    size = write(fh, buf, len);
B
Bernard Xiong 已提交
225
    if (size >= 0)
226 227 228
        return len - size;
    else
        return -1;
229 230 231
#endif
}

B
bernard 已提交
232 233 234
/*
 * Move the file position to a given offset from the file start.
 * Returns >=0 on success, <0 on failure.
235
 */
236 237
int _sys_seek(FILEHANDLE fh, long pos)
{
238 239 240
    if (fh < STDERR)
        return -1;

241
#ifndef RT_USING_DFS
242
    return -1;
243
#else
244

245
    /* position is relative to the start of file fh */
B
bernard 已提交
246
    return lseek(fh, pos, 0);
247 248 249
#endif
}

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

char *_sys_command_string(char *cmd, int len)
{
261
    /* no support */
262
    return RT_NULL;
263 264
}

265
/* This function writes a character to the console. */
266 267
void _ttywrch(int ch)
{
268
#ifdef RT_USING_CONSOLE
269 270 271 272
    char c;

    c = (char)ch;
    rt_kprintf(&c);
273
#endif
274 275
}

mysterywolf's avatar
mysterywolf 已提交
276
/* for exit() and abort() */
277
RT_WEAK void _sys_exit(int return_code)
278
{
mysterywolf's avatar
update  
mysterywolf 已提交
279 280
    extern void __rt_libc_exit(int status);
    __rt_libc_exit(return_code);
mysterywolf's avatar
update  
mysterywolf 已提交
281
    while(1);
282 283
}

284
/**
285
 * return current length of file.
286 287 288 289
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
290 291
long _sys_flen(FILEHANDLE fh)
{
292
#ifdef RT_USING_DFS
G
gztss 已提交
293
    struct stat stat;
mysterywolf's avatar
mysterywolf 已提交
294

G
gztss 已提交
295 296 297 298 299
    if (fh < STDERR)
        return -1;

    fstat(fh, &stat);
    return stat.st_size;
mysterywolf's avatar
mysterywolf 已提交
300 301
#else
    return -1;
G
gztss 已提交
302
#endif
303 304 305 306
}

int _sys_istty(FILEHANDLE fh)
{
307 308 309 310
    if((STDIN <= fh) && (fh <= STDERR))
        return 1;
    else
        return 0;
311 312
}

313 314
int remove(const char *filename)
{
315 316 317
#ifndef RT_USING_DFS
    return -1;
#else
318
    return unlink(filename);
319
#endif
320 321
}

马志远 已提交
322
#if defined(RT_USING_FINSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS)
323
/* use system(const char *string) implementation in the msh */
324
#else
325 326
int system(const char *string)
{
mysterywolf's avatar
update  
mysterywolf 已提交
327 328
    extern int __rt_libc_system(const char *string);
    return __rt_libc_system(string);
329
}
330
#endif
331 332 333 334

#ifdef __MICROLIB
#include <stdio.h>

335
int fputc(int c, FILE *f)
336
{
337
    char ch[2] = {0};
338

339
    ch[0] = c;
340
    rt_kprintf(&ch[0]);
341 342 343
    return 1;
}

344
int fgetc(FILE *f)
B
bernard 已提交
345
{
346
#ifdef RT_USING_POSIX
B
bernard 已提交
347 348 349 350 351 352 353
    char ch;

    if (libc_stdio_read(&ch, 1) == 1)
        return ch;
#endif

    return -1;
354 355
}
#endif