// // Task 2: Copy Directory // // C version. // #include #include #include #include #include #include #include #include #include #include #include "args.h" typedef char filename[256]; // // make_path( path ); // mkdir path (and all it's parental dirs void make_path( char *path ) { assert( strlen(path)>1 ); char *p = strchr(path+1,'/'); if( p != NULL ) { *p = '\0'; if( debug ) printf( "mkdir %s\n", path ); mkdir( path, 0700 ); *p = '/'; while( (p = strchr(p+1, '/')) != NULL ) { *p = '\0'; if( debug ) printf( "mkdir %s\n", path ); mkdir( path, 0700 ); *p = '/'; } } if( debug ) printf( "mkdir %s\n", path ); mkdir( path, 0700 ); //exit(1); } // // bool isd = isdir( path ); // Return true iff path is a directory; false otherwise // bool isdir( char *path ) { struct stat buf; if( stat(path,&buf) == 0 ) { if( S_ISDIR(buf.st_mode) ) { return true; } } return false; } typedef void (*callback)( char *dir ); int strcompare( const void *a, const void *b ) { return strcmp( (char *)a, (char *)b ); } #define MAXSUBDIRSPERDIR 1024 // // find_dirs( srcpath, callback ); // Recursively find all directories inside srcpath, // calling callback( dir ) for each one. // void find_dirs( char *srcpath, callback cb ) { if( ! isdir( srcpath ) ) return; DIR *dh = opendir( srcpath ); if( dh == NULL ) { fprintf( stderr, "Can't opendir %s\n", srcpath ); return; } struct dirent *dinfo; filename subdirs[MAXSUBDIRSPERDIR]; int ndirs = 0; while( (dinfo = readdir( dh )) != NULL ) { char *name = dinfo->d_name; filename dname; if( strcmp(name,".")==0 || strcmp(name,"..")==0 ) continue; sprintf( dname, "%s/%s", srcpath, name ); if( isdir(dname) ) { strcpy( subdirs[ndirs++], dname ); assert( ndirs