"Fossies" - the Fresh Open Source Software Archive

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