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