Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dodane przez: ~Anonim (2019-04-22 18:53) -> text
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <fcntl.h>
- int checkPath(char* source, char* goal){
- struct stat source_st,goal_st;
- if(stat(source,&source_st) == -1 || stat(goal,&goal_st) == -1){
- return 1; //Path doesn't exist
- }
- if(S_ISDIR(source_st.st_mode) & S_ISDIR(goal_st.st_mode)){
- return 0; //It's directory
- }
- if(S_ISREG(source_st.st_mode) & S_ISREG(goal_st.st_mode)){
- return 2; //It's regular file
- }
- else return -1; //It's something else
- }
- char* concat(const char *s1, const char *s2)
- {
- char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
- strcpy(result, s1);
- strcat(result, s2);
- return result;
- }
- int main(int argc, char* argv[]){
- struct dirent *src;
- struct dirent *goal;
- struct dirent *temp;
- int flag = 0,sourcehandle,destinationhandle,bytes;
- if(argc <= 2){
- printf("Invalid number of arguments. It must be at least 2.\n");
- }
- else{
- if(checkPath(argv[1],argv[2]) == 1) printf("Path doesn't exist\n");
- if(checkPath(argv[1],argv[2]) == -1 || checkPath(argv[1],argv[2]) == 2) printf("Path it's not directory\n");
- if(checkPath(argv[1],argv[2]) == 0){
- DIR *src_dir = opendir(argv[1]);
- if(src_dir == NULL){
- printf("Could not open current directory");
- }
- while ((src = readdir(src_dir)) != NULL) {
- DIR *goal_dir = opendir(argv[2]);
- if(src->d_type == 8) //Only files
- if ( !strcmp(src->d_name, ".") || !strcmp(src->d_name, "..") ){}
- else {
- while((goal = readdir(goal_dir)) != NULL){
- if(goal->d_type == 8) //Only files
- if(strcmp(src->d_name,goal->d_name) == 0){
- flag = 1;
- }
- }
- if(flag == 0){
- printf("File %s need to be synchronized\n",src->d_name);
- char* src_path = concat(argv[1],src->d_name);
- char* goal_path = concat(argv[2],src->d_name);
- FILE* pnReadFile = fopen(src_path,"r");
- printf("\n dest file name = %s\n", goal_path);
- FILE* pnWriteFile = fopen(goal_path, "w"); /*File Pointer to write in file*/
- free(goal_path);
- free(src_path);
- char buffer[1024] = {0}; /*Buffer to store files content*/
- while(fgets(buffer, 1024, pnReadFile))
- {
- fputs(buffer, pnWriteFile);
- }
- fclose(pnWriteFile);
- fclose(pnReadFile);
- }
- flag = 0;
- closedir(goal_dir);
- }
- }
- closedir(src_dir);
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement