diff options
author | Christos Zoulas <christos@zoulas.com> | 2022-10-23 14:22:53 +0000 |
---|---|---|
committer | Christos Zoulas <christos@zoulas.com> | 2022-10-23 14:22:53 +0000 |
commit | ae5c1ff8df0d6e00cbf5e31875d777edbb88d60a (patch) | |
tree | 8625f4da536d96d6ad437a14c708e605430fee02 | |
parent | b865fa09bcf241408c8fdb01275089e029ca24a1 (diff) | |
download | file-git-ae5c1ff8df0d6e00cbf5e31875d777edbb88d60a.tar.gz |
PR/397: dadv: Restore the ability of processing filenames from stdin
immediately, using the -I flag.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/file.man | 9 | ||||
-rw-r--r-- | src/file.c | 53 | ||||
-rw-r--r-- | src/file_opts.h | 6 |
4 files changed, 47 insertions, 25 deletions
@@ -1,3 +1,7 @@ +2022-10-23 10:21 Christos Zoulas <christos@zoulas.com> + + * PR/397: Restore the ability to process files from stdin immediately. + 2022-09-20 17:12 Christos Zoulas <christos@zoulas.com> * fixed various clustefuzz issues diff --git a/doc/file.man b/doc/file.man index 0fbe2bad..99111732 100644 --- a/doc/file.man +++ b/doc/file.man @@ -1,5 +1,5 @@ -.\" $File: file.man,v 1.144 2021/02/05 22:08:31 christos Exp $ -.Dd February 5, 2021 +.\" $File: file.man,v 1.145 2022/10/23 14:22:53 christos Exp $ +.Dd October 23, 2022 .Dt FILE __CSECTION__ .Os .Sh NAME @@ -8,7 +8,7 @@ .Sh SYNOPSIS .Nm .Bk -words -.Op Fl bcdEhiklLNnprsSvzZ0 +.Op Fl bcdEhIiklLNnprsSvzZ0 .Op Fl Fl apple .Op Fl Fl exclude-quiet .Op Fl Fl extension @@ -277,6 +277,9 @@ This option causes symlinks not to be followed This is the default if the environment variable .Dv POSIXLY_CORRECT is not defined. +.It Fl I , Fl Fl immediate +When processing filenames from stdin, don't collect their names first in order +to compute their maximum namelength first, process them immediately. .It Fl i , Fl Fl mime Causes the .Nm @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.208 2022/09/27 19:01:05 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.209 2022/10/23 14:22:53 christos Exp $") #endif /* lint */ #include "magic.h" @@ -85,8 +85,8 @@ int getopt_long(int, char * const *, const char *, # define IFLNK_L "" #endif -#define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprsSvzZ0" -#define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0" +#define FILE_FLAGS "bcCdE" IFLNK_h "Iik" IFLNK_L "lNnprsSvzZ0" +#define OPTSTRING "bcCde:Ef:F:hiIklLm:nNpP:rsSvzZ0" # define USAGE \ "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \ @@ -177,7 +177,7 @@ __dead #endif private void help(void); -private int unwrap(struct magic_set *, const char *); +private int unwrap(struct magic_set *, const char *, int); private int process(struct magic_set *ms, const char *, int); private struct magic_set *load(const char *, int); private void setparam(const char *); @@ -198,7 +198,7 @@ main(int argc, char *argv[]) int sandbox = 1; #endif struct magic_set *magic = NULL; - int longindex; + int longindex, immed = 0; const char *magicfile = NULL; /* where the magic is */ char *progname; @@ -278,7 +278,7 @@ main(int argc, char *argv[]) if ((magic = load(magicfile, flags)) == NULL) return 1; applyparam(magic); - e |= unwrap(magic, optarg); + e |= unwrap(magic, optarg, immed); ++didsomefiles; break; case 'F': @@ -287,6 +287,9 @@ main(int argc, char *argv[]) case 'i': flags |= MAGIC_MIME; break; + case 'I': + immed = 1; + break; case 'k': flags |= MAGIC_CONTINUE; break; @@ -506,7 +509,7 @@ load(const char *magicfile, int flags) * unwrap -- read a file of filenames, do each one. */ private int -unwrap(struct magic_set *ms, const char *fn) +unwrap(struct magic_set *ms, const char *fn, int immed) { FILE *f; ssize_t len; @@ -514,11 +517,8 @@ unwrap(struct magic_set *ms, const char *fn) size_t llen = 0; int wid = 0, cwid; int e = 0; - size_t fi = 0, fimax = 100; - char **flist = CAST(char **, malloc(sizeof(*flist) * fimax)); - - if (flist == NULL) -out: file_err(EXIT_FAILURE, "Cannot allocate memory for file list"); + size_t fi = 0, fimax = 0; + char **flist = NULL; if (strcmp("-", fn) == 0) f = stdin; @@ -532,26 +532,37 @@ out: file_err(EXIT_FAILURE, "Cannot allocate memory for file list"); while ((len = getline(&line, &llen, f)) > 0) { if (line[len - 1] == '\n') line[len - 1] = '\0'; + cwid = file_mbswidth(ms, line); + if (cwid > wid) + wid = cwid; + if (immed) { + e |= process(ms, line, wid); + free(line); + line = NULL; + llen = 0; + continue; + } if (fi >= fimax) { fimax += 100; char **nf = CAST(char **, realloc(flist, fimax * sizeof(*flist))); - if (nf == NULL) - goto out; + if (nf == NULL) { + file_err(EXIT_FAILURE, + "Cannot allocate memory for file list"); + } flist = nf; } flist[fi++] = line; - cwid = file_mbswidth(ms, line); - if (cwid > wid) - wid = cwid; line = NULL; llen = 0; } - fimax = fi; - for (fi = 0; fi < fimax; fi++) { - e |= process(ms, flist[fi], wid); - free(flist[fi]); + if (!immed) { + fimax = fi; + for (fi = 0; fi < fimax; fi++) { + e |= process(ms, flist[fi], wid); + free(flist[fi]); + } } free(flist); diff --git a/src/file_opts.h b/src/file_opts.h index 6d137441..88813856 100644 --- a/src/file_opts.h +++ b/src/file_opts.h @@ -37,7 +37,7 @@ OPT('e', "exclude", 1, 0, " performed for file. Valid tests are:\n" " %e\n") OPT_LONGONLY("exclude-quiet", 1, 0, - " TEST like exclude, but ignore unknown tests\n", OPT_EXCLUDE_QUIET) + " TEST like exclude, but ignore unknown tests\n", OPT_EXCLUDE_QUIET) OPT('f', "files-from", 1, 0, " FILE read the filenames to be examined from FILE\n") OPT('F', "separator", 1, 0, @@ -45,6 +45,10 @@ OPT('F', "separator", 1, 0, OPT('i', "mime", 0, 0, " output MIME type strings (--mime-type and\n" " --mime-encoding)\n") +OPT('I', "immediate", 0, 0, + " when reading filenames from stdin, process them\n" + " immediately, don't collect them to compute their\n" + " maximum name length\n") OPT_LONGONLY("apple", 0, 0, " output the Apple CREATOR/TYPE\n", OPT_APPLE) OPT_LONGONLY("extension", 0, 0, |