Advertisement
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>
- int main(){
- char name[256];
- int pipePW[2], pipeCW[2], pid;
- if (pipe(pipePW) < 0)
- perror("Błąd PIPE\n");
- if (pipe(pipeCW) < 0)
- perror("Błąd PIPE\n");
- pid = fork();
- if (pid == -1){
- perror("Błąd forkowania\n");
- exit(1);
- }
- if (pid == 0){
- close(pipePW[1]);
- close(pipeCW[0]);
- char buff[20] = "";
- char status;
- FILE* file;
- read(pipePW[0],&name,sizeof(name));
- if ((file = fopen(name,"r")) == NULL){
- status = -1;
- write(pipeCW[1],&status,1);
- exit(1);
- }
- do {
- status = fread(&buff,1,20,file);
- write(pipeCW[1],&status,1);
- write(pipeCW[1],&buff,status);
- } while (status != 0);
- exit(0);
- }
- else {
- close(pipePW[0]);
- close(pipeCW[1]);
- char buff[20] = "";
- char size;
- puts("Podaj nazwę pliku do wczytania");
- scanf("%s",name);
- write(pipePW[1],&name,sizeof(name));
- do {
- read(pipeCW[0],&size,1);
- if (size == -1){
- puts("Problem z plikiem");
- wait(NULL);
- exit(-1);
- }
- if (size == 0)
- break;
- read(pipeCW[0],&buff,size);
- printf("%s",buff);
- buff[0] = '\0';
- } while(size != 0);
- wait(NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement