syscalls.c 8.6 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
#include <compiler_private.h>
25
#ifdef RT_USING_POSIX_DEVIO
26
#include "libc.h"
27
#endif /* RT_USING_POSIX_DEVIO */
28

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

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

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

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

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

mysterywolf's avatar
mysterywolf 已提交
72
#ifndef DFS_USING_POSIX
73
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
74
    return 0; /* error */
75
#else
B
Bernard Xiong 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    /* 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)
        {
98
            mode |= (O_WRONLY | O_APPEND | O_CREAT);
B
Bernard Xiong 已提交
99 100
        }
    }
101 102

    fd = open(name, mode, 0);
B
Bernard Xiong 已提交
103
    if (fd < 0)
104
        return 0; /* error */
105
    else
B
bernard 已提交
106
        return fd;
mysterywolf's avatar
mysterywolf 已提交
107
#endif /* DFS_USING_POSIX */
108 109 110 111
}

int _sys_close(FILEHANDLE fh)
{
mysterywolf's avatar
mysterywolf 已提交
112
#ifdef DFS_USING_POSIX
113 114
    if (fh <= STDERR)
        return 0; /* error */
115

B
bernard 已提交
116
    return close(fh);
117
#else
118
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
119
    return 0;
mysterywolf's avatar
mysterywolf 已提交
120
#endif /* DFS_USING_POSIX */
121 122
}

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

153 154
    if (fh == STDIN)
    {
155
#ifdef RT_USING_POSIX_DEVIO
156 157
        if (libc_stdio_get_console() < 0)
        {
mysterywolf's avatar
mysterywolf 已提交
158
            LOG_W("Do not invoke standard output before initializing Compiler");
159
            return 0; /* error, but keep going */
160
        }
161
        size = read(STDIN_FILENO, buf, len);
162
        return len - size; /* success */
163
#else
164
        LOG_W("%s: %s", __func__, _WARNING_WITHOUT_DEVIO);
165
        return 0; /* error */
166
#endif /* RT_USING_POSIX_DEVIO */
167
    }
168
    else if (fh == STDOUT || fh == STDERR)
169
    {
170
        return -1; /* 100% error */
171
    }
172
    else
173 174 175
    {
        size = read(fh, buf, len);
        if (size >= 0)
176
        {
177
            return len - size; /* success */
178
        }
179
        else
180
        {
181
            return 0; /* error */
182
        }
183
    }
184
#else
185
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
186
    return 0; /* error */
mysterywolf's avatar
mysterywolf 已提交
187
#endif /* DFS_USING_POSIX */
188 189
}

B
bernard 已提交
190 191 192 193
/*
 * 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.
194 195 196 197
 * 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.
198
 */
199 200
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
mysterywolf's avatar
mysterywolf 已提交
201
#ifdef DFS_USING_POSIX
202
    int size;
mysterywolf's avatar
mysterywolf 已提交
203
#endif /* DFS_USING_POSIX */
B
Bernard Xiong 已提交
204

205
    if (fh == STDOUT || fh == STDERR)
206
    {
207 208 209 210
#ifdef RT_USING_CONSOLE
        rt_device_t console;
        console = rt_console_get_device();
        if (console)
211
        {
212
            rt_device_write(console, -1, buf, len);
213
        }
214 215
        return 0; /* success */
#else
216
        return 0; /* error */
217
#endif /* RT_USING_CONSOLE */
218
    }
219 220
    else if (fh == STDIN)
    {
221
        return -1; /* 100% error */
222
    }
223
    else
224
    {
mysterywolf's avatar
mysterywolf 已提交
225
#ifdef DFS_USING_POSIX
226 227
        size = write(fh, buf, len);
        if (size >= 0)
228 229 230
        {
            return len - size; /* success */
        }
231
        else
232
        {
233
            return 0; /* error */
234
        }
235
#else
236
        LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
237
        return 0; /* error */
mysterywolf's avatar
mysterywolf 已提交
238
#endif /* DFS_USING_POSIX */
239
    }
240 241
}

B
bernard 已提交
242 243 244
/*
 * Move the file position to a given offset from the file start.
 * Returns >=0 on success, <0 on failure.
245
 */
