source: rtems/c/src/lib/libc/lseek.c @ 364d75b

4.104.114.84.95
Last change on this file since 364d75b was cc5c4ac, checked in by Joel Sherrill <joel.sherrill@…>, on 08/17/01 at 13:18:01

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.8 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#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdio.h>
19
20#include <rtems/libio_.h>
21
22off_t lseek(
23  int     fd,
24  off_t   offset,
25  int     whence
26)
27{
28  rtems_libio_t *iop;
29  off_t          old_offset;
30  off_t          status;
31
32  rtems_libio_check_fd( fd );
33  iop = rtems_libio_iop( fd );
34  rtems_libio_check_is_open(iop);
35
36  /*
37   *  Check as many errors as possible before touching iop->offset.
38   */
39
40  if ( !iop->handlers->lseek_h )
41    set_errno_and_return_minus_one( ENOTSUP );
42
43  /*
44   *  Now process the lseek().
45   */
46
47  old_offset = iop->offset;
48  switch ( whence ) {
49    case SEEK_SET:
50      iop->offset = offset;
51      break;
52
53    case SEEK_CUR:
54      iop->offset += offset;
55      break;
56
57    case SEEK_END:
58      iop->offset = iop->size + offset;
59      break;
60
61    default:
62      set_errno_and_return_minus_one( EINVAL );
63  }
64
65  /*
66   *  At this time, handlers assume iop->offset has the desired
67   *  new offset.
68   */
69
70  status = (*iop->handlers->lseek_h)( iop, offset, whence );
71  if ( status == (off_t) -1 )
72    iop->offset = old_offset;
73
74  /*
75   *  So if the operation failed, we have to restore iop->offset.
76   */
77
78  return status;
79}
80
81/*
82 *  _lseek_r
83 *
84 *  This is the Newlib dependent reentrant version of lseek().
85 */
86
87#if defined(RTEMS_NEWLIB)
88
89#include <reent.h>
90
91off_t _lseek_r(
92  struct _reent *ptr,
93  int            fd,
94  off_t          offset,
95  int            whence
96)
97{
98  return lseek( fd, offset, whence );
99}
100#endif
101
Note: See TracBrowser for help on using the repository browser.