home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOOLS2.ZIP / TOUCH.C < prev    next >
Text File  |  1989-05-05  |  5KB  |  238 lines

  1. /**
  2.  
  3.     TOUCH.C - UNIX style file date modification command
  4.  
  5. **/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <process.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. #include <sys\types.h>
  13. #include <sys\utime.h>
  14. #include <sys\stat.h>
  15. #include <errno.h>
  16. #include <io.h>
  17.  
  18. /**
  19.  
  20.     Static variables for TOUCH
  21.  
  22. **/
  23.  
  24. #define TRUE        1
  25. #define FALSE        0
  26. #define ERROR        -1
  27.  
  28. static int Verbose ;        /* TRUE for verbose mode */
  29. static int NoCreate ;        /* True to not create 0 length file */
  30.  
  31. static struct utimbuf ModTime ; /* time to set modification date to */
  32.  
  33. static int days_in_month[] =
  34.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  35.  
  36. /**
  37.  
  38.     ParseTime() - parse the time string MMDDHHMM[YY]
  39.  
  40.     Return TRUE if able to extract time, FALSE otherwise
  41.  
  42. **/
  43.  
  44. static int ParseTime( buffer )
  45. char *buffer ;
  46. {
  47.     char *s ;
  48.     time_t timeval ;
  49.     struct tm *timebuf ;
  50.     int month, day, hour, min, year, i ;
  51.  
  52.     putenv("TZ=GMT") ;
  53.     tzset() ;
  54.  
  55.     if ( (strlen(buffer) != 8) && (strlen(buffer) !=10) )
  56.         return FALSE ;
  57.  
  58.     for ( s = buffer ; *s ; s++ )
  59.         if ( !isdigit(*s) )
  60.             return FALSE ;
  61.  
  62.     if ( strlen(buffer) == 10 )
  63.         year  = ((buffer[8]-'0') * 10) + (buffer[9]-'0') ;
  64.     else
  65.     {
  66.         time( &timeval ) ;
  67.         timebuf = localtime( &timeval ) ;
  68.         year = timebuf->tm_year ;
  69.     }
  70.     if ( year < 70 )
  71.         return FALSE ;
  72.  
  73.     month = ((buffer[0]-'0') * 10) + (buffer[1]-'0') ;
  74.     if ( month < 1 || month > 12 )
  75.         return FALSE ;
  76.  
  77.     if ( !(year % 4) )
  78.         days_in_month[1]++ ;
  79.  
  80.     day   = ((buffer[2]-'0') * 10) + (buffer[3]-'0') ;
  81.     if ( day < 1 || day > days_in_month[month-1] )
  82.         return FALSE ;
  83.  
  84.     hour  = ((buffer[4]-'0') * 10) + (buffer[5]-'0') ;
  85.     if ( hour > 23 )
  86.         return FALSE ;
  87.  
  88.     min   = ((buffer[6]-'0') * 10) + (buffer[7]-'0') ;
  89.     if ( min > 59 )
  90.         return FALSE ;
  91.  
  92.     if ( (month == 2) && (day == 29) && (year % 4) )
  93.         return FALSE ;
  94.  
  95.     /*-- calculate the number of seconds since 00:00:00 01/01/1970 ---*/
  96.  
  97.     timeval = 0L ;
  98.  
  99.     for ( i = 70 ; i < year ; i++ )
  100.         if ( i % 4 )
  101.             timeval += 365 ;
  102.         else
  103.             timeval += 366 ;
  104.  
  105.     for ( i = 0 ; i < month-1 ; i ++ )
  106.         timeval += days_in_month[i] ;
  107.  
  108.     timeval += (day-1) ;        /* time contains number of days */
  109.  
  110.     timeval *= 24 ;
  111.     timeval += hour ;        /* time contains number of hours */
  112.  
  113.     timeval *= 60 ;
  114.     timeval += min ;        /* time contains number of minutes */
  115.  
  116.     timeval *= 60 ;            /* time contains number of seconds */
  117.  
  118.     ModTime.actime  = timeval + timezone ;
  119.     ModTime.modtime = timeval + timezone ;
  120.  
  121.     return TRUE ;
  122. }
  123.  
  124. /**
  125.  
  126.     Banner() - print a program title banner
  127.  
  128. **/
  129.  
  130. static void Banner()
  131. {
  132.     fprintf( stderr, "TOUCH\n" ) ;
  133.     fprintf( stderr, "Copyright (c) 1987 by Computer Aided Planning, Inc.\n");
  134.     fprintf( stderr, "All Rights Reserved.\n\n") ;
  135. }
  136.  
  137. /**
  138.  
  139.     ShowUsage() - show the usage of the TOUCH command
  140.  
  141. **/
  142.  
  143. static void ShowUsage()
  144. {
  145.     fprintf( stderr, "Usage: TOUCH [-cmv?] [mmddhhmm[yy]] file ...\n\n" ) ;
  146.     fprintf( stderr, "\t-c : Do not create file if it doesn't exist\n" ) ;
  147.     fprintf( stderr, "\t-v : Verbose, echo each file name as touched\n") ;
  148.     fprintf( stderr, "\t-m : Set modification time only (no effect)\n") ;
  149.     fprintf( stderr, "\t-? : Display this message\n") ;
  150.     exit(1) ;
  151. }
  152.  
  153.  
  154. /**
  155.  
  156.     MAIN ROUTINE
  157.  
  158. **/
  159.  
  160. void main( argc, argv )
  161. int argc ;
  162. char *argv[] ;
  163. {
  164.     time_t timeval ;
  165.     int got_time = FALSE ;
  166.     int i, fh ;
  167.  
  168. /*+    Banner() ;*/
  169.  
  170.     if ( argc < 2 )
  171.     {
  172.         ShowUsage() ;
  173.     }
  174.  
  175.     for ( i = 1 ; i < argc ; i ++ )
  176.     {
  177.         if ( *argv[i] == '-' || *argv[i] == '/' )
  178.         {
  179.             switch( *(argv[i]+1) )
  180.             {
  181.             case 'c' :        /* no create mode */
  182.             case 'C' :
  183.                 NoCreate = TRUE ;
  184.                 break ;
  185.             case 'v' :        /* verbose mode */
  186.             case 'V' :
  187.                 Verbose = TRUE ;
  188.                 break ;
  189.             case '?' :        /* help */
  190.                 ShowUsage() ;
  191.                 break ;
  192.             }
  193.         }
  194.         else
  195.         {
  196.             if ( ! got_time )
  197.             {
  198.                 got_time = TRUE ;
  199.  
  200.                 if( ParseTime(argv[i]) )
  201.                     continue ;
  202.                 else
  203.                 {
  204.                     time ( &timeval ) ;
  205.                     ModTime.actime  = timeval ;
  206.                     ModTime.modtime = timeval ;
  207.                 }
  208.             }
  209.  
  210.             if( utime( argv[i], &ModTime ) == ERROR )
  211.             {
  212.                 /*--- create a 0 length file ---*/
  213.  
  214.                 if ( errno == ENOENT && ! NoCreate )
  215.                 {
  216.                     if ( (fh = creat( argv[i], S_IREAD|S_IWRITE)) != ERROR )
  217.                     {
  218.                         utime( argv[i], &ModTime ) ;
  219.                         if ( Verbose )
  220.                             fprintf( stderr, "\t%s\n", strupr(argv[i]) ) ;
  221.                         close(fh) ;
  222.                     }
  223.                     else
  224.                         fprintf( stderr, "\t%s : E%05d - %s\n", strupr(argv[i]),
  225.                             errno, sys_errlist[errno] ) ;
  226.                 }
  227.                 else
  228.                     fprintf( stderr, "\t%s : E%05d - %s\n", strupr(argv[i]),
  229.                         errno, sys_errlist[errno] ) ;
  230.             }
  231.  
  232.             if ( Verbose )
  233.                 fprintf( stderr, "\t%s\n", strupr(argv[i]) ) ;
  234.         }
  235.     }
  236. }
  237.