Routine to read arbitrary lenght lines from a stdio file desc.

J. Greg Davidson davidson at sdcsvax.UUCP
Fri Dec 27 16:21:11 AEST 1985


#include <stdio.h>

char *
readline( fd )
  FILE *fd;
/* Reads the next line ending in '\n' or EOF from fd.  Stores
   it, NUL terminated, into a malloc'ed string array.  Returns
   the address of the string.	- greg at vis.UUCP
   ( J. Greg Davidson    Virtual Infinity Systems, San Diego )
*/
{
  char *rdln();

  return rdln( fd, (unsigned) 1);
}

static char *
rdln( fd, l )
  FILE *fd;
  unsigned l;
/* See readline.  Call with l == 1. */
{
  int c;
  char *p;
  char *malloc();

  c = getc( fd );
  if (c == '\n' || c == EOF)
    {
      p = malloc( l ) + l;
      *--p = '\0';
    }
  else
    {
      p = rdln( fd, l + 1 );
      *--p = c;
    }
  return p;
}

/*
J. Greg Davidson                          Virtual Infinity Systems
(619) 452-8059               6231 Branting St; San Diego, CA 92122 

greg at vis.uucp                           ucbvax--| telesoft--|
davidson at sdcsvax.uucp                   decvax--+--sdcsvax--+--vis
davidson at ucsd.arpa                       ihnp4--|  noscvax--|
*/



More information about the Comp.sources.unix mailing list