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 ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
16     {
17         if (write(STDOUT_FILENO, buf, n) != n)
18         {
19             err_sys("write error");
20         }
21     }
22     if (n < 0)
23     {
24         err_sys("read error");
25     }
26     exit(0);
27 }
 1 // apuesunyj.h
 2 #ifndef APUE_SUNYJ
 3 #define APUE_SUNYJ
 4
 5 #include <errno.h>
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <stdarg.h>
 9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <unistd.h>
12
13 void err_quit(const char *fmt, ...);
14 void err_sys(const char *fmt, ...);
15
16
17 #endif
 1 // error.cpp
 2 #include "apuesunyj.h"
 3
 4 int64_t const MAXLINE = 4096; // max line length
 5 /*
 6  *  * Print a message and return to caller.
 7  *   * Caller specifies "errnoflag".
 8  *    */
 9 static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
10 {
11     char buf[MAXLINE];
12
13     vsnprintf(buf, MAXLINE, fmt, ap);
14     if (errnoflag)
15         snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s", strerror(
16                     error));
17     strcat(buf, "\n");
18     fflush(stdout);/* in case stdout and stderr are the same */
19     fputs(buf, stderr);
20     fflush(NULL);/* flushes all stdio output streams */
21 }
22
23 /*
24  *  * Fatal error unrelated to a system call.
25  *   * Print a message and terminate.
26  *    */
27 void err_quit(const char *fmt, ...)
28 {
29     va_list ap;
30
31     va_start(ap, fmt);
32     err_doit(0, 0, fmt, ap);
33     va_end(ap);
34     exit(1) ; // process terminate, not just like return, totally different
35 }
36
37 /*
38  *  * Fatal error related to a system call.
39  *   * Print a message and terminate.
40  *    */
41 void err_sys(const char *fmt, ...)
42 {
43     va_list ap;
44
45     va_start(ap, fmt);
46     err_doit(1, errno, fmt, ap);
47     va_end(ap);
48     exit(1) ;
49 }
# libApueSunyj.a
libApueSunyj.a: error.o
    ar cr libApueSunyj.a error.o
error.o: error.cpp apuesunyj.h
    g++ -c -g error.cpp -o error.o
clean:
    rm error.o libApueSunyj.a

时间: 2024-08-01 17:01:43

APUE_1.5CopyStandardInputToStandardOutputUsingStandardIO的相关文章

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.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

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 limi