home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SEQTOUCH.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  207 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  SEQTOUCH.C - Touch files in a directory with sequential time stamps.
  5. **
  6. **  Public domain by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <string.h>
  13. #ifdef __TURBOC__
  14.  #include <dir.h>
  15. #else
  16.  #include <direct.h>
  17. #endif
  18. #if defined(MSDOS) || defined(__MSDOS__)
  19.  #include "unistd.h"
  20. #else
  21.  #include <unistd.h>
  22. #endif
  23. #include "ftime.h"
  24. #include "datetime.h"
  25. #include "getopts.h"
  26. #include "errors.h"
  27. #include "snipfile.h"
  28. #include "dosfiles.h"
  29. #include "snipkbio.h"
  30. #include "dirport.h"
  31.  
  32. /*
  33. **  Options area - uses getopts() from SNIPPETS.
  34. */
  35.  
  36. char fname[FILENAME_MAX] = "";
  37. char pname[FILENAME_MAX] = "";
  38. char datestr[40];
  39. char timestr[20];
  40.  
  41. Boolean_T help    = False_;
  42. Boolean_T noquery = False_;
  43.  
  44. struct Option_Tag options[] = {
  45.       {'F',  False_, String_Tag,  fname,    NULL, NULL, NULL},
  46.       {'P',  False_, String_Tag,  pname,    NULL, NULL, NULL},
  47.       {'D',  False_, String_Tag,  datestr,  NULL, NULL, NULL},
  48.       {'T',  False_, String_Tag,  timestr,  NULL, NULL, NULL},
  49.       {'H',  False_, Boolean_Tag, &help,    NULL, NULL, NULL},
  50.       {'N',  False_, Boolean_Tag, &noquery, NULL, NULL, NULL},
  51.       {'\0', False_, Error_Tag,   NULL,     NULL, NULL, NULL}
  52. };
  53.  
  54. void loadtm(struct tm *ftm, struct ftime *ftimep)
  55. {
  56.       ftimep->ft_tsec  = ftm->tm_sec / 2;
  57.       ftimep->ft_min   = ftm->tm_min;
  58.       ftimep->ft_hour  = ftm->tm_hour;
  59.       ftimep->ft_day   = ftm->tm_mday;
  60.       ftimep->ft_month = ftm->tm_mon + 1;
  61.       ftimep->ft_year  = ftm->tm_year - 80;
  62. }
  63.  
  64. void usage(int errlvl)
  65. {
  66.       puts("SEQTOUCH - A utility to process a directory so its files "
  67.            "have sequentially");
  68.       puts("           increasing time/date stamps\n");
  69.       puts("Usage: SEQTOUCH [-N] [-Ddir] [-Ffile] [-Dmm-dd-yy] "
  70.            "[-Thh:mm:ss]\n");
  71.       puts("Where: <dir> is the directory to process");
  72.       puts("       <file> is an existing file whose time/date stamp "
  73.            "to use");
  74.       puts("       <mm-dd-yy> is a date in month-day-year format");
  75.       puts("       <hh:mm:ss> is a time in hours:minutes:seconds "
  76.            "format\n");
  77.       puts("       <-N> suppresses verification of arguments\n");
  78.       puts("All are optional. Specifying SEQTOUCH with no arguments "
  79.            "causes all files in");
  80.       puts("the current directory to be dated to the current date. "
  81.            "The timestamp of the");
  82.       puts("first file will be set to the current date and time and "
  83.            "each subsequent file");
  84.       puts("will have its time/date stamp set to 2 seconds later "
  85.            "than the preceding file.\n");
  86.       puts("Specifying the -T and/or -D options sets the time and/or date "
  87.            "of the first");
  88.       puts("file in the directory to the speified time and/or date.\n");
  89.       puts("Specifying a file with the -F option causes the timestamp of "
  90.            "first file in ");
  91.       puts("the directory to be set to match the specified file.\n");
  92.       puts("If the -N option is not specified, you will br prompted with "
  93.            "the specific");
  94.       puts("directory, time, and date (and file, if specified) to be used.");
  95.       puts("The option switches are not case sensitive, i.e. -f works just "
  96.            "like -F.");
  97.       exit(errlvl);
  98. }
  99.  
  100. int main(int argc, char *argv[])
  101. {
  102.       struct ftime ftimep;
  103.       struct tm ftm;
  104.       time_t ft;
  105.       DOSFileData ff;
  106.       char str[50];
  107.       char path[FILENAME_MAX];
  108.       char file[FILENAME_MAX];
  109.       size_t plen;
  110.  
  111.       if (Error_ == getopts(argc, argv))
  112.             usage(EXIT_FAILURE);
  113.  
  114.       if (help)
  115.             usage(EXIT_SUCCESS);
  116.  
  117.       if (NUL != pname[0])
  118.       {
  119.             if (!isdir(pname))
  120.                   ErrExit("%s is not a directory", pname);
  121.       }
  122.       else  getcwd(pname, FILENAME_MAX);
  123.  
  124.       if (NUL != fname[0])
  125.       {
  126.             FILE *fp;
  127.  
  128.             fp = cant(fname, "r");
  129.             if (Success_ != getftime(fileno(fp), &ftimep))
  130.                   ErrExit("Can't read timestamp of %s", fname);
  131.             ftime2tm(&ftimep, &ftm);
  132.             ft = ftime2time(&ftimep);
  133.       }
  134.       else
  135.       {
  136.             time(&ft);
  137.             ftm = *localtime(&ft);
  138.  
  139.             if (NUL != datestr[0])
  140.             {
  141.                   unsigned yy, mm, dd;
  142.  
  143.                   if (Success_ != parse_date(datestr, &yy, &mm, &dd, USA))
  144.                         ErrExit("Invalid date - %s", datestr);
  145.                   ftm.tm_year = yy - 1900;
  146.                   ftm.tm_mon  = mm - 1;
  147.                   ftm.tm_mday = dd;
  148.             }
  149.  
  150.             if (NUL != timestr[0])
  151.             {
  152.                   unsigned  hh, mm, ss;
  153.  
  154.                   if (Success_ != parse_time(timestr, &hh, &mm, &ss))
  155.                         ErrExit("Invalid time - %s", timestr);
  156.                   ftm.tm_hour = hh;
  157.                   ftm.tm_min  = mm;
  158.                   ftm.tm_sec  = ss;
  159.             }
  160.  
  161.             loadtm(&ftm, &ftimep);
  162.       }
  163.  
  164.       printf("\nDirectory %s\n", pname);
  165.       if (NUL == fname[0])
  166.             puts("No file specified\n");
  167.       else  printf("Using file %s\n\n", fname);
  168.       strftime(str, 50, "%A, %d-%b-%Y, %X", &ftm);
  169.       printf("Files will be timstamped sequentially beginning with\n  %s\n",
  170.              str);
  171.       if (!noquery)
  172.       {
  173.             if (False_ == getYN("OK?", 'N', 5))
  174.             {
  175.                   fputs("\nSEQTOUCH aborted by user\n", stderr);
  176.                   return EXIT_SUCCESS;
  177.             }
  178.       }
  179.  
  180.       strcpy(path, pname);
  181.       if ('\\' != LAST_CHAR(path))
  182.             strcat(path, "\\");
  183.       plen = strlen(path);
  184.       strcpy(file, path);
  185.  
  186.       strcat(path, "*.*");
  187.       if (Success_ == FIND_FIRST(path, _A_NORMAL, &ff)) do
  188.       {
  189.             FILE *fp;
  190.  
  191.             file[plen] = NUL;
  192.             strcat(file, ff_name(&ff));
  193.  
  194.             fprintf(stderr, "Touching %s\n", file);
  195.  
  196.             fp = cant(file, "r+");
  197.             setftime(fileno(fp), &ftimep);
  198.             fclose(fp);
  199.  
  200.             ftm.tm_sec += 2;
  201.             ft = mktime(&ftm);            /* Used to normalize time     */
  202.             loadtm(&ftm, &ftimep);
  203.       } while (Success_ == FIND_NEXT(&ff));
  204.  
  205.       return EXIT_SUCCESS;
  206. }
  207.