From: K-mart Cashier on 7 Feb 2010 11:43 I have a program that continuously reads in a text file. What I want to do is have the program exit when I press either ctrl-z or ctrl-c. Here what I've attempt so far. [cdalten(a)localhost oakland]$ more feed.c #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <signal.h> int do_loop(void) { FILE *fp; char line[BUFSIZ]; int status; if ((fp = popen("cat test.txt", "r")) == NULL ) { fprintf(stderr, "pipe error\n"); exit(1); } if ((fgets(line, BUFSIZ, fp)) == NULL) { fprintf(stderr, "can't open file\n"); } status = pclose(fp); printf("Process exited? %s\n", WIFEXITED(status) ? "yes" : "no"); printf("Process status: %d\n", WEXITSTATUS(status)); if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGQUIT)) { printf("caught signal %d \n", status); } return status; } int main(void) { fd_set wfds; struct timeval tv; int retval; FD_ZERO(&wfds); FD_SET(0, &wfds); tv.tv_sec = 5; tv.tv_usec = 0; retval = select(1, NULL, &wfds, NULL, &tv); while(1) { if (retval) { if (do_loop() > 0) exit(1); retval = select(1, NULL, &wfds, NULL, &tv); } } /*end retval*/ return 0; } [cdalten(a)localhost oakland]$ gcc -Wall -Wextra feed.c -o feed And here is what happens when I run it and then press ctrl-c. [cdalten(a)localhost oakland]$ ./feed Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 Process exited? yes Process status: 0 [cdalten(a)localhost oakland]$ How would I go about trapping ctrl-c/ctrl-z in this case? Chad
|
Pages: 1 Prev: How do I send a fd over TCP? Next: ANN: Seed7 Release 2010-02-07 |