fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAXLINE 1000 /*max line length */
  6.  
  7. int main(void) {
  8. char *s = malloc(MAXLINE);
  9. if (!s) {
  10. perror("malloc");
  11. exit(EXIT_FAILURE);
  12. }
  13. while(fgets(s, MAXLINE, stdin)) {
  14. char *newline = strchr(s, '\n');
  15. if (newline)
  16. *newline = '\0';
  17. printf("got: '%s'\n", s);
  18. if (!strcmp(s, "exit"))
  19. exit(EXIT_SUCCESS);
  20. }
  21. return feof(stdin) ? EXIT_SUCCESS : EXIT_FAILURE;
  22. }
Success #stdin #stdout 0s 2304KB
stdin
some input
do not exit yet
exit
won't be seen
stdout
got: 'some input'
got: 'do not exit yet'
got: 'exit'