1 | /* |
---|
2 | * read() - POSIX 1003.1b 6.4.1 - Read From a File |
---|
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 "libio_.h" |
---|
16 | |
---|
17 | /* XXX newlib has the prototype for this wrong. It will be a bit painful */ |
---|
18 | /* XXX to fix so we are choosing to delay fixing this. */ |
---|
19 | |
---|
20 | int read( |
---|
21 | int fd, |
---|
22 | void *buffer, |
---|
23 | unsigned32 count |
---|
24 | ) |
---|
25 | { |
---|
26 | int rc; /* XXX change to a size_t when prototype is fixed */ |
---|
27 | rtems_libio_t *iop; |
---|
28 | |
---|
29 | /* |
---|
30 | * If this file descriptor is mapped to an external set of handlers, |
---|
31 | * then pass the request on to them. |
---|
32 | */ |
---|
33 | |
---|
34 | if ( rtems_file_descriptor_type( fd ) ) { |
---|
35 | rtems_libio_read_t fp; |
---|
36 | |
---|
37 | fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].read; |
---|
38 | if ( fp == NULL ) |
---|
39 | set_errno_and_return_minus_one( EBADF ); |
---|
40 | |
---|
41 | return (*fp)( fd, buffer, count ); |
---|
42 | } |
---|
43 | |
---|
44 | /* |
---|
45 | * Now process the read(). |
---|
46 | */ |
---|
47 | |
---|
48 | iop = rtems_libio_iop( fd ); |
---|
49 | rtems_libio_check_fd( fd ); |
---|
50 | rtems_libio_check_buffer( buffer ); |
---|
51 | rtems_libio_check_count( count ); |
---|
52 | rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ ); |
---|
53 | |
---|
54 | if ( !iop->handlers->read ) |
---|
55 | set_errno_and_return_minus_one( ENOTSUP ); |
---|
56 | |
---|
57 | rc = (*iop->handlers->read)( iop, buffer, count ); |
---|
58 | |
---|
59 | if ( rc > 0 ) |
---|
60 | iop->offset += rc; |
---|
61 | |
---|
62 | return rc; |
---|
63 | } |
---|
64 | |
---|
65 | /* |
---|
66 | * _read_r |
---|
67 | * |
---|
68 | * This is the Newlib dependent reentrant version of read(). |
---|
69 | */ |
---|
70 | |
---|
71 | #if defined(RTEMS_NEWLIB) |
---|
72 | |
---|
73 | #include <reent.h> |
---|
74 | |
---|
75 | _ssize_t _read_r( |
---|
76 | struct _reent *ptr, |
---|
77 | int fd, |
---|
78 | void *buf, |
---|
79 | size_t nbytes |
---|
80 | ) |
---|
81 | { |
---|
82 | return read( fd, buf, nbytes ); |
---|
83 | } |
---|
84 | #endif |
---|