forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
726 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
#include "apue.h" | ||
|
||
int main() { | ||
int int1, int2; | ||
char line[MAXLINE]; | ||
int int1, int2; | ||
char line[MAXLINE]; | ||
|
||
if (setvbuf(stdin, NULL, _IOLBF, 0) != 0) { | ||
err_sys("setvbuf error"); | ||
} | ||
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) { | ||
err_sys("setvbuf error"); | ||
} | ||
if (setvbuf(stdin, NULL, _IOLBF, 0) != 0) { | ||
err_sys("setvbuf error"); | ||
} | ||
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) { | ||
err_sys("setvbuf error"); | ||
} | ||
|
||
while (fgets(line, MAXLINE, stdin) != NULL) { | ||
if (sscanf(line, "%d%d", &int1, &int2) == 2) { | ||
if (printf("%d\n", int1 + int2) == EOF) { | ||
err_sys("printf error"); | ||
} | ||
} else { | ||
if (printf("invalid args\n") == EOF) { | ||
err_sys("printf error"); | ||
} | ||
while (fgets(line, MAXLINE, stdin) != NULL) { | ||
if (sscanf(line, "%d%d", &int1, &int2) == 2) { | ||
if (printf("%d\n", int1 + int2) == EOF) { | ||
err_sys("printf error"); | ||
} | ||
} else { | ||
if (printf("invalid args\n") == EOF) { | ||
err_sys("printf error"); | ||
} | ||
} | ||
} | ||
} | ||
exit(0); | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <sys/wait.h> | ||
#include "apue.h" | ||
|
||
#define DEF_PAGER "/bin/more" | ||
|
||
int main(int argc, char *argv[]) { | ||
int n; | ||
int fd[2]; | ||
pid_t pid; | ||
char *pager, *argv0; | ||
char line[MAXLINE]; | ||
FILE *fp; | ||
|
||
if (argc != 2) { | ||
err_quit("usage: a.out <pathname>"); | ||
} | ||
|
||
if ((fp = fopen(argv[1], "r")) == NULL) { | ||
err_sys("can't open %s", argv[1]); | ||
} | ||
if (pipe(fd) < 0) { | ||
err_sys("pipe error"); | ||
} | ||
|
||
if ((pid = fork()) < 0) { | ||
err_sys("fork error"); | ||
} else if (pid > 0) { | ||
close(fd[0]); | ||
|
||
while (fgets(line, MAXLINE, fp) != NULL) { | ||
n = strlen(line); | ||
if (write(fd[1], line, n) != n) { | ||
err_sys("write error to pipe"); | ||
} | ||
} | ||
if (ferror(fp)) { | ||
err_sys("fgets error"); | ||
} | ||
|
||
// close(fd[1]); | ||
|
||
if (waitpid(pid, NULL, 0) < 0) { | ||
err_sys("waitpid error"); | ||
} | ||
exit(0); | ||
} else { | ||
close(fd[1]); | ||
if (fd[0] != STDIN_FILENO) { | ||
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) { | ||
err_sys("dup2 error to stdin"); | ||
} | ||
close(fd[0]); | ||
} | ||
|
||
if ((pager = getenv("PAGER")) == NULL) { | ||
pager = DEF_PAGER; | ||
} | ||
if ((argv0 = strrchr(pager, '/')) != NULL) { | ||
argv0++; | ||
} else { | ||
argv0 = pager; | ||
} | ||
|
||
if (execl(pager, argv0, (char *)0) < 0) { | ||
err_sys("excel error for %s", pager); | ||
} | ||
} | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "apue.h" | ||
#include <fcntl.h> | ||
|
||
#define FIFO "temp.fifo" | ||
|
||
int main() { | ||
int fdread, fdwrite; | ||
|
||
unlink(FIFO); | ||
if (mkfifo(FIFO, FILE_MODE) < 0) { | ||
err_sys("mkfifo error"); | ||
} | ||
if ((fdread = open(FIFO, O_RDONLY | O_NONBLOCK)) < 0) { | ||
err_sys("open error for reading"); | ||
} | ||
if ((fdwrite = open(FIFO, O_WRONLY)) < 0) { | ||
err_sys("open error for writing"); | ||
} | ||
clr_fl(fdread, O_NONBLOCK); | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/msg.h> | ||
|
||
int main() { | ||
int i, id; | ||
char *hello = "Hello World!\n"; | ||
for (i = 0; i < 5; i++) { | ||
if ((id = msgget(0, IPC_CREAT)) == -1) { | ||
printf("msgget error\n"); | ||
exit(1); | ||
} | ||
printf("id = %d\n", id); | ||
if (msgctl(id, IPC_RMID, NULL) == -1) { | ||
printf("msgctl IPC_RMID error\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
printf("\n\n"); | ||
|
||
for (i = 0; i < 5; i++) { | ||
if ((id = msgget(IPC_PRIVATE, 0)) == -1) { | ||
printf("msgget error\n"); | ||
exit(1); | ||
} | ||
printf("id = %d\n", id); | ||
if (msgsnd(id, hello, 0, IPC_NOWAIT) == -1) { | ||
printf("%s\n", strerror(errno)); | ||
printf("msgsnd error\n"); | ||
exit(1); | ||
} | ||
} | ||
exit(0); | ||
} |
Oops, something went wrong.