"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/repeats/rep_hard.c" (29 Oct 2021, 4342 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_hard.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_hard:  Prints out size/inode/device numbers for the specified files.
    2 
    3    Copyright (C) 2006-2021 by Brian Lindholm.
    4    This file is part of the littleutils utility set.
    5 
    6    The rep_hard utility is free software; you can redistribute it and/or modify
    7    it under the terms of the GNU General Public License as published by the
    8    Free Software Foundation; either version 3, or (at your option) any later
    9    version.
   10 
   11    The rep_hard utility is distributed in the hope that it will be useful, but
   12    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   14    more details.
   15 
   16    You should have received a copy of the GNU General Public License along with
   17    the littleutils.  If not, see <https://www.gnu.org/licenses/>. */
   18 
   19 
   20 #include <config.h>
   21 
   22 #if HAVE_INTTYPES_H
   23 # include <inttypes.h>
   24 #endif
   25 #include <limits.h>
   26 #ifdef HAVE_STDIO_H
   27 # include <stdio.h>
   28 #endif
   29 #ifdef HAVE_STDLIB_H
   30 # include <stdlib.h>
   31 #endif
   32 #ifdef HAVE_STRING_H
   33 # include <string.h>
   34 #endif
   35 #ifdef HAVE_SYS_STAT_H
   36 # include <sys/stat.h>
   37 #endif
   38 
   39 #ifdef HAVE_UNISTD_H
   40 # include <unistd.h>
   41 # define OPTEND -1
   42 #else
   43 # define OPTEND EOF
   44 #endif
   45 #ifdef HAVE_GETOPT_H
   46 # include <getopt.h>
   47 #endif
   48 
   49 #ifdef __MINGW32__
   50 extern int getopt (int argc, char * const *argv, const char *optstring);
   51 extern char *optarg;
   52 extern int optind;
   53 #endif
   54 
   55 #ifdef DJGPP
   56 /* speed up stat command for DJGPP */
   57 unsigned short _djstat_flags = 63;
   58 #endif
   59 
   60 #ifndef PATH_MAX
   61 # define PATH_MAX 4096
   62 #endif
   63 
   64 char *sizefmt_ps =
   65   (sizeof (off_t) <= sizeof (long) ? "%s\t%lu\t%d\t%d\n" : "%s\t%llu\t%d\t%d\n");
   66 
   67 
   68 /* help function */
   69 
   70 static void
   71 help (FILE *where)
   72 {
   73   fprintf (where,
   74     "rep_hard " PACKAGE_VERSION "\n"
   75     "usage: rep_hard [-f file_list] [-h(elp)] [-p(ipe)] file...\n");
   76 }
   77 
   78 
   79 /* print rep_hard function */
   80 
   81 static void
   82 printinfo (char *filename)
   83 {
   84   struct stat file_stats;
   85 
   86   if (stat (filename, &file_stats))
   87     fprintf (stderr, "rep_hard error: can't determine info for %s\n", filename);
   88   else
   89     fprintf (stdout, sizefmt_ps, filename, file_stats.st_size,
   90       (unsigned int) file_stats.st_ino, (unsigned int) file_stats.st_dev);
   91 }
   92 
   93 
   94 /* main program */
   95 
   96 int
   97 main (int argc, char *argv[])
   98 {
   99   FILE *infile;
  100   char filename[PATH_MAX], *listname, *newline, *rc;
  101   int argn, opt, use_file, use_pipe, verbose;
  102 
  103   /* parse options */
  104 
  105   listname = "";
  106   use_file = 0;
  107   use_pipe = 0;
  108   while ((opt = getopt (argc, argv, "f:hp")) != OPTEND)
  109     switch (opt)
  110       {
  111       case 'f':
  112         use_file = 1;
  113         listname = optarg;
  114         break;
  115       case 'h':
  116         help (stdout);
  117         return (0);
  118       case 'p':
  119         use_pipe = 1;
  120         break;
  121       case '?':
  122         help (stderr);
  123         return (1);
  124       }
  125 
  126   /* finalize options */
  127 
  128   if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
  129     {
  130       help (stdout);
  131       return (0);
  132     }
  133   if (verbose == 0)
  134     {
  135       if (((argc - optind) != 1) || use_file || use_pipe)
  136         verbose = 1;
  137       else
  138         verbose = -1;
  139     }
  140 
  141   /* process files in listed in file specified by -f option */
  142 
  143   if (use_file)
  144     {
  145       infile = fopen (listname, "r");
  146       if (infile == NULL)
  147         fprintf (stderr, "rep_hard error: can't open %s!\n", listname);
  148       else
  149         {
  150           while (!feof (infile))
  151             {
  152               rc = fgets (filename, PATH_MAX - 1, infile);
  153               if (rc != NULL)
  154                 {
  155                   newline = strchr (filename, '\n');
  156                   if (newline != NULL)
  157                     *newline = '\0';
  158                   if (strlen (filename) != 0)
  159                     printinfo (filename);
  160                 }
  161             }
  162           (void) fclose (infile);
  163         }
  164     }
  165 
  166   /* process files listed on stdin (i.e., the -p option) */
  167 
  168   if (use_pipe)
  169     while (!feof (stdin))
  170       {
  171         rc = fgets (filename, PATH_MAX - 1, stdin);
  172         if (rc != NULL)
  173           {
  174             newline = strchr (filename, '\n');
  175             if (newline != NULL)
  176               *newline = '\0';
  177             if (strlen (filename) != 0)
  178               printinfo (filename);
  179           }
  180       }
  181 
  182   /* process files given in the argument list */
  183 
  184   for (argn = optind; argn < argc; argn++)
  185     printinfo (argv[argn]);
  186 
  187   /* indicate successful finish */
  188 
  189   return (0);
  190 }