os/ossrv/genericopenlibs/cstdlib/LSTDIO/FSEEK.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* FSEEK.C
sl@0
     2
 * 
sl@0
     3
 * Portions Copyright (c) 1990-2004 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 */
sl@0
     6
sl@0
     7
/*
sl@0
     8
 * Copyright (c) 1990 The Regents of the University of California.
sl@0
     9
 * All rights reserved.
sl@0
    10
 *
sl@0
    11
 * Redistribution and use in source and binary forms are permitted
sl@0
    12
 * provided that the above copyright notice and this paragraph are
sl@0
    13
 * duplicated in all such forms and that any documentation,
sl@0
    14
 * advertising materials, and other materials related to such
sl@0
    15
 * distribution and use acknowledge that the software was developed
sl@0
    16
 * by the University of California, Berkeley.  The name of the
sl@0
    17
 * University may not be used to endorse or promote products derived
sl@0
    18
 * from this software without specific prior written permission.
sl@0
    19
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0
    20
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0
    21
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    22
 */
sl@0
    23
sl@0
    24
/*
sl@0
    25
FUNCTION
sl@0
    26
<<fseek>>---set file position
sl@0
    27
sl@0
    28
INDEX
sl@0
    29
	fseek
sl@0
    30
sl@0
    31
ANSI_SYNOPSIS
sl@0
    32
	#include <stdio.h>
sl@0
    33
	int fseek(FILE *<[fp]>, long <[offset]>, int <[whence]>)
sl@0
    34
sl@0
    35
TRAD_SYNOPSIS
sl@0
    36
	#include <stdio.h>
sl@0
    37
	int fseek(<[fp]>, <[offset]>, <[whence]>)
sl@0
    38
	FILE *<[fp]>;
sl@0
    39
	long <[offset]>;
sl@0
    40
	int <[whence]>;
sl@0
    41
sl@0
    42
DESCRIPTION
sl@0
    43
Objects of type <<FILE>> can have a ``position'' that records how much
sl@0
    44
of the file your program has already read.  Many of the <<stdio>> functions
sl@0
    45
depend on this position, and many change it as a side effect.
sl@0
    46
sl@0
    47
You can use <<fseek>> to set the position for the file identified by
sl@0
    48
<[fp]>.  The value of <[offset]> determines the new position, in one
sl@0
    49
of three ways selected by the value of <[whence]> (defined as macros
sl@0
    50
in `<<stdio.h>>'):
sl@0
    51
sl@0
    52
<<SEEK_SET>>---<[offset]> is the absolute file position (an offset
sl@0
    53
from the beginning of the file) desired.  <[offset]> must be positive.
sl@0
    54
sl@0
    55
<<SEEK_CUR>>---<[offset]> is relative to the current file position.
sl@0
    56
<[offset]> can meaningfully be either positive or negative.
sl@0
    57
sl@0
    58
<<SEEK_END>>---<[offset]> is relative to the current end of file.
sl@0
    59
<[offset]> can meaningfully be either positive (to increase the size
sl@0
    60
of the file) or negative.
sl@0
    61
sl@0
    62
See <<ftell>> to determine the current file position.
sl@0
    63
sl@0
    64
RETURNS
sl@0
    65
<<fseek>> returns <<0>> when successful.  If <<fseek>> fails, the
sl@0
    66
result is <<EOF>>.  The reason for failure is indicated in <<errno>>:
sl@0
    67
either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
sl@0
    68
repositioning) or <<EINVAL>> (invalid file position).
sl@0
    69
sl@0
    70
PORTABILITY
sl@0
    71
ANSI C requires <<fseek>>.
sl@0
    72
sl@0
    73
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
sl@0
    74
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
sl@0
    75
*/
sl@0
    76
sl@0
    77
#include <stdio_r.h>
sl@0
    78
#include <time.h>
sl@0
    79
#include <fcntl.h>
sl@0
    80
#include <stdlib_r.h>
sl@0
    81
#include <errno.h>
sl@0
    82
#include <sys/stat.h>
sl@0
    83
#include "LOCAL.H"
sl@0
    84
sl@0
    85
#define	POS_ERR	(-(fpos_t)1)
sl@0
    86
sl@0
    87
/*
sl@0
    88
 * Seek the given file to the given offset.
sl@0
    89
 * `Whence' must be one of the three SEEK_* macros.
sl@0
    90
 */
sl@0
    91
sl@0
    92
/**
sl@0
    93
 Reposition stream's position indicator.
sl@0
    94
@return   If successful the function returns 0. Otherwise it returns nonzero.
sl@0
    95
@param fp Pointer to an open file. 
sl@0
    96
@param offset Number of bytes from origin. 
sl@0
    97
@param whence Initial position from where offset is applied.
sl@0
    98
*/
sl@0
    99
EXPORT_C int
sl@0
   100
