home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / filutl / beyondc.arc / TEST.C < prev   
Text File  |  1987-09-01  |  5KB  |  175 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6. #include <memory.h>
  7.  
  8. /* Boolean type and values. */
  9. typedef int     BOOLEAN;
  10. #define TRUE    1
  11. #define FALSE   0
  12.  
  13. /* Exit values. */
  14. #define SUCCESS   0
  15. #define FAILURE   1
  16.  
  17. /* Structure for option values. */
  18. typedef struct options
  19. {
  20.     int   lngval;
  21. } OPTIONS;
  22.  
  23. /* Default option values. */
  24. #define DEF_LNGVAL   25
  25.  
  26. /* Function declarations. */
  27. void    cmpfiles (char *, char *, OPTIONS *);
  28. BOOLEAN readin   (FILE *, char ***, int *);
  29. void    compare  (char **, int, char **, int, int);
  30.  
  31. /* Program name. */
  32. static char   Progname[] = "test";
  33.  
  34. main (argc, argv)
  35.  
  36. int    argc;
  37. char   **argv;
  38. {
  39.     OPTIONS   opt;        /* Structure for option values. */
  40.     char      *thisopt;   /* Current option. */
  41.     char      *thisval;   /* Current option value. */
  42.  
  43.     /* Move past program name in arg list. */
  44.     argv++;
  45.     argc--;
  46.  
  47.     /* Initialize options. */
  48.     opt.lngval = DEF_LNGVAL;
  49.  
  50.     /* Process options. */
  51.     for ( ; argc > 0 && **argv == '-'; argc--, argv++)
  52.     {
  53.         /* Get option flag. */
  54.         thisopt = *argv + 1;
  55.  
  56.         /* Look for 'l' option. */
  57.         if (tolower (*thisopt) == 'l')
  58.         {
  59.             /* Get option value. */
  60.             thisval = thisopt + 1;
  61.  
  62.             /* Option value must be a positive integer. */
  63.             if (isdigit (*thisval) && (opt.lngval = atoi (thisval)))
  64.             {
  65.                 ;
  66.             }
  67.             else
  68.             {
  69.                 fprintf (stderr,
  70.                         "%s:  invalid value, \"%s\", for option \"%c\"\n",
  71.                         Progname, thisval, *thisopt);
  72.                 exit (FAILURE);
  73.             }
  74.         }
  75.         else
  76.         {
  77.             fprintf (stderr, "%s:  invalid option, \"%c\"\n",
  78.                     Progname, *thisopt);
  79.             exit (FAILURE);
  80.         }
  81.     }
  82.  
  83.     /* Must be exactly two arguments left. */
  84.     if (argc == 2)
  85.     {
  86.         cmpfiles (*argv, *(argv + 1), &opt);
  87.         exit (SUCCESS);
  88.     }
  89.     else
  90.     {
  91.         fprintf (stderr, "usage:  %s [-ln] file1 file2\n", Progname);
  92.         exit (FAILURE);
  93.     }
  94. }
  95.  
  96. static void cmpfiles (file1, file2, opt)
  97.  
  98. char      *file1;   /* (I) File name #1. */
  99. char      *file2;   /* (I) File name #2. */
  100. OPTIONS   *opt;     /* (O) Options. */
  101. {
  102.     FILE   *fp1;   /* File pointer #1. */
  103.     FILE   *fp2;   /* File pointer #2. */
  104.     char   **a1;   /* Array of lines #1. */
  105.     int    n1;     /* Number of lines in a1 (not including 0th element). */
  106.     char   **a2;   /* Array of lines #2. */
  107.     int    n2;     /* Number of lines in a2 (not including 0th element). */
  108.  
  109.     /* Open the files. */
  110.     if ((fp1 = fopen (file1, "rt")) && (fp2 = fopen (file2, "rt")))
  111.     {
  112.         /* Read each file and place the lines of text into an array. */
  113.         if (readin (fp1, &a1, &n1) && readin (fp2, &a2, &n2))
  114.         {
  115.             compare (a1, n1, a2, n2, opt->lngval);
  116.         }
  117.         else
  118.         {
  119.             fprintf (stderr,
  120.                     "%s:  could not read files into memory\n", Progname);
  121.         }
  122.     }
  123.     else
  124.     {
  125.         fprintf (stderr, "%s:  could not open files\n", Progname);
  126.     }
  127. }
  128.  
  129. static BOOLEAN readin (fp, a, n)
  130.  
  131. FILE   *fp;    /* (I) File pointer. */
  132. char   ***a;   /* (O) Array of lines. */
  133. int    *n;     /* (O) Number of lines in a (not including 0th element). */
  134. {
  135.     #define NLINES   1000   /* Maximum number of lines that will be handled. */
  136.     #define NBUF     512    /* Maximum line length that will be handled. */
  137.  
  138.     char      **array;         /* Array of lines. */
  139.     int       i;               /* Counter. */
  140.     int       len;             /* Length of line. */
  141.     char      buf[NBUF + 1];   /* Text buffer. */
  142.  
  143.     /* Allocate an array for a maximum number of lines of text. */
  144.     if (array = (char **) malloc ((NLINES + 1) * sizeof (char *)))
  145.     {
  146.         ;
  147.     }
  148.     else
  149.     {
  150.         return (FALSE);
  151.     }
  152.  
  153.     /* Read the lines of text. */
  154.     for (i = 1; fgets (buf, NBUF + 1, fp) && i < NLINES; i++)
  155.     {
  156.         /* Allocate an array. */
  157.         len = strlen (buf);
  158.         if (array[i] = malloc ((len + 1) * sizeof (char)))
  159.         {
  160.             /* Copy the text. */
  161.             memcpy (array[i], buf, len + 1);
  162.         }
  163.         else
  164.         {
  165.             return (FALSE);
  166.         }
  167.     }
  168.  
  169.     /* Return the array and the number of lines. */
  170.     *a = array;
  171.     *n = i - 1;
  172.  
  173.     return (TRUE);
  174. }
  175.