home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TOUCH12.ZIP / TOUCH.C < prev    next >
Text File  |  1990-01-03  |  7KB  |  177 lines

  1. /*
  2.     TOUCH.C     Force time and date stamps on file(s), for OS/2 1.2.
  3.  
  4.     This program does not require Microsoft or IBM Programmer's Toolkit
  5.     header files, but must be linked using LINK.EXE and DOSCALLS.LIB 
  6.     from retail OS/2 version 1.2.
  7.  
  8.     Compile:    cl /Zi -c touch.c
  9.                 link touch,touch,,doscalls,touch.def;
  10.  
  11.     Usage:      touch pathname [ pathname... ]
  12.  
  13.                 A pathname may include a drive and a relative or
  14.                 complete path.  A pathname must include a filename,
  15.                 which may include wildcard characters.
  16.  
  17.     Copyright (C) 1989 Ray Duncan
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #define API unsigned extern far pascal      // OS/2 API function prototypes
  24. API DosFindClose(unsigned);         
  25. API DosFindFirst2(char far *, unsigned far *, unsigned, void far *,
  26.                   unsigned, int far *, int, unsigned long);
  27. API DosFindNext(unsigned, void far *, unsigned, unsigned far *);
  28. API DosGetDateTime(void far *);
  29. API DosQPathInfo(char far *, unsigned, void far *, unsigned, unsigned long);
  30. API DosSetPathInfo(char far *, unsigned, void far *, unsigned, 
  31.                    unsigned, unsigned long);
  32.  
  33. #define MAXPATHNAME     260                 // max length of pathname
  34. #define MAXFILENAME     255                 // max length of filename
  35.  
  36. #define NORMAL    0x00                      // file attribute bits
  37. #define READ_ONLY 0x01                  
  38. #define HIDDEN    0x02
  39. #define SYSTEM    0x04
  40. #define DIRECTORY 0x10
  41. #define ARCHIVE   0x20
  42.  
  43. struct _finfo {                             // used by DosSetFileInfo 
  44.                     unsigned cdate;
  45.                     unsigned ctime;
  46.                     unsigned adate;
  47.                     unsigned atime;
  48.                     unsigned wdate;
  49.                     unsigned wtime; 
  50.                     long     fsize;
  51.                     long     falloc;
  52.                     unsigned fattr;
  53.               } ;
  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.               } dinfo;              
  66.  
  67. struct _sbuf  {                             // result buffer format 
  68.                     unsigned cdate;         // for DosFindFirst2
  69.                     unsigned ctime;         // info level 1
  70.                     unsigned adate;
  71.                     unsigned atime;
  72.                     unsigned wdate;
  73.                     unsigned wtime; 
  74.                     long fsize;
  75.                     long falloc;
  76.                     unsigned fattr;
  77.                     char fcount;
  78.                     char fname[MAXFILENAME]; 
  79.              } ;
  80.  
  81. main(int argc, char *argv[])
  82. {                                           
  83.     int i;                                  // scratch variable
  84.                                             
  85.     if(argc < 2)                            // check command line
  86.     {
  87.         printf("\nUsage:     touch pathname [pathname... ]\n");
  88.         printf("\n           A pathname may include a drive and a partial");
  89.         printf("\n           or complete path.  A pathname must include a");
  90.         printf("\n           filename, which may contain wildcards.\n");
  91.         printf("\nExamples:  touch foo.dat");
  92.         printf("\n           touch myfile* *.c\n");
  93.         exit(1);
  94.     }
  95.  
  96.     DosGetDateTime(&dinfo);                 // get current date&time 
  97.  
  98.     for(i = 1; i < argc; i++)               // process all pathnames 
  99.         findfiles(argv[i]);                 // in the command line 
  100.  
  101.     puts("");                               // final blank line 
  102. }
  103.  
  104. /*
  105.     Search for all files matching a command line argument
  106. */
  107. findfiles(char *cname)
  108. {
  109.     unsigned status;                        // receives function status 
  110.     unsigned handle = -1;                   // directory search handle 
  111.     int matches = 1;                        // no. of matches requested/found 
  112.     struct _sbuf sbuf;                      // receives search results
  113.  
  114.                                             // is there any match? 
  115.     if(!DosFindFirst2(cname,                // pathname from command line
  116.                      &handle,               // receives search handle
  117.                      NORMAL,                // attribute of files to match
  118.                      &sbuf,                 // receives search results
  119.                      sizeof(sbuf),          // length of result buffer
  120.                      &matches,              // receives match count
  121.                      1,                     // retrieve info level 1
  122.                      0L))                   // reserved
  123.     {
  124.         do
  125.         {
  126.             stampfile(cname, sbuf.fname);   // match found, set time/date
  127.  
  128.         } while(!DosFindNext(handle, &sbuf, sizeof(sbuf), &matches));
  129.     }
  130.  
  131.     else printf("\nno matches:  %s", strlwr(cname));
  132.  
  133.     DosFindClose(handle);                   // release search handle 
  134. }
  135.  
  136.  
  137. /*
  138.     Set the time and date stamp on a file
  139. */
  140. stampfile(char *cname, char *sname)
  141. {
  142.     char *p;                                // scratch pointer 
  143.     char pname[MAXPATHNAME];                // target pathname
  144.     char qname[MAXPATHNAME];                // qualified pathname
  145.     struct _finfo finfo;                    // file information structure
  146.  
  147.     memset(pname, 0, MAXPATHNAME);          // initialize buffer 
  148.     p = strrchr(cname, '\\');               // look for backslash 
  149.  
  150.     if(p != NULL)                           // any path present? 
  151.         memcpy(pname,cname,p-cname+1);      // yes, copy it 
  152.     else                                    // no, is drive present? 
  153.         if((strlen(cname) >= 2) && (cname[1] == ':'))
  154.         {
  155.             pname[0] = cname[0];            // yes, copy drive 
  156.             pname[1] = cname[1];
  157.         }
  158.     strcat(pname,sname);                    // form target pathname
  159.  
  160.                                             // get fully qualified filename
  161.     DosQPathInfo(pname, 5, qname, sizeof(qname), 0L);
  162.  
  163.                                             // read time and date stamps
  164.     if(DosQPathInfo(pname, 1, &finfo, sizeof(finfo), 0L))
  165.         printf("\ncan't touch:  %s", strlwr(qname));
  166.     else
  167.     {                                       // force date/time of last write
  168.         finfo.wdate = ((dinfo.year-1980)<<9) + (dinfo.mon<<5) + dinfo.day;
  169.         finfo.wtime = (dinfo.hour<<11) + (dinfo.min<<5);
  170.  
  171.                                             // write new date & time stamps
  172.         if(!DosSetPathInfo(pname, 1, &finfo, sizeof(finfo), 0x10, 0L))
  173.              printf("\ntouched:     %s", strlwr(qname));
  174.         else printf("\ncan't touch: %s", strlwr(qname));
  175.     }       
  176. }
  177.