syscalls.c 5.0 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2021-02-11     Meco Man     remove _gettimeofday_r() and _times_r()
9
 * 2020-02-13     Meco Man     re-implement exit() and abort()
10
 */
11

12 13 14
#include <reent.h>
#include <sys/errno.h>
#include <sys/time.h>
15 16
#include <stdio.h>

17 18
#include <rtthread.h>

B
Bernard Xiong 已提交
19 20 21 22
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#endif

23 24 25 26
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

27 28
/* Reentrant versions of system calls.  */

29 30 31 32 33 34 35 36
#ifndef _REENT_ONLY
int *
__errno ()
{
  return _rt_errno();
}
#endif

37 38 39
int
_close_r(struct _reent *ptr, int fd)
{
40
#ifndef RT_USING_DFS
B
BernardXiong 已提交
41
    return 0;
42
#else
B
BernardXiong 已提交
43
    return close(fd);
44
#endif
45 46 47 48 49
}

int
_execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
B
BernardXiong 已提交
50 51 52
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
53 54 55 56 57
}

int
_fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
B
BernardXiong 已提交
58 59 60
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
61 62 63 64 65
}

int
_fork_r(struct _reent *ptr)
{
B
BernardXiong 已提交
66 67 68
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
69 70 71 72 73
}

int
_fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
{
B
BernardXiong 已提交
74 75 76
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
77 78 79 80 81
}

int
_getpid_r(struct _reent *ptr)
{
B
BernardXiong 已提交
82
    return 0;
83 84 85 86 87
}

int
_isatty_r(struct _reent *ptr, int fd)
{
B
BernardXiong 已提交
88
    if (fd >=0 && fd < 3) return 1;
89

B
BernardXiong 已提交
90 91 92
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
93 94 95 96 97
}

int
_kill_r(struct _reent *ptr, int pid, int sig)
{
B
BernardXiong 已提交
98 99 100
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
101 102 103 104 105
}

int
_link_r(struct _reent *ptr, const char *old, const char *new)
{
B
BernardXiong 已提交
106 107 108
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
109 110 111 112 113
}

_off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
114
#ifndef RT_USING_DFS
B
BernardXiong 已提交
115
    return 0;
116
#else
B
BernardXiong 已提交
117
    _off_t rc;
118

B
BernardXiong 已提交
119 120
    rc = lseek(fd, pos, whence);
    return rc;
121
#endif
122 123 124 125 126
}

int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
127
#ifndef RT_USING_DFS
B
BernardXiong 已提交
128
    return 0;
129
#else
B
BernardXiong 已提交
130
    int rc;
131

B
BernardXiong 已提交
132 133
    rc = mkdir(name, mode);
    return rc;
134
#endif
135 136 137 138 139
}

int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
140
#ifndef RT_USING_DFS
B
BernardXiong 已提交
141
    return 0;
142
#else
B
BernardXiong 已提交
143
    int rc;
144

B
BernardXiong 已提交
145 146
    rc = open(file, flags, mode);
    return rc;
147
#endif
148 149
}

150
_ssize_t
151 152
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
153
#ifndef RT_USING_DFS
B
BernardXiong 已提交
154
    return 0;
155
#else
B
BernardXiong 已提交
156
    _ssize_t rc;
157

B
BernardXiong 已提交
158 159
    rc = read(fd, buf, nbytes);
    return rc;
160
#endif
161 162 163 164 165
}

int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
166
#ifndef RT_USING_DFS
B
BernardXiong 已提交
167
    return 0;
168
#else
B
BernardXiong 已提交
169
    int rc;
170

B
BernardXiong 已提交
171 172
    rc = rename(old, new);
    return rc;
173
#endif
174 175 176 177 178
}

void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
B
BernardXiong 已提交
179 180
    /* no use this routine to get memory */
    return RT_NULL;
181 182 183 184 185
}

int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
186
#ifndef RT_USING_DFS
B
BernardXiong 已提交
187
    return 0;
188
#else
B
BernardXiong 已提交
189
    int rc;
190

B
BernardXiong 已提交
191 192
    rc = stat(file, pstat);
    return rc;
193
#endif
194 195 196 197 198
}

int
_unlink_r(struct _reent *ptr, const char *file)
{
199
#ifndef RT_USING_DFS
200
    return -1;
201
#else
202
    return unlink(file);
203
#endif
204 205 206 207 208
}

int
_wait_r(struct _reent *ptr, int *status)
{
B
BernardXiong 已提交
209 210 211
    /* return "not supported" */
    ptr->_errno = ENOTSUP;
    return -1;
212 213
}

214
#ifdef RT_USING_DEVICE
215 216 217
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
B
bernard 已提交
218
#ifndef RT_USING_DFS
219
    if (fileno(stdout) == fd)
B
BernardXiong 已提交
220 221
    {
        rt_device_t console;
222

B
BernardXiong 已提交
223 224 225
        console = rt_console_get_device();
        if (console) return rt_device_write(console, -1, buf, nbytes);
    }
226

B
bernard 已提交
227 228
    return 0;

229
#else
B
BernardXiong 已提交
230
    _ssize_t rc;
B
bernard 已提交
231

B
BernardXiong 已提交
232 233
    rc = write(fd, buf, nbytes);
    return rc;
234
#endif
235
}
236
#endif
237 238 239 240 241

/* Memory routine */
void *
_malloc_r (struct _reent *ptr, size_t size)
{
B
BernardXiong 已提交
242
    void* result;
243

B
BernardXiong 已提交
244 245 246 247 248
    result = (void*)rt_malloc (size);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
249

B
BernardXiong 已提交
250
    return result;
251 252 253 254 255
}

void *
_realloc_r (struct _reent *ptr, void *old, size_t newlen)
{
B
BernardXiong 已提交
256
    void* result;
257

B
BernardXiong 已提交
258 259 260 261 262
    result = (void*)rt_realloc (old, newlen);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
263

B
BernardXiong 已提交
264
    return result;
265 266 267 268
}

void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
{
B
BernardXiong 已提交
269
    void* result;
270

B
BernardXiong 已提交
271 272 273 274 275
    result = (void*)rt_calloc (size, len);
    if (result == RT_NULL)
    {
        ptr->_errno = ENOMEM;
    }
276

B
BernardXiong 已提交
277
    return result;
278 279
}

280
void
281 282
_free_r (struct _reent *ptr, void *addr)
{
B
BernardXiong 已提交
283
    rt_free (addr);
284 285 286
}

void
287
exit (int status)
288
{
mysterywolf's avatar
mysterywolf 已提交
289 290
    extern rt_inline void __exit__(int status);
    __exit__(status);
291
}
292

293
void
294 295
_system(const char *s)
{
mysterywolf's avatar
mysterywolf 已提交
296 297
    extern rt_inline int __system__(const char *string);
    __system__(string);
298
}
299 300 301

void __libc_init_array(void)
{
B
BernardXiong 已提交
302
    /* we not use __libc init_aray to initialize C++ objects */
303
}
304 305 306

void abort(void)
{
mysterywolf's avatar
mysterywolf 已提交
307 308
    extern rt_inline void __abort__(void);
    __abort__();
309
}
310 311 312 313 314 315 316 317 318 319 320

uid_t getuid(void)
{
    return 0;
}

mode_t umask(mode_t mask)
{
    return 022;
}

321 322 323 324
int flock(int fd, int operation)
{
    return 0;
}
325 326

/*
mysterywolf's avatar
mysterywolf 已提交
327
These functions will be implemented and replaced by the 'common/time.c' file
328 329 330
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_  _times_r(struct _reent *ptr, struct tms *ptms);
*/