00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <ctype.h>
00017 #include <sys/types.h>
00018 #include <unistd.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <sys/stat.h>
00022 #include <errno.h>
00023 #include <fcntl.h>
00024 #include <string.h>
00025 #include <time.h>
00026
00027
00028
00029 char *Ftypetxt(mode_t st_mode, int flag)
00030 {
00031 if(flag&1)
00032 goto single_letters;
00033 if(S_ISDIR(st_mode))
00034 return("directory");
00035 else if(S_ISREG(st_mode))
00036 return("regular_file");
00037 else if(S_ISLNK(st_mode))
00038 return("symbolic_link");
00039 else if(S_ISBLK(st_mode))
00040 return("block_device");
00041 else if(S_ISCHR(st_mode))
00042 return("char_device");
00043 else if(S_ISFIFO(st_mode))
00044 return("name_pipe");
00045 else if(S_ISSOCK(st_mode))
00046 return("unix_socket");
00047 return("unknown");
00048 single_letters:;
00049 if(S_ISDIR(st_mode))
00050 return("d");
00051 else if(S_ISREG(st_mode))
00052 return("-");
00053 else if(S_ISLNK(st_mode))
00054 return("l");
00055 else if(S_ISBLK(st_mode))
00056 return("b");
00057 else if(S_ISCHR(st_mode))
00058 return("c");
00059 else if(S_ISFIFO(st_mode))
00060 return("p");
00061 else if(S_ISSOCK(st_mode))
00062 return("s");
00063 return("?");
00064 }
00065
00066
00067 char *Ftimetxt(time_t t, char timetext[40], int flag)
00068 {
00069 char *rpt;
00070 struct tm tms, *tmpt;
00071 static char months[12][4]= { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00072 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00073
00074 tmpt= localtime_r(&t, &tms);
00075 rpt= timetext;
00076 rpt[0]= 0;
00077 if(tmpt==0)
00078 sprintf(rpt+strlen(rpt), "%12.f", (double) t);
00079 else if(time(NULL)-t < 180*86400 && time(NULL)-t >= 0)
00080 sprintf(rpt+strlen(rpt), "%3s %2d %2.2d:%2.2d",
00081 months[tms.tm_mon], tms.tm_mday, tms.tm_hour, tms.tm_min);
00082 else
00083 sprintf(rpt+strlen(rpt), "%3s %2d %4.4d",
00084 months[tms.tm_mon], tms.tm_mday, 1900+tms.tm_year);
00085 return(timetext);
00086 }
00087
00088
00089
00090
00091
00092 int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
00093 {
00094 struct stat s1, s2;
00095 int ret, differs= 0, r1, r2, fd1= -1, fd2= -1, i, done;
00096 char buf1[4096], buf2[4096], a[4096], ttx1[40], ttx2[40];
00097 off_t r1count= 0, r2count= 0, diffcount= 0, first_diff= -1;
00098
00099 ret= lstat(adr1, &s1);
00100 if(ret==-1) {
00101 printf("? %s : cannot lstat() : %s\n", adr1, strerror(errno));
00102 return(0);
00103 }
00104 strcpy(a, Ftypetxt(s1.st_mode, 1));
00105 strcat(a, " ");
00106 if(adrc[0])
00107 strcat(a, adrc);
00108 else
00109 strcat(a, ".");
00110
00111 ret= lstat(adr2, &s2);
00112 if(ret==-1) {
00113 printf("? %s : cannot lstat() : %s\n", adr2, strerror(errno));
00114 return(0);
00115 }
00116
00117
00118 if(s1.st_mode != s2.st_mode) {
00119 if((s1.st_mode&~S_IFMT)!=(s2.st_mode&~S_IFMT))
00120 printf("%s : st_mode : %7.7o <> %7.7o\n", a, s1.st_mode, s2.st_mode);
00121 if((s1.st_mode&S_IFMT)!=(s2.st_mode&S_IFMT))
00122 printf("%s : type : %s <> %s\n",
00123 a, Ftypetxt(s1.st_mode, 0), Ftypetxt(s2.st_mode, 0));
00124 differs= 1;
00125 }
00126 if(s1.st_uid != s2.st_uid) {
00127 printf("%s : st_uid : %d <> %d\n", a, s1.st_uid, s2.st_uid);
00128 differs= 1;
00129 }
00130 if(s1.st_gid != s2.st_gid) {
00131 printf("%s : st_gid : %d <> %d\n", a, s1.st_gid, s2.st_gid);
00132 differs= 1;
00133 }
00134 if((S_ISCHR(s1.st_mode) && S_ISCHR(s2.st_mode)) ||
00135 (S_ISBLK(s1.st_mode) && S_ISBLK(s2.st_mode))) {
00136 if(s1.st_rdev != s2.st_rdev) {
00137 printf("%s : %s st_rdev : %lu <> %lu\n", a,
00138 (S_ISCHR(s1.st_mode) ? "S_IFCHR" : "S_IFBLK"),
00139 (unsigned long) s1.st_rdev, (unsigned long) s1.st_rdev);
00140 differs= 1;
00141 }
00142 }
00143 if(S_ISREG(s2.st_mode) && s1.st_size != s2.st_size) {
00144 printf("%s : st_size : %.f <> %.f diff= %.f\n",
00145 a, (double) s1.st_size, (double) s2.st_size,
00146 ((double) s1.st_size) - (double) s2.st_size);
00147 differs= 1;
00148 }
00149 if(s1.st_mtime != s2.st_mtime) {
00150 printf("%s : st_mtime : %s <> %s diff= %.f s\n",
00151 a, Ftimetxt(s1.st_mtime, ttx1, 0),
00152 Ftimetxt(s2.st_mtime, ttx2, 0),
00153 ((double) s1.st_mtime) - (double) s2.st_mtime);
00154 differs= 1;
00155 }
00156 if(flag&1) {
00157 if(s1.st_atime != s2.st_atime) {
00158 printf("%s : st_atime : %s <> %s diff= %.f s\n",
00159 a, Ftimetxt(s1.st_atime, ttx1, 0),
00160 Ftimetxt(s2.st_atime, ttx2, 0),
00161 ((double) s1.st_atime) - (double) s2.st_atime);
00162 differs= 1;
00163 }
00164 }
00165 if(flag&2) {
00166 if(s1.st_ctime != s2.st_ctime) {
00167 printf("%s : st_ctime : %s <> %s diff= %.f s\n",
00168 a, Ftimetxt(s1.st_ctime, ttx1, 0),
00169 Ftimetxt(s2.st_ctime, ttx2, 0),
00170 ((double) s1.st_ctime) - (double) s2.st_ctime);
00171 differs= 1;
00172 }
00173 }
00174 if(S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode)) {
00175 fd1= open(adr1, O_RDONLY);
00176 if(fd1==-1) {
00177 printf("- %s : cannot open() : %s\n", adr1, strerror(errno));
00178 return(0);
00179 }
00180 fd2= open(adr2, O_RDONLY);
00181 if(fd2==-1) {
00182 printf("- %s : cannot open() : %s\n", adr2, strerror(errno));
00183 close(fd1);
00184 return(0);
00185 }
00186
00187
00188 done= 0;
00189 while(!done) {
00190 r1= read(fd1, buf1, sizeof(buf1));
00191 r2= read(fd2, buf2, sizeof(buf2));
00192 if((r1==EOF && r2==EOF) || (r1==0 && r2==0))
00193 break;
00194 if(r1==EOF || r1==0) {
00195 if(r1==EOF)
00196 r1= 0;
00197 if(s1.st_size > r1count + r1)
00198 printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count);
00199 differs= 1;
00200 }
00201 r1count+= r1;
00202 if(r2==EOF || r2<r1) {
00203 if(r2==EOF)
00204 r2= 0;
00205 if(s2.st_size > r2count + r2)
00206 printf("- %s : early EOF after %.f bytes\n", adr2, (double) r2count);
00207 differs= 1;
00208 done= 1;
00209 }
00210 if(r2>r1) {
00211 if(s1.st_size > r1count + r1)
00212 printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count);
00213 differs= 1;
00214 done= 1;
00215 }
00216 r2count+= r2;
00217 if(r1>r2)
00218 r1= r2;
00219 for(i= 0; i<r1; i++) {
00220 if(buf1[i]!=buf2[i]) {
00221 if(first_diff<0)
00222 first_diff= i;
00223 diffcount++;
00224 }
00225 }
00226 }
00227 if(diffcount>0 || r1count!=r2count) {
00228 if(first_diff<0)
00229 first_diff= (r1count>r2count ? r2count : r1count);
00230 printf("%s : %s : differs by at least %.f bytes. First at %.f\n", a,
00231 (s1.st_mtime==s2.st_mtime ? "CONTENT":"content"),
00232 (double) (diffcount + abs(r1count-r2count)), (double) first_diff);
00233 differs= 1;
00234 }
00235 }
00236 if(fd1!=-1)
00237 close(fd1);
00238 if(fd2!=-1)
00239 close(fd2);
00240 return(!differs);
00241 }
00242
00243
00244 int main(int argc, char **argv)
00245 {
00246 int ret, i, with_ctime= 1;
00247 char adr1[4096], adr2[4096], adrc[4096];
00248
00249 if(argc<4) {
00250 fprintf(stderr, "usage: %s path prefix1 prefix2\n", argv[0]);
00251 exit(2);
00252 }
00253 for(i= 4; i<argc; i++) {
00254 if(strcmp(argv[i], "-no_ctime")==0)
00255 with_ctime= 0;
00256 else {
00257 fprintf(stderr, "%s : Option not recognized: '%s'\n", argv[0], argv[i]);
00258 exit(1);
00259 }
00260 }
00261
00262 if(strncmp(argv[1], argv[2], strlen(argv[2]))!=0) {
00263 fprintf(stderr, "%s: path '%s' does not match prefix1 '%s'\n",
00264 argv[0], argv[1], argv[2]);
00265 exit(2);
00266 }
00267 strcpy(adr1, argv[1]);
00268 strcpy(adrc, argv[1]+strlen(argv[2]));
00269 sprintf(adr2, "%s%s%s",
00270 argv[3], (adrc[0]=='/' || adrc[0]==0 ? "" : "/"), adrc);
00271
00272 ret= Compare_2_files(adr1, adr2, adrc, (with_ctime<<1));
00273 exit(ret<=0);
00274 }
00275