"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/littleutils/filesize.c" (29 Oct 2021, 5082 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 "filesize.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 /* filesize:  Prints out the file sizes of the specified files.
    2 
    3    Copyright (C) 2004-2021 by Brian Lindholm.
    4    This file is part of the littleutils utility set.
    5 
    6    The filesize 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 filesize 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 unsigned short _djstat_flags = 63;  /* speed up stat command for DJGPP */
   57 #endif
   58 
   59 #ifndef PATH_MAX
   60 # define PATH_MAX 256
   61 #endif
   62 
   63 char *sizefmt_q = (sizeof (off_t) <= sizeof (long) ? "%lu\n" : "%llu\n");
   64 char *sizefmt_v = (sizeof (off_t) <= sizeof (long) ? "%s\t%lu\n" : "%s\t%llu\n");
   65 off_t total_size = 0;
   66 
   67 
   68 /* help function */
   69 
   70 static void
   71 help (FILE *where)
   72 {
   73   fprintf (where,
   74     "filesize " PACKAGE_VERSION "\n"
   75     "usage: filesize [-f file_list] [-h(elp)] [-p(ipe)] [-q(uiet)]\n"
   76     "         [-t(otalize)] [-v(erbose)] file...\n");
   77 }
   78 
   79 
   80 /* print filesize function */
   81 
   82 static void
   83 printsize (char *filename, int totalize, int verbose)
   84 {
   85   struct stat file_stats;
   86 
   87   if (stat (filename, &file_stats))
   88     fprintf (stderr, "filesize error: can't determine size of %s\n", filename);
   89   else
   90     if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) &&
   91         ((file_stats.st_mode & S_IFREG) == S_IFREG))
   92       {
   93         if (totalize == 1)
   94           total_size += file_stats.st_size;
   95         else if (verbose == 1)
   96           fprintf (stdout, sizefmt_v, filename, file_stats.st_size);
   97         else
   98           fprintf (stdout, sizefmt_q, file_stats.st_size);
   99       }
  100 }
  101 
  102 
  103 /* main program */
  104 
  105 int
  106 main (int argc, char *argv[])
  107 {
  108   FILE *infile;
  109   char filename[PATH_MAX], *listname, *newline, *rc;
  110   int argn, opt, totalize, use_file, use_pipe, verbose;
  111 
  112   /* parse options */
  113 
  114   listname = "";
  115   totalize = 0;
  116   verbose = 0;
  117   use_file = 0;
  118   use_pipe = 0;
  119   while ((opt = getopt (argc, argv, "f:hpqtv")) != OPTEND)
  120     switch (opt)
  121       {
  122       case 'f':
  123         use_file = 1;
  124         listname = optarg;
  125         break;
  126       case 'h':
  127         help (stdout);
  128         return (0);
  129       case 'p':
  130         use_pipe = 1;
  131         break;
  132       case 'q':
  133         verbose = -1;
  134         break;
  135       case 't':
  136         totalize = 1;
  137         break;
  138       case 'v':
  139         verbose = 1;
  140         break;
  141       case '?':
  142         help (stderr);
  143         return (1);
  144       }
  145 
  146   /* finalize options */
  147 
  148   if ((optind == argc) && (use_file == 0) && (use_pipe == 0))
  149     {
  150       help (stdout);
  151       return (0);
  152     }
  153   if (verbose == 0)
  154     {
  155       if (((argc - optind) != 1) || use_file || use_pipe)
  156         verbose = 1;
  157       else
  158         verbose = -1;
  159     }
  160 
  161   /* process files in listed in file specified by -f option */
  162 
  163   if (use_file)
  164     {
  165       infile = fopen (listname, "r");
  166       if (infile == NULL)
  167         fprintf (stderr, "filesize error: can't open %s!\n", listname);
  168       else
  169         {
  170           while (!feof (infile))
  171             {
  172               rc = fgets (filename, PATH_MAX - 1, infile);
  173               if (rc != NULL)
  174                 {
  175                   newline = strchr (filename, '\n');
  176                   if (newline != NULL)
  177                     *newline = '\0';
  178                   if (strlen (filename) != 0)
  179                     printsize (filename, totalize, verbose);
  180                 }
  181             }
  182           (void) fclose (infile);
  183         }
  184     }
  185 
  186   /* process files listed on stdin (i.e., the -p option) */
  187 
  188   if (use_pipe)
  189     while (!feof (stdin))
  190       {
  191         rc = fgets (filename, PATH_MAX - 1, stdin);
  192         if (rc != NULL)
  193           {
  194             newline = strchr (filename, '\n');
  195             if (newline != NULL)
  196               *newline = '\0';
  197             if (strlen (filename) != 0)
  198               printsize (filename, totalize, verbose);
  199           }
  200       }
  201 
  202   /* process files given in the argument list */
  203 
  204   for (argn = optind; argn < argc; argn++)
  205     printsize (argv[argn], totalize, verbose);
  206 
  207   /* print total size if requested */
  208 
  209   if (totalize == 1)
  210     fprintf (stdout, sizefmt_q, total_size);
  211 
  212   /* indicate successful finish */
  213 
  214   return (0);
  215 }