"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/repeats/rep_hash.c" (29 Oct 2021, 3975 Bytes) of package /linux/privat/littleutils-1.2.5.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "rep_hash.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.4_vs_1.2.5.

    1 /* rep_hash:  Looks for duplicate files based on hashes.  Utilizes the output
    2    of the findhash command.
    3 
    4    Copyright (C) 2004-2021 by Brian Lindholm.
    5    This file is part of the littleutils utility set.
    6 
    7    The rep_hash utility is free software; you can redistribute it and/or modify
    8    it under the terms of the GNU General Public License as published by the
    9    Free Software Foundation; either version 3, or (at your option) any later
   10    version.
   11 
   12    The rep_hash utility is distributed in the hope that it will be useful, but
   13    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   15    more details.
   16 
   17    You should have received a copy of the GNU General Public License along with
   18    the littleutils.  If not, see <https://www.gnu.org/licenses/>. */
   19 
   20 
   21 #include <config.h>
   22 
   23 #ifdef HAVE_STDIO_H
   24 # include <stdio.h>
   25 #endif
   26 #ifdef HAVE_STDLIB_H
   27 # include <stdlib.h>
   28 #endif
   29 #ifdef HAVE_STRING_H
   30 # include <string.h>
   31 #endif
   32 
   33 #ifndef LINE_MAX
   34 # define LINE_MAX 8192
   35 #endif
   36 
   37 
   38 static int
   39 parse_three (char *line, char *field1, char *field2, char *field3)
   40 {
   41   char *ptr;
   42   int i, n, rc;
   43 
   44   i = 0;
   45   rc = 0;
   46   ptr = strtok (line, "\t\n");
   47   while (ptr != NULL)
   48     {
   49       i++;
   50       switch (i)
   51         {
   52         case 1:
   53           n = (int) strlen (ptr);
   54           if (n > LINE_MAX - 1) n = LINE_MAX - 1;
   55           strncpy (field1, ptr, (size_t) n);
   56           field1[n] = (char) '\0';
   57           rc += 1;
   58           break;
   59         case 2:
   60           n = (int) strlen (ptr);
   61           if (n > LINE_MAX - 1) n = LINE_MAX - 1;
   62           strncpy (field2, ptr, (size_t) n);
   63           field2[n] = (char) '\0';
   64           rc += 2;
   65           break;
   66         case 3:
   67           n = (int) strlen (ptr);
   68           if (n > LINE_MAX - 1) n = LINE_MAX - 1;
   69           strncpy (field3, ptr, (size_t) n);
   70           field3[n] = (char) '\0';
   71           rc += 4;
   72           break;
   73         default:
   74           rc = 0;
   75         }
   76       ptr = strtok (NULL, "\t\n");
   77     }
   78   if (rc == 7)
   79     return (0);
   80   else
   81     return (1);
   82 }
   83 
   84 
   85 int
   86 main (int argc, char *argv[])
   87 {
   88   /* declare variables */
   89 
   90   FILE *infile;
   91   char *status, line[LINE_MAX];
   92   char item01[LINE_MAX], item02[LINE_MAX], item03[LINE_MAX];
   93   char item11[LINE_MAX], item12[LINE_MAX], item13[LINE_MAX];
   94   int rc = 0;
   95 
   96   /* open file or standard input */
   97 
   98   if (argc == 1)
   99     infile = stdin;
  100   else if ((infile = fopen (argv[1], "r")) == NULL)
  101     {
  102       fprintf (stderr, "rep_hash error: can't open %s!\n", argv[1]);
  103       return (1);
  104     }
  105 
  106   /* read in first two lines */
  107 
  108   status = fgets (line, LINE_MAX, infile);
  109   if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  110     rc = parse_three (line, item01, item02, item03);
  111   else
  112     rc = 1;
  113   if ((!feof (infile)) && (rc == 0))
  114     {
  115       status = fgets (line, LINE_MAX, infile);
  116       if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  117         rc = parse_three (line, item11, item12, item13);
  118       else
  119         rc = 1;
  120     }
  121 
  122   /* loop through file, comparing successive pairs of lines */
  123 
  124   while ((!feof (infile)) && (rc == 0))
  125     {
  126       if ((strncmp (item02, item12, LINE_MAX) == 0) &&
  127         (strncmp (item03, item13, LINE_MAX) == 0))
  128           fprintf (stdout, "%s\t%s\n", item01, item11);
  129       strncpy (item01, item11, LINE_MAX);
  130       strncpy (item02, item12, LINE_MAX);
  131       strncpy (item03, item13, LINE_MAX);
  132       status = fgets (line, LINE_MAX, infile);
  133       if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  134         rc = parse_three (line, item11, item12, item13);
  135       else
  136         rc = 1;
  137     }
  138   if ((rc == 0) && (strncmp(item02, item12, LINE_MAX) == 0) &&
  139     (strncmp (item03, item13, LINE_MAX) == 0))
  140       fprintf (stdout, "%s\t%s\n", item01, item11);
  141 
  142   /* close file */
  143 
  144   if (argc > 1)
  145     (void) fclose (infile);
  146 
  147   /* return proper exit code */
  148 
  149   if (rc == 3)
  150     return (0);
  151   else
  152     return (1);
  153 }