Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- char buffor1[50], buffor2[50];
- char nazwa_pliku[50];
- int pipe_child[2], pipe_parent[2];
- int childpid, n;
- int main(){
- if(pipe(pipe_child) < 0)
- perror("Blad pipe_child\n");
- if(pipe(pipe_parent) < 0)
- perror("Blad pipe_parent\n");
- if((childpid = fork()) == -1){
- perror("nie moge forknac\n");
- } else {
- if(childpid == 0){
- close(pipe_child[1]);
- close(pipe_parent[0]);
- if((n = read(pipe_child[0], buffor1, sizeof(buffor1))) <= 0)
- perror("Blad odczytu\n");
- FILE *plik = fopen(buffor1, "r");
- if(plik == NULL){
- if(write(pipe_parent[1], "Blad otwarcia pliku\n", 20) != 20)
- perror("Blad zapisu\n");
- } else {
- while(fgets(buffor1, sizeof(buffor1), plik) != NULL)
- write(pipe_parent[1], buffor1, sizeof(buffor1));
- }
- fclose(plik);
- close(pipe_child[0]);
- close(pipe_parent[1]);
- } else {
- close(pipe_child[0]);
- close(pipe_parent[1]);
- printf("Nazwa pliku: ");
- scanf("%s", nazwa_pliku);
- if(write(pipe_child[1], nazwa_pliku, sizeof(nazwa_pliku)) != sizeof(nazwa_pliku))
- perror("Blad zapisu\n");
- while(read(pipe_parent[0], buffor2, sizeof(buffor2)) > 0)
- write(1, buffor2, strlen(buffor2));
- wait(NULL);
- close(pipe_child[1]);
- close(pipe_parent[0]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment