#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
Go to the source code of this file.
Functions | |
char * | Ftypetxt (mode_t st_mode, int flag) |
char * | Ftimetxt (time_t t, char timetext[40], int flag) |
int | Compare_2_files (char *adr1, char *adr2, char *adrc, int flag) |
int | main (int argc, char **argv) |
int Compare_2_files | ( | char * | adr1, | |
char * | adr2, | |||
char * | adrc, | |||
int | flag | |||
) |
Definition at line 92 of file compare_file.c.
References Ftimetxt(), and Ftypetxt().
Referenced by main().
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 /* Attributes */ 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, 00121 (unsigned int) (s1.st_mode & ~S_IFMT), 00122 (unsigned int) (s2.st_mode & ~S_IFMT)); 00123 if((s1.st_mode&S_IFMT)!=(s2.st_mode&S_IFMT)) 00124 printf("%s : type : %s <> %s\n", 00125 a, Ftypetxt(s1.st_mode, 0), Ftypetxt(s2.st_mode, 0)); 00126 differs= 1; 00127 } 00128 if(s1.st_uid != s2.st_uid) { 00129 printf("%s : st_uid : %d <> %d\n", a, s1.st_uid, s2.st_uid); 00130 differs= 1; 00131 } 00132 if(s1.st_gid != s2.st_gid) { 00133 printf("%s : st_gid : %d <> %d\n", a, s1.st_gid, s2.st_gid); 00134 differs= 1; 00135 } 00136 if((S_ISCHR(s1.st_mode) && S_ISCHR(s2.st_mode)) || 00137 (S_ISBLK(s1.st_mode) && S_ISBLK(s2.st_mode))) { 00138 if(s1.st_rdev != s2.st_rdev) { 00139 printf("%s : %s st_rdev : %lu <> %lu\n", a, 00140 (S_ISCHR(s1.st_mode) ? "S_IFCHR" : "S_IFBLK"), 00141 (unsigned long) s1.st_rdev, (unsigned long) s1.st_rdev); 00142 differs= 1; 00143 } 00144 } 00145 if(S_ISREG(s2.st_mode) && s1.st_size != s2.st_size) { 00146 printf("%s : st_size : %.f <> %.f diff= %.f\n", 00147 a, (double) s1.st_size, (double) s2.st_size, 00148 ((double) s1.st_size) - (double) s2.st_size); 00149 differs= 1; 00150 } 00151 if(s1.st_mtime != s2.st_mtime) { 00152 printf("%s : st_mtime : %s <> %s diff= %.f s\n", 00153 a, Ftimetxt(s1.st_mtime, ttx1, 0), 00154 Ftimetxt(s2.st_mtime, ttx2, 0), 00155 ((double) s1.st_mtime) - (double) s2.st_mtime); 00156 differs= 1; 00157 } 00158 if(flag&1) { 00159 if(s1.st_atime != s2.st_atime) { 00160 printf("%s : st_atime : %s <> %s diff= %.f s\n", 00161 a, Ftimetxt(s1.st_atime, ttx1, 0), 00162 Ftimetxt(s2.st_atime, ttx2, 0), 00163 ((double) s1.st_atime) - (double) s2.st_atime); 00164 differs= 1; 00165 } 00166 } 00167 if(flag&2) { 00168 if(s1.st_ctime != s2.st_ctime) { 00169 printf("%s : st_ctime : %s <> %s diff= %.f s\n", 00170 a, Ftimetxt(s1.st_ctime, ttx1, 0), 00171 Ftimetxt(s2.st_ctime, ttx2, 0), 00172 ((double) s1.st_ctime) - (double) s2.st_ctime); 00173 differs= 1; 00174 } 00175 } 00176 if(S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode)) { 00177 fd1= open(adr1, O_RDONLY); 00178 if(fd1==-1) { 00179 printf("- %s : cannot open() : %s\n", adr1, strerror(errno)); 00180 return(0); 00181 } 00182 fd2= open(adr2, O_RDONLY); 00183 if(fd2==-1) { 00184 printf("- %s : cannot open() : %s\n", adr2, strerror(errno)); 00185 close(fd1); 00186 return(0); 00187 } 00188 00189 /* Content */ 00190 done= 0; 00191 while(!done) { 00192 r1= read(fd1, buf1, sizeof(buf1)); 00193 r2= read(fd2, buf2, sizeof(buf2)); 00194 if((r1==EOF && r2==EOF) || (r1==0 && r2==0)) 00195 break; 00196 if(r1==EOF || r1==0) { 00197 if(r1==EOF) 00198 r1= 0; 00199 if(s1.st_size > r1count + r1) 00200 printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count); 00201 differs= 1; 00202 } 00203 r1count+= r1; 00204 if(r2==EOF || r2<r1) { 00205 if(r2==EOF) 00206 r2= 0; 00207 if(s2.st_size > r2count + r2) 00208 printf("- %s : early EOF after %.f bytes\n", adr2, (double) r2count); 00209 differs= 1; 00210 done= 1; 00211 } 00212 if(r2>r1) { 00213 if(s1.st_size > r1count + r1) 00214 printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count); 00215 differs= 1; 00216 done= 1; 00217 } 00218 r2count+= r2; 00219 if(r1>r2) 00220 r1= r2; 00221 for(i= 0; i<r1; i++) { 00222 if(buf1[i]!=buf2[i]) { 00223 if(first_diff<0) 00224 first_diff= i; 00225 diffcount++; 00226 } 00227 } 00228 } 00229 if(diffcount>0 || r1count!=r2count) { 00230 if(first_diff<0) 00231 first_diff= (r1count>r2count ? r2count : r1count); 00232 printf("%s : %s : differs by at least %.f bytes. First at %.f\n", a, 00233 (s1.st_mtime==s2.st_mtime ? "CONTENT":"content"), 00234 (double) (diffcount + abs(r1count-r2count)), (double) first_diff); 00235 differs= 1; 00236 } 00237 } 00238 if(fd1!=-1) 00239 close(fd1); 00240 if(fd2!=-1) 00241 close(fd2); 00242 return(!differs); 00243 }
char* Ftimetxt | ( | time_t | t, | |
char | timetext[40], | |||
int | flag | |||
) |
Definition at line 67 of file compare_file.c.
Referenced by Compare_2_files().
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 }
char* Ftypetxt | ( | mode_t | st_mode, | |
int | flag | |||
) |
Definition at line 29 of file compare_file.c.
Referenced by Compare_2_files().
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 }
int main | ( | int | argc, | |
char ** | argv | |||
) |
Definition at line 246 of file compare_file.c.
References Compare_2_files().
00247 { 00248 int ret, i, with_ctime= 1; 00249 char adr1[4096], adr2[4096], adrc[4096]; 00250 00251 if(argc<4) { 00252 fprintf(stderr, "usage: %s path prefix1 prefix2\n", argv[0]); 00253 exit(2); 00254 } 00255 for(i= 4; i<argc; i++) { 00256 if(strcmp(argv[i], "-no_ctime")==0) 00257 with_ctime= 0; 00258 else { 00259 fprintf(stderr, "%s : Option not recognized: '%s'\n", argv[0], argv[i]); 00260 exit(1); 00261 } 00262 } 00263 00264 if(strncmp(argv[1], argv[2], strlen(argv[2]))!=0) { 00265 fprintf(stderr, "%s: path '%s' does not match prefix1 '%s'\n", 00266 argv[0], argv[1], argv[2]); 00267 exit(2); 00268 } 00269 strcpy(adr1, argv[1]); 00270 strcpy(adrc, argv[1]+strlen(argv[2])); 00271 sprintf(adr2, "%s%s%s", 00272 argv[3], (adrc[0]=='/' || adrc[0]==0 ? "" : "/"), adrc); 00273 00274 ret= Compare_2_files(adr1, adr2, adrc, (with_ctime<<1)); 00275 exit(ret<=0); 00276 }