Contiki 2.5
syscalls.c
1 /* Support files for GNU libc. Files in the system namespace go here.
2  Files in the C namespace (ie those that do not start with an
3  underscore) go in .c. */
4 
5 
6 #if 0
7 
8 /*You can comment this three lines if you want to use _gettimeofday and _times*/
9 
10 
11 #if defined(_SMALL_PRINTF) || defined(SMALL_SCANF)
12 #define NO_TIME
13 #endif
14 
15 #include <_ansi.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/fcntl.h>
19 #include <stdio.h>
20 
21 #ifndef NO_TIME
22 
23 #include <time.h>
24 #include <sys/time.h>
25 #include <sys/times.h>
26 
27 #endif
28 
29 #include <errno.h>
30 #include <reent.h>
31 #include <unistd.h>
32 #include "swi.h"
33 
34 /* Forward prototypes. */
35 
36 int _system _PARAMS ((const char *));
37 
38 
39 
40 int _rename _PARAMS ((const char *, const char *));
41 int isatty _PARAMS ((int));
42 
43 #ifndef NO_TIME
44 clock_t _times _PARAMS ((struct tms *));
45 int _gettimeofday _PARAMS ((struct timeval *, struct timezone *));
46 #endif
47 
48 
49 void _raise _PARAMS ((void));
50 
51 int _unlink _PARAMS ((void));
52 int _link _PARAMS ((void));
53 
54 int _stat _PARAMS ((const char *, struct stat *));
55 
56 int _fstat _PARAMS ((int, struct stat *));
57 
58 //caddr_t _sbrk _PARAMS ((int));
59 int _getpid _PARAMS ((int));
60 
61 int _kill _PARAMS ((int, int));
62 void _exit _PARAMS ((int));
63 int _close _PARAMS ((int));
64 
65 int _swiclose _PARAMS ((int));
66 int _open _PARAMS ((const char *, int, ...));
67 int _swiopen _PARAMS ((const char *, int));
68 int _write _PARAMS ((int, char *, int));
69 int _swiwrite _PARAMS ((int, char *, int));
70 int _lseek _PARAMS ((int, int, int));
71 int _swilseek _PARAMS ((int, int, int));
72 int _read _PARAMS ((int, char *, int));
73 int _swiread _PARAMS ((int, char *, int));
74 void initialise_monitor_handles _PARAMS ((void));
75 
76 static int wrap _PARAMS ((int));
77 static int error _PARAMS ((int));
78 static int get_errno _PARAMS ((void));
79 static int remap_handle _PARAMS ((int));
80 static int findslot _PARAMS ((int));
81 
82 /* Register name faking - works in collusion with the linker. */
83 register char * stack_ptr asm ("sp");
84 
85 
86 /* following is copied from libc/stdio/local.h to check std streams */
87 extern void _EXFUN(__sinit,(struct _reent *));
88 
89 #ifndef _SMALL_PRINTF
90 #define CHECK_INIT(fp) \
91  do \
92  { \
93  if ((fp)->_data == 0) \
94  (fp)->_data = _REENT; \
95  if (!(fp)->_data->__sdidinit) \
96  __sinit ((fp)->_data); \
97  } \
98  while (0)
99 #endif
100 /* Adjust our internal handles to stay away from std* handles. */
101 #define FILE_HANDLE_OFFSET (0x20)
102 
103 static int std_files_checked;
104 static int monitor_stdin;
105 static int monitor_stdout;
106 static int monitor_stderr;
107 
108 /* Struct used to keep track of the file position, just so we
109  can implement fseek(fh,x,SEEK_CUR). */
110 typedef struct
111 {
112  int handle;
113  int pos;
114 }
115 poslog;
116 
117 #define MAX_OPEN_FILES 20
118 static poslog openfiles [MAX_OPEN_FILES];
119 /*
120 static int findslot (int fh)
121 {
122  int i;
123  for (i = 0; i < MAX_OPEN_FILES; i ++)
124  if (openfiles[i].handle == fh)
125  break;
126  return i;
127 }
128 */
129 
130 /* Function to convert std(in|out|err) handles to internal versions. */
131 /*
132 static int remap_handle (int fh)
133 {
134  if (!std_files_checked)
135  {
136  CHECK_INIT(stdin);
137  CHECK_INIT(stdout);
138  CHECK_INIT(stderr);
139  std_files_checked = 1;
140  }
141  if (fh == STDIN_FILENO)
142  return monitor_stdin;
143  if (fh == STDOUT_FILENO)
144  return monitor_stdout;
145  if (fh == STDERR_FILENO)
146  return monitor_stderr;
147 
148  return fh - FILE_HANDLE_OFFSET;
149 }
150 */
151 
152 /*
153 void
154 initialise_monitor_handles (void)
155 {
156  int i;
157 
158  int fh;
159  const char * name;
160 
161  name = ":tt";
162  asm ("mov r0,%2; mov r1, #0; swi %a1; mov %0, r0"
163  : "=r"(fh)
164  : "i" (SWI_Open),"r"(name)
165  : "r0","r1");
166  monitor_stdin = fh;
167 
168  name = ":tt";
169  asm ("mov r0,%2; mov r1, #4; swi %a1; mov %0, r0"
170  : "=r"(fh)
171  : "i" (SWI_Open),"r"(name)
172  : "r0","r1");
173  monitor_stdout = monitor_stderr = fh;
174 
175  for (i = 0; i < MAX_OPEN_FILES; i ++)
176  openfiles[i].handle = -1;
177 
178  openfiles[0].handle = monitor_stdin;
179  openfiles[0].pos = 0;
180  openfiles[1].handle = monitor_stdout;
181  openfiles[1].pos = 0;
182 }
183 */
184 
185 
186 
187 
188 static int get_errno (void)
189 {
190  asm ("swi %a0" :: "i" (SWI_GetErrno));
191 }
192 
193 static int error (int result)
194 {
195  errno = get_errno ();
196  return result;
197 }
198 
199 
200 
201 static int wrap (int result)
202 {
203  if (result == -1)
204  return error (-1);
205  return result;
206 }
207 
208 #ifndef NO_FILE
209 int _read (int file,
210  char * ptr,
211  int len)
212 {
213  return 0;
214 }
215 
216 int _lseek (int file,
217  int ptr,
218  int dir)
219 {
220  return 0;
221 }
222 
223 extern void __io_putchar( char c );
224 
225 int _write (int file,
226  char * ptr,
227  int len)
228 {
229  int todo;
230 
231  for (todo = 0; todo < len; todo++)
232  {
233  __io_putchar( *ptr++ );
234  }
235 
236  return len;
237 }
238 
239 int _open (const char * path,
240  int flags,
241  ...)
242 {
243  return -1;
244 }
245 
246 int _close (int file)
247 {
248  return -1;
249 }
250 
251 void _exit (int n)
252 {
253  /* FIXME: return code is thrown away. */
254  while(1);
255 }
256 
257 
258 
259 int _kill (int n, int m)
260 {
261  return -1;
262 }
263 
264 
265 #if 0 //VC090825: moved to independent lib std_sbrk.lib
266 caddr_t _sbrk (int incr)
267 {
268  extern char end; /* Defined by the linker */
269  static char *heap_end;
270  char *prev_heap_end;
271 
272  if (heap_end == 0) {
273  heap_end = &end;
274  }
275  prev_heap_end = heap_end;
276  if (heap_end + incr > stack_ptr)
277  {
278  _write (1, "Heap and stack collision\n", 25);
279  abort ();
280  }
281 
282  heap_end += incr;
283  return (caddr_t) prev_heap_end;
284 }
285 #endif //if 0 //VC090825: moved to independent lib std_sbrk.lib
286 
287 #endif
288 
289 #include <sys/stat.h>
290 
291 #ifndef NO_FILE
292 int _fstat(int file, struct stat *st)
293 {
294  st->st_mode = S_IFCHR;
295  return 0;
296 }
297 
298 
299 #endif
300 
301 int _stat (const char *fname, struct stat *st)
302 {
303  st->st_mode = S_IFCHR;
304  return 0;
305 }
306 
307 
308 #ifndef NO_FILE
309 int _link (void)
310 {
311  return -1;
312 }
313 
314 int _unlink (void)
315 {
316  return -1;
317 }
318 #endif
319 
320 void _raise (void)
321 {
322  return;
323 }
324 
325 #ifndef NO_TIME
326 
327 int _gettimeofday (struct timeval * tp, struct timezone * tzp)
328 {
329 
330  if (tp)
331  {
332  /* Ask the host for the seconds since the Unix epoch. */
333  {
334  int value;
335  asm ("swi %a1; mov %0, r0" : "=r" (value): "i" (SWI_Time) : "r0");
336  tp->tv_sec = value;
337  }
338  tp->tv_usec = 0;
339  }
340 
341  /* Return fixed data for the timezone. */
342  if (tzp)
343  {
344  tzp->tz_minuteswest = 0;
345  tzp->tz_dsttime = 0;
346  }
347 
348  return 0;
349 }
350 
351 
352 
353 /* Return a clock that ticks at 100Hz. */
354 clock_t _times (struct tms * tp)
355 {
356  clock_t timeval;
357 
358  asm ("swi %a1; mov %0, r0" : "=r" (timeval): "i" (SWI_Clock) : "r0");
359 
360  if (tp)
361  {
362  tp->tms_utime = timeval; /* user time */
363  tp->tms_stime = 0; /* system time */
364  tp->tms_cutime = 0; /* user time, children */
365  tp->tms_cstime = 0; /* system time, children */
366  }
367 
368  return timeval;
369 };
370 
371 #endif
372 
373 
374 int isatty (int fd)
375 {
376  return 1;
377  fd = fd;
378 }
379 
380 
381 
382 int _system (const char *s)
383 {
384  if (s == NULL)
385  return 0;
386  errno = ENOSYS;
387  return -1;
388 }
389 
390 
391 #ifndef NO_FILE
392 
393 int _rename (const char * oldpath, const char * newpath)
394 {
395  errno = ENOSYS;
396  return -1;
397 }
398 
399 #endif
400 
401 #endif
402 
403