source: rtems/c/src/lib/libc/lseek.c @ cbdb144

Last change on this file since cbdb144 was cbdb144, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 16, 2001 at 10:15:35 PM

2001-08-16 Joel Sherrill <joel@…>

  • libc/lseek.c: Modified after discussion with Eugeny S. Mints <jack@…> to correct the behavior. There were two mistakes. First, iop->offset was incorrectly set for SEEK_END. Second, iop->offset should be left unmodified if there are errors. This modification attempts to fix both situations.
  • Property mode set to 100644
File size: 1.7 KB
Line 
1/*
2 *  lseek() - POSIX 1003.1b 6.5.3 - Reposition Read/Write File Offset
3 *
4 *  COPYRIGHT (c) 1989-1999.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.OARcorp.com/rtems/license.html.
10 *
11 *  $Id$
12 */
13
14#include <stdio.h>
15
16#include "libio_.h"
17
18off_t lseek(
19  int     fd,
20  off_t   offset,
21  int     whence
22)
23{
24  rtems_libio_t *iop;
25  off_t          old_offset;
26  off_t          status;
27
28  rtems_libio_check_fd( fd );
29  iop = rtems_libio_iop( fd );
30  rtems_libio_check_is_open(iop);
31
32  /*
33   *  Check as many errors as possible before touching iop->offset.
34   */
35
36  if ( !iop->handlers->lseek )
37    set_errno_and_return_minus_one( ENOTSUP );
38
39  /*
40   *  Now process the lseek().
41   */
42
43  old_offset = iop->offset;
44  switch ( whence ) {
45    case SEEK_SET:
46      iop->offset = offset;
47      break;
48
49    case SEEK_CUR:
50      iop->offset += offset;
51      break;
52
53    case SEEK_END:
54      iop->offset = iop->size + offset;
55      break;
56
57    default:
58      set_errno_and_return_minus_one( EINVAL );
59  }
60
61  /*
62   *  At this time, handlers assume iop->offset has the desired
63   *  new offset.
64   */
65
66  status = (*iop->handlers->lseek)( iop, offset, whence );
67  if ( !status )
68    return 0;
69
70  /*
71   *  So if the operation failed, we have to restore iop->offset.
72   */
73
74  iop->offset = old_offset;
75  return status;
76}
77
78/*
79 *  _lseek_r
80 *
81 *  This is the Newlib dependent reentrant version of lseek().
82 */
83
84#if defined(RTEMS_NEWLIB)
85
86#include <reent.h>
87
88off_t _lseek_r( 
89  struct _reent *ptr,
90  int            fd,
91  off_t          offset,
92  int            whence
93)
94{
95  return lseek( fd, offset, whence );
96}
97#endif
98
Note: See TracBrowser for help on using the repository browser.