home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / FGETS.C < prev    next >
Text File  |  1987-10-04  |  2KB  |  61 lines

  1. #define NOCCARGC  /* no arg count passing */
  2. #include stdio.h
  3. #include clib.def
  4. /*
  5. ** Gets an entire string (including its newline
  6. ** terminator) or size-1 characters, whichever comes
  7. ** first. The input is terminated by a null character.
  8. ** Entry: str  = Pointer to destination buffer.
  9. **        size = Size of the destination buffer.
  10. **        fd   = File descriptor of pertinent file.
  11. ** Returns str on success, else NULL.
  12. */
  13. fgets(str, size, fd) char *str; int size, fd; {
  14.   return (Ugets(str, size, fd, 1));
  15.   }
  16.  
  17. /*
  18. ** Gets an entire string from stdin (excluding its newline
  19. ** terminator) or size-1 characters, whichever comes
  20. ** first. The input is terminated by a null character.
  21. ** The user buffer must be large enough to hold the data.
  22. ** Entry: str  = Pointer to destination buffer.
  23. ** Returns str on success, else NULL.
  24. */
  25. gets(str) char *str; {
  26.   return (Ugets(str, 32767, stdin, 0));
  27.   }
  28.  
  29. Ugets(str, size, fd, nl) char *str; int size, fd, nl; {
  30.   int backup;
  31.   char *next;
  32.   next = str;
  33.   while(--size > 0) {
  34.     switch (*next = fgetc(fd)) {
  35.       case  EOF: *next = NULL;
  36.                  if(next == str) return (NULL);
  37.                  return (str);
  38.       case '\n': *(next + nl) = NULL;
  39.                  return (str);
  40.       case  RUB: if(next > str) backup = 1; else backup = 0;
  41.                  goto backout;
  42.       case WIPE: backup = next - str;
  43.         backout:
  44.                  if(iscons(fd)) {
  45.                    fputs("\b \b\b \b", stderr);
  46.                    ++size;
  47.                    while(backup--) {
  48.                      fputs("\b \b", stderr);
  49.                      if(*--next < 32) fputs("\b \b", stderr);
  50.                      ++size;
  51.                      }
  52.                    continue;
  53.                    }
  54.         default: ++next;
  55.       }
  56.     }
  57.   *next = NULL;
  58.   return (str);
  59.   }
  60.  
  61.