#include #include #include #include #include #include #include #include #define true 1 #define false 0 #define null (char*)0 /* time_t getlogtime(char *filepath); time_t getfiletime(char *filepaht); int timecmp(time_t t1, time_t t2); ¿©±â¼­ ÆÄÀϽð£À» ºñ±³ÇÏ¿© log ½Ã°£º¸´Ù ÃÖ½ÅÀÇ ÆÄÀÏÀÏ °æ¿ì ÆÄÀÏÀ» º¹»çÇÑ´Ù system ÇÔ¼ö·Î ÆÄÀÏÀ» º¹»ç ±×¸®°í ÃֽŽð£Àº º¯¼ö¿¡ ±â·ÏÇÑ´Ù. setlogtime(char *filepath, tiem_t the_time); */ /*log ½Ã°£ Á¤º¸ ¾ò±â*/ int getlogtime(char *filepath, time_t *the_time) { FILE *in; struct tm ti; char buf_t[20], buf_fd[1024], *seps = "-", *token; int ntm; memset(buf_fd , 0x00, sizeof(buf_fd)); printf("getlogtime : %s\n", filepath); if ((in = fopen(filepath, "r")) == NULL) { fprintf(stderr, "Cannot open output file. \n"); return 1; } fread(buf_fd,50,1,in); printf("%s\n", buf_fd); token = strtok( buf_fd, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_sec = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_min = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_hour = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_mday = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_mon = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_year = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_wday = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_yday = ntm; } token = strtok( null, seps ); if(token == null) return false; else { ntm = atoi(token); ti.tm_isdst = ntm; } *the_time = mktime(&ti); printf("%s\n" , ctime(the_time)); return true; } /*file »ý¼º½Ã°£ res = true ¼º°ø, false ½ÇÆÐ*/ int getfiletime(char *filepath, time_t* the_time) { struct stat fileinfo_t; int res = 0 ; res = stat(filepath , &fileinfo_t); if(!res) { memcpy(the_time, &fileinfo_t.st_ctime, sizeof(time_t)); res = true; } else if ( res < 0 ) { res = false; printf("fail : getfiletime\n"); } return res; } /*µÎ ½Ã°£°£ÀÇ Â÷ t1 -t2 */ int timecmp(time_t t1, time_t t2) { double diff_t; diff_t = difftime(t1, t2); if(diff_t > 0) return 1; else if(diff_t < 0) return -1; else if (diff_t == 0) return 0; } /*log ½Ã°£ Á¤º¸¸¦ ±â·Ï true ¼º°ø, false ½ÇÆÐ*/ int setlogtime(char *filepath, time_t *the_time) { FILE *out; struct tm *ti; char buf_t[20], buf_fd[1024]; ti = localtime(the_time); strftime(buf_t, 20, "%Y-%m-%d %H:%M:%S", ti); printf("%s\n", buf_t); if ((out = fopen(filepath, "w")) == NULL) { fprintf(stderr, "Cannot open output file. \n"); return false; } sprintf(buf_fd, "%d-%d-%d-%d-%d-%d-%d-%d-%d" ,ti->tm_sec,ti->tm_min, ti->tm_hour, ti->tm_mday, ti->tm_mon, ti->tm_year, ti->tm_wday, ti->tm_yday, ti->tm_isdst); fputs(buf_fd, out); return true; } void copyfile(char *srcfilepath, char* dstfilepath) { char command[40]; printf("copy file name : %s\n", srcfilepath); sprintf(command, "cp %s %s" ,srcfilepath, dstfilepath); printf("%s\n", command); system(command); return ; } int main() { struct dirent *entry; struct stat statbuf; time_t the_time, log_time, newrcd_time; int res = 0; /* char dir[] = "/export/home/apache/htdocs/FWIS/WAFSCHT/WEBD/", dstdir[] = "/export/home/apache/htdocs/taemin-test/", logdir[] = "/export/home/apache/htdocs/taemin-test/setup/log.txt"; */ char dir[] = "/db/air-temp/src/", dstdir[] = "/db/air-temp/dst/", logdir[] = "/db/air-temp/setup/log.txt"; DIR *dp; /* open directory*/ if((dp = opendir(dir)) == NULL) { fprintf(stderr, "cannot open directory : %s\n", dir); printf("exit sql-parser program\n"); return 0; } else printf("open directory\n"); chdir(dir); getlogtime(logdir, &log_time); newrcd_time = log_time; /* search directory */ while((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if( S_ISDIR(statbuf.st_mode)) { if(strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0 ) continue ; else { getfiletime(entry->d_name, &the_time); res = timecmp(the_time, log_time); if(res > 0) { printf("file name : %s\n", entry->d_name); printf("time of create file : %s\n", ctime(&the_time)); copyfile(entry->d_name, dstdir); if(timecmp(the_time, newrcd_time) > 0) { printf("new record : %s\n", ctime(&the_time)); newrcd_time = the_time; } } } } else { getfiletime(entry->d_name, &the_time); res = timecmp(the_time, log_time); if(res > 0) { printf("file name : %s\n", entry->d_name); printf("time of create file : %s\n", ctime(&the_time)); copyfile(entry->d_name, dstdir); if(timecmp(the_time, newrcd_time) > 0) { printf("new record : %s\n", ctime(&the_time)); newrcd_time = the_time; } } } } chdir(".."); setlogtime(logdir, &newrcd_time); return 0; }