From 857e8afe74520dab491cd15a6fd7464980c56609 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 9 Oct 2017 20:18:03 +0200 Subject: add -q for quiet mode to redirect output of programs to /dev/null --- xe.1 | 5 ++++- xe.c | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/xe.1 b/xe.1 index 4cf45bc..a693a95 100644 --- a/xe.1 +++ b/xe.1 @@ -6,7 +6,7 @@ .Nd execute a command for every argument .Sh SYNOPSIS .Nm -.Op Fl 0FLRnv +.Op Fl 0FLRnqv .Oo Fl p | Fl I Ar replace-arg Oc .Op Fl N Ar maxargs .Op Fl j Ar maxjobs @@ -123,6 +123,9 @@ Return with status 122 when no arguments have been specified never executes a command when no arguments are specified. .It Fl n Dry run: don't run the resulting commands, just print them. +.It Fl q +Quiet mode: +redirect standard output and standard error of commands to /dev/null. .It Fl v Verbose: print commands to standard error before running them. When used twice, also print job id and exit status for each command. diff --git a/xe.c b/xe.c index 846a28e..2c7e8a8 100644 --- a/xe.c +++ b/xe.c @@ -30,7 +30,7 @@ static int maxatonce = 1; static int maxjobs = 1; static int runjobs = 0; static int failed = 0; -static int Aflag, Fflag, Lflag, Rflag, aflag, nflag, pflag, vflag; +static int Aflag, Fflag, Lflag, Rflag, aflag, nflag, pflag, qflag, vflag; static long iterations = 0; static FILE *traceout; static FILE *input; @@ -256,6 +256,14 @@ run() fprintf(stderr, "xe: no command\n"); exit(126); } + if (qflag) { + int fd = open("/dev/null", O_RDONLY); + if (fd >= 0) { + dup2(fd, 2); + dup2(fd, 1); + close(fd); + } + } execvp(args[0], args); fprintf(stderr, "xe: %s: %s\n", args[0], strerror(errno)); exit(errno == ENOENT ? 127 : 126); @@ -578,7 +586,7 @@ main(int argc, char *argv[], char *envp[]) traceout = stdout; - while ((c = getopt(argc, argv, "+0A:FI:LN:Raf:j:nps:v")) != -1) + while ((c = getopt(argc, argv, "+0A:FI:LN:Raf:j:npqs:v")) != -1) switch (c) { case '0': delim = '\0'; break; case 'A': argsep = optarg; Aflag++; break; @@ -592,11 +600,12 @@ main(int argc, char *argv[], char *envp[]) case 'j': maxjobs = parse_jobs(optarg); break; case 'n': nflag++; break; case 'p': pflag++; break; + case 'q': qflag++; break; case 's': sflag = optarg; break; case 'v': vflag++; traceout = stderr; break; default: fprintf(stderr, - "Usage: %s [-0FLRnv] [-p | -I arg] [-N maxargs] [-j maxjobs] COMMAND...\n" + "Usage: %s [-0FLRnqv] [-p | -I arg] [-N maxargs] [-j maxjobs] COMMAND...\n" " | -f ARGFILE COMMAND...\n" " | -s SHELLSCRIPT\n" " | -a COMMAND... -- ARGS...\n" -- cgit 1.4.1