home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / crc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-08  |  9.0 KB  |  221 lines

  1. /*============================================================================*
  2.  * main() module: crc.c - Test for crcfile() subroutine.
  3.  *
  4.  * (C)Copyright IBM Corporation, 1989, 1990, 1991, 1992.       Brian E. Yoder
  5.  *
  6.  * This program calls the crcfile() subroutine for each file that is
  7.  * specified on the command line.  It displays each file's length and CRC.
  8.  *
  9.  * See the crcfile.c module for more information.
  10.  *
  11.  * The code to process the file specifications on the command line was
  12.  * taken from the chmod.c source file.
  13.  *
  14.  * 10/30/90 - Created for PS/2 AIX.
  15.  * 10/30/90 - Initial version.
  16.  * 05/02/91 - Ported from PS/2 AIX to DOS.
  17.  * 05/04/91 - Updated per new slrewind() interface in speclist.c file.
  18.  * 05/08/91 - Updated per new slmake() interface in speclist.c file.
  19.  *            Also, added support for -s (subdirectory descent) flag.
  20.  * 06/07/92 - Added -l flag to calculate long (32-bit) CRC values.
  21.  *============================================================================*/
  22.  
  23. static char copr[] = "(c)Copyright IBM Corporation, 1991.  All rights reserved.";
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <memory.h>
  28. #include <sys/types.h>
  29.  
  30. #include "util.h"
  31.  
  32. static  char     buff[2048+1];      /* Buffer for output filenames */
  33.  
  34. /*============================================================================*
  35.  * Define internal functions to allow forward access
  36.  *============================================================================*/
  37.  
  38. static void syntax();
  39.  
  40. /*============================================================================*
  41.  * Main Program Entry Point
  42.  *============================================================================*/
  43.  
  44. main(argc, argv)
  45.  
  46. int argc;                           /* arg count */
  47. char *argv[];                       /* arg pointers */
  48.  
  49. {
  50.   int  rc;                          /* Return code storage */
  51.   long    lrc;                      /* Long return code */
  52.   char   *flagstr;                  /* Pointer to string of flags */
  53.  
  54.   ushort crc;                       /* CRC value: 16-bit */
  55.   ulong  crc32;                     /* CRC value: 32-bit */
  56.   ulong  len;                       /* File length */
  57.  
  58.   SLPATH *speclist;                 /* Pointer to linked list of path specs */
  59.   ushort  mflags;                   /* Match flags for slrewind() */
  60.   ushort  recurse;                  /* Recursive directory descent? */
  61.   ushort  find32;                   /* Find 32-bit CRC? */
  62.  
  63.   FINFO  *fdata;                    /* Pointers returned by slmatch() */
  64.   SLPATH *pdata;
  65.   SLNAME *ndata;
  66.  
  67.   recurse = FALSE;                  /* Initially: don't descend subdirectories */
  68.   find32 = FALSE;                   /* Initially: find 16-bit CRC, as before */
  69.  
  70. /*----------------------------------------------------------------------------*
  71.  * Be sure we have at least one argument:
  72.  *----------------------------------------------------------------------------*/
  73.  
  74.   argc--;                           /* Ignore 1st argument (program name) */
  75.   argv++;
  76.  
  77.   if (argc <= 0) syntax();          /* If no arguments: Display syntax */
  78.  
  79.  /*---------------------------------------------------------------------------*
  80.   * Process flags, if any
  81.   *---------------------------------------------------------------------------*/
  82.  
  83.   flagstr = *argv;                  /* Point 'flagstr' to argument */
  84.   if (*flagstr == '-')              /* If it begins with '-': It's a list of flags */
  85.   {
  86.      flagstr++;                          /* Point past the dash */
  87.  
  88.      while (*flagstr != '\0')            /* For each character in flag string: */
  89.      {
  90.         switch (*flagstr)
  91.         {
  92.            case 's':
  93.            case 'S':
  94.               recurse = TRUE;
  95.               break;
  96.  
  97.            case 'l':
  98.            case 'L':
  99.               find32 = TRUE;
  100.               break;
  101.  
  102.            default:
  103.               fprintf(stderr, "Invalid flag '%c'.  For help, enter command with no arguments.\n",
  104.                  *flagstr);
  105.               exit(2);
  106.               break;
  107.         }
  108.         flagstr++;                            /* Check next character */
  109.      }
  110.  
  111.      argc--;                             /* Done with flags: Discard them */
  112.      argv++;
  113.   }
  114.  
  115.   if (argc <= 0) syntax();          /* If no arguments: Display syntax */
  116.  
  117.  /*---------------------------------------------------------------------------*
  118.   * Process file specifications and build specification list
  119.   *---------------------------------------------------------------------------*/
  120.  
  121.   rc = slmake(&speclist, TRUE, TRUE, argc, argv);
  122.  
  123.   if (rc != 0)                      /* If there was an error: */
  124.   {                                 /* Analyze rc, show msg, and return */
  125.      if (rc == REGX_MEMORY)
  126.         fprintf(stderr, "Out of memory while processing '%s'.\n",
  127.            slerrspec());
  128.      else
  129.         fprintf(stderr, "Invalid filespec: '%s'.\n",
  130.            slerrspec());
  131.  
  132.      return(2);
  133.   }
  134.  
  135.  /*---------------------------------------------------------------------------*
  136.   * Attempt to match files to the specification list
  137.   *---------------------------------------------------------------------------*/
  138.  
  139.   mflags = 0;                       /* Set list of files to match to include: */
  140.   mflags = mflags | SL_NORMAL |          /* Ordinary files */
  141.                     SL_HIDDEN |          /* Hidden files */
  142.                     SL_SYSTEM;           /* System files */
  143.  
  144.   slrewind(speclist, mflags, recurse);/* Initialize slmatch() */
  145.   for (;;)                          /* Loop to find all matching DOS files: */
  146.   {
  147.      fdata = slmatch(&pdata,             /* Get next matching DOS file */
  148.                      &ndata);
  149.      if (fdata == NULL)                  /* If none: */
  150.         break;                                /* Done */
  151.  
  152.      strcpy(buff, pdata->path);          /* Store path portion of filename */
  153.      pathcat(buff, fdata->Fname);        /* Add name to end of path string */
  154. //   strlwr(buff);                       /* And convert to lower case */
  155.  
  156.      if (find32)                         /* Find file's CRC and length */
  157.         rc = crcfile32(buff, &crc32, &len);
  158.      else
  159.         rc = crcfile(buff, &crc, &len);
  160.  
  161.      if (rc != 0)                        /* If error: */
  162.      {                                        /* Display message */
  163.         fprintf(stderr, "crc: Cannot process file '%s'.\n", buff);
  164.      }
  165.      else                                /* Else: No error: */
  166.      {                                        /* Display CRC, length, name */
  167.         if (find32)
  168.            printf("%12lu  0x%08lX  %s\n", len, crc32, buff);
  169.         else
  170.            printf("%12lu  0x%04X  %s\n",  len, crc, buff);
  171.      }
  172.   }
  173.  
  174.  /*---------------------------------------------------------------------------*
  175.   * Display (on stderr) all filespecs that had no matching files
  176.   *---------------------------------------------------------------------------*/
  177.  
  178.   lrc = slnotfound(speclist);       /* Display path\names w/no matching files */
  179.   if (lrc == 0L)                    /* If all fspecs were matched: */
  180.      return(0);                          /* Return successfully */
  181.   else                              /* Otherwise:  One or more not found: */
  182.      return(2);                          /* Return with error */
  183.  
  184. } /* end of main() */
  185.  
  186. /*============================================================================*
  187.  * syntax() - Display command syntax and exit to operating system!
  188.  *============================================================================*/
  189. static void syntax()
  190. {
  191.   fprintf(stderr, "\n");
  192.   fprintf(stderr, "Usage: crc [-s] [-l] fspec ...\n");
  193.   fprintf(stderr, "\n");
  194.   fprintf(stderr, "  This program finds the length and CRC for each file that\n");
  195.   fprintf(stderr, "  matches the file specifications.  The information for each\n");
  196.   fprintf(stderr, "  file is displayed on stdout as follows:\n");
  197.   fprintf(stderr, "\n");
  198.   fprintf(stderr, "     length  CRC  filename\n");
  199.   fprintf(stderr, "\n");
  200.   fprintf(stderr, "  The length is displayed in decimal format.  The CRC is displayed\n");
  201.   fprintf(stderr, "  in hexadecimal format with a leading '0x'.\n");
  202.   fprintf(stderr, "\n");
  203.   fprintf(stderr, "  If -s is specified, then crc will descend subdirectories\n");
  204.   fprintf(stderr, "  to look for files that match the file specifications.\n");
  205.   fprintf(stderr, "\n");
  206.   fprintf(stderr, "  If -l (letter el) is specified, then crc will find the 32-bit\n");
  207.   fprintf(stderr, "  (long) CRC for each file.  The default is to find the 16-bit\n");
  208.   fprintf(stderr, "  CRC for each file.\n");
  209.   fprintf(stderr, "\n");
  210.  
  211. //fprintf(stderr, "\n");
  212. //fprintf(stderr, "Examples:\n");
  213. //fprintf(stderr, "\n");
  214. //fprintf(stderr, "  crc *.c *.h       Display CRCs for all .c and .h files in the\n");
  215. //fprintf(stderr, "                    current directory.\n");
  216. //fprintf(stderr, "\n");
  217. //fprintf(stderr, "  crc -s c:\\        Display CRCs for all files on drive C.\n");
  218. //fprintf(stderr, "\n");
  219.   exit(1);
  220. }
  221.