home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / fgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1015 b   |  73 lines

  1.  
  2. /* fgets */
  3.  
  4. #include <file.h>
  5. #include "std-guts.h"
  6.  
  7. char * fgets(buf, nbytes, fd)
  8. char * buf;
  9. int nbytes;
  10. struct file * fd;
  11. {
  12.   int c;
  13.   char * b = buf;
  14.   int crflag = 0;        /* more kludgery for eunuchsisms */
  15.   int anything = 0;
  16.  
  17. #ifdef DEBUG
  18.   fprintf(stderr, "fgets(%X, %d, %X)", buf, nbytes, fd);
  19. #endif
  20.  
  21.   if (feof(fd))
  22.     {
  23. #ifdef DEBUG
  24.     fprintf(stderr, "->EOF\n");
  25. #endif
  26.     return(NULL);
  27.     }
  28.  
  29.   for ( ; nbytes > 0 ; nbytes--)
  30.     {
  31.     c = fgetc(fd);
  32. /*
  33. #ifdef DEBUG
  34.     fprintf(stderr, " '%02X'", c);
  35. #endif
  36. */
  37.     if (c == EOF)
  38.         {
  39.         goto done;
  40.         }
  41.     if (c == '\r')
  42.         {
  43.         crflag = 1;
  44.         }
  45.         else
  46.         {
  47.         if (crflag & (c != '\n'))
  48.             *b++ = '\r';
  49.         crflag = 0;    /* fucks up when reading a string of cr's.
  50.                    BFD.  */
  51.  
  52.         anything = 1;
  53.         *b++ = c;
  54.         if (c == '\n')
  55.             goto done;
  56.         }
  57.     }
  58. done:
  59.   *b++ = '\0';
  60.   if (anything)
  61.       return(buf);
  62.     else
  63.         return(NULL);
  64. }
  65.  
  66. char * gets(buf)
  67. char * buf;
  68. {
  69.   fgets(buf, 99999999, stdin);
  70.   return(buf);
  71. }
  72.  
  73.