Back to index

glibc  2.9
read.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
00002    Ported to standalone by Joel Sherrill jsherril@redstone-emh2.army.mil,
00003      On-Line Applications Research Corporation.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <unistd.h>
00023 #include <stddef.h>
00024 
00025 #include "filedesc.h"
00026 #include <fcntl.h>
00027 #include <standalone.h>
00028 
00029 /* Read NBYTES into BUF from FD.  Return the number read or -1.  */
00030 ssize_t
00031 __libc_read (int fd, void *buf, size_t nbytes)
00032 {
00033   char *buffer = (char *) buf;
00034   int data;
00035   int poll;
00036 
00037   __set_errno (0);
00038 
00039   if (nbytes == 0)
00040     return 0;
00041 
00042   if ( !__FD_Is_valid( fd ) || !__FD_Table[ fd ].in_use )
00043     {
00044       __set_errno (EBADF);
00045       return -1;
00046     }
00047   if (buf == NULL)
00048     {
00049       __set_errno (EINVAL);
00050       return -1;
00051     }
00052 
00053   if ( __FD_Table[ fd ].flags & O_WRONLY )  /* is it write only? */
00054     {
00055       __set_errno (EBADF);
00056       return -1;
00057     }
00058 
00059   /* If this is a non-blocking fd, then we want to poll the console.  */
00060 
00061   poll = ( __FD_Table[ fd ].flags & O_NONBLOCK ) ? 1 : 0;
00062 
00063   /* Read a single character.  This is a cheap way to insure that the
00064      upper layers get every character because _Console_Getc can't timeout
00065      or otherwise know when to stop.  */
00066 
00067 
00068   data = _Console_Getc(poll);
00069 
00070   if ( data == -1 )                 /* if no data return */
00071     return -1;
00072 
00073   (void) _Console_Putc(data);       /* echo the character */
00074 
00075   if ( data == '\r' ) {            /* translate CR -> CR/LF */
00076     (void) _Console_Putc('\n');
00077     data = '\n';
00078   }
00079 
00080   *buffer = data;
00081   return 1;
00082 }
00083 libc_hidden_def (__libc_read)
00084 weak_alias (__libc_read, __read)
00085 libc_hidden_weak (__read)
00086 weak_alias (__libc_read, read)