From ac428f59cd18fbede058cc2aea4feb8679829222 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sun, 23 Oct 2022 14:22:53 +0000 Subject: [PATCH 1/3] PR/397: dadv: Restore the ability of processing filenames from stdin immediately, using the -I flag. (cherry picked from commit ae5c1ff8df0d6e00cbf5e31875d777edbb88d60a) --- doc/file.man | 9 ++++++--- src/file.c | 51 ++++++++++++++++++++++++++++++------------------- src/file_opts.h | 6 +++++- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/doc/file.man b/doc/file.man index 0fbe2bad9b89..99111732935a 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 diff --git a/src/file.c b/src/file.c index 1566a17ff00b..c3b15cab9da3 100644 --- a/src/file.c +++ b/src/file.c @@ -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; @@ -504,7 +507,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; @@ -512,11 +515,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; @@ -530,26 +530,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 978c8b64434c..12aab752d848 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, From d5e82a1bb004a69121b68544fac83c788f62544b Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Mon, 24 Oct 2022 20:21:54 +0000 Subject: [PATCH 2/3] In immediate mode, print with the current width. (cherry picked from commit 7d489233e5f27decdbcfb0e8ecba0b0b5fdc6066) --- src/file.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file.c b/src/file.c index c3b15cab9da3..2cf7bca68eec 100644 --- a/src/file.c +++ b/src/file.c @@ -531,15 +531,15 @@ unwrap(struct magic_set *ms, const char *fn, int immed) 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); + e |= process(ms, line, cwid); free(line); line = NULL; llen = 0; continue; } + if (cwid > wid) + wid = cwid; if (fi >= fimax) { fimax += 100; char **nf = CAST(char **, From 5d5531f83be2e504e0128532d46dcfaf512da16d Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Wed, 26 Oct 2022 16:56:14 +0000 Subject: [PATCH 3/3] Use the -n flag to produce immediate results and kill the short-lived -I. (cherry picked from commit 425f9897f0ed791a6c216c24375bd9b3a42330a2) --- doc/file.man | 9 +++------ src/file.c | 19 ++++++++----------- src/file_opts.h | 4 ---- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/doc/file.man b/doc/file.man index 99111732935a..c0f1ad937c28 100644 --- a/doc/file.man +++ b/doc/file.man @@ -1,5 +1,5 @@ -.\" $File: file.man,v 1.145 2022/10/23 14:22:53 christos Exp $ -.Dd October 23, 2022 +.\" $File: file.man,v 1.146 2022/10/26 16:56:14 christos Exp $ +.Dd October 26, 2022 .Dt FILE __CSECTION__ .Os .Sh NAME @@ -8,7 +8,7 @@ .Sh SYNOPSIS .Nm .Bk -words -.Op Fl bcdEhIiklLNnprsSvzZ0 +.Op Fl bcdEhiklLNnprsSvzZ0 .Op Fl Fl apple .Op Fl Fl exclude-quiet .Op Fl Fl extension @@ -277,9 +277,6 @@ 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 diff --git a/src/file.c b/src/file.c index 2cf7bca68eec..cdbd72d0b11e 100644 --- a/src/file.c +++ b/src/file.c @@ -85,8 +85,8 @@ int getopt_long(int, char * const *, const char *, # define IFLNK_L "" #endif -#define FILE_FLAGS "bcCdE" IFLNK_h "Iik" IFLNK_L "lNnprsSvzZ0" -#define OPTSTRING "bcCde:Ef:F:hiIklLm:nNpP:rsSvzZ0" +#define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprsSvzZ0" +#define OPTSTRING "bcCde:Ef:F:hiklLm: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 *, int); +private int unwrap(struct magic_set *, const char *); 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, immed = 0; + int longindex; 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, immed); + e |= unwrap(magic, optarg); ++didsomefiles; break; case 'F': @@ -287,9 +287,6 @@ main(int argc, char *argv[]) case 'i': flags |= MAGIC_MIME; break; - case 'I': - immed = 1; - break; case 'k': flags |= MAGIC_CONTINUE; break; @@ -507,7 +504,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, int immed) +unwrap(struct magic_set *ms, const char *fn) { FILE *f; ssize_t len; @@ -531,7 +528,7 @@ unwrap(struct magic_set *ms, const char *fn, int immed) if (line[len - 1] == '\n') line[len - 1] = '\0'; cwid = file_mbswidth(ms, line); - if (immed) { + if (nobuffer) { e |= process(ms, line, cwid); free(line); line = NULL; @@ -555,7 +552,7 @@ unwrap(struct magic_set *ms, const char *fn, int immed) llen = 0; } - if (!immed) { + if (!nobuffer) { fimax = fi; for (fi = 0; fi < fimax; fi++) { e |= process(ms, flist[fi], wid); diff --git a/src/file_opts.h b/src/file_opts.h index 12aab752d848..c78a8df2a800 100644 --- a/src/file_opts.h +++ b/src/file_opts.h @@ -45,10 +45,6 @@ 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,