Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <string.h>
- #define MAX_PATH 256
- #define COPIE 1
- #define LINK 2
- void CopyDirectory( char *source, char *dest, mode_t mod)
- {
- struct stat stbuf;
- struct dirent *dirent;
- DIR *dp;
- char tmp_src[MAX_PATH];
- char tmp_dest[MAX_PATH];
- int in_handle;
- int out_handle;
- void *buffer;
- int i;
- /* creaza directorul */
- mkdir(dest, mod);
- dp = opendir(source);
- if (dp != NULL)
- {
- dirent = readdir(dp);
- while (dirent != NULL)
- {
- if ( strcmp(dirent->d_name , ".") &&
- strcmp(dirent->d_name , ".."))
- {
- sprintf(tmp_src,"%s/%s",source,dirent->d_name);
- stat(tmp_src,&stbuf);
- /* if this is a file */
- if(!S_ISDIR(stbuf.st_mode))
- {
- sprintf(tmp_dest,"%s/%s",dest,dirent->d_name);
- /* deschid fisierul sursa pentru citire */
- in_handle = (int)open(tmp_src, O_RDONLY );
- /* deschid fisierul destinatie pentru scriere */
- out_handle = (int)open(tmp_dest, O_CREAT | O_APPEND | O_WRONLY , mod);
- /* aloc memorie pentru buffer citire de marimea fisierului de intrare*/
- buffer = malloc( stbuf.st_size);
- if (buffer == NULL)
- {
- printf("Eroare alocare dinamica\n");
- exit(0);
- }
- i = read(in_handle, buffer, stbuf.st_size);
- while ( i != 0){
- write(out_handle, buffer, i) ;
- i = read(in_handle, buffer, stbuf.st_size);
- }
- /* close files */
- close(in_handle);
- close(out_handle);
- free(buffer);
- }
- /* if this is a directory */
- else
- {
- sprintf(tmp_dest,"%s/%s",dest,dirent->d_name);
- CopyDirectory(tmp_src, tmp_dest, stbuf.st_mode);
- }
- }
- dirent = readdir(dp);
- }
- closedir(dp);
- }
- }
- int main(int argc, char *argv[])
- {
- char *source;
- char *dest;
- int optiune = 0;
- struct stat stbuf;
- if ( argc != 4 )
- {
- printf("Numarul de parametri nu e corect : Sursa Destinatie Optiune\n");
- return 0;
- }
- source = argv[1];
- dest = argv[2];
- /* valideaza sursa */
- stat(source, &stbuf);
- if(!S_ISDIR(stbuf.st_mode))
- {
- printf("Directorul sursa nu e valid \n");
- return 0;
- }
- /* Valideaza optiunea */
- optiune = atoi(argv[3]);
- if (optiune == COPIE)
- {
- printf(" Copiaza de la %s la %s \n", source, dest);
- CopyDirectory(source,dest,stbuf.st_mode);
- }
- else if (optiune == LINK)
- {
- printf(" Link de la %s la %s \n", source, dest);
- symlink(source,dest);
- }
- else
- {
- printf(" Optiunea e incorecta : Copiere = 1 .. Link = 2\n");
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement