home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOUCH_U.ZIP / TOU_ORIG.C < prev    next >
C/C++ Source or Header  |  1989-08-15  |  7KB  |  278 lines

  1. /* touch.c
  2. **
  3. ** Copyright (c) 1989, Christopher Laforet
  4. ** Released into the Public Domain
  5. **
  6. **
  7. ** (ver 1) turbo.c 2.0 version
  8. ** (ver 2) msc 5.1 os/2-dos bound version
  9. */
  10.  
  11. #define LINT_ARGS
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <os2.h>
  19.  
  20.  
  21.  
  22. #define MAJOR_VERSION        2
  23. #define MINOR_VERSION        1
  24.  
  25.  
  26. extern void get_time(FTIME *time,FDATE *date);
  27. extern void show_usage(void);
  28. extern void touch(unsigned char *filename,FTIME *time,FDATE *date);
  29.  
  30. short verbose_flag = 0;
  31. short confirm_flag = 0;
  32. short create_flag = 0;
  33. short total_files = 0;
  34. short total_touched = 0;
  35. short total_created = 0;
  36.  
  37.  
  38. main(argc,argv)
  39. int argc;
  40. char *argv[];
  41.     {
  42.     FTIME start_time;
  43.     FDATE start_date;
  44.     short count;
  45.     short files = 0;
  46.  
  47.     get_time(&start_time,&start_date);
  48.     fprintf(stderr,"TOUCH (v %u.%02u of %s): A File Date and Time Modification Utility\n",MAJOR_VERSION,MINOR_VERSION,__DATE__);
  49.     fprintf(stderr,"Copyright (c) 1989, Christopher Laforet.  Released into the Public Domain.\n\n");
  50.     if (argc == 1)
  51.         {
  52.         show_usage();
  53.         }
  54.     else
  55.         {
  56.         for (count = 1; count < argc; count++)
  57.             {
  58.             if ((*argv[count] == '/') || (*argv[count] == '-'))     /* check for flags */
  59.                 {
  60.                 switch (argv[count][1])
  61.                     {
  62.                     case '?':
  63.                         show_usage();
  64.                         break;
  65.                     case 'C':
  66.                     case 'c':
  67.                         confirm_flag = 1;
  68.                         break;
  69.                     case 'V':
  70.                     case 'v':
  71.                         verbose_flag = 1;
  72.                         break;
  73.                     case 'K':
  74.                     case 'k':
  75.                         create_flag = 1;
  76.                         break;
  77.                     default:
  78.                         printf("Warning: Invalid flag argument \"%s\".\n",&argv[count][1]);
  79.                         break;
  80.                     }
  81.                 }
  82.             }
  83.         for (count = 1; count < argc; count++)
  84.             {
  85.             if ((*argv[count] != '/') && (*argv[count] != '-'))     /* check for files */
  86.                 {
  87.                 files = 1;
  88.                 touch(argv[count],&start_time,&start_date);
  89.                 }
  90.             }
  91.         if (!files)
  92.             {
  93.             printf("Error: No valid filename(s) specified on command line.\n");
  94.             }
  95.         else
  96.             {
  97.             printf("\nTotal Files Found: %u\n",total_files);
  98.             if (create_flag)
  99.                 {
  100.                 printf("Total Files Created: %u\n",total_created);
  101.                 printf("Total Files Touched/Created: %u\n\n",total_touched);
  102.                 }
  103.             else
  104.                 {
  105.                 printf("Total Files Touched: %u\n\n",total_touched);
  106.                 }
  107.             }
  108.         }
  109.     return(0);
  110.     }
  111.  
  112.  
  113. void get_time(time,date)
  114. FTIME *time;
  115. FDATE *date;
  116.     {
  117.     DATETIME datetime;
  118.  
  119.     DosGetDateTime(&datetime);
  120.     time->hours = datetime.hours;
  121.     time->minutes = datetime.minutes;
  122.     time->twosecs = datetime.seconds >> 1;       /* get two-second interval */
  123.  
  124.     date->year = datetime.year - 1980;
  125.     date->month = datetime.month;
  126.     date->day = datetime.day;
  127.     }
  128.  
  129.  
  130. void show_usage()
  131.     {
  132.     printf("Usage:  TOUCH [flags] pathname [pathname(s)]\n");
  133.     printf("    where flags are:\n");
  134.     printf("\t-k   Create file if not existant.\n");
  135.     printf("\t-c   Confirm each file to be touched.\n");
  136.     printf("\t-v   Verbose mode.  Show each file touched.\n");
  137.     printf("\t-?   Show this usage screen.\n\n");
  138.     }
  139.  
  140.  
  141. void touch(filename,time,date)
  142. unsigned char *filename;
  143. FTIME *time;
  144. FDATE *date;
  145.     {
  146.     unsigned char buffer[100];
  147.     unsigned char drive[_MAX_DRIVE];
  148.     unsigned char dir[_MAX_DIR];
  149.     unsigned char file[_MAX_FNAME];
  150.     unsigned char ext[_MAX_EXT];
  151.     unsigned char *cptr;
  152.     FILEFINDBUF findbuf;
  153.     HDIR hdir = 0xffff;
  154.     FILESTATUS filestatus;
  155.     short num = 1;
  156.     HFILE fh;
  157.     unsigned short action;
  158.     short key;
  159.     int rtn;
  160.     short confirm;
  161.     short found = 0;
  162.  
  163.     _splitpath(filename,drive,dir,file,ext);       /* split pathname */
  164.     rtn = DosFindFirst(filename,&hdir,0x20,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num,0L);
  165.     while (!rtn)
  166.         {
  167.         ++total_files;
  168.         ++found;
  169.         confirm = 0;
  170.         _splitpath(findbuf.achName,buffer,buffer,file,ext);  /* buffer is used as dummy areas */
  171.         _makepath(buffer,drive,dir,file,ext);      /* rebuild pathname */
  172.         strlwr(buffer);
  173.         if (confirm_flag)
  174.             {
  175.             strupr(buffer);
  176.             fprintf(stderr,"Is it OK to touch \"%s\"? ",buffer);
  177.             do
  178.                 {
  179.                 key = getch();
  180.                 key = toupper(key);
  181.                 }
  182.             while ((key != 'N') && (key != 'Y'));
  183.             fprintf(stderr,"%c\n",key);
  184.             if (key == 'Y')
  185.                 {
  186.                 confirm = 1;
  187.                 }
  188.             }
  189.         if (!confirm_flag || (confirm_flag && confirm))
  190.             {
  191.             if (DosOpen((char far *)buffer,(PHFILE)&fh,(PUSHORT)&action,0L,0x20,0x1,_osmajor < 3 ? 0x2 : 0x42,0L))
  192.                 {
  193.                 printf("Error: Unable to open \"%s\"!\n",buffer);
  194.                 }
  195.             else
  196.                 {
  197.                 if (verbose_flag)
  198.                     {
  199.                     printf("Touching %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",findbuf.achName,time->hours,time->minutes,time->twosecs << 1,date->month,date->day,date->year + 1980);
  200.                     }
  201.                 memset(&filestatus,0,sizeof(FILESTATUS));
  202.                 memcpy(&filestatus.ftimeLastWrite,time,sizeof(FTIME));
  203.                 memcpy(&filestatus.fdateLastWrite,date,sizeof(FDATE));
  204.                 if (DosSetFileInfo(fh,0x1,(PFILESTATUS)&filestatus,sizeof(FILESTATUS)))
  205.                     {
  206.                     printf("Error: Unable to change date on \"%s\"!\n",buffer);
  207.                     }
  208.                 else
  209.                     {
  210.                     ++total_touched;
  211.                     }
  212.                 DosClose(fh);
  213.                 }
  214.             }
  215.         num = 1;
  216.         rtn = DosFindNext(hdir,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num);
  217.         }
  218.     if (!found && create_flag)
  219.         {
  220.         cptr = filename;
  221.         while (*cptr)
  222.             {
  223.             found = 1;
  224.             if ((*cptr == '?') || (*cptr == '*'))    /* no wild cards */
  225.                 {
  226.                 found = 0;
  227.                 break;
  228.                 }
  229.             ++cptr;
  230.             }
  231.         if (found)
  232.             {
  233.             confirm = 0;
  234.             if (confirm_flag)
  235.                 {
  236.                 strupr(filename);
  237.                 fprintf(stderr,"Is it OK to create \"%s\"? ",filename);
  238.                 do
  239.                     {
  240.                     key = getch();
  241.                     key = toupper(key);
  242.                     }
  243.                 while ((key != 'N') && (key != 'Y'));
  244.                 fprintf(stderr,"%c\n",key);
  245.                 if (key == 'Y')
  246.                     {
  247.                     confirm = 1;
  248.                     }
  249.                 }
  250.             if (!confirm_flag || (confirm_flag && confirm))
  251.                 {
  252.                 if (DosOpen((char far *)filename,(PHFILE)&fh,(PUSHORT)&action,0L,0x20,0x11,_osmajor <= 4 ? 0x2 : 0x42,0L))
  253.                     {
  254.                     printf("Error: Unable to create \"%s\"!\n",filename);
  255.                     }
  256.                 else
  257.                     {
  258.                     if (verbose_flag)
  259.                         {
  260.                         printf("Creating %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",findbuf.achName,time->hours,time->minutes,time->twosecs << 1,date->month,date->day,date->year + 1980);
  261.                         }
  262.                     memset(&filestatus,0,sizeof(FILESTATUS));
  263.                     memcpy(&filestatus.ftimeLastWrite,time,sizeof(FTIME));
  264.                     memcpy(&filestatus.fdateLastWrite,date,sizeof(FDATE));
  265.                     DosSetFileInfo(fh,0x1,(PFILESTATUS)&filestatus,sizeof(FILESTATUS));
  266.                     ++total_created;
  267.                     ++total_touched;
  268.                     DosClose(fh);
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.     if (!found)
  274.         {
  275.         printf("Error: Unable to find \"%s\"!\n",filename);
  276.         }
  277.     }
  278.