gets.c 371 B

123456789101112131415161718192021222324252627
  1. /*
  2. * gets.c - read a line from a stream
  3. */
  4. /* $Id$ */
  5. #include <stdio.h>
  6. char *
  7. gets(char *s)
  8. {
  9. register FILE *stream = stdin;
  10. register int ch;
  11. register char *ptr;
  12. ptr = s;
  13. while ((ch = getc(stream)) != EOF && ch != '\n')
  14. *ptr++ = ch;
  15. if (ch == EOF) {
  16. if (feof(stream)) {
  17. if (ptr == s) return NULL;
  18. } else return NULL;
  19. }
  20. *ptr = '\0';
  21. return s;
  22. }