Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <fcntl.h>
  9. #include <dirent.h>
  10.  
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13.  
  14. struct options {
  15.     bool option_R;
  16.     bool option_t;
  17.     int numBytes;
  18.     bool option_f;
  19.     bool option_v;
  20. };
  21.  
  22. struct list_ls {
  23.     char *name;
  24.     char *destiny;
  25.     struct stat *stat;
  26.     struct list_ls *next;
  27.     struct list_ls *before;
  28. };
  29.  
  30. struct list_ls *emptyList()
  31. {
  32.     return NULL;   
  33. }
  34.  
  35. int info(char *s){
  36.   struct stat b;
  37.   //char *inf,*tmp;
  38.   int per=0;
  39.   int res=0;
  40.  
  41.   if(lstat(s,&b)!=0){ printf("\nError: Info: Parametros incorrectos para el lstat*  en   %s\n",s); exit(0);}
  42.   else {
  43.      
  44.   if (b.st_mode & S_IRUSR) per=4; else per=0;
  45.   if (b.st_mode & S_IWUSR) per+=2; else per+=0;
  46.   if (b.st_mode & S_IXUSR) per+=1; else per+=0;
  47.   res=per*64;
  48.   if (b.st_mode & S_IRGRP) per=4; else per=0;
  49.   if (b.st_mode & S_IWGRP) per+=2; else per+=0;
  50.   if (b.st_mode & S_IXGRP) per+=1; else per+=0;
  51.   res+=(per*8);
  52.   if (b.st_mode & S_IROTH) per=4; else per=0;
  53.   if (b.st_mode & S_IWOTH) per+=2; else per+=0;
  54.   if (b.st_mode & S_IXOTH) per+=1; else per+=0;
  55.   res+=per;
  56.   return res;
  57.    
  58.   }
  59. }
  60.  
  61. int isEmptyList(struct list_ls *list)
  62. {
  63.     return (list==NULL)?0:1;   
  64. }
  65.  
  66. void deleteList(struct list_ls **list)
  67. {
  68.     struct list_ls *aux;
  69.    
  70.     while (*list!=NULL){
  71.         aux = *list;
  72.         free(aux->name);
  73.         free(aux->destiny);
  74.         free(aux->stat);
  75.         *list=aux->next;
  76.         free(aux);
  77.     }
  78.    
  79.     return;
  80. }
  81.  
  82. struct list_ls *add(struct list_ls **list, char *d_name, char *d, struct stat *s)
  83. {
  84.     struct list_ls *aux,*new;
  85.  
  86.     if((new = malloc(sizeof(struct list_ls)))==NULL){
  87.         printf("Cannot allocate memory\n");
  88.         exit(EXIT_FAILURE);
  89.     }
  90.  
  91.     new->name=d_name;
  92.     new->destiny=d;
  93.     new->stat=s;
  94.     new->next=NULL;
  95.     new->before=NULL;
  96.    
  97.     if (*list==NULL) { //Caso en que la lista este vacia
  98.         *list = new;
  99.         return new;
  100.     }
  101.  
  102.     aux = *list;
  103.     while((aux->next!=NULL) && (strcmp(d_name,aux->name)<0))//Hasta que se encuentre el nombre o llegar al final de la lista
  104.         aux = aux->next;
  105.     if (strcmp(d_name,aux->name)>=0){
  106.         if (aux==*list){//estamos en el principio
  107.             new->next=*list;
  108.             (*list)->before=new;
  109.             *list=new;
  110.             return *list;
  111.         } else {//Insertar por el medio
  112.             new->next=aux;
  113.             new->before=aux->before;
  114.             (new->before)->next=new;
  115.             aux->before=new;
  116.             return *list;
  117.         }
  118.     } else {//Es el mas grande de todos
  119.         new->before=aux;
  120.         aux->next=new;
  121.         return *list;
  122.     }
  123.  
  124.    
  125.     return *list;
  126. }
  127.  
  128. void prototype()
  129. {//Escribe el prototipo que tiene que tener el programa para ejecutarse
  130.     printf("copy [-R] [-t numBytes] [-f] [-v] <sources> <dest>\n");
  131.     exit(EXIT_FAILURE);
  132. }
  133.  
  134. void init_options(struct options *options)
  135. {//Inicializa los valores de las opciones
  136.     options->option_R = false;
  137.     options->option_t = false;
  138.     options->numBytes = -1;
  139.     options->option_f = false;
  140.     options->option_v = false;
  141. }
  142.  
  143. void print_options(struct options option)
  144. {//Escribe los valores que tienen las opciones
  145.     printf("Options:\n");
  146.     if (option.option_R)
  147.         printf("-R: true\n");
  148.     else
  149.         printf("-R: false\n");
  150.     if (option.option_t)
  151.         printf("-t: true, numBytes = %d\n",option.numBytes);
  152.     else
  153.         printf("-t: false\n");
  154.     if (option.option_f)
  155.         printf("-f: true\n");
  156.     else
  157.         printf("-f: false\n");
  158.     if (option.option_v)
  159.         printf("-v: true\n");
  160.     else
  161.         printf("-v: false\n");
  162.  
  163. }
  164.  
  165. void avaliable_R(struct options *options, char ** argv, int *cnt)
  166. {
  167.     options->option_R = true;
  168. }
  169.  
  170. void avaliable_t(struct options *options, char ** argv, int *cnt)
  171. {
  172.     options->option_t = true;
  173.     if (argv[0] != NULL)
  174.         options->numBytes = atoi(argv[0]);
  175.     else
  176.         prototype();
  177.     *cnt = *cnt + 1;//Como leo un argumento pues lo salto para no leerlo despues
  178. }
  179.  
  180. void avaliable_f(struct options *options, char ** argv, int *cnt)
  181. {
  182.     options->option_f = true;
  183. }
  184.  
  185. void avaliable_v(struct options *options, char ** argv, int *cnt)
  186. {
  187.     options->option_v = true;
  188. }
  189.  
  190. void search_options(char *argv[], struct options *options)
  191. {//Busca las opciones en la linea de comandos
  192.     struct activate{//para activar las opciones
  193.         char *name;
  194.         void (*funtion) (struct options *, char **, int *);
  195.     };
  196.     struct activate act_opt[] = {//Opciones disponibles
  197.         {"-R", avaliable_R},
  198.         {"-t", avaliable_t},
  199.         {"-f", avaliable_f},
  200.         {"-v", avaliable_v},
  201.         {NULL, NULL},
  202.     };
  203.     int j, i;
  204.    
  205.     for (i = 0; argv[i]!=NULL; i++) {
  206.         for (j = 0; act_opt[j].name!=NULL; j++) {//Miro por todas las opciones
  207.             //printf("%d \t",j);
  208.             if (!strcmp(act_opt[j].name, argv[i])) {
  209.                 (*act_opt[j].funtion)(options, argv+i+1, &i);//lanzo la funcion
  210.                 break; 
  211.             }
  212.         }
  213.         //printf("\nj = %d , name %s\ni =  %d , name %s\n",j,act_opt[j].name,i,argv[i]);
  214.         if ((!strncmp(argv[i],"-",1))&&(act_opt[j].name==NULL))//Opcion no valida
  215.             prototype();
  216.     }
  217.        
  218.    
  219. }
  220.  
  221. char **search_sources(int argc, char **argv)
  222. {//Devuelve un array de 'strings' de carpetas origen
  223.     int i, cnt = 0;
  224.    
  225.     for (i = 0; i < argc; i++) {
  226.         if (!strncmp(argv[i],"-",1)){//opcion
  227.             if (!strcmp(argv[i],"-t"))//pa que no mire el numBytes
  228.                 i++;
  229.         }
  230.         else
  231.             cnt++;
  232.     }
  233.    
  234.     char **aux;
  235.     if ((aux = malloc(sizeof(char *)*(cnt+1)))==NULL) {
  236.         printf("Cannot allocate memory\n");
  237.         exit(EXIT_FAILURE);
  238.     }
  239.     aux[cnt] = NULL;
  240.    
  241.     int j = 0;
  242.     for (i = 0; i < argc; i++) {
  243.         if (!strncmp(argv[i],"-",1)){//opcion
  244.             if (!strcmp(argv[i],"-t"))//pa que no mire el numBytes
  245.                 i++;
  246.         }
  247.         else {
  248.             aux[j] = strdup(argv[i]);
  249.             j++;
  250.         }
  251.     }
  252.    
  253.     return aux;
  254. }
  255.  
  256. void delete_sources(char ***sources)
  257. {//Libera la memoria que se uso para la lista de directorios origen
  258.     int i;
  259.    
  260.     char **aux = *sources;
  261.     for (i = 0; aux[i]!=NULL; i++)
  262.         free(aux[i]);
  263.        
  264.     free(aux);
  265.    
  266.     aux = NULL;
  267. }
  268.  
  269. void print_sources(char **sources)
  270. {//Imprime las rutas de origen
  271.     int i;
  272.    
  273.     printf("Source list:\n");
  274.     for (i = 0; sources[i] != NULL; i++)
  275.         printf("\t%s\n",sources[i]);
  276.        
  277.     return;
  278. }
  279.  
  280. bool is_write_user(mode_t mode)
  281. {//Indica si el usuario puede escribir
  282.     return (mode & S_IWUSR)?true: false;
  283. }
  284.  
  285. bool is_read_user(mode_t mode)
  286. {//Indica si el usuario puede leer
  287.     return (mode & S_IRUSR)?true: false;
  288. }
  289.  
  290. bool is_link(mode_t mode)
  291. {//devuelve true si es un link
  292.     return (S_ISLNK(mode))?true: false;
  293. }
  294.  
  295. bool is_write_numBytes(struct options options, off_t size)
  296. {//Inidica si con las opciones disponibles se puede escribir un archivo con tamaño size
  297.     if (!options.option_t)
  298.         return true;
  299.        
  300.     return (size > options.numBytes)? false: true;
  301. }
  302.  
  303. bool is_write_file(char *source, char *destiny, struct options options)
  304. {//Indica si el archivo source se puede copiar en destiny
  305.     if (options.option_f)
  306.         return true;
  307.  
  308.     struct stat buf;
  309.     if (lstat(destiny, &buf))//Si da false es que ese será el nombre del archivo a crear
  310.         return true;
  311. //printf("source: %s\n",source);
  312. //printf("destiny: %s\n", destiny);
  313.     if (!S_ISDIR(buf.st_mode))//Si existe el archivo y no es un directorio
  314.         return false;
  315.     char *file = rindex(source, '/');
  316.     file = (file == NULL)? source: file+1; //Nombre del archivo
  317.    
  318.     char *path;
  319.     if ((path = malloc(sizeof(char)*(strlen(destiny)+strlen(file)+2)))==NULL){
  320.         printf("Cannot allocate Memory\n");
  321.         exit(EXIT_FAILURE);
  322.     }
  323.    
  324.     strcpy(path, destiny);
  325.     strcat(path, "/");
  326.     strcat(path, file);
  327.    
  328.     bool exist = (!lstat(path, &buf))? true: false;
  329.     free(path);
  330.     return !exist;//Si existe no se podra escribir el archivo
  331. }
  332.  
  333. char *file_to_creat(char *source, char *destiny)
  334. {//Devuelve un path del archivo que se va a crear
  335.     struct stat stat;
  336.     if (lstat(destiny,&stat)==0) {
  337.         if (!S_ISDIR(stat.st_mode))//Es que existe el archivo
  338.             return strdup(destiny);
  339.     } else
  340.         return strdup(destiny);
  341. //printf("source : %s\n", source);
  342. //printf("destiny: %s\n", destiny);    
  343.     char *file = rindex(source, '/');
  344.     file = (file == NULL)? source: file+1; //Nombre del archivo
  345.    
  346.     char *path;
  347.     if ((path = malloc(sizeof(char)*(strlen(destiny)+strlen(file)+2)))==NULL){
  348.         printf("Cannot allocate Memory\n");
  349.         exit(EXIT_FAILURE);
  350.     }
  351.    
  352.     strcpy(path, destiny);
  353.     strcat(path, "/");
  354.     strcat(path, file);
  355.    
  356.     return path;
  357. }
  358.  
  359. char *link_to_creat(char *source, char *link, char *destiny)
  360. {
  361.        char *aux_source  = rindex(source,'/');
  362.        char *aux_destiny = rindex(destiny, '/');
  363.        char *aux_link    = rindex(link, '/');
  364.  
  365.        if (aux_source == NULL)
  366.                return file_to_creat(link, destiny);
  367.        
  368.        int size_destiny = 0;
  369.        if (aux_destiny == NULL) {
  370.                aux_destiny = destiny;
  371.                size_destiny = strlen(destiny);
  372.        } else {
  373.                size_destiny = strlen(destiny)-strlen(aux_destiny);
  374.        }
  375.  
  376.        if (strcmp(aux_destiny, aux_source) != 0)
  377.                return file_to_creat(link, source);
  378.  
  379.        if (aux_link == NULL)
  380.                aux_link = link;
  381.        else
  382.                aux_link++;
  383.                
  384.        char *new_name = malloc(sizeof(char) * (strlen(aux_link)+size_destiny+2));
  385.        if (new_name == NULL) {
  386.                printf("Cannot allocate memory\n");
  387.                exit(EXIT_FAILURE);
  388.        }
  389.  
  390.        strncpy(new_name, destiny, size_destiny);
  391.        new_name[size_destiny] = '\0';
  392.        strcat(new_name, "/");
  393.        strcat(new_name, aux_link);
  394.        return new_name;
  395. }
  396.  
  397. int file_cp(char *source, char *destiny, struct options options, char *link)
  398. {//Funcion para copiar un archivo
  399.     char *aux;
  400.     if ((link != NULL) && (!options.option_R))
  401.         aux = link;
  402.     else
  403.         aux = source;
  404. //printf("aux: %s\n",aux);
  405.     struct stat stat;//para coger los datos de los que se van copiar de verdad
  406.     if (lstat(aux,&stat)) {//Si da fallo es de los extraños extraños
  407.         perror("file_cp: lstat");
  408.         return -1;
  409.     }
  410.    
  411.     if (!is_write_numBytes(options, stat.st_size)) {
  412.         printf("size of '%s': %lu\n", source, stat.st_size);
  413.         printf("avaliable bytes to copy: %d\n", options.numBytes);
  414.         return -1;
  415.     }
  416.    
  417.     if (!is_write_file(source, destiny, options)) {
  418.         printf("option -f not avaliable\nCannot rewrite the file\n");
  419.         return -1;
  420.     }
  421.     char *new_file = file_to_creat(aux, destiny);
  422.     if (link != NULL && options.option_R) {
  423. printf("source : %s\n",source);
  424.         if (symlink(source, new_file)==-1) {
  425.             perror("file_cp: symlink");
  426.             free(new_file);
  427.             return -1;
  428.         }
  429.         free(new_file);
  430.         return 0;
  431.     }
  432.     //Aqui ya está todo correcto para para copiar
  433.     int fd_source = open(aux, O_RDONLY);
  434.     if (fd_source == -1) {
  435.         printf("file_cp: Cannot open file: %s\n",aux);
  436.         free(new_file);
  437.         return -1;
  438.     }  
  439.     remove(new_file);//si existia el archivo antes lo eliminamos
  440.     int fd_creat = open(new_file, O_CREAT | O_WRONLY | O_EXCL, stat.st_mode);
  441.     if (fd_creat == -1) {
  442.         printf("file_cp: Cannot open to write: %s\n",new_file);
  443.         free(new_file);
  444.         return -1;
  445.     }
  446.        
  447.     void *buffer = malloc(stat.st_size);
  448.     if (buffer == NULL) {
  449.         printf("Cannot allocate memory\n");
  450.         close(fd_source);
  451.         close(fd_creat);
  452.         exit(EXIT_FAILURE);
  453.     }
  454.     if (read(fd_source, buffer, stat.st_size)==-1) {
  455.         printf("file_cp: Cannot read file\n");
  456.         close(fd_source);
  457.         close(fd_creat);
  458.         free(buffer);
  459.         return -1;
  460.     }
  461.    
  462.     if (write(fd_creat, buffer, stat.st_size)==-1) {
  463.         printf("file_cp: Cannot write file\n");
  464.         close(fd_source);
  465.         close(fd_creat);
  466.         free(buffer);
  467.         return -1;
  468.     }
  469.     chmod(new_file,info(aux));
  470.     if (options.option_v)
  471.        printf("%s -> %s\n",aux,new_file);
  472.     free(new_file);
  473.     free(buffer);
  474.     close(fd_source);
  475.     close(fd_creat);
  476.    
  477.     return 0;
  478. }
  479.  
  480. char *directory_and_file(char *dir, char *file)
  481. {
  482.     char *new = malloc(sizeof(char)*(strlen(dir)+strlen(file)+2));
  483.    
  484.     if (new == NULL) {
  485.         printf("Cannot allocate memory\n");
  486.         exit(EXIT_FAILURE);
  487.     }
  488.    
  489.     strcpy(new, dir);
  490.     strcat(new, "/");
  491.     strcat(new, file);
  492.    
  493.     return new;
  494. }
  495.  
  496. char *cat_file(char *dir, char *name)
  497. {
  498.     char *new = malloc(sizeof(char)*(strlen(dir)+strlen(name)+2));
  499.     if (new == NULL) {
  500.         printf("Cannot allocate memory\n");
  501.         exit(EXIT_FAILURE);
  502.     }
  503.    
  504.     strcpy(new, dir);
  505.     strcat(new, "/");
  506.     strcat(new, name);
  507.  
  508.     return new;
  509. }
  510.  
  511. void main_cp(char *source, char *destiny, struct options options);
  512.  
  513. int directory_cp(char *source, char *destiny, struct options options, const char *link)
  514. {//Funcion para copiar una carpeta
  515.  
  516.     DIR            *dir;
  517.     struct dirent  *myDirec;
  518.     struct list_ls *list;
  519.     char *name;
  520.     char *dest;
  521.    
  522.     struct stat stat, buffin, *aux_stat;
  523.     if (lstat(source, &stat)) {
  524.         perror("directory_cp: lstat");
  525.         return -1;
  526.     }
  527.  
  528.     /*if (!is_write_numBytes(options, stat.st_size)) {
  529.         printf("size of '%s': %lu\n", source, stat.st_size);
  530.         printf("avaliable bytes to copy: %d\n", options.numBytes);
  531.         return -1;
  532.     }*/
  533.    
  534.     //Introduzco todo en una lista y después se manda copiar
  535.     list=emptyList();
  536.    
  537.     if ((dir = opendir(source))==NULL) {
  538.         printf("directory_cp: open_dir: Cannot open directory\n");
  539.         return -1;
  540.     }
  541.     while ((myDirec=readdir(dir))!=NULL) {
  542.         name = directory_and_file(source, myDirec->d_name);
  543.         dest = directory_and_file(destiny, myDirec->d_name);       
  544.         if (!((strcmp(myDirec->d_name,".")==0)||(strcmp(myDirec->d_name,"..")==0))) {
  545.             if ((aux_stat = malloc(sizeof(struct stat)))==NULL) {
  546.                 printf("Cannot allocate memory\n");
  547.                 exit(EXIT_FAILURE);
  548.             }
  549.             if (lstat(name, aux_stat)==-1) {
  550.                 perror("directory_cp: lstat");
  551.                 exit(EXIT_FAILURE);
  552.             }
  553.             list = add(&list, name, dest, aux_stat);
  554.         } else {
  555.             free(name);
  556.             free(dest);
  557.         }
  558.  
  559.     }
  560.     closedir(dir);
  561.  
  562.     char *folder = destiny;
  563.     if (lstat(folder, &buffin)==-1)
  564.         if (mkdir(folder, stat.st_mode)==-1) {
  565.             printf("directory_cp: Cannot create a new folder\n");
  566.             deleteList(&list);
  567.             //free(folder);
  568.             return -1;
  569.         }
  570.        if (chmod(folder,info(source))==-1)
  571.         perror("directory_cp: chmod");
  572.        if (options.option_v)
  573.           printf("%s -> %s\n",source,folder);
  574.            
  575.        
  576.  
  577.     struct list_ls *aux = list;
  578.     while (aux != NULL) {
  579.         if (!((!options.option_R) && (S_ISDIR(aux->stat->st_mode))))
  580.             main_cp(aux->name ,aux->destiny, options);
  581. //printf("aux : %s\n",aux->name);
  582.         aux = aux->next;
  583.     }
  584.  
  585.     deleteList(&list);
  586.     //free(folder);
  587.    
  588.     return 0;
  589. }
  590.  
  591. char *name_to_creat(char *source, char *link, char *destiny)
  592. {/*
  593. printf("source: %s\n",source);
  594. printf("link: %s\n",link);
  595. printf("destiny: %s\n",destiny);
  596. */ 
  597.     int size_d = strlen(destiny);
  598.     char *aux_d = rindex(destiny,'/');
  599.     if (aux_d != NULL)
  600.         size_d -= strlen(aux_d++);
  601.    
  602.     char *aux_s = rindex(source,'/');
  603.     aux_s = (aux_s == NULL)? source: aux_s + 1;
  604.    
  605.     char *new = malloc(sizeof(char) * (2+size_d+strlen(aux_s)));
  606.     if (new == NULL) {
  607.         printf("Cannot allocate memory\n");
  608.         exit(EXIT_FAILURE);
  609.     }
  610.    
  611.     strncpy(new, destiny, size_d);
  612.     new[size_d] = '\0';
  613.     strcat(new, "/");
  614.     strcat(new, aux_s);
  615. //printf("\n\tnew_file : %s\n",new);
  616.     return new;
  617. }
  618.  
  619. int link_cp(char *source, char *destiny, struct options options)
  620. {
  621.     static int cnt = 0;
  622.     cnt++;
  623.     int parada = 92;
  624.     char *link;
  625.     char buffer[PATH_MAX];
  626.     link = realpath(source, buffer);
  627.     if (link == NULL) {
  628.         perror("link_cp: realpath");
  629.         return -1;
  630.     }
  631.     struct stat l_stat;
  632.     if (lstat(link,&l_stat)==-1) {
  633.         printf("link_cp: The referenced file not found\n");
  634.         return -1;
  635.     }
  636. if (cnt == parada) {
  637.     printf("source: %s\n",source);
  638.     printf("link: %s\n",link);
  639. }
  640.    
  641.     if (S_ISDIR(l_stat.st_mode) & (!options.option_R)) {
  642.         char *new_folder = link_to_creat(source,link,destiny);
  643. if (cnt == parada) {
  644.     printf("destino: %s\n",new_folder);
  645.     exit(0);
  646. }
  647.        if (mkdir(new_folder, 0755)==-1) {
  648.             printf("link_cp: Cannot create folder\n");
  649.             free(new_folder);
  650.             return -1;
  651.         }
  652.        
  653.     if (chmod(new_folder,info(link))==-1)
  654.         perror("link_cp: chmod");  
  655.     if (options.option_v)
  656.       printf("%s -> %s\n",link,new_folder);
  657.         main_cp(link,new_folder,options);
  658.         free(new_folder);
  659.         return 0;
  660.     }
  661.     if (options.option_R) {//Crear el enlace   
  662.         char *new_link = name_to_creat(source,link,destiny);
  663. //printf("source: %s\n",source);
  664. //printf("new: %s\n", new_link);
  665. if (cnt == parada) {
  666.     printf("destino: %s\n",new_link);
  667.     exit(0);
  668. }
  669.         if (symlink(source,new_link)==-1) {
  670.             printf("link_cp: Fail to create link: %s\n",source);
  671.             free(new_link);
  672.             return -1;
  673.         }
  674.         if (chmod(new_link,info(link))==-1)
  675.             perror("link_cp: chmod");
  676.         if (options.option_v)
  677.           printf("%s -> %s\n",link,new_link);
  678.        free(new_link);
  679.         return 0;
  680.     }
  681.     else {//Copiar el archivo al que apunta
  682.         return file_cp(link,destiny,options,NULL);
  683.     }
  684. }
  685.  
  686. void main_cp(char *source, char *destiny, struct options options)
  687. {//Se tienen la lista de directorios origen, la ruta de destino y las opciones
  688.         struct stat buf;
  689.         if (lstat(source,&buf)) {//Si no existe
  690.             printf("main_cp: No such file or directory: %s\n",source);
  691.             return;
  692.         }  
  693.        
  694.         if (!is_read_user(buf.st_mode)) {
  695.             printf("main_cp: Cannot read %s\n",source);
  696.             return;
  697.         }
  698.        
  699.         char *link = NULL;
  700.         if (is_link(buf.st_mode)){//Si es un link
  701.             int result =link_cp(source, destiny, options);
  702.             if (result == 0)
  703.                 return;
  704.             else {
  705.                 printf("main_cp: Fail to copy %s(it's a link) to %s\n",source, destiny);
  706.                 return;
  707.             }
  708. //printf("%s -> %s\n",source, buffer);
  709.         }
  710.        
  711.         if (S_ISDIR(buf.st_mode)) {//Si es un directorio
  712.             if (directory_cp(source, destiny, options, link) != 0)
  713.                 printf("main_cp: Fail to copy folder '%s' into '%s'\n",source,destiny);
  714.         } else {//Si es un archivo
  715.             if (file_cp(source, destiny, options, link) != 0)
  716.                 printf("main_cp: Fail to copy '%s' into '%s'\n",source,destiny);
  717.         }
  718.        
  719.        
  720.  
  721. }
  722.  
  723. void pre_cp(char **sources, char *destiny, struct options options) {
  724.     int i;
  725.     struct stat buf;
  726.    
  727.     if (!lstat(destiny, &buf))
  728.         if (!is_write_user(buf.st_mode)) {
  729.             printf("pre_cp: Cannot write into destiny\n(destiny: %s)\n",destiny);
  730.             return;
  731.         }
  732.    
  733.     for (i = 0; sources[i] != NULL; i++) //Lista de origenes... empezamos uno a uno
  734.         main_cp(sources[i], destiny, options);
  735.  
  736. }
  737.  
  738.  
  739. int main(int argc, char *argv[])
  740. {
  741.     if (argc < 3)
  742.         prototype();
  743.    
  744.     //Opciones
  745.     struct options options_cp;
  746.     init_options(&options_cp);//Vacia las opciones
  747.     search_options(argv+1, &options_cp);//Activa opciones
  748. //print_options(options_cp);
  749.  
  750.     //rutas
  751.     char **sources = NULL;
  752.     char *destiny = NULL;
  753.     sources = search_sources(argc-2, argv+1);//-2 poqe la última es el destino
  754. //print_sources(sources);
  755.     if (sources[0] == NULL) {//No tiene destino para copiar
  756.         printf("destiny not found\n");
  757.         exit(EXIT_FAILURE);
  758.     }
  759.     destiny = strdup(argv[argc-1]);
  760. //printf("Destino:\n\t%s\n",destiny);
  761.  
  762.     //Empezar a copiar los elementos
  763. //printf("\n\n\n");
  764.     pre_cp(sources, destiny, options_cp);
  765.  
  766.     delete_sources(&sources);
  767.     if (destiny != NULL)
  768.         free(destiny);
  769.        
  770.     exit(EXIT_SUCCESS);
  771.     return 0;
  772. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement