Subversion Repositories Kolibri OS

Rev

Rev 4872 | Rev 4921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Reentrant versions of lseek system call. */
  2.  
  3. #include <reent.h>
  4. #include <unistd.h>
  5. #include <_syslist.h>
  6. #include <errno.h>
  7.  
  8. /* Some targets provides their own versions of this functions.  Those
  9.    targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS.  */
  10.  
  11. #ifdef _REENT_ONLY
  12. #ifndef REENTRANT_SYSCALLS_PROVIDED
  13. #define REENTRANT_SYSCALLS_PROVIDED
  14. #endif
  15. #endif
  16.  
  17. #ifndef REENTRANT_SYSCALLS_PROVIDED
  18.  
  19. #pragma pack(push, 1)
  20. typedef struct
  21. {
  22.   char sec;
  23.   char min;
  24.   char hour;
  25.   char rsv;
  26. }detime_t;
  27.  
  28. typedef struct
  29. {
  30.   char  day;
  31.   char  month;
  32.   short year;
  33. }dedate_t;
  34.  
  35. typedef struct
  36. {
  37.   unsigned    attr;
  38.   unsigned    flags;
  39.   union
  40.   {
  41.      detime_t  ctime;
  42.      unsigned  cr_time;
  43.   };
  44.   union
  45.   {
  46.      dedate_t  cdate;
  47.      unsigned  cr_date;
  48.   };
  49.   union
  50.   {
  51.      detime_t  atime;
  52.      unsigned  acc_time;
  53.   };
  54.   union
  55.   {
  56.      dedate_t  adate;
  57.      unsigned  acc_date;
  58.   };
  59.   union
  60.   {
  61.      detime_t  mtime;
  62.      unsigned  mod_time;
  63.   };
  64.   union
  65.   {
  66.      dedate_t  mdate;
  67.      unsigned  mod_date;
  68.   };
  69.   unsigned    size;
  70.   unsigned    size_high;
  71. } FILEINFO;
  72.  
  73. #pragma pack(pop)
  74.  
  75.  
  76. extern unsigned  __NFiles;
  77.  
  78. #define __handle_check( __h, __r )                \
  79.         if( (__h) < 0  ||  (__h) > __NFiles ) {   \
  80.            ptr->_errno =  EBADF ;                 \
  81.            return( __r );                         \
  82.         }
  83.  
  84.  
  85. /*
  86. FUNCTION
  87.         <<_lseek_r>>---Reentrant version of lseek
  88.  
  89. INDEX
  90.         _lseek_r
  91.  
  92. ANSI_SYNOPSIS
  93.         #include <reent.h>
  94.         off_t _lseek_r(struct _reent *<[ptr]>,
  95.                        int <[fd]>, off_t <[pos]>, int <[whence]>);
  96.  
  97. TRAD_SYNOPSIS
  98.         #include <reent.h>
  99.         off_t _lseek_r(<[ptr]>, <[fd]>, <[pos]>, <[whence]>)
  100.         struct _reent *<[ptr]>;
  101.         int <[fd]>;
  102.         off_t <[pos]>;
  103.         int <[whence]>;
  104.  
  105. DESCRIPTION
  106.         This is a reentrant version of <<lseek>>.  It
  107.         takes a pointer to the global data block, which holds
  108.         <<errno>>.
  109. */
  110.  
  111. _off_t
  112. _DEFUN (_lseek_r, (ptr, fd, pos, whence),
  113.      struct _reent *ptr _AND
  114.      int fd _AND
  115.      _off_t pos _AND
  116.      int whence)
  117. {
  118.     _off_t ret;
  119.     __file_handle *fh;
  120.  
  121.     __handle_check( fd, -1 );
  122.     fh = (__file_handle*) __getOSHandle( fd );
  123.  
  124.     switch(whence)
  125.     {
  126.         case SEEK_SET:
  127.             ret = pos;
  128.             break;
  129.         case SEEK_CUR:
  130.             ret = fh->offset + pos;
  131.             break;
  132.         case SEEK_END:
  133.         {
  134.             FILEINFO info;
  135.             get_fileinfo(fh->name, &info);
  136.             ret = pos + info.size;
  137.             break;
  138.         }
  139.         default:
  140.             ptr->_errno = EINVAL;
  141.             return -1;
  142.     };
  143.  
  144.     fh->offset = ret;
  145.  
  146.     return( ret );
  147. }
  148.  
  149. _off_t
  150. _DEFUN (lseek, (fd, pos, whence),
  151.      int fd _AND
  152.      _off_t pos _AND
  153.      int whence)
  154.  
  155. {
  156.     return _lseek_r(_REENT, fd, pos, whence);
  157. };
  158.  
  159. #endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */
  160.