source: rtems/cpukit/libcsupport/src/lseek.c @ dfd218d5

4.104.114.84.95
Last change on this file since dfd218d5 was dfd218d5, checked in by Joel Sherrill <joel.sherrill@…>, on 08/16/01 at 22:20:06

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 )
72    return 0;
73
74  /*
75   *  So if the operation failed, we have to restore iop->offset.
76   */
77
78  iop->offset = old_offset;
79  return status;
80}
81
82/*
83 *  _lseek_r
84 *
85 *  This is the Newlib dependent reentrant version of lseek().
86 */
87
88#if defined(RTEMS_NEWLIB)
89
90#include <reent.h>
91
92off_t _lseek_r(
93  struct _reent *ptr,
94  int            fd,
95  off_t          offset,
96  int            whence
97)
98{
99  return lseek( fd, offset, whence );
100}
101#endif
102
Note: See TracBrowser for help on using the repository browser.