Back to index

glibc  2.9
Functions
read.c File Reference
#include <errno.h>
#include <unistd.h>
#include <stddef.h>
#include "filedesc.h"
#include <fcntl.h>
#include <standalone.h>

Go to the source code of this file.

Functions

ssize_t __libc_read (int fd, void *buf, size_t nbytes)

Function Documentation

ssize_t __libc_read ( int  fd,
void *  buf,
size_t  nbytes 
)

Definition at line 31 of file read.c.

{
  char *buffer = (char *) buf;
  int data;
  int poll;

  __set_errno (0);

  if (nbytes == 0)
    return 0;

  if ( !__FD_Is_valid( fd ) || !__FD_Table[ fd ].in_use )
    {
      __set_errno (EBADF);
      return -1;
    }
  if (buf == NULL)
    {
      __set_errno (EINVAL);
      return -1;
    }

  if ( __FD_Table[ fd ].flags & O_WRONLY )  /* is it write only? */
    {
      __set_errno (EBADF);
      return -1;
    }

  /* If this is a non-blocking fd, then we want to poll the console.  */

  poll = ( __FD_Table[ fd ].flags & O_NONBLOCK ) ? 1 : 0;

  /* Read a single character.  This is a cheap way to insure that the
     upper layers get every character because _Console_Getc can't timeout
     or otherwise know when to stop.  */


  data = _Console_Getc(poll);

  if ( data == -1 )                 /* if no data return */
    return -1;

  (void) _Console_Putc(data);       /* echo the character */

  if ( data == '\r' ) {            /* translate CR -> CR/LF */
    (void) _Console_Putc('\n');
    data = '\n';
  }

  *buffer = data;
  return 1;
}

Here is the call graph for this function: