Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <errno.h>
- #define MSGSIZE 512
- int main()
- {
- char inbuf[MSGSIZE];
- int p[2], pid, nbytes, filesize, filepos;
- DIR *d;
- struct dirent *dir;
- FILE *curr;
- FILE *new;
- int status;
- char *filename;
- char *bytes = (char *)calloc(MSGSIZE,sizeof(char));
- if (pipe(p) < 0)
- {
- exit(1);
- }
- int i = chdir("/home/ira/osisp/6/images/test");
- d = opendir( "." );
- if( d == NULL )
- {
- printf("Cannot open dir");
- exit(EXIT_FAILURE);
- }
- while( ( dir = readdir( d ) ) )
- {
- if( strcmp( dir->d_name, "." ) == 0 ||
- strcmp( dir->d_name, ".." ) == 0 )
- {
- continue;
- }
- filename = dir->d_name;
- printf("%s \n",filename); //do not fgt
- curr = fopen(dir->d_name,"rb");
- if (curr == NULL)
- {
- printf("Error opening file");
- exit(EXIT_FAILURE);
- }
- fseek(curr,0, SEEK_END);
- filesize = ftell(curr);
- fseek(curr,0,SEEK_SET);
- filepos =0;
- while(filepos != filesize)
- {
- if(filesize-filepos > MSGSIZE)
- {
- fread(bytes,sizeof(char), MSGSIZE, curr);
- write(p[1], bytes, MSGSIZE);
- filepos = filepos + MSGSIZE;
- }
- else
- {
- fread(bytes,sizeof(char),filesize-filepos,curr);
- write(p[1], bytes, filesize-filepos);
- fclose(curr);
- //printf("Wrote 1 file \n");
- break;
- }
- }
- //if((pid = fork()) > 0)
- //{
- // waitpid(-1,&status,WUNTRACED);
- //}
- pid_t pid = fork();
- //if(pid > 0 )
- //{
- // wait(&status);
- //}
- if(pid == 0)
- {
- int reading_bytes = 0;
- chdir("/home/ira/osisp/6/images/out");
- new = fopen(filename,"wb");
- while(reading_bytes != filesize)
- {
- if(filesize-reading_bytes > MSGSIZE)
- {
- read(p[0], bytes, MSGSIZE);
- fwrite(bytes,sizeof(char),MSGSIZE,new);
- reading_bytes = reading_bytes + MSGSIZE;
- }
- else
- {
- read(p[0], bytes,filesize-reading_bytes);
- fwrite(bytes,sizeof(char),filesize-reading_bytes,new);
- fclose(new);
- char *path = (char *)calloc(40,sizeof(char));
- strcpy(path,"/home/ira/osisp/6/images/out");
- strcat(path,filename);
- int i =execl("select_channel"," ",path, NULL);
- printf("%d \n", i);
- printf("%d \n", errno);
- //printf("Red 1 file \n");
- break;
- }
- }
- exit(0);
- }
- }
- //if ((pid > 0) && (WIFEXITED(status)) )
- //{
- // while(wait(&status) != -1);
- //}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement