-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscalls.c
531 lines (424 loc) · 15.1 KB
/
syscalls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/** \file syscalls.c
* \brief System calls for newlib's libc
* \details System calls for newlib's libc
*
* \author Freddie Chopin, http://www.freddiechopin.info/
* \date 2010-11-11
*/
/******************************************************************************
* project: syscalls
* chip: (any)
* compiler: arm-none-eabi-gcc (Sourcery G++ Lite 2010q1-188) 4.4.1
*
* prefix: (none)
*
* available global functions:
* int _close_r(struct _reent *r, int file)
* int _execve_r(struct _reent *r, const char *path, char *const argv[], char *const envp[])
* void _exit(int status)
* pid_t _fork_r(struct _reent *r)
* int _fstat_r(struct _reent *r, int file, struct stat *st)
* pid_t _getpid_r(struct _reent *r)
* int _isatty_r(struct _reent *r, int file)
* int _kill_r(struct _reent *r, pid_t pid, int signal)
* int _link_r(struct _reent *r, const char *old, const char *new)
* off_t _lseek_r(struct _reent *r, int file, off_t offset, int whence)
* int _open_r(struct _reent *r, const char *pathname, int flags, int mode)
* ssize_t _read_r(struct _reent *r, int file, void *buf, size_t nbyte)
* caddr_t _sbrk_r(struct _reent *r, int size)
* int _stat_r(struct _reent *r, const char *pathname, struct stat *st)
* int _times_r(struct _reent *r, struct tms *buf)
* int _unlink_r(struct _reent *r, const char *pathname)
* pid_t _wait_r(struct _reent *r, int *stat_loc)
* ssize_t _write_r(struct _reent *r, int file, const void *buf, size_t nbyte)
*
* available local functions:
*
* available interrupt handlers:
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*
+=============================================================================+
| configuration
+=============================================================================+
*/
#define SYSCALLS_HAVE_CLOSE_R 1
#define SYSCALLS_HAVE_EXECVE_R 1
#define SYSCALLS_HAVE_EXIT 1
#define SYSCALLS_HAVE_FORK_R 1
#define SYSCALLS_HAVE_FSTAT_R 1
#define SYSCALLS_HAVE_GETPID_R 1
#define SYSCALLS_HAVE_ISATTY_R 1
#define SYSCALLS_HAVE_KILL_R 1
#define SYSCALLS_HAVE_LINK_R 1
#define SYSCALLS_HAVE_LSEEK_R 1
#define SYSCALLS_HAVE_OPEN_R 1
#define SYSCALLS_HAVE_READ_R 1
#define SYSCALLS_HAVE_SBRK_R 1
#define SYSCALLS_HAVE_STAT_R 1
#define SYSCALLS_HAVE_TIMES_R 1
#define SYSCALLS_HAVE_UNLINK_R 1
#define SYSCALLS_HAVE_WAIT_R 1
#define SYSCALLS_HAVE_WRITE_R 1
/*
+=============================================================================+
| includes
+=============================================================================+
*/
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
/*
+=============================================================================+
| module variables
+=============================================================================+
*/
char *environ_initial[1] = {0};
char **environ = environ_initial;
#undef errno
extern int errno;
/*
+=============================================================================+
| local functions' declarations
+=============================================================================+
*/
/*
+=============================================================================+
| global functions
+=============================================================================+
*/
#if SYSCALLS_HAVE_CLOSE_R == 1
/*------------------------------------------------------------------------*//**
* \brief Close a file.
* \details Close a file.
*
* \param [in] file indicates the file descriptor to close.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _close_r(struct _reent *r, int file)
{
r = r; // suppress warning
file = file; // suppress warning
return -1;
}
#endif
#if SYSCALLS_HAVE_EXECVE_R == 1
/*------------------------------------------------------------------------*//**
* \brief Transfer control to a new process.
* \details Transfer control to a new process.
*
* \param [in] path specifies the path name of the new process image file.
* \param [in] argv is the argument list for the new process image. This should
* contain an array of pointers to character strings, and the array should be
* terminated by a NULL pointer. The value in argv[0] should point to a file
* name that is associated with the process being started by the exec()
* function.
* \param [in] envp specifies the environment for the new process image. This
* should contain an array of pointers to character strings, and the array
* should be terminated by a NULL pointer.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _execve_r(struct _reent *r, const char *path, char *const argv[], char *const envp[])
{
r = r; // suppress warning
path = path; // suppress warning
argv = argv; // suppress warning
envp = envp; // suppress warning
errno = ENOMEM;
return -1;
}
#endif
#if SYSCALLS_HAVE_EXIT == 1
/*------------------------------------------------------------------------*//**
* \brief Exit a program.
* \details Exit a program without cleaning up files.
*
* \param [in] status represents the termination status.
*//*-------------------------------------------------------------------------*/
void _exit(int status)
{
status = status; // suppress warning
while (1);
}
#endif
#if SYSCALLS_HAVE_FORK_R == 1
/*------------------------------------------------------------------------*//**
* \brief Create a new process.
* \details Create a new process.
*
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
pid_t _fork_r(struct _reent *r)
{
r = r; // suppress warning
errno = EAGAIN;
return -1;
}
#endif
#if SYSCALLS_HAVE_FSTAT_R == 1
/*------------------------------------------------------------------------*//**
* \brief Status of an open file.
* \details Status of an open file.
*
* \param [in] file is a file descriptor referring to a file for which status is
* returned.
* \param [out] st points to a stat structure where status information about the
* file is to be placed.
* \return 0 for success.
*//*-------------------------------------------------------------------------*/
int _fstat_r(struct _reent *r, int file, struct stat *st)
{
r = r; // suppress warning
file = file; // suppress warning
st = st; // suppress warning
st->st_mode = S_IFCHR;
return 0;
}
#endif
#if SYSCALLS_HAVE_GETPID_R == 1
/*------------------------------------------------------------------------*//**
* \brief Get calling process ID.
* \details Get calling process ID.
*
* \return 1.
*//*-------------------------------------------------------------------------*/
pid_t _getpid_r(struct _reent *r)
{
r = r; // suppress warning
return 1;
}
#endif
#if SYSCALLS_HAVE_ISATTY_R == 1
/*------------------------------------------------------------------------*//**
* \brief Query whether output stream is a terminal.
* \details Query whether output stream is a terminal.
*
* \param [in] file is the file descriptor to test.
* \return 1.
*//*-------------------------------------------------------------------------*/
int _isatty_r(struct _reent *r, int file)
{
r = r; // suppress warning
file = file; // suppress warning
return 1;
}
#endif
#if SYSCALLS_HAVE_KILL_R == 1
/*------------------------------------------------------------------------*//**
* \brief Send a signal.
* \details Send a signal.
*
* \param [in] pid is a process ID or process group ID.
* \param [in] signal specifies the signal to be sent.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _kill_r(struct _reent *r, pid_t pid, int signal)
{
r = r; // suppress warning
pid = pid; // suppress warning
signal = signal; // suppress warning
errno = EINVAL;
return -1;
}
#endif
#if SYSCALLS_HAVE_LINK_R == 1
/*------------------------------------------------------------------------*//**
* \brief Establish a new name for an existing file.
* \details Establish a new name for an existing file.
*
* \param [in] old points to a path name that names an existing file.
* \param [in] new points to a path name that names the new directory entry to
* be created.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _link_r(struct _reent *r, const char *old, const char *new)
{
r = r; // suppress warning
old = old; // suppress warning
new = new; // suppress warning
errno = EMLINK;
return -1;
}
#endif
#if SYSCALLS_HAVE_LSEEK_R == 1
/*------------------------------------------------------------------------*//**
* \brief Set position in a file.
* \details Set position in a file.
*
* \param [in] file is the file descriptor of an open file.
* \param [in] offset specifies the number of bytes to offset the file pointer
* from a specified file origin.
* \param [in] whence specifies the location from which to start seeking.
* \return 0 for success.
*//*-------------------------------------------------------------------------*/
off_t _lseek_r(struct _reent *r, int file, off_t offset, int whence)
{
r = r; // suppress warning
file = file; // suppress warning
offset = offset; // suppress warning
whence = whence; // suppress warning
return 0;
}
#endif
#if SYSCALLS_HAVE_OPEN_R == 1
/*------------------------------------------------------------------------*//**
* \brief Open a file.
* \details Open a file.
*
* \param [in] pathname is the name of the file to open.
* \param [in] flags is the bitwise inclusive-OR of the file access modes and
* file status flags.
* \param [in] mode specifies what permissions the file has when it is created.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _open_r(struct _reent *r, const char *pathname, int flags, int mode)
{
r = r; // suppress warning
pathname = pathname; // suppress warning
flags = flags; // suppress warning
mode = mode; // suppress warning
return -1;
}
#endif
#if SYSCALLS_HAVE_READ_R == 1
/*------------------------------------------------------------------------*//**
* \brief Read from a file.
* \details Read from a file.
*
* \param [in] file is the file descriptor that references an open file.
* \param [in] buf points to the buffer to place the read information into.
* \param [in] nbyte specifies the maximum number of bytes to attempt to read.
* \return 0 for success.
*//*-------------------------------------------------------------------------*/
ssize_t _read_r(struct _reent *r, int file, void *buf, size_t nbyte)
{
r = r; // suppress warning
file = file; // suppress warning
buf = buf; // suppress warning
nbyte = nbyte; // suppress warning
return 0;
}
#endif
#if SYSCALLS_HAVE_SBRK_R == 1
/*------------------------------------------------------------------------*//**
* \brief Increase program data space.
* \details Increase program data space. This version of _sbrk_r() requires the
* heap area to be defined explicitly in linker script with symbols __heap_start
* and __heap_end.
*
* \param [in] size is the requested data space size.
* \return pointer to new data space.
*//*-------------------------------------------------------------------------*/
caddr_t _sbrk_r(struct _reent *r, int size)
{
extern char __heap_start; // imported from linker script
extern char __heap_end; // imported from linker script
static char *current_heap_end = &__heap_start;
char *previous_heap_end;
r = r; // suppress warning
previous_heap_end = current_heap_end;
if (current_heap_end + size > &__heap_end) // is there enough space on the heap left?
{
errno = ENOMEM; // not enough memory left
return (caddr_t) -1; // return immediatelly
}
current_heap_end += size; // increase data space
return (caddr_t)previous_heap_end; // return requested data space
}
#endif
#if SYSCALLS_HAVE_STAT_R == 1
/*------------------------------------------------------------------------*//**
* \brief Status of a file (by name).
* \details Status of a file (by name).
*
* \param [in] pathname pPoints to a path name that names a file.
* \param [out] st points to a stat structure where status information about the
* file is to be placed.
* \return 0 for success.
*//*-------------------------------------------------------------------------*/
int _stat_r(struct _reent *r, const char *pathname, struct stat *st)
{
r = r; // suppress warning
pathname = pathname; // suppress warning
st->st_mode = S_IFCHR;
return 0;
}
#endif
#if SYSCALLS_HAVE_TIMES_R == 1
/*------------------------------------------------------------------------*//**
* \brief Timing information for current process.
* \details Timing information for current process.
*
* \param [out] buf points to a tms structure where timing information about the
* process is to be placed.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _times_r(struct _reent *r, struct tms *buf)
{
r = r; // suppress warning
buf = buf; // suppress warning
return -1;
}
#endif
#if SYSCALLS_HAVE_UNLINK_R == 1
/*------------------------------------------------------------------------*//**
* \brief Remove a files directory entry.
* \details Remove a files directory entry.
*
* \param [in] pathname points to the path name that names the file to be
* unlinked.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
int _unlink_r(struct _reent *r, const char *pathname)
{
r = r; // suppress warning
pathname = pathname; // suppress warning
errno = ENOENT;
return -1;
}
#endif
#if SYSCALLS_HAVE_WAIT_R == 1
/*------------------------------------------------------------------------*//**
* \brief Wait for a child process.
* \details Wait for a child process.
*
* \param [in,out] stat_loc specifies the location where the child process' exit
* status is stored.
* \return -1 for failure.
*//*-------------------------------------------------------------------------*/
pid_t _wait_r(struct _reent *r, int *stat_loc)
{
r = r; // suppress warning
stat_loc = stat_loc; // suppress warning
errno = ECHILD;
return -1;
}
#endif
#if SYSCALLS_HAVE_WRITE_R == 1
/*------------------------------------------------------------------------*//**
* \brief Wait for a child process.
* \details Wait for a child process.
*
* \param [in] file is the file descriptor of an open file to write to.
* \param [in] buf is an array of data to write to the open file.
* \param [in] nbyte is the number of bytes to write to the file.
* \return 0 for success.
*//*-------------------------------------------------------------------------*/
ssize_t _write_r(struct _reent *r, int file, const void *buf, size_t nbyte)
{
r = r; // suppress warning
file = file; // suppress warning
buf = buf; // suppress warning
nbyte = nbyte; // suppress warning
return 0;
}
#endif
#ifdef __cplusplus
}
#endif
/******************************************************************************
* END OF FILE
******************************************************************************/