syscalls.c

00001 /* Support files for GNU libc.  Files in the system namespace go here.
00002    Files in the C namespace (ie those that do not start with an
00003    underscore) go in .c.  */
00004 
00005 
00006 #if 0
00007 
00008 /*You can comment this three lines if you want to use _gettimeofday and _times*/
00009 
00010 
00011 #if defined(_SMALL_PRINTF) || defined(SMALL_SCANF)
00012 #define NO_TIME
00013 #endif
00014 
00015 #include <_ansi.h>
00016 #include <sys/types.h>
00017 #include <sys/stat.h>
00018 #include <sys/fcntl.h>
00019 #include <stdio.h>
00020 
00021 #ifndef NO_TIME
00022 
00023 #include <time.h>
00024 #include <sys/time.h>
00025 #include <sys/times.h>
00026 
00027 #endif
00028 
00029 #include <errno.h>
00030 #include <reent.h>
00031 #include <unistd.h>
00032 #include "swi.h"
00033 
00034 /* Forward prototypes.  */
00035 
00036 int     _system     _PARAMS ((const char *));
00037 
00038 
00039 
00040 int     _rename     _PARAMS ((const char *, const char *));
00041 int     isatty          _PARAMS ((int));
00042 
00043 #ifndef NO_TIME
00044 clock_t _times          _PARAMS ((struct tms *));
00045 int     _gettimeofday   _PARAMS ((struct timeval *, struct timezone *));
00046 #endif
00047 
00048 
00049 void    _raise          _PARAMS ((void));
00050 
00051 int     _unlink         _PARAMS ((void));
00052 int     _link           _PARAMS ((void));
00053 
00054 int     _stat           _PARAMS ((const char *, struct stat *));
00055 
00056 int     _fstat          _PARAMS ((int, struct stat *));
00057 
00058 //caddr_t _sbrk         _PARAMS ((int));
00059 int     _getpid         _PARAMS ((int));
00060 
00061 int     _kill           _PARAMS ((int, int));
00062 void    _exit           _PARAMS ((int));
00063 int     _close          _PARAMS ((int));
00064 
00065 int     _swiclose       _PARAMS ((int));
00066 int     _open           _PARAMS ((const char *, int, ...));
00067 int     _swiopen        _PARAMS ((const char *, int));
00068 int     _write          _PARAMS ((int, char *, int));
00069 int     _swiwrite       _PARAMS ((int, char *, int));
00070 int     _lseek          _PARAMS ((int, int, int));
00071 int     _swilseek       _PARAMS ((int, int, int));
00072 int     _read           _PARAMS ((int, char *, int));
00073 int     _swiread        _PARAMS ((int, char *, int));
00074 void    initialise_monitor_handles _PARAMS ((void));
00075 
00076 static int      wrap            _PARAMS ((int));
00077 static int      error           _PARAMS ((int));
00078 static int      get_errno       _PARAMS ((void));
00079 static int      remap_handle    _PARAMS ((int));
00080 static int      findslot        _PARAMS ((int));
00081 
00082 /* Register name faking - works in collusion with the linker.  */
00083 register char * stack_ptr asm ("sp");
00084 
00085 
00086 /* following is copied from libc/stdio/local.h to check std streams */
00087 extern void   _EXFUN(__sinit,(struct _reent *));
00088 
00089 #ifndef _SMALL_PRINTF
00090 #define CHECK_INIT(fp) \
00091   do                                    \
00092     {                                   \
00093       if ((fp)->_data == 0)             \
00094         (fp)->_data = _REENT;           \
00095       if (!(fp)->_data->__sdidinit)     \
00096         __sinit ((fp)->_data);          \
00097     }                                   \
00098   while (0)
00099 #endif
00100 /* Adjust our internal handles to stay away from std* handles.  */
00101 #define FILE_HANDLE_OFFSET (0x20)
00102 
00103 static int std_files_checked;
00104 static int monitor_stdin;
00105 static int monitor_stdout;
00106 static int monitor_stderr;
00107 
00108 /* Struct used to keep track of the file position, just so we
00109    can implement fseek(fh,x,SEEK_CUR).  */
00110 typedef struct
00111 {
00112   int handle;
00113   int pos;
00114 }
00115 poslog;
00116 
00117 #define MAX_OPEN_FILES 20
00118 static poslog openfiles [MAX_OPEN_FILES];
00119 /*
00120 static int findslot (int fh)
00121 {
00122   int i;
00123   for (i = 0; i < MAX_OPEN_FILES; i ++)
00124     if (openfiles[i].handle == fh)
00125       break;
00126   return i;
00127 }
00128 */
00129 
00130 /* Function to convert std(in|out|err) handles to internal versions.  */
00131 /*
00132 static int remap_handle (int fh)
00133 {
00134   if (!std_files_checked)
00135     {
00136        CHECK_INIT(stdin);
00137        CHECK_INIT(stdout);
00138        CHECK_INIT(stderr);
00139        std_files_checked = 1;
00140     }
00141   if (fh == STDIN_FILENO)
00142     return monitor_stdin;
00143   if (fh == STDOUT_FILENO)
00144     return monitor_stdout;
00145   if (fh == STDERR_FILENO)
00146     return monitor_stderr;
00147 
00148   return fh - FILE_HANDLE_OFFSET;
00149 }
00150 */
00151 
00152 /*
00153 void
00154 initialise_monitor_handles (void)
00155 {
00156   int i;
00157   
00158   int fh;
00159   const char * name;
00160 
00161   name = ":tt";
00162   asm ("mov r0,%2; mov r1, #0; swi %a1; mov %0, r0"
00163        : "=r"(fh)
00164        : "i" (SWI_Open),"r"(name)
00165        : "r0","r1");
00166   monitor_stdin = fh;
00167 
00168   name = ":tt";
00169   asm ("mov r0,%2; mov r1, #4; swi %a1; mov %0, r0"
00170        : "=r"(fh)
00171        : "i" (SWI_Open),"r"(name)
00172        : "r0","r1");
00173   monitor_stdout = monitor_stderr = fh;
00174 
00175   for (i = 0; i < MAX_OPEN_FILES; i ++)
00176     openfiles[i].handle = -1;
00177 
00178   openfiles[0].handle = monitor_stdin;
00179   openfiles[0].pos = 0;
00180   openfiles[1].handle = monitor_stdout;
00181   openfiles[1].pos = 0;
00182 }
00183 */
00184 
00185 
00186 
00187 
00188 static int get_errno (void)
00189 {
00190   asm ("swi %a0" :: "i" (SWI_GetErrno));
00191 }
00192 
00193 static int error (int result)
00194 {
00195   errno = get_errno ();
00196   return result;
00197 }
00198 
00199 
00200 
00201 static int wrap (int result)
00202 {
00203   if (result == -1)
00204     return error (-1);
00205   return result;
00206 }
00207 
00208 #ifndef NO_FILE
00209 int _read (int file,
00210        char * ptr,
00211        int len)
00212 {
00213   return 0;
00214 }
00215 
00216 int _lseek (int file,
00217         int ptr,
00218         int dir)
00219 {
00220   return 0;
00221 }
00222 
00223 extern void __io_putchar( char c );
00224 
00225 int _write (int    file,
00226         char * ptr,
00227         int    len)
00228 {
00229    int todo;
00230   
00231    for (todo = 0; todo < len; todo++)
00232    {
00233       __io_putchar( *ptr++ );
00234    }
00235 
00236    return len;
00237 }
00238 
00239 int _open (const char * path,
00240        int          flags,
00241        ...)
00242 {
00243   return -1;
00244 }
00245 
00246 int _close (int file)
00247 {
00248   return -1;
00249 }
00250 
00251 void _exit (int n)
00252 {
00253   /* FIXME: return code is thrown away.  */
00254   while(1);
00255 }
00256 
00257 
00258 
00259 int _kill (int n, int m)
00260 {
00261    return -1;
00262 }
00263 
00264 
00265 #if 0 //VC090825: moved to independent lib std_sbrk.lib
00266 caddr_t _sbrk (int incr)
00267 {
00268  extern char end;               /* Defined by the linker */
00269   static char *heap_end;
00270   char *prev_heap_end;
00271  
00272   if (heap_end == 0) {
00273     heap_end = &end;
00274   }
00275   prev_heap_end = heap_end;
00276   if (heap_end + incr > stack_ptr)
00277     {
00278       _write (1, "Heap and stack collision\n", 25);
00279       abort ();
00280     }
00281 
00282   heap_end += incr;
00283   return (caddr_t) prev_heap_end;
00284 }
00285 #endif //if 0 //VC090825: moved to independent lib std_sbrk.lib
00286         
00287 #endif
00288 
00289 #include <sys/stat.h>
00290 
00291 #ifndef NO_FILE
00292 int _fstat(int file, struct stat *st)
00293 {
00294   st->st_mode = S_IFCHR;
00295   return 0;
00296 }
00297 
00298 
00299 #endif
00300 
00301 int _stat (const char *fname, struct stat *st)
00302 {
00303   st->st_mode = S_IFCHR;
00304   return 0;
00305 }
00306 
00307 
00308 #ifndef NO_FILE
00309 int _link (void)
00310 {
00311   return -1;
00312 }
00313 
00314 int _unlink (void)
00315 {
00316   return -1;
00317 }
00318 #endif
00319 
00320 void _raise (void)
00321 {
00322   return;
00323 }
00324 
00325 #ifndef NO_TIME
00326 
00327 int _gettimeofday (struct timeval * tp, struct timezone * tzp)
00328 {
00329 
00330   if (tp)
00331     {
00332     /* Ask the host for the seconds since the Unix epoch.  */
00333       {
00334         int value;
00335         asm ("swi %a1; mov %0, r0" : "=r" (value): "i" (SWI_Time) : "r0");
00336         tp->tv_sec = value;
00337       }
00338       tp->tv_usec = 0;
00339     }
00340 
00341   /* Return fixed data for the timezone.  */
00342   if (tzp)
00343     {
00344       tzp->tz_minuteswest = 0;
00345       tzp->tz_dsttime = 0;
00346     }
00347 
00348   return 0;
00349 }
00350 
00351 
00352 
00353 /* Return a clock that ticks at 100Hz.  */
00354 clock_t _times (struct tms * tp)
00355 {
00356   clock_t timeval;
00357 
00358   asm ("swi %a1; mov %0, r0" : "=r" (timeval): "i" (SWI_Clock) : "r0");
00359 
00360   if (tp)
00361     {
00362       tp->tms_utime  = timeval; /* user time */
00363       tp->tms_stime  = 0;       /* system time */
00364       tp->tms_cutime = 0;       /* user time, children */
00365       tp->tms_cstime = 0;       /* system time, children */
00366     }
00367   
00368   return timeval;
00369 };
00370 
00371 #endif
00372 
00373 
00374 int isatty (int fd)
00375 {
00376   return 1;
00377   fd = fd;
00378 }
00379 
00380 
00381 
00382 int _system (const char *s)
00383 {
00384   if (s == NULL)
00385     return 0;
00386   errno = ENOSYS;
00387   return -1;
00388 }
00389 
00390 
00391 #ifndef NO_FILE
00392 
00393 int _rename (const char * oldpath, const char * newpath)
00394 {
00395   errno = ENOSYS;
00396   return -1;
00397 }
00398 
00399 #endif
00400 
00401 #endif
00402  
00403 

Generated on Mon Apr 11 14:23:40 2011 for Contiki 2.5 by  doxygen 1.6.1