common/readline2.c
///////////////////////////////////////////////////////////////////////////////
// Filename: readline2.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: read a line from a file descriptor
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date Time Name Description
// -------- -------- -------- ------------------------------------------------
// 96/03/12 02:26:27 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
/* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include <unistd.h>
// 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 //
/*
* Read a line from a descriptor. Read the line one byte at a time,
* looking for the newline. We store the newline in the buffer,
* then follow it with a null (the same as fgets(3)).
* We return the number of characters up to, but not including,
* the null (the same as strlen(3)).
*
* This function is taken from [Stev90].
*/
int
readline(int fd, char* ptr, int maxlen)
{
int n, rc;
char c;
for (n = 1; n < maxlen; n++)
{
if ( (rc = read(fd, &c, 1)) == 1)
{
*ptr++ = c;
if (c == '\n')
break;
}
else
if (rc == 0)
{
if (n == 1)
return(0); // EOF, no data read
else
break; // EOF, some data was read
}
else
return(-1); // error
}
*ptr = 0;
return(n);
}
// Main /////////////////////////////////////////////////////////////// Main //
/* NONE */