1 | /* |
---|
2 | * lseek() - POSIX 1003.1b 6.5.3 - Reposition Read/Write File Offset |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1998. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * Copyright assigned to U.S. Government, 1994. |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.OARcorp.com/rtems/license.html. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | |
---|
17 | #include "libio_.h" |
---|
18 | |
---|
19 | off_t lseek( |
---|
20 | int fd, |
---|
21 | off_t offset, |
---|
22 | int whence |
---|
23 | ) |
---|
24 | { |
---|
25 | rtems_libio_t *iop; |
---|
26 | |
---|
27 | rtems_libio_check_fd( fd ); |
---|
28 | iop = rtems_libio_iop( fd ); |
---|
29 | rtems_libio_check_is_open(iop); |
---|
30 | |
---|
31 | /* |
---|
32 | * If this file descriptor is mapped to an external set of handlers, |
---|
33 | * then pass the request on to them. |
---|
34 | */ |
---|
35 | |
---|
36 | if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) { |
---|
37 | rtems_libio_lseek_t fp; |
---|
38 | |
---|
39 | fp = rtems_libio_handlers[ |
---|
40 | (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].lseek; |
---|
41 | if ( fp == NULL ) |
---|
42 | set_errno_and_return_minus_one( EBADF ); |
---|
43 | |
---|
44 | return (*fp)( fd, offset, whence ); |
---|
45 | } |
---|
46 | |
---|
47 | /* |
---|
48 | * Now process the lseek(). |
---|
49 | */ |
---|
50 | |
---|
51 | switch ( whence ) { |
---|
52 | case SEEK_SET: |
---|
53 | iop->offset = offset; |
---|
54 | break; |
---|
55 | |
---|
56 | case SEEK_CUR: |
---|
57 | iop->offset += offset; |
---|
58 | break; |
---|
59 | |
---|
60 | case SEEK_END: |
---|
61 | iop->offset = iop->size - offset; |
---|
62 | break; |
---|
63 | |
---|
64 | default: |
---|
65 | errno = EINVAL; |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | if ( !iop->handlers->lseek ) |
---|
70 | set_errno_and_return_minus_one( ENOTSUP ); |
---|
71 | |
---|
72 | return (*iop->handlers->lseek)( iop, offset, whence ); |
---|
73 | } |
---|
74 | |
---|
75 | /* |
---|
76 | * _lseek_r |
---|
77 | * |
---|
78 | * This is the Newlib dependent reentrant version of lseek(). |
---|
79 | */ |
---|
80 | |
---|
81 | #if defined(RTEMS_NEWLIB) |
---|
82 | |
---|
83 | #include <reent.h> |
---|
84 | |
---|
85 | off_t _lseek_r( |
---|
86 | struct _reent *ptr, |
---|
87 | int fd, |
---|
88 | off_t offset, |
---|
89 | int whence |
---|
90 | ) |
---|
91 | { |
---|
92 | return lseek( fd, offset, whence ); |
---|
93 | } |
---|
94 | #endif |
---|
95 | |
---|