home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / os2touch.zip / TOUCH.C next >
C/C++ Source or Header  |  1988-08-05  |  6KB  |  194 lines

  1. /*
  2.     TOUCH.C     Force time and date stamps on file(s)
  3.  
  4.     Compile:    C> cl touch.c
  5.  
  6.     Usage:      C> TOUCH pathname [ pathname... ]
  7.  
  8.     Pathnames may include wildcard characters
  9.  
  10.     Copyright (C) 1988 Ray Duncan
  11. */
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #define API unsigned extern far pascal
  18.  
  19. API DosClose(unsigned);                 /* OS/2 function prototypes */
  20.  
  21. API DosFindClose(unsigned);         
  22.  
  23. API DosFindFirst(char far *, unsigned far *, unsigned, char far *, 
  24.                  unsigned, int far *, unsigned long);
  25.  
  26. API DosFindNext(unsigned, char far *, unsigned, unsigned far *);
  27.  
  28. API DosGetDateTime(void far *);
  29.  
  30. API DosOpen(char far *, unsigned far *, unsigned far *, unsigned long,
  31.             unsigned, unsigned, unsigned, unsigned long);           
  32.  
  33. API DosSetFileInfo(unsigned, int, void far *, int);
  34.  
  35.  
  36. #define READ_ONLY 0x01                  /* file attribute bits */
  37. #define HIDDEN    0x02
  38. #define SYSTEM    0x04
  39. #define DIRECTORY 0x10
  40. #define ARCHIVE   0x20
  41.  
  42. #define ATTR      0                     /* attributes to use 
  43.                                            during file search */
  44.  
  45. struct _finfo {                         /* used by DosSetFileInfo */
  46.                     unsigned cdate;
  47.                     unsigned ctime;
  48.                     unsigned adate;
  49.                     unsigned atime;
  50.                     unsigned wdate;
  51.                     unsigned wtime; 
  52.               } 
  53.                 finfo;
  54.  
  55. struct _dinfo {                         /* used by DosGetDateTime */
  56.                     char hour;
  57.                     char min;
  58.                     char sec;
  59.                     char csec;
  60.                     char day;
  61.                     char mon;
  62.                     int  year;
  63.                     int  zone;
  64.                     char dow;       
  65.               } 
  66.                 dinfo;              
  67.  
  68.  
  69. main(int argc, char *argv[])
  70. {
  71.     int i;
  72.  
  73.     if(argc < 2)
  74.     {
  75.         printf("\ntouch: missing filename\n");
  76.         exit(1);
  77.     }
  78.  
  79.     DosGetDateTime(&dinfo);             /* get current date&time */
  80.  
  81.                                         /* set up date & time of
  82.                                            last write in directory 
  83.                                            format for DosSetFileInfo */
  84.  
  85.     finfo.wdate = ((dinfo.year-1980)<<9) + (dinfo.mon<<5) + dinfo.day;
  86.  
  87.     finfo.wtime = (dinfo.hour<<11) + (dinfo.min<<5);
  88.  
  89.                                         /* file creation and last 
  90.                                            access fields useless 
  91.                                            in FAT file systems */
  92.  
  93.     finfo.cdate = finfo.ctime = finfo.adate = finfo.atime = 0;
  94.  
  95.     for(i = 1; i < argc; i++)           /* process all pathnames */
  96.     {                                   /* in the command line */
  97.  
  98.         findfiles(argv[i]);             /* find & touch any matches */
  99.     }                                   /* for this pathname */
  100.  
  101.     puts("");                           /* final blank line */
  102. }
  103.  
  104.  
  105. /*
  106.     Search for all files matching a command line argument
  107. */
  108.  
  109. findfiles(char *cname)
  110. {
  111.     char resbuf[36];                    /* receives search results */
  112.     unsigned status;                    /* receives function status */
  113.     unsigned handle = -1;               /* directory search handle */
  114.     unsigned attr = 0;                  /* attribute for search */
  115.     int matches;                        /* number of matches found */
  116.  
  117.     matches = 1;                        /* request max of 1 match */
  118.  
  119.                                         /* is there any match? */
  120.     if(DosFindFirst(cname, &handle, ATTR, resbuf, 36, &matches, 0L) == 0)
  121.     {
  122.         stampfile(cname, &resbuf[23]);  /* initial match found,
  123.                                            force file time & date */
  124.  
  125.                                         /* any additional matches? */
  126.         while(DosFindNext(handle, resbuf, 36, &matches) == 0)
  127.         {
  128.                                         /* force file time & date */
  129.             stampfile(cname, &resbuf[23]);  
  130.         }
  131.     
  132.         DosFindClose(handle);           /* release search handle */
  133.     }
  134.     else
  135.         printf("\nno matches:  %s", strlwr(cname));
  136.  
  137. }
  138.  
  139.  
  140. /*
  141.     Set the time and date stamp on a file
  142. */
  143.  
  144. stampfile(char *cname, char *sname)
  145. {
  146.     unsigned status, handle, action;
  147.     
  148.     char *p;                            /* scratch pointer */
  149.     char qbuff[80];                     /* qualified filename */
  150.  
  151.     memset(qbuff,0,80);                 /* initialize buffer */
  152.  
  153.     p = strrchr(cname, '\\');           /* look for backslash
  154.                                            in original pathname */
  155.  
  156.     if(p != NULL)                       /* any path present? */
  157.  
  158.         memcpy(qbuff,cname,p-cname+1);  /* yes, copy it */
  159.  
  160.     else                                /* no, is drive present? */
  161.  
  162.         if((strlen(cname) >= 2) && (cname[1] == ':'))
  163.         {
  164.             qbuff[0] = cname[0];        /* yes, copy drive */
  165.             qbuff[1] = cname[1];
  166.         }
  167.     
  168.     strcat(qbuff,sname);                /* add filename from search
  169.                                            to drive +/or path */
  170.  
  171.                                         /* try to open the file */
  172.     status = DosOpen(qbuff, &handle, &action, 0L, 0, 1, 0x22, 0L);
  173.  
  174.     if(status)                          /* open fails if read-only */
  175.                                         /* or already opened for write */
  176.         printf("\ncan't open:  %s", strlwr(qbuff));
  177.  
  178.     else                                /* if open succeeded, set */
  179.     {                                   /* new time & date stamp */
  180.         status = DosSetFileInfo(handle, 1, &finfo, sizeof(finfo));
  181.  
  182.         if(status)                      /* should never fail, but... */
  183.                                         
  184.             printf("\ncan't touch: %s", strlwr(qbuff));
  185.         
  186.         else                            /* audit touched files */
  187.             
  188.             printf("\ntouched:     %s", strlwr(qbuff));
  189.  
  190.         DosClose(handle);               /* release file handle */
  191.     }       
  192. }
  193.  
  194.