PR/397: dadv: Restore the ability of processing filenames from stdin

immediately, using the -I flag.

(cherry picked from commit ae5c1ff8df0d6e00cbf5e31875d777edbb88d60a)
This commit is contained in:
Christos Zoulas 2022-10-23 14:22:53 +00:00 committed by Xin LI
parent ac33800609
commit ac428f59cd
3 changed files with 42 additions and 24 deletions

View file

@ -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

View file

@ -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);

View file

@ -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,