APUE_1.10ReadCommandsFromStandardInputAndExecuteThem

使用信号捕抓ctrl + c信号

/*
 * 1.10ReadCommandsFromStandardInputAndExecuteThem.cpp
 *
 *  Created on: Feb 11, 2015
 *      Author: sunyj
 */

#include "../apuesunyj.h"
#include <sys/wait.h>

static void sig_int(int); // our signal-catching function,static limit this function in this file

int main(void)
{
    char buf[MAXLINE];
    pid_t pid;
    int status;
    if (signal(SIGINT, sig_int) == SIG_ERR) // catch the ctrl + c signal
    {
        err_sys("signal error");
    }

    printf("%% "); /* print prompt (printf requires %% to print %) */
    // ctrl + c invoke function sig_int, ctrl + d end this loop
    while (fgets(buf, MAXLINE, stdin) != NULL)
    {
        if (buf[strlen(buf) - 1] == ‘\n‘)
        {
            buf[strlen(buf) - 1] = 0; /* replace newline with null */
        }
        if ((pid = fork()) < 0)
        {
            err_sys("fork error");
        }
        else if (pid == 0)
        {   /* child */
            execlp(buf, buf, (char *) 0);
            err_ret("couldn‘t execute: %s", buf);
            exit(127);
        }
        /* parent */
        if ((pid = waitpid(pid, &status, 0)) < 0)
            err_sys("waitpid error");
        printf("%% ");
    }
    printf("EOF(ctrl + d) received\n");
    printf("bye bye\n");
    exit(0);
}

void sig_int(int signo)
{
    printf("interrupt by signal ctrl + c \n%% ");
}

时间: 2024-10-02 02:37:39

APUE_1.10ReadCommandsFromStandardInputAndExecuteThem的相关文章

APUE_1.9Signals Figure1.10ReadCommandsFromStandardInputAndExecuteThem

/* * 1.10ReadCommandsFromStandardInputAndExecuteThem.cpp * * Created on: Feb 11, 2015 * Author: sunyj */ #include "../apuesunyj.h" #include <sys/wait.h> static void sig_int(int); // our signal-catching function,static limit this function i

APUE_1.5CopyStandardInputToStandardOutputUsingStandardIO

1 /* 2 * 1.5CopyStandardInputToStandardOutputUsingStandardIO.cpp 3 * 4 * Created on: Feb 10, 2015 5 * Author: sunyj 6 */ 7 8 #include "../apuesunyj.h" 9 10 #define BUFFSIZE 4096 11 int main(void) 12 { 13 int n; 14 char buf[BUFFSIZE]; 15 while ((

APUE_1.7ErrorHandling Figure1.8Demonstrate strerror and perror

man 3 errno /* * 1.8DemonstrateStrerrorAndPerror.cpp * * Created on: Feb 11, 2015 * Author: sunyj */ #include "../apuesunyj.h" int main(int argc, char *argv[]) { fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); errno = ENOENT; perror

APUE_1.4CopyStandardInputToStandardOutput

1 // CopyStandardInputToStandardOutput.cpp 2 #include "../apuesunyj.h" 3 4 #define BUFFSIZE 4096 5 6 int main(void) 7 { 8 int n; 9 char buf[BUFFSIZE]; 10 11 while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) 12 { 13 if (write(STDOUT_FILENO,

APUE_1.3ListAllTheFilesInADirectory

1 // 1.3ListAllTheFilesInADirectory.cpp 2 #include <dirent.h> 3 4 #include "../apuesunyj.h" 5 6 int main(int argc, char *argv[]) 7 { 8 DIR *dp; 9 struct dirent *dirp; 10 if (argc != 2) 11 { 12 err_quit("usage: ls directory_name")

APUE_1.6PrintTheProcessID

1 /* 2 * 1.6PrintTheProcessID.cpp 3 * 4 * Created on: Feb 11, 2015 5 * Author: sunyj 6 */ 7 8 #include "../apuesunyj.h" 9 10 int main(void) 11 { 12 printf("hello world from process ID %ld\n", static_cast<long>(getpid())); 13 retu

APUE_1.8DemonstrateStrerrorAndPerror

/* * 1.8DemonstrateStrerrorAndPerror.cpp * * Created on: Feb 11, 2015 * Author: sunyj */ #include "../apuesunyj.h" int main(int argc, char *argv[]) { fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); errno = ENOENT; perror(argv[0]); r