home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch15 / touch.c < prev   
Encoding:
C/C++ Source or Header  |  1988-12-12  |  5.3 KB  |  152 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. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #define API unsigned extern far pascal
  17.  
  18. API DosClose(unsigned);                 /* OS/2 function prototypes */
  19. API DosFindClose(unsigned);         
  20. API DosFindFirst(char far *, unsigned far *, unsigned, char far *, 
  21.                  unsigned, int far *, unsigned long);
  22. API DosFindNext(unsigned, char far *, unsigned, unsigned far *);
  23. API DosGetDateTime(void far *);
  24. API DosOpen(char far *, unsigned far *, unsigned far *, unsigned long,
  25.             unsigned, unsigned, unsigned, unsigned long);           
  26. API DosSetFileInfo(unsigned, int, void far *, int);
  27.  
  28. #define READ_ONLY 0x01                  /* file attribute bits */
  29. #define HIDDEN    0x02
  30. #define SYSTEM    0x04
  31. #define DIRECTORY 0x10
  32. #define ARCHIVE   0x20
  33.  
  34. #define ATTR      0                     /* attributes to use 
  35.                                            during file search */
  36.  
  37. struct _finfo {                         /* used by DosSetFileInfo */
  38.                     unsigned cdate;
  39.                     unsigned ctime;
  40.                     unsigned adate;
  41.                     unsigned atime;
  42.                     unsigned wdate;
  43.                     unsigned wtime; 
  44.               }     finfo;
  45.  
  46. struct _dinfo {                         /* used by DosGetDateTime */
  47.                     char hour;
  48.                     char min;
  49.                     char sec;
  50.                     char csec;
  51.                     char day;
  52.                     char mon;
  53.                     int  year;
  54.                     int  zone;
  55.                     char dow;       
  56.               }     dinfo;              
  57.  
  58.  
  59. main(int argc, char *argv[])
  60. {
  61.     int i;
  62.  
  63.     if(argc < 2)
  64.     {
  65.         printf("\ntouch: missing filename\n");
  66.         exit(1);
  67.     }
  68.  
  69.     DosGetDateTime(&dinfo);             /* get current date&time */
  70.  
  71.                                         /* set up date & time of
  72.                                            last write in directory 
  73.                                            format for DosSetFileInfo */
  74.     finfo.wdate = ((dinfo.year-1980)<<9) + (dinfo.mon<<5) + dinfo.day;
  75.     finfo.wtime = (dinfo.hour<<11) + (dinfo.min<<5);
  76.  
  77.                                         /* file creation and last 
  78.                                            access fields useless 
  79.                                            in FAT file systems */
  80.     finfo.cdate = finfo.ctime = finfo.adate = finfo.atime = 0;
  81.  
  82.     for(i = 1; i < argc; i++)           /* process all pathnames */
  83.         findfiles(argv[i]);             /* in the command line */
  84.  
  85.     puts("");                           /* final blank line */
  86. }
  87.  
  88. /*
  89.     Search for all files matching a command line argument
  90. */
  91. findfiles(char *cname)
  92. {
  93.     char resbuf[36];                    /* receives search results */
  94.     unsigned status;                    /* receives function status */
  95.     unsigned handle = -1;               /* directory search handle */
  96.     unsigned attr = 0;                  /* attribute for search */
  97.     int matches = 1;                    /* no. of matches requested/found */
  98.  
  99.                                         /* is there any match? */
  100.     if(DosFindFirst(cname, &handle, ATTR, resbuf, 36, &matches, 0L) == 0)
  101.     {
  102.         stampfile(cname, &resbuf[23]);  /* initial match found */
  103.  
  104.                                         /* any additional matches? */
  105.         while(DosFindNext(handle, resbuf, 36, &matches) == 0)
  106.             stampfile(cname, &resbuf[23]);
  107.     }
  108.     else printf("\nno matches:  %s", strlwr(cname));
  109.     DosFindClose(handle);               /* release search handle */
  110. }
  111.  
  112.  
  113. /*
  114.     Set the time and date stamp on a file
  115. */
  116.  
  117. stampfile(char *cname, char *sname)
  118. {
  119.     unsigned status, handle, action;    /* scratch variables */
  120.     char *p;                            /* scratch pointer */
  121.     char qbuff[80];                     /* qualified filename */
  122.  
  123.     memset(qbuff,0,80);                 /* initialize buffer */
  124.  
  125.     p = strrchr(cname, '\\');           /* look for backslash */
  126.  
  127.     if(p != NULL)                       /* any path present? */
  128.         memcpy(qbuff,cname,p-cname+1);  /* yes, copy it */
  129.     else                                /* no, is drive present? */
  130.         if((strlen(cname) >= 2) && (cname[1] == ':'))
  131.         {
  132.             qbuff[0] = cname[0];        /* yes, copy drive */
  133.             qbuff[1] = cname[1];
  134.         }
  135.     strcat(qbuff,sname);                /* add filename from search
  136.                                            to drive +/or path */
  137.  
  138.                                         /* try to open the file */
  139.     if(DosOpen(qbuff, &handle, &action, 0L, 0, 1, 0x22, 0L))
  140.         printf("\ncan't open:  %s", strlwr(qbuff));
  141.     else
  142.     {                                   /* set new time & date stamp */
  143.         if(DosSetFileInfo(handle, 1, &finfo, sizeof(finfo)))
  144.             printf("\ncan't touch: %s", strlwr(qbuff));
  145.         else                            /* audit touched files */
  146.             printf("\ntouched:     %s", strlwr(qbuff));
  147.  
  148.         DosClose(handle);               /* release file handle */
  149.     }       
  150. }
  151.  
  152.