home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOUCH_U.ZIP / TOUCH.C < prev    next >
C/C++ Source or Header  |  1990-05-04  |  11KB  |  449 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. ** 1990.05.04 Reed Shilts: Minor mods to match Berkeley Unix 4.2
  10. **      unix: -c prevent automatic creation, -f ignored.
  11. **      C.Laf: -k confirm, -v verbose
  12. ** 1990.05.04 Reed Shilts: long filename support added (quick hack!)
  13. **
  14. **
  15. */
  16.  
  17. #define LINT_ARGS
  18. #define __OS2_SDK_12__  1
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <conio.h>
  25. #include <os2.h>
  26.  
  27.  
  28.  
  29. #define MAJOR_VERSION        3
  30. #define MINOR_VERSION        0
  31.  
  32.  
  33. extern void get_time(FTIME *time,FDATE *date);
  34. extern void show_usage(void);
  35. extern void touch(unsigned char *filename,FTIME *time,FDATE *date);
  36.  
  37. short verbose_flag = 0;
  38. short confirm_flag = 0;
  39. short create_flag  = 1;         /* default TRUE - unix style */
  40. short total_files  = 0;
  41. short total_touched = 0;
  42. short total_created = 0;
  43.  
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48.     {
  49.     FTIME start_time;
  50.     FDATE start_date;
  51.     short count;
  52.     short files = 0;
  53.  
  54.     get_time(&start_time,&start_date);
  55.     fprintf(stderr,"TOUCH (v %u.%02u of %s): A File Date and Time Modification Utility\n",MAJOR_VERSION,MINOR_VERSION,__DATE__);
  56.     fprintf(stderr,"Copyright (c) 1989, Christopher Laforet.  Released into the Public Domain.\n\n");
  57.     if (argc == 1)
  58.         {
  59.         show_usage();
  60.         }
  61.     else
  62.         {
  63.         for (count = 1; count < argc; count++)
  64.             {
  65.             if ((*argv[count] == '/') || (*argv[count] == '-'))     /* check for flags */
  66.                 {
  67.                 switch (argv[count][1])
  68.                     {
  69.                     case '?':
  70.                         show_usage();
  71.                         break;
  72.                     case 'F':               /* force is currently ignored */
  73.                     case 'f':
  74.                         break;
  75.                     case 'K':
  76.                     case 'k':
  77.                         confirm_flag = 1;
  78.                         break;
  79.                     case 'V':
  80.                     case 'v':
  81.                         verbose_flag = 1;
  82.                         break;
  83.                     case 'C':
  84.                     case 'c':
  85.                         create_flag = 0;    /* yes - create defaults true */    
  86.                         break;
  87.                     default:
  88.                         printf("Warning: Invalid flag argument \"%s\".\n",&argv[count][1]);
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.         for (count = 1; count < argc; count++)
  94.             {
  95.             if ((*argv[count] != '/') && (*argv[count] != '-'))     /* check for files */
  96.                 {
  97.                 files = 1;
  98.                 touch(argv[count],&start_time,&start_date);
  99.                 }
  100.             }
  101.         if (!files)
  102.             {
  103.             printf("Error: No valid filename(s) specified on command line.\n");
  104.             }
  105.         else
  106.             {
  107.             printf("\nTotal Files Found: %u\n",total_files);
  108.             if (create_flag)
  109.                 {
  110.                 printf("Total Files Created: %u\n",total_created);
  111.                 printf("Total Files Touched/Created: %u\n\n",total_touched);
  112.                 }
  113.             else
  114.                 {
  115.                 printf("Total Files Touched: %u\n\n",total_touched);
  116.                 }
  117.             }
  118.         }
  119.     return(0);
  120.     }
  121.  
  122.  
  123. void get_time(time,date)
  124. FTIME *time;
  125. FDATE *date;
  126.     {
  127.     DATETIME datetime;
  128.  
  129.     DosGetDateTime(&datetime);
  130.     time->hours = datetime.hours;
  131.     time->minutes = datetime.minutes;
  132.     time->twosecs = datetime.seconds >> 1;       /* get two-second interval */
  133.  
  134.     date->year = datetime.year - 1980;
  135.     date->month = datetime.month;
  136.     date->day = datetime.day;
  137.     }
  138.  
  139.  
  140. void show_usage()
  141.     {
  142.     printf("Usage:  TOUCH [flags] pathname [pathname(s)]\n");
  143.     printf("    where flags are:\n");
  144.     printf("\t-c   Prevent creation of file if not existant.\n");
  145.     printf("\t-k   Confirm each file to be touched.\n");
  146.     printf("\t-f   Force touch despite read/write permissions (currently ignored).\n");
  147.     printf("\t-v   Verbose mode.  Show each file touched.\n");
  148.     printf("\t-?   Show this usage screen.\n\n");
  149.     }
  150.  
  151.  
  152.  
  153. /* Big vars made static */
  154. #ifdef __OS2_SDK_12__
  155. FILEFINDBUF findbuf;
  156.  
  157. void touch(filename,time,date)
  158. unsigned char *filename;
  159. FTIME *time;
  160. FDATE *date;
  161.     {
  162.     unsigned char *cptr;
  163.     HDIR hdir = 0xffff;
  164.     FILESTATUS filestatus;
  165.     short num = 1;
  166.     HFILE fh;
  167.     unsigned short action;
  168.     short key;
  169.     int rtn;
  170.     short confirm;
  171.     short found = 0;
  172.  
  173.     rtn = DosFindFirst( filename, &hdir, 0x20, (PFILEFINDBUF)&findbuf, 
  174.                         sizeof(FILEFINDBUF), &num, 0L);
  175.     while (!rtn)
  176.         {
  177.         ++total_files;
  178.         ++found;
  179.         confirm = 0;
  180.  
  181.         if (confirm_flag)
  182.             {
  183.             fprintf(stderr,"Is it OK to touch \"%s\"? ", findbuf.achName);
  184.             do
  185.                 {
  186.                 key = getch();
  187.                 key = toupper(key);
  188.                 }
  189.             while ((key != 'N') && (key != 'Y'));
  190.             fprintf(stderr,"%c\n",key);
  191.             if (key == 'Y')
  192.                 {
  193.                 confirm = 1;
  194.                 }
  195.             }
  196.         
  197.         if (!confirm_flag || (confirm_flag && confirm))
  198.             {
  199.             if (DosOpen( (char far *)findbuf.achName, (PHFILE)&fh, 
  200.                          (PUSHORT)&action, 0L, 0x20, 0x1, 
  201.                          _osmajor < 3 ? 0x2 : 0x42, 0L)
  202.                        ) 
  203.                 {
  204.                 printf("Error: Unable to open \"%s\"!\n", findbuf.achName);
  205.                 }
  206.             else
  207.                 {
  208.                 if (verbose_flag)
  209.                     {
  210.                     printf("Touching %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",
  211.                             findbuf.achName,
  212.                             time->hours,time->minutes,time->twosecs << 1,
  213.                             date->month,date->day,date->year + 1980);
  214.                     }
  215.                 memset(&filestatus, 0, sizeof(FILESTATUS));
  216.                 memcpy(&filestatus.ftimeLastWrite, time, sizeof(FTIME));
  217.                 memcpy(&filestatus.fdateLastWrite, date, sizeof(FDATE));
  218.  
  219.                 if (DosSetFileInfo(fh, 0x01, (PBYTE)&filestatus, sizeof(FILESTATUS)))
  220.                     {
  221.                     printf("Error: Unable to change date on \"%s\"!\n", findbuf.achName);
  222.                     }
  223.                 else
  224.                     {
  225.                     ++total_touched;
  226.                     }
  227.                 DosClose(fh);
  228.                 }
  229.             }
  230.         num = 1;
  231.         rtn = DosFindNext(hdir,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num);
  232.         }
  233.     if (!found && create_flag)
  234.         {
  235.         cptr = filename;
  236.         while (*cptr)
  237.             {
  238.             found = 1;
  239.             if ((*cptr == '?') || (*cptr == '*'))    /* no wild cards */
  240.                 {
  241.                 found = 0;
  242.                 break;
  243.                 }
  244.             ++cptr;
  245.             }
  246.         if (found)
  247.             {
  248.             confirm = 0;
  249.             if (confirm_flag)
  250.                 {
  251.                 strupr(filename);
  252.                 fprintf(stderr,"Is it OK to create \"%s\"? ",filename);
  253.                 do
  254.                     {
  255.                     key = getch();
  256.                     key = toupper(key);
  257.                     }
  258.                 while ((key != 'N') && (key != 'Y'));
  259.                 fprintf(stderr,"%c\n",key);
  260.                 if (key == 'Y')
  261.                     {
  262.                     confirm = 1;
  263.                     }
  264.                 }
  265.             if (!confirm_flag || (confirm_flag && confirm))
  266.                 {
  267.                 if (DosOpen( (char far *)filename, (PHFILE)&fh, (PUSHORT)&action, 
  268.                              0L, 0x20, 0x11, _osmajor <= 4 ? 0x2 : 0x42, 0L)
  269.                            )
  270.                     {
  271.                     printf("Error: Unable to create \"%s\"!\n",filename);
  272.                     }
  273.                 else
  274.                     {
  275.                     if (verbose_flag)
  276.                         {
  277.                         printf("Creating %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",
  278.                                 findbuf.achName,
  279.                                 time->hours,time->minutes,time->twosecs << 1,
  280.                                 date->month,date->day,date->year + 1980);
  281.                         }
  282.                     memset(&filestatus, 0, sizeof(FILESTATUS));
  283.                     memcpy(&filestatus.ftimeLastWrite, time, sizeof(FTIME));
  284.                     memcpy(&filestatus.fdateLastWrite, date, sizeof(FDATE));
  285.                     DosSetFileInfo(fh, 0x01, (PBYTE)&filestatus, sizeof(FILESTATUS));
  286.                     ++total_created;
  287.                     ++total_touched;
  288.                     DosClose(fh);
  289.                     }
  290.                 }
  291.             }
  292.         }
  293.     if (!found)
  294.         {
  295.         printf("Error: Unable to find \"%s\"!\n",filename);
  296.         }
  297.     }
  298.  
  299. #else
  300.  
  301. void touch(filename,time,date)
  302. unsigned char *filename;
  303. FTIME *time;
  304. FDATE *date;
  305.     {
  306.     unsigned char buffer[100]; 
  307.     unsigned char drive[_MAX_DRIVE];
  308.     unsigned char dir[_MAX_DIR];    
  309.     unsigned char file[_MAX_FNAME];
  310.     unsigned char ext[_MAX_EXT];
  311.     FILEFINDBUF findbuf;
  312.  
  313.     unsigned char *cptr;
  314.     HDIR hdir = 0xffff;
  315.     FILESTATUS filestatus;
  316.     short num = 1;
  317.     HFILE fh;
  318.     unsigned short action;
  319.     short key;
  320.     int rtn;
  321.     short confirm;
  322.     short found = 0;
  323.  
  324.     _splitpath(filename,drive,dir,file,ext);       /* split pathname */
  325.     rtn = DosFindFirst( filename, &hdir, 0x20, (PFILEFINDBUF)&findbuf, sizeof(FILEFINDBUF), &num, 0L);
  326.     while (!rtn)
  327.         {
  328.         ++total_files;
  329.         ++found;
  330.         confirm = 0;
  331.         _splitpath(findbuf.achName,buffer,buffer,file,ext);  /* buffer is used as dummy areas */
  332.         _makepath(buffer,drive,dir,file,ext);      /* rebuild pathname */
  333.         strlwr(buffer);
  334.         if (confirm_flag)
  335.             {
  336.             strupr(buffer);
  337.             fprintf(stderr,"Is it OK to touch \"%s\"? ",buffer);
  338.             do
  339.                 {
  340.                 key = getch();
  341.                 key = toupper(key);
  342.                 }
  343.             while ((key != 'N') && (key != 'Y'));
  344.             fprintf(stderr,"%c\n",key);
  345.             if (key == 'Y')
  346.                 {
  347.                 confirm = 1;
  348.                 }
  349.             }
  350.         if (!confirm_flag || (confirm_flag && confirm))
  351.             {
  352.             if (DosOpen( (char far *)buffer, (PHFILE)&fh, (PUSHORT)&action, 0L, 0x20, 0x1, _osmajor < 3 ? 0x2 : 0x42,0L))
  353.                 {
  354.                 printf("Error: Unable to open \"%s\"!\n",buffer);
  355.                 }
  356.             else
  357.                 {
  358.                 if (verbose_flag)
  359.                     {
  360.                     printf("Touching %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",
  361.                             findbuf.achName,
  362.                             time->hours,time->minutes,time->twosecs << 1,
  363.                             date->month,date->day,date->year + 1980);
  364.                     }
  365.                 memset(&filestatus,0,sizeof(FILESTATUS));
  366.                 memcpy(&filestatus.ftimeLastWrite,time,sizeof(FTIME));
  367.                 memcpy(&filestatus.fdateLastWrite,date,sizeof(FDATE));
  368.  
  369.                 if (DosSetFileInfo(fh, 0x1, (PBYTE)&filestatus, sizeof(FILESTATUS)))
  370.                     {
  371.                     printf("Error: Unable to change date on \"%s\"!\n",buffer);
  372.                     }
  373.                 else
  374.                     {
  375.                     ++total_touched;
  376.                     }
  377.                 DosClose(fh);
  378.                 }
  379.             }
  380.         num = 1;
  381.         rtn = DosFindNext(hdir,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num);
  382.         }
  383.     if (!found && create_flag)
  384.         {
  385.         cptr = filename;
  386.         while (*cptr)
  387.             {
  388.             found = 1;
  389.             if ((*cptr == '?') || (*cptr == '*'))    /* no wild cards */
  390.                 {
  391.                 found = 0;
  392.                 break;
  393.                 }
  394.             ++cptr;
  395.             }
  396.         if (found)
  397.             {
  398.             confirm = 0;
  399.             if (confirm_flag)
  400.                 {
  401.                 strupr(filename);
  402.                 fprintf(stderr,"Is it OK to create \"%s\"? ",filename);
  403.                 do
  404.                     {
  405.                     key = getch();
  406.                     key = toupper(key);
  407.                     }
  408.                 while ((key != 'N') && (key != 'Y'));
  409.                 fprintf(stderr,"%c\n",key);
  410.                 if (key == 'Y')
  411.                     {
  412.                     confirm = 1;
  413.                     }
  414.                 }
  415.             if (!confirm_flag || (confirm_flag && confirm))
  416.                 {
  417.                 if (DosOpen((char far *)filename,(PHFILE)&fh,(PUSHORT)&action,0L,0x20,0x11,_osmajor <= 4 ? 0x2 : 0x42,0L))
  418.                     {
  419.                     printf("Error: Unable to create \"%s\"!\n",filename);
  420.                     }
  421.                 else
  422.                     {
  423.                     if (verbose_flag)
  424.                         {
  425.                         printf("Creating %-12.12s -> %02u:%02u:%02u on %02u/%02u/%04u\n",
  426.                                 findbuf.achName,
  427.                                 time->hours,time->minutes,time->twosecs << 1,
  428.                                 date->month,date->day,date->year + 1980);
  429.                         }
  430.                     memset(&filestatus,0,sizeof(FILESTATUS));
  431.                     memcpy(&filestatus.ftimeLastWrite,time,sizeof(FTIME));
  432.                     memcpy(&filestatus.fdateLastWrite,date,sizeof(FDATE));
  433.                     DosSetFileInfo(fh, 0x1, (PBYTE)&filestatus, sizeof(FILESTATUS));
  434.                     ++total_created;
  435.                     ++total_touched;
  436.                     DosClose(fh);
  437.                     }
  438.                 }
  439.             }
  440.         }
  441.     if (!found)
  442.         {
  443.         printf("Error: Unable to find \"%s\"!\n",filename);
  444.         }
  445.     }
  446.  
  447. #endif
  448.  
  449.