home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / stuff2.arj / DOPATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  5.9 KB  |  213 lines

  1. /*
  2. Stuff 2.0.
  3.  
  4. Checksum:  927958532 (verify with "brik")
  5.  
  6. (C) Copyright 1988 Rahul Dhesi.  Permission is granted to copy and
  7. distribute this file in modified or unmodified form, whether for
  8. noncommercial or commercial use, provided (a) this copyright notice
  9. is preserved, (b) no attempt is made to restrict redistribution of
  10. this file, and (c) this file is not distributed as part of any
  11. collection whose redistribution is restricted by a compilation
  12. copyright.
  13. */
  14.  
  15. #include "stuff.h"
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <dos.h>
  19.  
  20. extern int last_opt;
  21. extern request_rec *options[];
  22. extern char fmtstr[];
  23. extern FILE *outfile;
  24.  
  25. /*
  26. receives a filename that was found.  Filters it and prints it
  27. if it is selected.
  28. */
  29.  
  30. long total_size;              /* accumulated file sizes */
  31. extern long size_limit;       /* size limit per volume */
  32.  
  33. void newfile(void)
  34. {
  35.    printf ("---\n");
  36. }
  37.  
  38. void dopath (char *pathname, struct file_info *file_info)
  39. {
  40.    int i;
  41.    int select = 0;
  42.    int exclude = 0;
  43.    int have_name = 0;
  44.    int name_matched = 0;
  45.    int have_condition = 0;
  46.    request_rec *p_opt;
  47.  
  48.    strlwr (pathname);
  49.    if (last_opt < 0)             /* no selection */
  50.       select = 1;
  51.    else {
  52.       for (i = 0; i <= last_opt;  i++) {
  53.          p_opt = options[i];
  54.          switch (p_opt->choice) {
  55.             case PRINT:
  56.                 break;        /* ignore */
  57.             case NAME: {
  58.                int matched;
  59.  
  60.                have_condition = 1;
  61.                if (findlast (p_opt->info.name, "/\\") == NULL) {
  62.                   matched = matchstr (fptr(pathname), p_opt->info.name);
  63.                } else {
  64.                   matched = matchstr (pathname, p_opt->info.name);
  65.                }
  66.                if (p_opt->negate) {
  67.                   if (matched) {
  68.                      exclude = 1;
  69.                      goto done;
  70.                   }
  71.                } else {
  72.                   have_name = 1;       /* at least one filename argument */
  73.                   if (matched) {
  74.                      name_matched = 1; /* at least one filename matched */
  75.                      select = 1;
  76.                   }
  77.                }
  78.                break;
  79.             }
  80.             case MTIME: {
  81.                int time_ok = 0;
  82.                have_condition = 1;
  83.                time_ok = cmptime (file_info->utime,
  84.                      p_opt->info.mtime_info.choice,
  85.                      p_opt->info.mtime_info.value * (60L * 60L * 24L));
  86.                if (p_opt->negate && time_ok ||
  87.                   !p_opt->negate && !time_ok) {
  88.                   exclude = 1;
  89.                   goto done;
  90.                } else
  91.                   select = 1;
  92.                break;
  93.             }
  94.  
  95.             case NEWER: {
  96.                int time_ok = 0;
  97.                have_condition = 1;
  98.                time_ok = cmptime2 (file_info->utime,
  99.                               BIGGER,
  100.                               p_opt->info.mtime_info.value);
  101.                if (p_opt->negate && time_ok ||
  102.                   !p_opt->negate && !time_ok) {
  103.                   exclude = 1;
  104.                   goto done;
  105.                } else
  106.                   select = 1;
  107.                break;
  108.             }
  109.  
  110.             case OLDER: {
  111.                int time_ok = 0;
  112.                have_condition = 1;
  113.                time_ok = cmptime2 (file_info->utime,
  114.                               SMALLER,
  115.                               p_opt->info.mtime_info.value);
  116.                if (p_opt->negate && time_ok ||
  117.                   !p_opt->negate && !time_ok) {
  118.                   exclude = 1;
  119.                   goto done;
  120.                } else
  121.                   select = 1;
  122.                break;
  123.             }
  124.  
  125.             case SIZE: {
  126.                int size_ok = 0;
  127.                have_condition = 1;
  128.                size_ok = cmpsize (file_info->size,
  129.                               p_opt->info.size_info.choice,
  130.                               p_opt->info.size_info.value);
  131.                if (p_opt->negate && size_ok ||
  132.                   !p_opt->negate && !size_ok) {
  133.                   exclude = 1;
  134.                   goto done;
  135.                } else
  136.                   select = 1;
  137.                break;
  138.             }
  139.  
  140.             case MODIFIED: {
  141.                int mod_ok = 0;
  142.                have_condition = 1;
  143.                if (file_info->attrib & FA_ARCH)
  144.                   mod_ok = 1;
  145.                if (p_opt->negate && mod_ok ||
  146.                   !p_opt->negate && !mod_ok) {
  147.                   exclude = 1;
  148.                   goto done;
  149.                } else
  150.                   select = 1;
  151.                break;
  152.             }  
  153.  
  154.             default: bugreport("dopath");
  155.          }
  156.       }
  157.    }
  158. done: /* come from case NAME in switch */
  159. #if 0
  160. printf ("have_condition=%d, exclude=%d, select=%d\n",
  161. have_condition, exclude, select);
  162. #endif
  163.  
  164.    if (
  165.          have_condition && ((select || !have_name) && !exclude) && 
  166.                (have_name && name_matched || !have_name)
  167.          || 
  168.          !have_condition
  169.       )
  170.    {
  171.       /* accumulate file size;  if too much, open next file */
  172.       total_size = total_size + file_info->size;
  173.       if (size_limit != 0L && total_size > size_limit) {
  174.          total_size = file_info->size;
  175.          newfile();
  176.       }
  177.  
  178.       if (*fmtstr != '\0')
  179.          fprintf (outfile, fmtstr, pathname);
  180.       else
  181.          fprintf (outfile, "%s\n", pathname);
  182.    }
  183. }
  184.  
  185. /*
  186. returns pointer to filename part of pathname
  187. */
  188. char *fptr (char *pathname)
  189. {
  190.    char *p;
  191.    p = findlast (pathname, ":/\\"); /* look for : / and \ */
  192.    if (p == NULL)
  193.       return (pathname);
  194.    else
  195.       return (p+1);
  196. }
  197.  
  198.  
  199. #define  KBYTE 1024
  200.  
  201. int cmpsize (long size, compare_pt choice, long value)
  202. {
  203.    size /= KBYTE;
  204.  
  205.    if (choice == SAME && size == value ||
  206.          choice == BIGGER && size > value ||
  207.          choice == SMALLER && size < value) {
  208.       return (1);
  209.    } else {
  210.       return (0);
  211.    }
  212. }
  213.