common/readn.c
///////////////////////////////////////////////////////////////////////////////
// Filename: readn.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: simulate normal I/O read() for sockets, pipes,...
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date Time Name Description
// -------- -------- -------- ------------------------------------------------
// 96/03/12 01:09:58 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
/* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
// Local headers ///////////////////////////////////////////// Local headers //
/* NONE */
// Macros /////////////////////////////////////////////////////////// Macros //
/* NONE */
// File scope objects /////////////////////////////////// File scope objects //
/* NONE */
// External variables, functions, and classes ///////////// External objects //
/* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
/* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
/* NONE */
// Functions and class implementation /// Functions and class implementation //
/*
Taken from [Stev92], page 406: (code from page 408)
------------------------------------------------------------------------------
Some devices, notably terminals, networks, and any SVR4 streams devices,
have the following two properties.
1. A "read" operation may return less than asked for, even though we have not
encountered the end of file. This is not an error, and we should just
continue reading from the device.
2. A "write" operation cam also return less than we specified. This may be
caused by flow control constraints by downstream modules, for example.
Again, it's not an error, and we should continue writing the reminder of
the data. (Normally this short return from "write" only occurs with a
nonblocking descriptor if a signal is caught.)
------------------------------------------------------------------------------
*/
// normal read arguments
int
readn(int fd, char* ptr, int numbytes)
{
int nleft, nread;
nleft = numbytes;
while (nleft > 0)
{
nread = read(fd, ptr, nleft);
if (nread < 0)
return(nread); /* error, return < 0 */
else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return(numbytes - nleft); /* return >= 0 */
}
// Main /////////////////////////////////////////////////////////////// Main //
/* NONE */