Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <errno.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define MOD_COPY "c"
- #define MOD_LINK "l"
- #define DIRECTOR_CURENT "."
- #define DIRECTOR_ANTERIOR ".."
- #define MAX 200
- void creeaza_director_destinatie(char *dir)
- {
- DIR *s=opendir(dir);
- if (s==NULL) mkdir(dir, 0755);
- else closedir(s);
- }
- int copie(char *sursa, char *destinatie, char *mod)
- {
- DIR *s;
- struct dirent *dit;
- s=opendir(sursa);
- if (!strcmp(MOD_COPY, mod))
- {
- char *next_s;
- char *next_d;
- int l_next_s;
- int l_next_d;
- while ((dit = readdir(s)) != NULL)
- {
- if (dit->d_type == DT_DIR)
- {
- int curent = !strcmp(DIRECTOR_CURENT, dit->d_name);
- int precedent = !strcmp(DIRECTOR_ANTERIOR, dit->d_name);
- if (curent || precedent) continue;
- l_next_d = strlen(destinatie) + strlen(dit->d_name) + 1;
- next_d = (char *)malloc(l_next_d*sizeof(char));
- l_next_s = strlen(sursa) + strlen(dit->d_name) + 1;
- next_s = (char *)malloc(l_next_s*sizeof(char));
- sprintf(next_d, "%s/%s", destinatie, dit->d_name);
- sprintf(next_s, "%s/%s", sursa, dit->d_name);
- creeaza_director_destinatie(next_d);
- copie(sursa, destinatie, mod);
- free(next_d);
- free(next_s);
- }
- else
- {
- FILE * s;
- FILE * dest;
- char buffer[MAX];
- int readed;
- int writed;
- int dim_sursa = strlen(sursa) + strlen(dit->d_name) + 1;
- char *nume_fisier_sursa = (char *) malloc(dim_sursa*sizeof(char));
- sprintf(nume_fisier_sursa, "%s/%s", sursa, dit->d_name);
- int dim_dest = strlen(destinatie) + strlen(dit->d_name) + 1;
- char *nume_dest = (char *) malloc(dim_dest*sizeof(char));
- sprintf(nume_dest, "%s/%s", destinatie, dit->d_name);
- s=fopen(nume_fisier_sursa, "rb");
- dest=fopen(nume_dest, "wb");
- while (!feof(s))
- {
- readed = fread(buffer, 1, MAX, s);
- writed=fwrite(buffer, 1, readed, dest);
- }
- fclose(s);
- fclose(dest);
- free(nume_fisier_sursa);
- free(nume_dest);
- }
- }
- }
- else
- {
- char *link_sursa;
- char *link_dest;
- int dim_sursa, dim_dest;
- while ((dit = readdir(s)) != NULL)
- {
- int curent = !strcmp(DIRECTOR_CURENT, dit->d_name);
- int precedent = !strcmp(DIRECTOR_ANTERIOR, dit->d_name);
- if (curent || precedent) continue;
- dim_sursa = strlen(sursa) + strlen(dit->d_name) + 1;
- link_sursa = (char *) malloc(dim_sursa*sizeof(char));
- sprintf(link_sursa, "%s/%s", sursa, dit->d_name);
- dim_dest = strlen(destinatie) + strlen(dit->d_name) + 1;
- link_dest = (char *) malloc(dim_dest*sizeof(char));
- sprintf(link_dest, "%s/%s", destinatie, dit->d_name);
- symlink(link_sursa, link_dest);
- free(link_sursa);
- free(link_dest);
- }
- }
- closedir(s);
- return 1;
- }
- int main(int argc, char *argv[])
- {
- char *director_sursa;
- char *director_destinatie;
- char *mod;
- if (argc < 4)
- {
- printf("Usage: %s <Director sursa> <director destinatie> <mod>\n", argv[0]);
- printf("\t<mod> poate fi \"c\" sau \"l\"\n");
- return 0;
- }
- director_sursa = argv[1];
- director_destinatie = argv[2];
- mod = argv[3];
- if (strcmp(MOD_COPY, mod) && strcmp(MOD_LINK, mod))
- {
- printf("\t<mod> trebuie sa fie \"c\" or \"l\"\n");
- return 0;
- }
- creeaza_director_destinatie(director_destinatie);
- copie(director_sursa, director_destinatie, mod);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement