home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / dlibssrc / fgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-05  |  649 b   |  24 lines

  1. #include <stdio.h>
  2.  
  3. char *fgets(data, limit, fp)
  4. char *data;
  5. register int limit;
  6. register FILE *fp;
  7. /*
  8.  *    Get data from <fp> and puts it in the <data> buffer.  At most,
  9.  *    <limit>-1 characters will be read.  Input will also be terminated
  10.  *    when a newline is read.  <data> will be '\0' terminated and the
  11.  *    newline, if read, will be included.  A pointer to the start of
  12.  *    <data> is returned, or NULL for EOF.
  13.  */
  14. {
  15.     register char *p = data;
  16.     register int c;
  17.  
  18.     while((--limit > 0) && ((c = fgetc(fp)) != EOF))
  19.         if((*p++ = c) == '\n')
  20.             break;
  21.     *p = '\0';
  22.     return((c == EOF && p == data) ? NULL : data);    /* NULL == EOF */
  23. }
  24.