Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define BUFF_SIZE (1 << 10)
- int main(int argc, char *argv[]) {
- int pbuff[2];
- if (pipe(pbuff)) {
- perror("pipe");
- exit(1);
- }
- pid_t pid = fork();
- if (pid == -1) {
- perror("");
- exit(-1);
- }
- if (pid == 0) {//kid
- close(pbuff[0]);
- dup2(pbuff[1], STDOUT_FILENO);
- execlp("ls", "ls", "-l", NULL);
- fprintf(stderr, "execl error\n");
- exit(1);
- }
- close(pbuff[1]);
- char s[BUFF_SIZE];
- memset(s, 0, BUFF_SIZE);
- int cnt = 0;
- while (read(pbuff[0], &s, BUFF_SIZE)) {
- cnt += strlen(s);
- }
- printf("Cnt: %d\n", cnt);
- close(pbuff[0]);
- int stat;
- wait(&stat);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement