| 7 | #include <sys/wait.h> |
| 8 | |
| 9 | int main(int argc, char *argv[]) |
| 10 | { |
| 11 | pid_t child; |
| 12 | int infd, outfd, errfd, status; |
| 13 | char buf[5] = "test"; |
| 14 | |
| 15 | /* We call ourselves, to test pipe. */ |
| 16 | if (argc == 2) { |
| 17 | if (strcmp(argv[1], "out") == 0) { |
| 18 | if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) |
| 19 | exit(1); |
| 20 | } else if (strcmp(argv[1], "in") == 0) { |
| 21 | if (read(STDIN_FILENO, buf, sizeof(buf)) != sizeof(buf)) |
| 22 | exit(1); |
| 23 | if (memcmp(buf, "test", sizeof(buf)) != 0) |
| 24 | exit(1); |
| 25 | } else if (strcmp(argv[1], "inout") == 0) { |
| 26 | if (read(STDIN_FILENO, buf, sizeof(buf)) != sizeof(buf)) |
| 27 | exit(1); |
| 28 | buf[0]++; |
| 29 | if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) |
| 30 | exit(1); |
| 31 | } else if (strcmp(argv[1], "err") == 0) { |
| 32 | if (write(STDERR_FILENO, buf, sizeof(buf)) != sizeof(buf)) |
| 33 | exit(1); |
| 34 | } else |
| 35 | abort(); |
| 36 | exit(0); |
| 37 | } |
| 38 | |
| 39 | /* We assume no fd leaks, so close them now. */ |
| 40 | close(3); |
| 41 | close(4); |
| 42 | close(5); |
| 43 | close(6); |
| 44 | close(7); |
| 45 | close(8); |
| 46 | close(9); |
| 47 | close(10); |
| 48 | |
| 49 | /* This is how many tests you plan to run */ |
| 50 | plan_tests(67); |
| 51 | child = pipecmd(&infd, &outfd, &errfd, argv[0], "inout", NULL); |
| 52 | if (!ok1(child > 0)) |
| 53 | exit(1); |
| 54 | ok1(write(infd, buf, sizeof(buf)) == sizeof(buf)); |
| 55 | ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf)); |
| 56 | ok1(read(errfd, buf, sizeof(buf)) == 0); |
| 57 | ok1(close(infd) == 0); |
| 58 | ok1(close(outfd) == 0); |
| 59 | ok1(close(errfd) == 0); |
| 60 | buf[0]--; |
| 61 | ok1(memcmp(buf, "test", sizeof(buf)) == 0); |
| 62 | ok1(waitpid(child, &status, 0) == child); |
| 63 | ok1(WIFEXITED(status)); |
| 64 | ok1(WEXITSTATUS(status) == 0); |
| 65 | |
| 66 | child = pipecmd(&infd, NULL, NULL, argv[0], "in", NULL); |