| 1 |
|
| 2 |
#include <sys/types.h> |
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <dirent.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <gutils.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <time.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define OLD "2011" |
| 16 |
#define NEW "2012" |
| 17 |
|
| 18 |
static int IsCopyright(char *orig,char *buffer) { |
| 19 |
char *pt; |
| 20 |
|
| 21 |
if ( strstr(orig,"/* Copyright (C)")==NULL ) |
| 22 |
return( 0 ); |
| 23 |
if ( strstr(orig,NEW)!=NULL ) |
| 24 |
return( 0 ); |
| 25 |
if ( (pt=strstr(orig,OLD))==NULL ) |
| 26 |
return( 0 ); |
| 27 |
|
| 28 |
strncpy(buffer,orig,pt-orig); buffer[pt-orig] = '\0'; |
| 29 |
|
| 30 |
if ( pt[-1]=='-' ) |
| 31 |
strcat(buffer,NEW); |
| 32 |
else if ( pt[-1]==',' ) { |
| 33 |
buffer[(pt-orig)-1]='-'; |
| 34 |
strcat(buffer,NEW); |
| 35 |
} else if ( pt[-1]==' ' && pt[strlen(OLD)]==' ' ) { |
| 36 |
strcat(buffer,OLD); |
| 37 |
strcat(buffer,","); |
| 38 |
strcat(buffer,NEW); |
| 39 |
} else |
| 40 |
return( 0 ); |
| 41 |
|
| 42 |
strcat(buffer,pt+strlen(OLD)); |
| 43 |
|
| 44 |
return( 1 ); |
| 45 |
} |
| 46 |
|
| 47 |
static void ProcessFile(char *filename,FILE *output, char *dir) { |
| 48 |
char buffer[1024]; |
| 49 |
FILE *src; |
| 50 |
char *lines[4]; |
| 51 |
int i,j; |
| 52 |
struct stat sb; |
| 53 |
time_t now; |
| 54 |
struct tm *tm; |
| 55 |
|
| 56 |
src = fopen(filename,"rs"); |
| 57 |
if ( src==NULL ) { |
| 58 |
fprintf( stderr, "Failed to open %s/%s\n", dir, filename); |
| 59 |
exit(1); |
| 60 |
} |
| 61 |
for ( i=0; i<4; ++i ) { |
| 62 |
if ( fgets(buffer,sizeof(buffer),src)==NULL ) { |
| 63 |
fclose(src); |
| 64 |
for ( j=0; j<i; ++j ) free(lines[j]); |
| 65 |
return; |
| 66 |
} |
| 67 |
lines[i] = strdup(buffer); |
| 68 |
} |
| 69 |
fclose(src); |
| 70 |
if ( IsCopyright(lines[0],buffer)) { |
| 71 |
stat(filename,&sb); |
| 72 |
if (!getenv("SOURCE_DATE_EPOCH")) { |
| 73 |
tm = localtime(&sb.st_mtime); |
| 74 |
} else { |
| 75 |
tm = gmtime(&sb.st_mtime); |
| 76 |
} |
| 77 |
fprintf( output, "*** %s~\t%d-%0d-%0d %02d:%02d:%02d.000000000 -0800\n", filename, |
| 78 |
tm->tm_year+1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec ); |
| 79 |
now = GetTime(); |
| 80 |
tm = localtime(&now); |
| 81 |
fprintf( output, "--- %s\t%d-%0d-%0d %02d:%02d:%02d.000000000 -0800\n", filename, |
| 82 |
tm->tm_year+1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec ); |
| 83 |
fprintf( output, "***************\n" ); |
| 84 |
fprintf( output, "*** 1,4 ****\n" ); |
| 85 |
fprintf( output, "! %s", lines[0] ); |
| 86 |
fprintf( output, " %s", lines[1] ); |
| 87 |
fprintf( output, " %s", lines[2] ); |
| 88 |
fprintf( output, " %s", lines[3] ); |
| 89 |
fprintf( output, "--- 1,4 ----\n" ); |
| 90 |
fprintf( output, "! %s", buffer ); |
| 91 |
fprintf( output, " %s", lines[1] ); |
| 92 |
fprintf( output, " %s", lines[2] ); |
| 93 |
fprintf( output, " %s", lines[3] ); |
| 94 |
} |
| 95 |
for ( j=0; j<4; ++j ) free(lines[j]); |
| 96 |
} |
| 97 |
|
| 98 |
static void ProcessDir(char *dir) { |
| 99 |
char buffer[1025]; |
| 100 |
char *here = strdup(getcwd(buffer,sizeof(buffer))); |
| 101 |
DIR *d; |
| 102 |
struct dirent *ent; |
| 103 |
FILE *output; |
| 104 |
|
| 105 |
if ( chdir(dir)==-1 ) { |
| 106 |
fprintf(stderr, "Failed on %s\n", dir ); |
| 107 |
exit( 1 ); |
| 108 |
} |
| 109 |
d = opendir("."); |
| 110 |
if ( d==NULL ) { |
| 111 |
fprintf(stderr, "Failed on %s\n", dir ); |
| 112 |
exit( 1 ); |
| 113 |
} |
| 114 |
output = fopen("copyright.patch","w"); |
| 115 |
if ( output==NULL ) { |
| 116 |
fprintf(stderr, "Failed to create \"copyright.patch\" in %s\n", dir ); |
| 117 |
exit( 1 ); |
| 118 |
} |
| 119 |
while ( (ent = readdir(d))!=NULL ) { |
| 120 |
int len = strlen( ent->d_name ); |
| 121 |
if ( ent->d_name[len-2]=='.' && |
| 122 |
(ent->d_name[len-1]=='c' || ent->d_name[len-1]=='h')) |
| 123 |
ProcessFile(ent->d_name,output, dir); |
| 124 |
} |
| 125 |
if ( ftell(output)==0 ) { |
| 126 |
fprintf( stderr, "Nothing in %s\n", dir ); |
| 127 |
fclose(output); |
| 128 |
unlink("copyright.patch"); |
| 129 |
} else |
| 130 |
fclose(output); |
| 131 |
closedir(d); |
| 132 |
chdir(here); |
| 133 |
free(here); |
| 134 |
} |
| 135 |
|
| 136 |
int main(int argc, char **argv) { |
| 137 |
int i; |
| 138 |
|
| 139 |
if ( argc==1 ) |
| 140 |
ProcessDir("."); |
| 141 |
else { |
| 142 |
for ( i=1 ; i<argc; ++i ) |
| 143 |
ProcessDir(argv[i]); |
| 144 |
} |
| 145 |
return( 0 ); |
| 146 |
} |