home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdio / fseek.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  7KB  |  252 lines

  1. /*    $NetBSD: fseek.c,v 1.8 1995/03/05 06:56:09 jtc Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Chris Torek.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. #if 0
  41. static char sccsid[] = "@(#)fseek.c    8.3 (Berkeley) 1/2/94";
  42. #endif
  43. static char rcsid[] = "$NetBSD: fseek.c,v 1.8 1995/03/05 06:56:09 jtc Exp $";
  44. #endif /* LIBC_SCCS and not lint */
  45.  
  46. #define _KERNEL
  47. #include "ixemul.h"
  48.  
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <fcntl.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <errno.h>
  55. #include "local.h"
  56.  
  57. #define    POS_ERR    (-(fpos_t)1)
  58.  
  59. /*
  60.  * Seek the given file to the given offset.
  61.  * `Whence' must be one of the three SEEK_* macros.
  62.  */
  63. int
  64. fseek(fp, offset, whence)
  65.     register FILE *fp;
  66.     long offset;
  67.     int whence;
  68. {
  69.     register fpos_t (*seekfn) __P((void *, fpos_t, int));
  70.     fpos_t target, curoff;
  71.     size_t n;
  72.     struct stat st;
  73.     int havepos;
  74.  
  75.     /*
  76.      * Have to be able to seek.
  77.      */
  78.     if ((seekfn = fp->_seek) == NULL) {
  79.         errno = ESPIPE;            /* historic practice */
  80.         return (EOF);
  81.     }
  82.  
  83.     /*
  84.      * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
  85.      * After this, whence is either SEEK_SET or SEEK_END.
  86.      */
  87.     switch (whence) {
  88.  
  89.     case SEEK_CUR:
  90.         /*
  91.          * In order to seek relative to the current stream offset,
  92.          * we have to first find the current stream offset a la
  93.          * ftell (see ftell for details).
  94.          */
  95.         __sflush(fp);    /* may adjust seek offset on append stream */
  96.         if (fp->_flags & __SOFF)
  97.             curoff = fp->_offset;
  98.         else {
  99.             curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
  100.             if (curoff == -1L)
  101.                 return (EOF);
  102.         }
  103.         if (fp->_flags & __SRD) {
  104.             curoff -= fp->_r;
  105.             if (HASUB(fp))
  106.                 curoff -= fp->_ur;
  107.         } else if (fp->_flags & __SWR && fp->_p != NULL)
  108.             curoff += fp->_p - fp->_bf._base;
  109.  
  110.         offset += curoff;
  111.         whence = SEEK_SET;
  112.         havepos = 1;
  113.         break;
  114.  
  115.     case SEEK_SET:
  116.     case SEEK_END:
  117.         curoff = 0;        /* XXX just to keep gcc quiet */
  118.         havepos = 0;
  119.         break;
  120.  
  121.     default:
  122.         errno = EINVAL;
  123.         return (EOF);
  124.     }
  125.  
  126.     /*
  127.      * Can only optimise if:
  128.      *    reading (and not reading-and-writing);
  129.      *    not unbuffered; and
  130.      *    this is a `regular' Unix file (and hence seekfn==__sseek).
  131.      * We must check __NBF first, because it is possible to have __NBF
  132.      * and __SOPT both set.
  133.      */
  134.     if (fp->_bf._base == NULL)
  135.         __smakebuf(fp);
  136.     if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
  137.         goto dumb;
  138.     if ((fp->_flags & __SOPT) == 0) {
  139.         if (seekfn != __sseek ||
  140.             fp->_file < 0 || fstat(fp->_file, &st) ||
  141.             (st.st_mode & S_IFMT) != S_IFREG) {
  142.             fp->_flags |= __SNPT;
  143.             goto dumb;
  144.         }
  145.         fp->_blksize = st.st_blksize;
  146.         fp->_flags |= __SOPT;
  147.     }
  148.  
  149.     /*
  150.      * We are reading; we can try to optimise.
  151.      * Figure out where we are going and where we are now.
  152.      */
  153.     if (whence == SEEK_SET)
  154.         target = offset;
  155.     else {
  156.         if (fstat(fp->_file, &st))
  157.             goto dumb;
  158.         target = st.st_size + offset;
  159.     }
  160.  
  161.     if (!havepos) {
  162.         if (fp->_flags & __SOFF)
  163.             curoff = fp->_offset;
  164.         else {
  165.             curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
  166.             if (curoff == POS_ERR)
  167.                 goto dumb;
  168.         }
  169.         curoff -= fp->_r;
  170.         if (HASUB(fp))
  171.             curoff -= fp->_ur;
  172.     }
  173.  
  174.     /*
  175.      * Compute the number of bytes in the input buffer (pretending
  176.      * that any ungetc() input has been discarded).  Adjust current
  177.      * offset backwards by this count so that it represents the
  178.      * file offset for the first byte in the current input buffer.
  179.      */
  180.     if (HASUB(fp)) {
  181.         curoff += fp->_r;    /* kill off ungetc */
  182.         n = fp->_up - fp->_bf._base;
  183.         curoff -= n;
  184.         n += fp->_ur;
  185.     } else {
  186.         n = fp->_p - fp->_bf._base;
  187.         curoff -= n;
  188.         n += fp->_r;
  189.     }
  190.  
  191.     /*
  192.      * If the target offset is within the current buffer,
  193.      * simply adjust the pointers, clear EOF, undo ungetc(),
  194.      * and return.  (If the buffer was modified, we have to
  195.      * skip this; see fgetln.c.)
  196.      */
  197.     if ((fp->_flags & __SMOD) == 0 &&
  198.         target >= curoff && target < curoff + n) {
  199.         register int o = target - curoff;
  200.  
  201.         fp->_p = fp->_bf._base + o;
  202.         fp->_r = n - o;
  203.         if (HASUB(fp))
  204.             FREEUB(fp);
  205.         fp->_flags &= ~__SEOF;
  206.         return (0);
  207.     }
  208.  
  209.     /*
  210.      * The place we want to get to is not within the current buffer,
  211.      * but we can still be kind to the kernel copyout mechanism.
  212.      * By aligning the file offset to a block boundary, we can let
  213.      * the kernel use the VM hardware to map pages instead of
  214.      * copying bytes laboriously.  Using a block boundary also
  215.      * ensures that we only read one block, rather than two.
  216.      */
  217.     curoff = target & ~(fp->_blksize - 1);
  218.     if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
  219.         goto dumb;
  220.     fp->_r = 0;
  221.      fp->_p = fp->_bf._base;
  222.     if (HASUB(fp))
  223.         FREEUB(fp);
  224.     fp->_flags &= ~__SEOF;
  225.     n = target - curoff;
  226.     if (n) {
  227.         if (__srefill(fp) || fp->_r < n)
  228.             goto dumb;
  229.         fp->_p += n;
  230.         fp->_r -= n;
  231.     }
  232.     return (0);
  233.  
  234.     /*
  235.      * We get here if we cannot optimise the seek ... just
  236.      * do it.  Allow the seek function to change fp->_bf._base.
  237.      */
  238. dumb:
  239.     if (__sflush(fp) ||
  240.         (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
  241.         return (EOF);
  242.     }
  243.     /* success: clear EOF indicator and discard ungetc() data */
  244.     if (HASUB(fp))
  245.         FREEUB(fp);
  246.     fp->_p = fp->_bf._base;
  247.     fp->_r = 0;
  248.     /* fp->_w = 0; */    /* unnecessary (I think...) */
  249.     fp->_flags &= ~__SEOF;
  250.     return (0);
  251. }
  252.