1/* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of 5 * the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 15 * MA 02111-1307 USA 16 */ 17/* 18 The evil 'set file time' function for testing file removal 19 Use: 20 setfiletime Age file* 21 */ 22 23#include <stdio.h> 24#include <sys/time.h> 25#include <sys/time.h> 26#include <unistd.h> 27#include <stdlib.h> 28 29 extern char *optarg; 30 extern int optind; 31 extern int optopt; 32 extern int opterr; 33 extern int optreset; 34 35char *Name = "???"; 36 37char *msg[] = { 38 "%s: setfiletime AGE file*\n", 39 " AGE is fK, where f amount (fraction)", 40 " K is amount - S/s sec, M/m min, H/h hour, D/d day", 41 0 42}; 43void usage(void) 44{ 45 int i; 46 for( i = 0; msg[i]; ++i ){ 47 if( i == 0 ) fprintf( stderr, msg[i], Name ); 48 else fprintf( stderr, "%s\n", msg[i] ); 49 } 50 exit(1); 51} 52 53 54int main( int argc, char **argv ) 55{ 56 int c; 57 int age; 58 float fage; 59 char *s, *t; 60 struct timeval tval[2]; 61 setlinebuf(stdout); 62 if( argv[0] ) Name = argv[0]; 63 while( (c = getopt( argc, argv, "=" ) ) != EOF ){ 64 switch( c ){ 65 case '=': usage(); break; 66 default: usage(); break; 67 } 68 } 69 if( argc - optind == 0 ) usage(); 70 s = argv[optind++]; 71 t = 0; 72 fage = strtod(s,&t); 73 printf("age '%f' (%s)\n", fage, s ); 74 if( !t ) usage(); 75 if( !fage ) usage(); 76 switch( t[0] ){ 77 case 's': case 'S': break; 78 case 'm': case 'M': fage *= 60; break; 79 case 'h': case 'H': fage *= 60*60; break; 80 case 'd': case 'D': fage *= 24*60*60; break; 81 } 82 printf( "back '%f' sec\n", fage ); 83 if( gettimeofday( &tval[0], 0 ) ){ 84 perror( "gettimeofday" ); exit(1); 85 } 86 printf( "now '%d'\n", (int)tval[0].tv_sec ); 87 tval[0].tv_sec -= (int)fage; 88 tval[1] = tval[0]; 89 while( optind < argc ){ 90 s = argv[optind++]; 91 printf( "setting '%s' back %d\n", s, (int)fage ); 92 if( utimes( s, tval ) ){ 93 perror( "utimes" ); exit(1); 94 } 95 } 96 return(0); 97} 98