폴 더 복사 (Linux)

C 실현
#include "unistd.h"            //       
#include "string.h"
#include "dirent.h"
#include "utime.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#define MAX_PATH 1024                         //         
void copy_data(char *spath, char *dpath)      //      
{
      int nbyte;
                            //  n   
    int pDir_s, pDir_d;
                   //     pDir_s,       pDir_d
    char buf[BUFSIZ] ;
                    //       
        struct stat file_stat;
              //     file_stat
    struct utimbuf mod_time;
            //      
        stat(spath, &file_stat) ;
            //       
    if ((pDir_s = open(spath, 0)) == - 1)      //     
    
  {
                printf("       %s,    !
", spath) ; // exit(0); } pDir_d = creat(dpath, 0777); // , , while ((nbyte = read(pDir_s, buf, BUFSIZ)) > 0) // , { if (write(pDir_d, buf, nbyte) != nbyte) { printf(" !
") ; // , exit(0); } } mod_time.actime = file_stat.st_atime; // , mod_time.modtime = file_stat.st_mtime; utime(dpath, &mod_time) ; chmod(dpath, file_stat.st_mode); // , close(pDir_s) ; // close(pDir_d) ; } void mycp(char *source, char *des) // , { struct dirent *ent = NULL; // dirent struct utimbuf mod_time; // char spath[MAX_PATH] = "", dpath[MAX_PATH] = "" ; // spath, dpath DIR *pDir; // struct stat file_stat; // file_stat strcpy(spath, source) ; strcpy(dpath, des) ; pDir = opendir(source); // while (NULL != (ent = readdir(pDir))) // , { if (strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".") == 0) // '.' '..', continue; if (ent->d_type == 4) //d_type = 4, { strcat(dpath, "/"); strcat(dpath, ent->d_name) ; // strcat(spath, "/"); strcat(spath, ent->d_name) ; // stat(spath, &file_stat); // mkdir(dpath, 0777); // , , mod_time.actime = file_stat.st_atime; // mod_time.modtime = file_stat.st_mtime; mycp(spath, dpath); // chmod(dpath, file_stat.st_mode); // , utime(dpath, & mod_time) ; // , strcpy(dpath, des); // strcpy(spath, source) ; } else { //d_type != 4, , copy_data strcat(dpath, "/"); // strcat(dpath, ent->d_name) ; strcat(spath, "/"); // strcat(spath, ent->d_name) ; copy_data(spath, dpath); // strcpy(dpath, des); // strcpy(spath, source); } } } int main(int argc, char *argv[]) { DIR *pDir; struct stat file_stat; struct utimbuf mod_time; // if (argc < 2) // 2 , , { printf("argument error!
"); return 0; } printf("Copy start!
"); // copy if (pDir = opendir(argv[1]) == NULL) { printf("The file you want to copy do not exist!
"); return 0; } stat(argv[1], &file_stat) ; // if (pDir = opendir(argv[2]) == NULL); // , mkdir(argv[2], file_stat.st_mode); mod_time.actime = file_stat.st_atime; // , mod_time.modtime = file_stat.st_mtime; utime(argv[2], & mod_time) ; // , mycp(argv[1], argv[2]) ; // copy printf("Copy complete!
") ; // copy return 0; }

좋은 웹페이지 즐겨찾기