"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/repeats/rep_cmp.c" (29 Oct 2021, 3028 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_cmp.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_cmp:  Performs a final paranoid check for duplicate files.  Utilizes
    2    results of the filesize command.
    3 
    4    Copyright (C) 2013-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], command[LINE_MAX];
   85   char item1[LINE_MAX], item2[LINE_MAX];
   86   int rc1 = 0, rc2 = 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   /* loop through file, comparing pairs of files */
   99 
  100   while ((!feof (infile)) && (rc1 == 0))
  101     {
  102       status = fgets (line, LINE_MAX, infile);
  103       if ((status != NULL) && ((int) strlen (line) > 1) && (line[0] != '#'))
  104         rc1 = parse_two (line, item1, item2);
  105       else
  106         rc1 = 1;
  107       if (rc1 == 0)
  108         {
  109           /* printf("echo \'%s\' \'%s\'", item1, item2); */
  110           sprintf(command, "cmp -s \'%s\' \'%s\'", item1, item2);
  111           rc2 = system(command);
  112           if (rc2 == 0)
  113             fprintf (stdout, "%s\t%s\n", item1, item2);
  114         }
  115     }
  116 
  117   /* close file */
  118 
  119   if (argc > 1)
  120     (void) fclose (infile);
  121 
  122   /* return proper exit code */
  123 
  124   if (rc1 == 0)
  125     return (0);
  126   else
  127     return (1);
  128 }