"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "littleutils/filesize.c" between
littleutils-1.2.4.tar.lz and littleutils-1.2.5.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

filesize.c  (littleutils-1.2.4.tar.lz):filesize.c  (littleutils-1.2.5.tar.lz)
/* filesize: Prints out the file sizes of the specified files. /* filesize: Prints out the file sizes of the specified files.
Copyright (C) 2004-2020 by Brian Lindholm. Copyright (C) 2004-2021 by Brian Lindholm.
This file is part of the littleutils utility set. This file is part of the littleutils utility set.
The filesize utility is free software; you can redistribute it and/or modify The filesize utility is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any later Free Software Foundation; either version 3, or (at your option) any later
version. version.
The filesize utility is distributed in the hope that it will be useful, but The filesize utility is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details. more details.
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
the littleutils. If not, see <https://www.gnu.org/licenses/>. */ the littleutils. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h> #include <config.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <limits.h> #include <limits.h>
#include <stdio.h> #ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
# include <stdlib.h> # include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
# include <string.h> # include <string.h>
#endif #endif
#ifdef HAVE_SYS_STAT_H #ifdef HAVE_SYS_STAT_H
# include <sys/stat.h> # include <sys/stat.h>
#endif #endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
# define OPTEND -1 # define OPTEND -1
#else #else
# define OPTEND EOF # define OPTEND EOF
#endif #endif
#ifdef HAVE_GETOPT_H
#if HAVE_INTTYPES_H # include <getopt.h>
# include <inttypes.h>
#endif #endif
#ifdef __MINGW32__ #ifdef __MINGW32__
extern int getopt (int argc, char * const *argv, const char *optstring); extern int getopt (int argc, char * const *argv, const char *optstring);
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
#endif #endif
#ifdef DJGPP #ifdef DJGPP
unsigned short _djstat_flags = 63; /* speed up stat command for DJGPP */ unsigned short _djstat_flags = 63; /* speed up stat command for DJGPP */
#endif #endif
#ifndef PATH_MAX #ifndef PATH_MAX
# define PATH_MAX 256 # define PATH_MAX 256
#endif #endif
char *sizefmt_q = (sizeof (off_t) <= sizeof (long) ? "%lu\n" : "%llu\n"); char *sizefmt_q = (sizeof (off_t) <= sizeof (long) ? "%lu\n" : "%llu\n");
char *sizefmt_v = (sizeof (off_t) <= sizeof (long) ? "%s\t%lu\n" : "%s\t%llu\n") ; char *sizefmt_v = (sizeof (off_t) <= sizeof (long) ? "%s\t%lu\n" : "%s\t%llu\n") ;
off_t total_size = 0;
/* help function */ /* help function */
static void static void
help (FILE *where) help (FILE *where)
{ {
fprintf (where, fprintf (where,
"filesize " PACKAGE_VERSION "\n" "filesize " PACKAGE_VERSION "\n"
"usage: filesize [-f file_list] [-h(elp)] [-p(ipe)] [-q(uiet)]\n" "usage: filesize [-f file_list] [-h(elp)] [-p(ipe)] [-q(uiet)]\n"
" [-v(erbose)] file...\n"); " [-t(otalize)] [-v(erbose)] file...\n");
} }
/* print filesize function */ /* print filesize function */
static void static void
printsize (char *filename, int verbose) printsize (char *filename, int totalize, int verbose)
{ {
struct stat file_stats; struct stat file_stats;
if (stat (filename, &file_stats)) if (stat (filename, &file_stats))
fprintf (stderr, "filesize error: can't determine size of %s\n", filename); fprintf (stderr, "filesize error: can't determine size of %s\n", filename);
else else
if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) && if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) &&
((file_stats.st_mode & S_IFREG) == S_IFREG)) ((file_stats.st_mode & S_IFREG) == S_IFREG))
{ {
if (verbose == 1) if (totalize == 1)
total_size += file_stats.st_size;
else if (verbose == 1)
fprintf (stdout, sizefmt_v, filename, file_stats.st_size); fprintf (stdout, sizefmt_v, filename, file_stats.st_size);
else else
fprintf (stdout, sizefmt_q, file_stats.st_size); fprintf (stdout, sizefmt_q, file_stats.st_size);
} }
} }
/* main program */ /* main program */
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
FILE *infile; FILE *infile;
char filename[PATH_MAX], *listname, *newline, *rc; char filename[PATH_MAX], *listname, *newline, *rc;
int argn, opt, use_file, use_pipe, verbose; int argn, opt, totalize, use_file, use_pipe, verbose;
/* parse options */ /* parse options */
listname = ""; listname = "";
totalize = 0;
verbose = 0; verbose = 0;
use_file = 0; use_file = 0;
use_pipe = 0; use_pipe = 0;
while ((opt = getopt (argc, argv, "f:hpqv")) != OPTEND) while ((opt = getopt (argc, argv, "f:hpqtv")) != OPTEND)
switch (opt) switch (opt)
{ {
case 'f': case 'f':
use_file = 1; use_file = 1;
listname = optarg; listname = optarg;
break; break;
case 'h': case 'h':
help (stdout); help (stdout);
return (0); return (0);
case 'p': case 'p':
use_pipe = 1; use_pipe = 1;
break; break;
case 'q': case 'q':
verbose = -1; verbose = -1;
break; break;
case 't':
totalize = 1;
break;
case 'v': case 'v':
verbose = 1; verbose = 1;
break; break;
case '?': case '?':
help (stderr); help (stderr);
return (1); return (1);
} }
/* finalize options */ /* finalize options */
skipping to change at line 167 skipping to change at line 175
{ {
while (!feof (infile)) while (!feof (infile))
{ {
rc = fgets (filename, PATH_MAX - 1, infile); rc = fgets (filename, PATH_MAX - 1, infile);
if (rc != NULL) if (rc != NULL)
{ {
newline = strchr (filename, '\n'); newline = strchr (filename, '\n');
if (newline != NULL) if (newline != NULL)
*newline = '\0'; *newline = '\0';
if (strlen (filename) != 0) if (strlen (filename) != 0)
printsize (filename, verbose); printsize (filename, totalize, verbose);
} }
} }
(void) fclose (infile); (void) fclose (infile);
} }
} }
/* process files listed on stdin (i.e., the -p option) */ /* process files listed on stdin (i.e., the -p option) */
if (use_pipe) if (use_pipe)
while (!feof (stdin)) while (!feof (stdin))
{ {
rc = fgets (filename, PATH_MAX - 1, stdin); rc = fgets (filename, PATH_MAX - 1, stdin);
if (rc != NULL) if (rc != NULL)
{ {
newline = strchr (filename, '\n'); newline = strchr (filename, '\n');
if (newline != NULL) if (newline != NULL)
*newline = '\0'; *newline = '\0';
if (strlen (filename) != 0) if (strlen (filename) != 0)
printsize (filename, verbose); printsize (filename, totalize, verbose);
} }
} }
/* process files given in the argument list */ /* process files given in the argument list */
for (argn = optind; argn < argc; argn++) for (argn = optind; argn < argc; argn++)
printsize (argv[argn], verbose); printsize (argv[argn], totalize, verbose);
/* print total size if requested */
if (totalize == 1)
fprintf (stdout, sizefmt_q, total_size);
/* indicate successful finish */ /* indicate successful finish */
return (0); return (0);
} }
 End of changes. 16 change blocks. 
16 lines changed or deleted 29 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)