home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / util / cdro / 003 / cdr.c next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  8.5 KB  |  427 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dir.h>
  4. #include <stdlib.h>
  5. #include <process.h>
  6. #include "regexp.h"
  7.  
  8. #define    FALSE    0
  9. #define TRUE    1
  10. #define MAXREG    10              /* Maximum number -s options allowed */
  11.  
  12. main (int argc,char *argv[],char *env[])
  13.  
  14. {
  15.     /* Extern routines */
  16.  
  17.     extern    char    *optarg;
  18.     extern    int    optind;
  19.     extern  int    getopt();
  20.     extern  char    *cdrom();
  21.     extern    char    *unzip();
  22.     extern  char    *unarc();
  23.     extern    char    *tmpdir();
  24.     extern  void     createdir();
  25.     extern    char    *getindex();
  26.     extern    void    usage();
  27.  
  28.     /* Declarations */
  29.  
  30.     FILE    *fp;
  31.     char    name[BUFSIZ];
  32.     char    line[BUFSIZ];
  33.     char    directory[BUFSIZ];
  34.     char    description[BUFSIZ];
  35.     char    search[BUFSIZ];
  36.     char    command[BUFSIZ];
  37.     char    todir[BUFSIZ];
  38.     char    ext[BUFSIZ];
  39.  
  40.     int    c;
  41.     int    desc;
  42.     int    found;
  43.     int    unpack;
  44.     int     copy;
  45.     int    delete;
  46.     int    force;
  47.     int    and;
  48.     int    expr;
  49.     int    prev;
  50.     int    batch;
  51.     int     extension;
  52.  
  53.     struct    regexp    *exp[MAXREG];
  54.     int    neg[MAXREG];
  55.  
  56.     /* Set the default values */
  57.  
  58.     strcpy (todir,tmpdir());
  59.     strcpy (name,getindex());
  60.     strcpy (search,"");
  61.     strcpy (directory,"");
  62.     strcpy (ext,"*.*");
  63.     desc         = TRUE;
  64.     unpack       = FALSE;
  65.     copy         = FALSE;
  66.     delete    = FALSE;
  67.     force      = FALSE;
  68.     and       = TRUE;
  69.     batch     = FALSE;
  70.     extension = FALSE;
  71.     expr      = 0;
  72.     prev      = '\0';
  73.  
  74.     /* Option handling */
  75.  
  76.     while ((c = getopt(argc, argv, "i:s:t:e:fucdnorgbh?")) != EOF)
  77.     {
  78.  
  79.         switch (c)
  80.         {
  81.         case 'I':
  82.         case 'i':
  83.             strcpy (name,optarg);
  84.             break;
  85.         case 'N':
  86.         case 'n':
  87.             break;
  88.         case 'S':
  89.         case 's':
  90.             strcpy (search,optarg);
  91.  
  92.             /* Every search string is mapped to lower case */
  93.             /* to make search more easily               */
  94.  
  95.             strlwr (search);
  96.  
  97.             /* If previous option letter is an 'n' or 'N'   */
  98.             /* then this means negate the following regular */
  99.             /* expression                    */
  100.  
  101.             if (prev == 'n' || prev == 'N')
  102.             {
  103.                 neg[expr] = TRUE;
  104.             }
  105.             else
  106.             {
  107.                 neg[expr] = FALSE;
  108.             }
  109.  
  110.             /* Skip this regular expression if maximum exceeded */
  111.  
  112.             if (expr > MAXREG - 1)
  113.             {
  114.                 printf ("Skipping search string %s more %d regular expressions exceeded\n",optarg,MAXREG);
  115.             }
  116.             else
  117.             {
  118.                 /* Compile this regular expression */
  119.                 exp[expr] = regcomp(search);
  120.                 expr++;
  121.             }
  122.             break;
  123.         case 'F':
  124.         case 'f':
  125.             /* Full path names only used in search */
  126.             desc = FALSE;
  127.             break;
  128.         case 'U':
  129.         case 'u':
  130.             if (copy)
  131.             {
  132.                 printf ("Options -u and -c are mutal exclusive\n");
  133.                 exit (1);
  134.             }
  135.             unpack = TRUE;
  136.             break;
  137.         case 'C':
  138.         case 'c':
  139.             if (unpack)
  140.             {
  141.                 printf ("Options -u and -c are mutual exclusive\n");
  142.                 exit (1);
  143.             }
  144.  
  145.             copy   = TRUE;
  146.             break;
  147.         case 'T':
  148.         case 't':
  149.             /* Target dir */
  150.             strcpy(todir,optarg);
  151.             break;
  152.         case 'D':
  153.         case 'd':
  154.             /* Wipe target dir */
  155.             delete  = TRUE;
  156.             break;
  157.         case 'R':
  158.         case 'r':
  159.             /* Forced wipe */
  160.             force   = TRUE;
  161.             break;
  162.         case 'O':
  163.         case 'o':
  164.             /* Match if one of the search options matches */
  165.             /* Default it has to match all search options */
  166.  
  167.             and     =  FALSE;
  168.             break;
  169.         case 'B':
  170.         case 'b':
  171.             /* Output only the commands but do nothing */
  172.             batch   =  TRUE;
  173.             break;
  174.         case 'E':
  175.         case 'e':
  176.             /* Filenames to be extracted default all files */
  177.             strcpy(ext,optarg);
  178.             extension = TRUE;
  179.             break;
  180.         case 'h':
  181.         case 'H':
  182.         case '?':
  183.             /* HELP !!!! */
  184.             usage();
  185.             exit (1);
  186.             break;
  187.         default:
  188.             /* ILLEGAL OPTION BUT ANYWAY HELP !!!! */
  189.             usage();
  190.             exit (1);
  191.             break;
  192.         }
  193.  
  194.         prev = c;
  195.     }
  196.     /* -b option in conjuction with -u or -c is useful */
  197.  
  198.     if (batch && !(copy || unpack))
  199.     {
  200.         printf ("-b option is only useful together with -c or -u option \n");
  201.         exit (1);
  202.     }
  203.  
  204.     if (extension && !unpack)
  205.     {
  206.         printf ("-e option can only be used together with -u option\n");
  207.         exit (1);
  208.     }
  209.  
  210.     /* If no search expression is given match everything */
  211.  
  212.     if (expr == 0)
  213.     {
  214.         exp[0] = regcomp ("");
  215.         neg[0] = FALSE;
  216.         expr++;
  217.     }
  218.  
  219.     /* If target directory doesn't exsist create it */
  220.  
  221.     if (!batch && !isdir(todir))
  222.     {
  223.         createdir(todir);
  224.     }
  225.  
  226.     /* If target directory is only the drive letter then use */
  227.     /* force option to wipe it                  */
  228.  
  229.     if (delete && !force && todir[1] == ':' && todir[2] == '\0')
  230.     {
  231.         printf ("Use -r -d to delete an entire disk\n");
  232.         exit (1);
  233.     }
  234.  
  235.     /* If target directory is the current directory the use */
  236.     /* force option to wipe it                */
  237.  
  238.     if (delete && !force && (strcmp(todir,".") == 0))
  239.     {
  240.         printf ("Not purging current directory\n");
  241.         printf ("Use -d -r to remove data in current directory");
  242.         exit (1);
  243.     }
  244.  
  245.     if (delete)
  246.     {
  247.         /* We may wipe target directory clean */
  248.          cleanup (todir);
  249.     }
  250.  
  251.     /* Quit after we have wiped target dir but nothing */
  252.     /* to do afterwards                   */
  253.  
  254.     if (delete && !unpack && !copy) exit(1);
  255.  
  256.     /* Open index file */
  257.  
  258.     if ((fp = fopen (name,"r")) == (FILE *) NULL)
  259.     {
  260.         printf ("Error opening file %s\n",name);
  261.         exit (1);
  262.     }
  263.  
  264.     /* We use the string name later again so initialise it */
  265.     strcpy (name,"");
  266.  
  267.     /* Read lines from index file */
  268.     while (fgets(line,BUFSIZ,fp) != (char *) NULL)
  269.     {
  270.         /* If entry is directory */
  271.         if (strncmp (line,"Directory",7) == 0)
  272.         {
  273.             /* Scan directory name in line */
  274.  
  275.             sscanf (line,"%*s %s\n",directory);
  276.  
  277.             /* Map directory name to lowercase */
  278.  
  279.             strlwr(directory);
  280.  
  281.             /* Replace every forward slash by a backward slash */
  282.  
  283.             for (c=0 ; c < strlen(directory) ; c++)
  284.             {
  285.                 if (directory[c] == '/') directory[c]= '\\';
  286.             }
  287.  
  288.             continue;
  289.         }
  290.  
  291.         /* Skip lines that do not contain any useful information */
  292.  
  293.         if (strncmp (line," Filename   Type Length   Date   Description",15) == 0 ) continue;
  294.         if (strncmp (line,"==============================================",10) == 0 ) continue;
  295.  
  296.         /* And also empty lines */
  297.  
  298.         if (strlen(line) < 2) continue;
  299.  
  300.         /* Map line to lower case and remove newline at end of line */
  301.  
  302.         strlwr(line);
  303.         line[strlen(line)-1] = '\0';
  304.  
  305.         /* We haven't found a thing yet */
  306.  
  307.         found=FALSE;
  308.  
  309.         /* Use only the filenames from the line if description       */
  310.         /* is not to be used in search and add full path do filename */
  311.         if (!desc)
  312.         {
  313.             sscanf  (line,"%s %*s",name);
  314.             sprintf (line,"%s\\%s%s",cdrom(),directory,name);
  315.         }
  316.  
  317.         /* Flush standard output and stderr               */
  318.         /* I noticed some trouble with output redirection */
  319.         /* These lines fix this problem               */
  320.  
  321.         fflush(stdout);
  322.         fflush(stderr);
  323.  
  324.  
  325.         if (and)
  326.         {
  327.             /* We have a match if all regular expressions */
  328.             /* match the line                       */
  329.  
  330.             for (c=0; c < expr ; c++)
  331.             {
  332.                 if (neg[c] == TRUE ? regexec(exp[c],line) == 0 : regexec(exp[c],line) != 0)
  333.                 {
  334.                     found = TRUE;
  335.                 }
  336.                 else
  337.                 {
  338.                     found = FALSE;
  339.                     break;
  340.                 }
  341.             }
  342.         }
  343.         else
  344.         {
  345.             /* We have a match if a single expression matches the line */
  346.  
  347.             for (c=0; c < expr ; c++)
  348.             {
  349.                 if (neg[c] == TRUE ? regexec(exp[c],line) == 0 : regexec(exp[c],line) != 0)
  350.                 {
  351.                     found = TRUE;
  352.                     break;
  353.                 }
  354.             }
  355.         }
  356.  
  357.         /* If search only print the matching line */
  358.  
  359.         if (found)
  360.         {
  361.             if (!unpack && !copy && !batch) printf ("%s\n",line);
  362.         }
  363.  
  364.         /* Remove description,date and size from line */
  365.  
  366.         if (desc)
  367.         {
  368.             sscanf (line,"%s %*s",name);
  369.         }
  370.  
  371.         if (found && unpack)
  372.         {
  373.             /* Build command line for zip files */
  374.  
  375.             if (strcmp(strrchr(name,'.'),".zip") == 0)
  376.             {
  377.                 sprintf (command,"%s %s\\%s%s %s %s",unzip(),cdrom(),directory,name,todir,ext);
  378.                 printf  ("%s\n",command);
  379.                 if (!batch) system  (command);
  380.                 continue;
  381.             }
  382.  
  383.             /* The same for arc files */
  384.  
  385.             if (strcmp(strrchr(name,'.'),".arc") == 0)
  386.             {
  387.                 sprintf (command,"%s %s\\%s%s %s %s",unarc(),cdrom(),directory,name,todir,ext);
  388.                 printf  ("%s\n",command);
  389.                 if (!batch) system  (command);
  390.                 continue;
  391.             }
  392.  
  393.             /* Copy the rest that matches */
  394.  
  395.             sprintf (command,"copy %s\\%s%s %s",cdrom(),directory,name,todir);
  396.             printf  ("%s\n",command);
  397.             if (!batch) system  (command);
  398.  
  399.         }
  400.  
  401.         if (found && copy)
  402.         {
  403.             /* Build copy command for matching line */
  404.  
  405.             sprintf (command,"copy %s\\%s%s %s",cdrom(),directory,name,todir);
  406.             printf  ("%s\n",command);
  407.             if (!batch) system  (command);
  408.         }
  409.     }
  410.  
  411.     fclose (fp);
  412.  
  413.     /* Execute command set by POST environment variable */
  414.     /* if something is copied or unpacked               */
  415.     /* Useful for virus scanning the target directory   */
  416.  
  417.     if ((getenv("POST") != NULL) && (copy || unpack) && !batch)
  418.     {
  419.         system (getenv("POST"));
  420.     }
  421.  
  422. }
  423.  
  424.  
  425.  
  426.  
  427.