/* mnh.c * * Creates a new history file with only the last version present. * Sources which have been deleted more than a year ago will be removed * from the history file. */ #include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h" #include "xscanf.h" #include "cmain.h" typedef struct { char source[FILENAME_MAX]; char type[10]; int version; char date[32]; char user[32]; char owner[32]; long size; double jd; } h_struct; #define MAXLINE 256 #define MAXSOURCE 1024 static char line[MAXLINE]; static h_struct h[MAXSOURCE]; static char *gip_root; static char *ttostr( time_t tp ) { static char ttostr_b[24]; /* static memory */ struct tm *tmp; /* the time struct */ tmp = localtime( &tp ); /* local time */ strftime( ttostr_b, sizeof( ttostr_b ), "%d/%b/%y %H:%M:%S", tmp ); return( ttostr_b ); /* return to caller */ } static long getjd( char *date ) { static char *months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; char buf[MAXLINE]; char *p; double d, m, y; double julianday_c( double *, double *, double * ); int i; strcpy( buf, date ); p = strtok( buf, "/ :" ); d = atof( p ); p = strtok( NULL, "/ :" ); i = 0; while ( i < 12 && strcmp( months[i], p ) ) i++; m = i; p = strtok( NULL, "/ :" ); y = atof( p ) + 1900.0; p = strtok( NULL, "/ :" ); d += atof( p ) / 24.0; p = strtok( NULL, "/ :" ); d += atof( p ) / 24.0 / 60.0; p = strtok( NULL, "/ :" ); d += atof( p ) / 24.0 / 3600.0; return( julianday_c( &d, &m, &y ) ); } static void checkold( h_struct h ) { FILE *f; char oldname[FILENAME_MAX+1]; char veryoldname[FILENAME_MAX+1]; int v; for ( v = 0; v <= h.version; v++ ) { sprintf( oldname, "%s/old/%s.%d", gip_root, h.source, v ); sprintf( veryoldname, "%s/veryold/%s.%d", gip_root, h.source, v ); f = fopen( oldname, "r" ); if ( f != NULL ) { char cmd[1024]; int count = 0; fclose( f ); sprintf( cmd, "mv %s %s", oldname, veryoldname ); while (f = fopen( veryoldname, "r" )) { fclose( f ); strcat( veryoldname, "_" ); } printf( "%s\n", cmd ); system ( cmd ); } } } MAIN_PROGRAM_ENTRY { FILE *f1; FILE *f2; FILE *f3; char file1[FILENAME_MAX]; char file2[FILENAME_MAX]; char file3[FILENAME_MAX]; char source[FILENAME_MAX]; char type[10]; int version; char date[32]; char user[32]; char owner[32]; long size; int count1 = 0; int count2 = 0; int n; int nf; int ns = 0; double today; time_t now = time( NULL ); gip_root = getenv( "gip_root" ); today = getjd( ttostr( now ) ); sprintf( file1, "%s/sys/history", gip_root ); sprintf( file2, "history.new" ); sprintf( file3, "%s/doc/history.doc", gip_root ); f1 = fopen( file1, "r" ); f2 = fopen( file2, "w" ); f3 = fopen( file3, "r" ); if ( f1 == NULL || f2 == NULL || f3 == NULL ) { fprintf( stderr, "file open error\n" ); return( EXIT_FAILURE ); } while (fgets( line, MAXLINE, f3) != NULL) { fprintf( f2, "#%s", line ); } fclose( f3 ); while ((nf = xscanf( f1, "%s %s %d %s %s %s %d", source, type, &version, date, user, owner, &size )) != -1) { int l, m, n; if (nf == 6) size = 0; for ( n = 0; n < ns && strcmp( source, h[n].source ); n++); strcpy( h[n].source, source ); strcpy( h[n].type, type ); h[n].version = version; l = m = 0; do { h[n].jd = getjd( date ); if (date[l] == ':') h[n].date[m++] = '\\'; h[n].date[m++] = date[l++]; } while (date[l]); h[n].date[m] = 0; strcpy( h[n].user, user ); strcpy( h[n].owner, owner ); h[n].size = size; if (n == ns) ns++; } fclose( f1 ); for ( n = 0; n < ns; n++) { if ( !strcmp( "WASTEBASKET", h[n].owner ) && ( today - h[n].jd ) > 366.0 ) { checkold( h[n] ); count2++; } else { count1++; fprintf( f2, "%s:%s:%d:%s:%s:%s:%d\n", h[n].source, h[n].type, h[n].version, h[n].date, h[n].user, h[n].owner, h[n].size ); } } printf( "Original number of sources: %5d\n", ns ); printf( "Sources removed : %5d\n", count2 ); printf( "Remainig sources : %5d\n", count1 ); fclose( f2 ); return( EXIT_SUCCESS ); }