"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/repeats/rep_size.c" (29 Oct 2021, 3784 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_size.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_size:  Looks for duplicate files based on filesize.  Utilizes the output
    2    of the filesize command.
    3 
    4    Copyright (C) 2004-2021 by Brian Lindholm.
    5    This file is part of the littleutils utility set.
    6 
    7    The rep_size 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_size 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_two (char *line, char *field1, char *field2)
   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         default:
   67           rc = 0;
   68         }
   69       ptr = strtok (NULL, "\t\n");
   70     }
   71   if (rc == 3)
   72     return (0);
   73   else
   74     return (1);
   75 }
   76 
   77 
   78 int
   79 main (int argc, char *argv[])
   80 {
   81   /* declare variables */
   82 
   83   FILE *infile;
   84   char *status, line[LINE_MAX];
   85   char item01[LINE_MAX], item02[LINE_MAX], item11[LINE_MAX], item12[LINE_MAX];
   86   int last_matched, rc = 0;
   87 
   88   /* open file or standard input */
   89 
   90   if (argc == 1)
   91     infile = stdin;
   92   else if ((infile = fopen (argv[1], "r")) == NULL)
   93     {
   94       fprintf (stderr, "rep_size error: can't open %s!\n", argv[1]);
   95       return (1);
   96     }
   97 
   98   /* read in first two lines */
   99 
  100   status = fgets (line, LINE_MAX, infile);
  101   if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  102     rc = parse_two (line, item01, item02);
  103   else
  104     rc = 1;
  105   if ((!feof (infile)) && (rc == 0))
  106     {
  107       status = fgets (line, LINE_MAX, infile);
  108       if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  109         rc = parse_two (line, item11, item12);
  110       else
  111         rc = 1;
  112     }
  113 
  114   /* loop through file, comparing successive pairs of lines */
  115 
  116   last_matched = 0;
  117   while ((!feof (infile)) && (rc == 0))
  118     {
  119       if (strncmp (item02, item12, LINE_MAX) == 0)
  120         {
  121           if (last_matched == 0)
  122             fprintf (stdout, "%s\n", item01);
  123           fprintf (stdout, "%s\n", item11);
  124           last_matched = 1;
  125         }
  126       else
  127         last_matched = 0;
  128       strncpy (item01, item11, LINE_MAX);
  129       strncpy (item02, item12, LINE_MAX);
  130       status = fgets (line, LINE_MAX, infile);
  131       if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  132         rc = parse_two (line, item11, item12);
  133       else
  134         rc = 1;
  135     }
  136   if ((rc == 0) && (strncmp (item02, item12, LINE_MAX) == 0))
  137     {
  138       if (last_matched == 0)
  139         fprintf (stdout, "%s\n", item01);
  140       fprintf (stdout, "%s\n", item11);
  141     }
  142 
  143   /* close file */
  144 
  145   if (argc > 1)
  146     (void) fclose (infile);
  147 
  148   /* return proper exit code */
  149 
  150   if (rc == 0)
  151     return (0);
  152   else
  153     return (1);
  154 }