[07a3253d] | 1 | /* |
---|
| 2 | * write() - POSIX 1003.1b 6.4.2 - Write to 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 | |
---|
| 18 | /* |
---|
| 19 | * write |
---|
| 20 | * |
---|
| 21 | * This routine writes count bytes from from buffer pointed to by buffer |
---|
| 22 | * to the file associated with the open file descriptor, fildes. |
---|
| 23 | */ |
---|
| 24 | |
---|
[e22d644] | 25 | ssize_t write( |
---|
[07a3253d] | 26 | int fd, |
---|
| 27 | const void *buffer, |
---|
[e22d644] | 28 | size_t count |
---|
[07a3253d] | 29 | ) |
---|
| 30 | { |
---|
| 31 | rtems_status_code rc; |
---|
| 32 | rtems_libio_t *iop; |
---|
| 33 | |
---|
[cca4400] | 34 | rtems_libio_check_fd( fd ); |
---|
| 35 | iop = rtems_libio_iop( fd ); |
---|
| 36 | rtems_libio_check_buffer( buffer ); |
---|
| 37 | rtems_libio_check_count( count ); |
---|
| 38 | rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE ); |
---|
| 39 | |
---|
[07a3253d] | 40 | /* |
---|
| 41 | * If this file descriptor is mapped to an external set of handlers, |
---|
| 42 | * then pass the request on to them. |
---|
| 43 | */ |
---|
| 44 | |
---|
[cca4400] | 45 | if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) { |
---|
[07a3253d] | 46 | rtems_libio_write_t fp; |
---|
| 47 | |
---|
[cca4400] | 48 | fp = rtems_libio_handlers[ |
---|
| 49 | (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].write; |
---|
[07a3253d] | 50 | if ( fp == NULL ) |
---|
| 51 | set_errno_and_return_minus_one( EBADF ); |
---|
| 52 | |
---|
| 53 | return (*fp)( fd, buffer, count ); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | /* |
---|
| 57 | * Now process the write() request. |
---|
| 58 | */ |
---|
| 59 | |
---|
| 60 | if ( !iop->handlers->write ) |
---|
| 61 | set_errno_and_return_minus_one( ENOTSUP ); |
---|
| 62 | |
---|
| 63 | rc = (*iop->handlers->write)( iop, buffer, count ); |
---|
| 64 | |
---|
| 65 | if ( rc > 0 ) |
---|
| 66 | iop->offset += rc; |
---|
| 67 | |
---|
| 68 | return rc; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /* |
---|
| 72 | * _write_r |
---|
| 73 | * |
---|
| 74 | * This is the Newlib dependent reentrant version of write(). |
---|
| 75 | */ |
---|
| 76 | |
---|
| 77 | #if defined(RTEMS_NEWLIB) |
---|
| 78 | |
---|
| 79 | #include <reent.h> |
---|
| 80 | |
---|
| 81 | long _write_r( |
---|
| 82 | struct _reent *ptr, |
---|
| 83 | int fd, |
---|
| 84 | const void *buf, |
---|
| 85 | size_t nbytes |
---|
| 86 | ) |
---|
| 87 | { |
---|
| 88 | return write( fd, buf, nbytes ); |
---|
| 89 | } |
---|
| 90 | #endif |
---|