Skip to content

Instantly share code, notes, and snippets.

@uhziel
Created September 30, 2011 15:09
Show Gist options
  • Save uhziel/1254030 to your computer and use it in GitHub Desktop.
Save uhziel/1254030 to your computer and use it in GitHub Desktop.
apue.2e.fig1.10.c
#include "apue.h"
#include <sys/wait.h>
static void sig_int(int signo);
int main()
{
char buf[MAXLINE];
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR)
{
err_sys("signal error.");
}
printf("%% ");
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
{
buf[strlen(buf) - 1] = '\0';
}
pid = fork();
if (pid < 0)
{
err_sys("fork error");
}
else if (pid == 0)
{
/* child */
execlp(buf, buf, (char*)0);
err_ret("couldn't exec: %s", buf);
}
else
{
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
{
err_sys("waitpid error");
}
printf("%% ");
}
}
return 0;
}
void sig_int(int signo)
{
printf("interrupt\n%% ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment