home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / dev / c / mkid.lha / src / fid.c < prev    next >
C/C++ Source or Header  |  1995-07-23  |  2KB  |  132 lines

  1. static char copyright[] = "@(#)Copyright (c) 1986, Greg McGary";
  2. static char sccsid[] = "@(#)fid.c    1.2 86/10/17";
  3.  
  4. #include    "bool.h"
  5. #include    <stdio.h>
  6. #include    "string.h"
  7. #include    <ctype.h>
  8. #include    "radix.h"
  9. #include    "id.h"
  10. #include    "bitops.h"
  11. #include    "extern.h"
  12.  
  13. void fileId();
  14.  
  15. FILE        *IdFILE;
  16. struct idhead    Idh;
  17. struct idarg    *IdArgs;
  18.  
  19. #ifdef AMIGA
  20. char *verstag = "\0$VER: fid " TOOL_VERSION " " __AMIGADATE__ "\r\n";
  21. #endif
  22.  
  23. char *MyName;
  24. static void
  25. usage()
  26. {
  27.     fprintf(stderr, "Usage: %s [-f<file>] file1 file2\n", MyName);
  28.     exit(1);
  29. }
  30. main(argc, argv)
  31.     int        argc;
  32.     char        **argv;
  33. {
  34.     char        *idFile = IDFILE;
  35.     char        *arg;
  36.     float        occurPercent = 0.0;
  37.     int        occurNumber = 0;
  38.     int        op;
  39.  
  40.     MyName = basename(GETARG(argc, argv));
  41.  
  42.     while (argc) {
  43.         arg = GETARG(argc, argv);
  44.         switch (op = *arg++)
  45.         {
  46.         case '-':
  47.         case '+':
  48.             break;
  49.         default:
  50.             UNGETARG(argc, argv);
  51.             goto argsdone;
  52.         }
  53.         while (*arg) switch (*arg++)
  54.         {
  55.         case 'f': idFile = arg; goto nextarg;
  56.         default: usage();
  57.         }
  58.     nextarg:;
  59.     }
  60. argsdone:
  61.  
  62.     idFile = spanPath(getDirToName(idFile), idFile);
  63.     if ((IdFILE = initID(idFile, &Idh, &IdArgs)) == NULL) {
  64.         filerr("open", idFile);
  65.         exit(1);
  66.     }
  67.  
  68.     if (argc < 1 || argc > 2)
  69.         usage();
  70.  
  71.     fileId(argc, argv);
  72.     exit(0);
  73. }
  74.  
  75. void
  76. fileId(argc, argv)
  77.     int        argc;
  78.     char        **argv;
  79. {
  80.     char        *buf;
  81.     int        want, got;
  82.     int        bitoff[2];
  83.     int        i, j;
  84.     int        argLength;
  85.     int        pathLength;
  86.     int        lengthDiff;
  87.     char        *pathVec;
  88.     register struct idarg    *idArgs;
  89.  
  90.     want = 0;
  91.     for (j = 0; j < argc; j++, argv++) {
  92.         want |= (1<<j);
  93.         argLength = strlen(*argv);
  94.         bitoff[j] = -1;
  95.         for (idArgs = IdArgs, i = 0; i < Idh.idh_pthc; i++, idArgs++) {
  96.             pathLength = strlen(idArgs->ida_arg);
  97.             if (argLength > pathLength)
  98.                 continue;
  99.             lengthDiff = pathLength - argLength;
  100.             if (strequ(&idArgs->ida_arg[lengthDiff], *argv)) {
  101.                 bitoff[j] = i;
  102.                 break;
  103.             }
  104.         }
  105.         if (bitoff[j] < 0) {
  106.             fprintf(stderr, "%s: not found\n", *argv);
  107.             exit(1);
  108.         }
  109.     }
  110.  
  111.     buf = malloc((int)Idh.idh_bsiz);
  112.     fseek(IdFILE, Idh.idh_namo, 0);
  113.  
  114.     for (i = 0; i < Idh.idh_namc; i++) {
  115.         pathVec = 1 + buf + fgets0(buf, Idh.idh_bsiz, IdFILE);
  116.         getsFF(pathVec, IdFILE);
  117.         got = 0;
  118.         while ((*pathVec & 0xff) != 0xff) {
  119.             j = strToInt(pathVec, Idh.idh_vecc);
  120.             if ((want & (1<<0)) && j == bitoff[0])
  121.                 got |= (1<<0);
  122.             if ((want & (1<<1)) && j == bitoff[1])
  123.                 got |= (1<<1);
  124.             if (got == want) {
  125.                 printf("%s\n", ID_STRING(buf));
  126.                 break;
  127.             }
  128.             pathVec += Idh.idh_vecc;
  129.         }
  130.     }
  131. }
  132.