home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / C80TCOG.ZIP / GETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-08  |  1.3 KB  |  63 lines

  1. /* gets - get string from standard input or file */
  2. /* Version 1.1 1982/11/25 21:08            */
  3. /*
  4.     Kernighan & Ritchie, "The C Programming
  5. Language", Prentice-Hall, Inc. Englewood Cliffs, NJ
  6. 1978, p. 155.
  7.  
  8.     submitted by    William G. Hutchison, Jr.
  9.             P.O. Box 278
  10.             Exton, PA 19341-0278
  11.             U.S.A.
  12.  
  13.             CompuServe 70665,1307
  14.  */
  15.  
  16. #ifdef MAINLY
  17. #else
  18. #include "a:c80def.h"
  19. #endif
  20.  
  21. #ifdef UNIX
  22. char *
  23. #endif
  24. gets(s, n)        /* read at most n chars from
  25.                STDIN into string s,
  26.                removing terminal NEWLINE. */
  27. register char *s;
  28. register int n;
  29. {
  30. register int c;
  31. register char *cs;
  32.  
  33. cs= s;
  34. while (--n > 0 && (c= getchar()) != EOF)
  35.     if ((*cs++ = c) == NEWLINE) {
  36.         cs--    /* remove NEWLINE    */;
  37.         break;
  38.         }
  39. *cs= EOS;
  40. return ((c == EOF && cs == s)? NULL: s);
  41. }            /* end gets        */
  42.  
  43. #ifdef UNIX
  44. char *
  45. #endif
  46. fgets(s, n, iop)    /* read at most n chars from
  47.                file iop into string s */
  48. register char *s;
  49. register int n;
  50. register FILE *iop;
  51. {
  52. register int c;
  53. register char *cs;
  54.  
  55. cs= s;
  56. while (--n > 0 && (c= getc(iop)) != EOF)
  57.     if ((*cs++ = c) == NEWLINE) {
  58.         break;
  59.         }
  60. *cs= EOS;
  61. return ((c == EOF && cs == s)? NULL: s);
  62. }            /* end fgets        */
  63.