fseek (register FILE *fp,long offset,int whence)
sl@0
   101
{
sl@0
   102
  struct _reent *ptr;
sl@0
   103
  fpos_t (*seekfn)(void *, fpos_t, int);
sl@0
   104
  fpos_t target, curoff = -1;
sl@0
   105
  size_t n;
sl@0
   106
  struct stat st;
sl@0
   107
  int havepos;
sl@0
   108
sl@0
   109
  /* Make sure stdio is set up.  */
sl@0
   110
sl@0
   111
  CHECK_INIT (fp);
sl@0
   112
  ptr = fp->_data;
sl@0
   113
sl@0
   114
  /* If we've been doing some writing, and we're in append mode
sl@0
   115
     then we don't really know where the filepos is.  */
sl@0
   116
sl@0
   117
  if (fp->_flags & __SAPP && fp->_flags & __SWR)
sl@0
   118
    {
sl@0
   119
      /* So flush the buffer and seek to the end.  */
sl@0
   120
      fflush (fp);
sl@0
   121
    }
sl@0
   122
sl@0
   123
  /* Have to be able to seek.  */
sl@0
   124
sl@0
   125
  if ((seekfn = fp->_seek) == NULL)
sl@0
   126
    {
sl@0
   127
      ptr->_errno = ESPIPE;	/* ??? */
sl@0
   128
      return EOF;
sl@0
   129
    }
sl@0
   130
sl@0
   131
  /*
sl@0
   132
   * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
sl@0
   133
   * After this, whence is either SEEK_SET or SEEK_END.
sl@0
   134
   */
sl@0
   135
sl@0
   136
  switch (whence)
sl@0
   137
    {
sl@0
   138
    case SEEK_CUR:
sl@0
   139
      /*
sl@0
   140
       * In order to seek relative to the current stream offset,
sl@0
   141
       * we have to first find the current stream offset a la
sl@0
   142
       * ftell (see ftell for details).
sl@0
   143
       */
sl@0
   144
      if (fp->_flags & __SOFF)
sl@0
   145
		curoff = fp->_offset;
sl@0
   146
      else
sl@0
   147
		{
sl@0
   148
		  curoff = (*seekfn) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
sl@0
   149
		  if (curoff == -1L)
sl@0
   150
			return EOF;
sl@0
   151
		}
sl@0
   152
      if (fp->_flags & __SRD)
sl@0
   153
		{
sl@0
   154
		  curoff -= fp->_r;
sl@0
   155
		  if (HASUB (fp))
sl@0
   156
			curoff -= fp->_ur;
sl@0
   157
		}
sl@0
   158
      else if (fp->_flags & __SWR && fp->_p != NULL)
sl@0
   159
		curoff += fp->_p - fp->_bf._base;
sl@0
   160
sl@0
   161
      offset += curoff;
sl@0
   162
      whence = SEEK_SET;
sl@0
   163
      havepos = 1;
sl@0
   164
      break;
sl@0
   165
sl@0
   166
    case SEEK_SET:
sl@0
   167
    case SEEK_END:
sl@0
   168
      havepos = 0;
sl@0
   169
      break;
sl@0
   170
sl@0
   171
    default:
sl@0
   172
      ptr->_errno = EINVAL;
sl@0
   173
      return (EOF);
sl@0
   174
    }
sl@0
   175
sl@0
   176
  /*
sl@0
   177
   * Can only optimise if:
sl@0
   178
   *	reading (and not reading-and-writing);
sl@0
   179
   *	not unbuffered; and
sl@0
   180
   *	this is a `regular' Unix file (and hence seekfn==__sseek).
sl@0
   181
   * We must check __NBF first, because it is possible to have __NBF
sl@0
   182
   * and __SOPT both set.
sl@0
   183
   */
sl@0
   184
sl@0
   185
  if (fp->_bf._base == NULL)
sl@0
   186
    __smakebuf (fp);
sl@0
   187
  if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
sl@0
   188
    goto dumb;
sl@0
   189
  if ((fp->_flags & __SOPT) == 0)
sl@0
   190
    {
sl@0
   191
      if (seekfn != __sseek
sl@0
   192
	  || fp->_file < 0
sl@0
   193
	  || _fstat_r (ptr, fp->_file, &st)
sl@0
   194
	  || (st.st_mode & S_IFMT) != S_IFREG)
sl@0
   195
	{
sl@0
   196
	  fp->_flags |= __SNPT;
sl@0
   197
	  goto dumb;
sl@0
   198
	}
sl@0
   199
#ifdef	HAVE_BLKSIZE
sl@0
   200
      fp->_blksize = st.st_blksize;
sl@0
   201
#else
sl@0
   202
      fp->_blksize = 1024;
sl@0
   203
#endif
sl@0
   204
      fp->_flags |= __SOPT;
sl@0
   205
    }
sl@0
   206
sl@0
   207
  /*
sl@0
   208
   * We are reading; we can try to optimise.
sl@0
   209
   * Figure out where we are going and where we are now.
sl@0
   210
   */
sl@0
   211
sl@0
   212
  if (whence == SEEK_SET)
sl@0
   213
    target = offset;
sl@0
   214
  else
sl@0
   215
    {
sl@0
   216
      if (_fstat_r (ptr, fp->_file, &st))
sl@0
   217
	goto dumb;
sl@0
   218
      target = st.st_size + offset;
sl@0
   219
    }
sl@0
   220
sl@0
   221
  if (!havepos)
sl@0
   222
    {
sl@0
   223
      if (fp->_flags & __SOFF)
sl@0
   224
	curoff = fp->_offset;
sl@0
   225
      else
sl@0
   226
	{
sl@0
   227
	  curoff = (*seekfn) (fp->_cookie, 0L, SEEK_CUR);
sl@0
   228
	  if (curoff == POS_ERR)
sl@0
   229
	    goto dumb;
sl@0
   230
	}
sl@0
   231
      curoff -= fp->_r;
sl@0
   232
      if (HASUB (fp))
sl@0
   233
	curoff -= fp->_ur;
sl@0
   234
    }
sl@0
   235
sl@0
   236
  /*
sl@0
   237
   * Compute the number of bytes in the input buffer (pretending
sl@0
   238
   * that any ungetc() input has been discarded).  Adjust current
sl@0
   239
   * offset backwards by this count so that it represents the
sl@0
   240
   * file offset for the first byte in the current input buffer.
sl@0
   241
   */
sl@0
   242
sl@0
   243
  if (HASUB (fp))
sl@0
   244
    {
sl@0
   245
      n = fp->_up - fp->_bf._base;
sl@0
   246
      curoff -= n;
sl@0
   247
      n += fp->_ur;
sl@0
   248
    }
sl@0
   249
  else
sl@0
   250
    {
sl@0
   251
      n = fp->_p - fp->_bf._base;
sl@0
   252
      curoff -= n;
sl@0
   253
      n += fp->_r;
sl@0
   254
    }
sl@0
   255
sl@0
   256
  /*
sl@0
   257
   * If the target offset is within the current buffer,
sl@0
   258
   * simply adjust the pointers, clear EOF, undo ungetc(),
sl@0
   259
   * and return.  (If the buffer was modified, we have to
sl@0
   260
   * skip this; see fgetline.c.)
sl@0
   261
   */
sl@0
   262
sl@0
   263
  if ((fp->_flags & __SMOD) == 0 &&
sl@0
   264
      target >= curoff && target < (fpos_t)(curoff + n))
sl@0
   265
    {
sl@0
   266
      register int o = target - curoff;
sl@0
   267
sl@0
   268
      fp->_p = fp->_bf._base + o;
sl@0
   269
      fp->_r = n - o;
sl@0
   270
      if (HASUB (fp))
sl@0
   271
	FREEUB (fp);
sl@0
   272
      fp->_flags &= ~__SEOF;
sl@0
   273
      return 0;
sl@0
   274
    }
sl@0
   275
sl@0
   276
  /*
sl@0
   277
   * The place we want to get to is not within the current buffer,
sl@0
   278
   * but we can still be kind to the kernel copyout mechanism.
sl@0
   279
   * By aligning the file offset to a block boundary, we can let
sl@0
   280
   * the kernel use the VM hardware to map pages instead of
sl@0
   281
   * copying bytes laboriously.  Using a block boundary also
sl@0
   282
   * ensures that we only read one block, rather than two.
sl@0
   283
   */
sl@0
   284
sl@0
   285
  curoff = target & ~(fp->_blksize - 1);
sl@0
   286
  if ((*seekfn) (fp->_cookie, curoff, SEEK_SET) == POS_ERR)
sl@0
   287
    goto dumb;
sl@0
   288
  fp->_r = 0;
sl@0
   289
  if (HASUB (fp))
sl@0
   290
    FREEUB (fp);
sl@0
   291
  fp->_flags &= ~__SEOF;
sl@0
   292
  n = target - curoff;
sl@0
   293
  if (n)
sl@0
   294
    {
sl@0
   295
      if (__srefill (fp) || fp->_r < (fpos_t)n)
sl@0
   296
	goto dumb;
sl@0
   297
      fp->_p += n;
sl@0
   298
      fp->_r -= n;
sl@0
   299
    }
sl@0
   300
  return 0;
sl@0
   301
sl@0
   302
  /*
sl@0
   303
   * We get here if we cannot optimise the seek ... just
sl@0
   304
   * do it.  Allow the seek function to change fp->_bf._base.
sl@0
   305
   */
sl@0
   306
sl@0
   307
dumb:
sl@0
   308
  if (fflush (fp) || (*seekfn) (fp->_cookie, offset, whence) == POS_ERR)
sl@0
   309
    return EOF;
sl@0
   310
  /* success: clear EOF indicator and discard ungetc() data */
sl@0
   311
  if (HASUB (fp))
sl@0
   312
    FREEUB (fp);
sl@0
   313
  fp->_p = fp->_bf._base;
sl@0
   314
  fp->_r = 0;
sl@0
   315
  /* fp->_w = 0; *//* unnecessary (I think...) */
sl@0
   316
  fp->_flags &= ~__SEOF;
sl@0
   317
  return 0;
sl@0
   318
}