home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / lseek.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  6.9 KB  |  282 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ *  @;
  7.  
  8.  
  9. 1.1
  10. date    92.05.14.19.55.40;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @lseek(2) function, does file expansion if necessary
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  *  This file is part of ixemul.library for the Amiga.
  27.  *  Copyright (C) 1991, 1992  Markus M. Wild
  28.  *
  29.  *  This library is free software; you can redistribute it and/or
  30.  *  modify it under the terms of the GNU Library General Public
  31.  *  License as published by the Free Software Foundation; either
  32.  *  version 2 of the License, or (at your option) any later version.
  33.  *
  34.  *  This library is distributed in the hope that it will be useful,
  35.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  37.  *  Library General Public License for more details.
  38.  *
  39.  *  You should have received a copy of the GNU Library General Public
  40.  *  License along with this library; if not, write to the Free
  41.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42.  *
  43.  *  $Id$
  44.  *
  45.  *  $Log$
  46.  */
  47.  
  48. #define KERNEL
  49. #include "ixemul.h"
  50.  
  51. #ifdef DEBUG
  52. #define DP(a) kprintf a
  53. #else
  54. #define DP(a)
  55. #endif
  56.  
  57.  
  58. static inline int
  59. __extend_file (struct file *f, int add_to_eof, int *err)
  60. {
  61.   int res = 0;
  62.   int buf_size, written;
  63.   char *buf;
  64.  
  65.   /* now you know perhaps, that starting with OS2.0 there's a packet that
  66.      sets the size of a file (used in [f]truncate()). Too bad, that the 
  67.      following statement from the autodocs render this packet useless... :
  68.  
  69.      `` Do NOT count on any specific values to be in the extended area. ''
  70.    
  71.      Since **IX requires the new area to be zero'd, we can just as well
  72.      write zeros without the use of the new packet ;-(( */
  73.   
  74.       
  75.   buf_size = add_to_eof > 32*1024 ? 32*1024 : add_to_eof;
  76.   while (! (buf = (char *) kmalloc (buf_size)) && buf_size)
  77.     buf_size >>= 1;
  78.   if (buf)
  79.     {
  80.       bzero (buf, buf_size);
  81.  
  82.       for (written = 0; written < add_to_eof; )
  83.         {
  84.       SendPacket3(f,__rwport,ACTION_WRITE,f->f_fh->fh_Arg1,(long)buf,buf_size);
  85.       __wait_packet(&f->f_sp);
  86.       res = LastResult (f);
  87.           if (res < 0)
  88.             {
  89.               *err = LastError (f);
  90.               break;
  91.             }
  92.           written += res;
  93.           buf_size = add_to_eof - written > buf_size ? 
  94.                buf_size : add_to_eof - written;
  95.     }
  96.       kfree (buf);
  97.     }
  98.   else
  99.     {
  100.       *err = ENOMEM;
  101.       res = -1;
  102.     }
  103.  
  104.   if (res >= 0)
  105.     {
  106.       SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  107.      __wait_packet (&f->f_sp);
  108.       LastError(f) = 0;
  109.       SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  110.      __wait_packet (&f->f_sp);
  111.      res = LastError (f) ? -1 : LastResult (f);
  112.      *err = __ioerr_to_errno (LastError (f));
  113.    }
  114.  
  115.   return res;
  116. }
  117.  
  118.  
  119. off_t
  120. lseek (int fd, off_t off, int dir)
  121. {
  122.   struct file *f = u.u_ofile[fd];
  123.   int omask;
  124.   int err, res;
  125.   int previous_pos, shouldbe_pos;
  126.  
  127.   /* if this is an open fd */
  128.   if (fd >= 0 && fd < NOFILE && f)
  129.     {
  130.       if (f->f_type == DTYPE_FILE)
  131.     {
  132.           if (HANDLER_NIL(f))
  133.         {
  134.           errno = ESPIPE;
  135.           return -1;
  136.         }
  137.  
  138.       omask = syscall (SYS_sigsetmask, ~0);
  139.       __get_file (f);
  140.       __wait_packet(&f->f_sp);
  141.  
  142.       /* there's a subtle difference between Unix lseek() and AmigaDOS
  143.        * Seek(): lseek() returns the *current* position of the `pointer',
  144.        * Seek() returns the *previous* position. So in some cases, we
  145.        * have to do another Seek() to find out where we are.
  146.        * Thanks Mike for pointing me at this! */
  147.  
  148.       switch (dir)
  149.         {
  150.         case SEEK_SET:
  151.           /* previous doesn't matter, don't need to seek */
  152.           previous_pos = 0;
  153.           break;
  154.  
  155.         case SEEK_CUR:
  156.           /* first find out current position */
  157.           LastError(f) = 0;
  158.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_CURRENT);
  159.           __wait_packet (&f->f_sp);
  160.           if (LastError(f) || LastResult(f) < 0)
  161.             {
  162.               err = __ioerr_to_errno(LastError(f));
  163.               previous_pos = -1;
  164.             }
  165.           else
  166.             previous_pos = LastResult(f);
  167.           break;
  168.           
  169.         case SEEK_END:
  170.           /* first find out end position (have to do twice.. argl) */
  171.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  172.           __wait_packet (&f->f_sp);
  173.           LastError(f) = 0;
  174.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_CURRENT);
  175.           __wait_packet (&f->f_sp);
  176.           if (LastError(f) || LastResult(f) < 0)
  177.             {
  178.               err = __ioerr_to_errno(LastError(f));
  179.               previous_pos = -1;
  180.             }
  181.           else
  182.             previous_pos = LastResult(f);
  183.           break;
  184.         }
  185.       
  186.       shouldbe_pos = previous_pos + off;
  187.       if (shouldbe_pos < 0)
  188.         {
  189.           /* that way we make sure that invalid seek errors later result
  190.              from seeking past eof, so we can enlarge the file */
  191.  
  192.           err = EINVAL;
  193.           res = -1;
  194.         }
  195.       else if (previous_pos >= 0)
  196.         {
  197.           /* reset the error field */
  198.           LastError(f) = 0;
  199.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,off,dir-1);
  200.           __wait_packet(&f->f_sp);
  201.  
  202.           if (LastError (f) == ERROR_SEEK_ERROR)
  203.             {
  204.               /* in this case, assume the user wanted to seek past eof.
  205.                  Thus get the current eof position, so that we can
  206.                  tell __extend_file how much to enlarge the file */
  207.           
  208.           LastError(f) = 0;
  209.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  210.           __wait_packet (&f->f_sp);
  211.             }
  212.       
  213.           if (LastError (f) || LastResult (f) < 0)
  214.             {
  215.               err = __ioerr_to_errno(LastError(f));
  216.               res = -1;
  217.             }
  218.           else
  219.             {
  220.               err = 0;
  221.  
  222.               if (previous_pos != shouldbe_pos)
  223.             {
  224.               SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_CURRENT);
  225.               __wait_packet (&f->f_sp);
  226.               if (LastError (f) || LastResult(f) < 0)
  227.                 {
  228.                       err = __ioerr_to_errno(LastError(f));
  229.                       res = -1;
  230.                     }
  231.               else
  232.                     res = LastResult(f);
  233.  
  234.                   if (res >= 0 && res < shouldbe_pos && (f->f_flags & FWRITE))
  235.                     {
  236.                       /* extend the file... */
  237.                   res = __extend_file (f, shouldbe_pos - res, &err);
  238.                 }
  239.             }
  240.           else
  241.             res = shouldbe_pos;
  242.             }
  243.         }
  244.       else
  245.         res = -1;
  246.       
  247.       LastResult(f) = 0;
  248.       __release_file (f);
  249.       syscall (SYS_sigsetmask, omask);
  250.       errno = err;
  251.       return res;
  252.     }
  253.       else if (f->f_type == DTYPE_MEM)
  254.     {
  255.       int real_off;
  256.       int old_off;
  257.  
  258.       omask = syscall (SYS_sigsetmask, ~0);
  259.       __get_file (f);
  260.       old_off = f->f_mf.mf_offset;
  261.       
  262.       real_off = (dir == L_SET ? off : 
  263.               (dir == L_INCR ? 
  264.                old_off + off : f->f_stb.st_size + off));
  265.       if (real_off < 0) real_off = 0;
  266.       else if (real_off > f->f_stb.st_size) real_off = f->f_stb.st_size;
  267.       f->f_mf.mf_offset = real_off;
  268.       __release_file (f);
  269.       syscall (SYS_sigsetmask, omask);
  270.       return old_off;
  271.     }
  272.       else
  273.     errno = ESPIPE;
  274.     }
  275.   else
  276.     errno = EBADF;
  277.  
  278.   return -1;
  279. }
  280.  
  281. @
  282.