#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <sys/types.h> #include <utime.h> #include <errno.h> void check_time_format(int year,int month,int day,int hour,int minute,int second) { int valid_day=0; if(year<1970 || year>2034) { printf("year input error, range: 1970-2034./n"); exit(-1); } if(month<1 || month >12) { printf("month input error, rang: 1-12./n"); exit(-1); } else { if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { valid_day=31; } else if(month==4 || month==6 || month==9 || month==11) { valid_day=30; } else { if(year%400==0 || year%4==0 && year%100!=0) { valid_day=29; } else { valid_day=28; } } } if(day<1 || day>valid_day) { printf("day input error, range: 1-%d./n",valid_day); exit(-1); } if(hour<0 || hour>23) { printf("hour input error, range: 0-23./n"); exit(-1); } if(minute<0 || minute>59) { printf("minute input error, range: 0-59./n"); exit(-1); } if(second<0 || second>59) { printf("second input error, range: 0-59./n"); exit(-1); } } void change_time(char *filename,struct tm *tt) { int i,ret; unsigned long time; struct utimbuf buf; time=mktime(tt); printf("%u/n",time); buf.actime=time; buf.modtime=time; ret=utime(filename,&buf); if(ret==-1) { if(errno==ENOENT) { fprintf(stderr,"%s does not exist./n",filename); exit(-1); } else { fprintf(stderr,"utime error./n"); exit(-1); } } } int main(int argc,char *argv[]) { int i; struct tm t; if(argc==1) { fprintf(stderr,"%s filename.../n",argv[0]); exit(-1); } printf("Input date: "); scanf("%d-%d-%d-%d-%d-%d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec); check_time_format(t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec); printf("%d,%d,%d,%d,%d,%d/n",t.tm_year-=1900,t.tm_mon-=1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec); for(i=1;i<argc;i++) change_time(argv[i],&t); exit(0); }