1/* 2 The evil 'set file time' function for testing file removal 3 Use: 4 setfiletime Age file* 5 */ 6 7#include <stdio.h> 8#include <sys/time.h> 9#include <sys/time.h> 10#include <unistd.h> 11#include <stdlib.h> 12 13 extern char *optarg; 14 extern int optind; 15 extern int optopt; 16 extern int opterr; 17 extern int optreset; 18 19char *Name = "???"; 20 21char *msg[] = { 22 "%s: setfiletime AGE file*\n", 23 " AGE is fK, where f amount (fraction)", 24 " K is amount - S/s sec, M/m min, H/h hour, D/d day", 25 0 26}; 27void usage(void) 28{ 29 int i; 30 for( i = 0; msg[i]; ++i ){ 31 if( i == 0 ) fprintf( stderr, msg[i], Name ); 32 else fprintf( stderr, "%s\n", msg[i] ); 33 } 34 exit(1); 35} 36 37 38int main( int argc, char **argv ) 39{ 40 int c; 41 int age; 42 float fage; 43 char *s, *t; 44 struct timeval tval[2]; 45 setlinebuf(stdout); 46 if( argv[0] ) Name = argv[0]; 47 while( (c = getopt( argc, argv, "=" ) ) != EOF ){ 48 switch( c ){ 49 case '=': usage(); break; 50 default: usage(); break; 51 } 52 } 53 if( argc - optind == 0 ) usage(); 54 s = argv[optind++]; 55 t = 0; 56 fage = strtod(s,&t); 57 printf("age '%f' (%s)\n", fage, s ); 58 if( !t ) usage(); 59 if( !fage ) usage(); 60 switch( t[0] ){ 61 case 's': case 'S': break; 62 case 'm': case 'M': fage *= 60; break; 63 case 'h': case 'H': fage *= 60*60; break; 64 case 'd': case 'D': fage *= 24*60*60; break; 65 } 66 printf( "back '%f' sec\n", fage ); 67 if( gettimeofday( &tval[0], 0 ) ){ 68 perror( "gettimeofday" ); exit(1); 69 } 70 printf( "now '%d'\n", (int)tval[0].tv_sec ); 71 tval[0].tv_sec -= (int)fage; 72 tval[1] = tval[0]; 73 while( optind < argc ){ 74 s = argv[optind++]; 75 printf( "setting '%s' back %d\n", s, (int)fage ); 76 if( utimes( s, tval ) ){ 77 perror( "utimes" ); exit(1); 78 } 79 } 80 return(0); 81} 82