246 247
int _sys_seek(FILEHANDLE fh, long pos)
{
mysterywolf's avatar
mysterywolf 已提交
248
#ifdef DFS_USING_POSIX
249
    if (fh < STDERR)
250
        return 0; /* error */
251

252
    /* position is relative to the start of file fh */
B
bernard 已提交
253
    return lseek(fh, pos, 0);
254
#else
255
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
256
    return 0; /* error */
mysterywolf's avatar
mysterywolf 已提交
257
#endif /* DFS_USING_POSIX */
258 259
}

260 261 262
/**
 * used by tmpnam() or tmpfile()
 */
263
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
264
{
mysterywolf's avatar
mysterywolf 已提交
265 266
    rt_snprintf(name, maxlength, "tem%03d", fileno);
    return 1;
267 268 269 270
}

char *_sys_command_string(char *cmd, int len)
{
271
    /* no support */
272
    return RT_NULL;
273 274
}

275
/* This function writes a character to the console. */
276 277
void _ttywrch(int ch)
{
278
#ifdef RT_USING_CONSOLE
279 280 281 282
    char c;

    c = (char)ch;
    rt_kprintf(&c);
283
#endif /* RT_USING_CONSOLE */
284 285
}

mysterywolf's avatar
mysterywolf 已提交
286
/* for exit() and abort() */
287
RT_WEAK void _sys_exit(int return_code)
288
{
mysterywolf's avatar
update  
mysterywolf 已提交
289 290
    extern void __rt_libc_exit(int status);
    __rt_libc_exit(return_code);
mysterywolf's avatar
update  
mysterywolf 已提交
291
    while(1);
292 293
}

294
/**
295
 * return current length of file.
296 297 298 299
 *
 * @param fh - file handle
 * @return file length, or -1 on failed
 */
300 301
long _sys_flen(FILEHANDLE fh)
{
mysterywolf's avatar
mysterywolf 已提交
302
#ifdef DFS_USING_POSIX
G
gztss 已提交
303
    struct stat stat;
mysterywolf's avatar
mysterywolf 已提交
304

G
gztss 已提交
305
    if (fh < STDERR)
306
        return 0; /* error */
G
gztss 已提交
307 308 309

    fstat(fh, &stat);
    return stat.st_size;
mysterywolf's avatar
mysterywolf 已提交
310
#else
311
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
312
    return 0;
mysterywolf's avatar
mysterywolf 已提交
313
#endif /* DFS_USING_POSIX */
314 315 316 317
}

int _sys_istty(FILEHANDLE fh)
{
318 319 320 321
    if((STDIN <= fh) && (fh <= STDERR))
        return 1;
    else
        return 0;
322 323
}

324 325
int remove(const char *filename)
{
mysterywolf's avatar
mysterywolf 已提交
326
#ifdef DFS_USING_POSIX
327
    return unlink(filename);
328
#else
329
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
330
    return 0; /* error */
mysterywolf's avatar
mysterywolf 已提交
331
#endif /* DFS_USING_POSIX */
332 333
}

334 335 336
#ifdef __MICROLIB
#include <stdio.h>

337
int fputc(int c, FILE *f)
338
{
339
#ifdef RT_USING_CONSOLE
340
    char ch[2] = {0};
341

342
    ch[0] = c;
343
    rt_kprintf(&ch[0]);
344
    return 1;
345 346 347
#else
    return 0; /* error */
#endif /* RT_USING_CONSOLE */
348 349
}

350
int fgetc(FILE *f)
B
bernard 已提交
351
{
352
#ifdef RT_USING_POSIX_DEVIO
B
bernard 已提交
353 354
    char ch;

355 356
    if (libc_stdio_get_console() < 0)
    {
mysterywolf's avatar
mysterywolf 已提交
357
        LOG_W("Do not invoke standard output before initializing Compiler");
358
        return 0;
359 360 361
    }

    if(read(STDIN_FILENO, &ch, 1) == 1)
B
bernard 已提交
362
        return ch;
363
#endif /* RT_USING_POSIX_DEVIO */
364
    LOG_W("%s: %s", __func__, _WARNING_WITHOUT_DEVIO);
365
    return 0; /* error */
366
}
367 368

#endif /* __MICROLIB */