peek(1): remove gotos, fix style to fit bonsai/coreutils
This commit is contained in:
parent
6c481d569d
commit
3ba22dbaf7
52
peek/peek.c
52
peek/peek.c
@ -1,6 +1,5 @@
|
||||
#include <errno.h> /* errno */
|
||||
#include <stdio.h> /* fprintf(3), getc(3), putc(3), stderr, stdin, stdout, EOF,
|
||||
* NULL */
|
||||
#include <stdio.h> /* fprintf(3), getc(3), perror(3), putc(3), stderr, stdin,
|
||||
* stdout, EOF, NULL */
|
||||
#include <stdlib.h> /* size_t */
|
||||
#include <string.h> /* strerror(3) */
|
||||
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
|
||||
@ -10,7 +9,14 @@
|
||||
#include <unistd.h> /* dup(2), execvp(3), fork(2), getopt(3), isatty(3),
|
||||
* pipe(2), STDIN_FILENO */
|
||||
|
||||
static int oserr(char *s){ perror(s); return EX_OSERR; }
|
||||
|
||||
static char *program_name = "peek";
|
||||
static int usage(char *s){
|
||||
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n",
|
||||
s == NULL ? program_name : s);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int c;
|
||||
@ -26,7 +32,7 @@ int main(int argc, char *argv[]){
|
||||
struct termios t;
|
||||
|
||||
if(argc < 1)
|
||||
goto usage;
|
||||
usage(argv[0]);
|
||||
|
||||
eof = EOF;
|
||||
include_eof = 0;
|
||||
@ -37,10 +43,8 @@ int main(int argc, char *argv[]){
|
||||
case 'o': outputs[0] = stdout; break;
|
||||
case 'e': outputs[1] = stderr; break;
|
||||
case 'p':
|
||||
if(pipe(p) != 0)
|
||||
goto die;
|
||||
else if((outputs[2] = fdopen(p[1], "ab")) == NULL)
|
||||
goto die;
|
||||
if(pipe(p) != 0 || (outputs[2] = fdopen(p[1], "ab")) == NULL)
|
||||
return oserr(argv[0]);
|
||||
break;
|
||||
case 't':
|
||||
if(isatty(STDIN_FILENO) != 1){
|
||||
@ -48,33 +52,27 @@ int main(int argc, char *argv[]){
|
||||
" (option -t specified)\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
default: goto usage;
|
||||
default: return usage(argv[0]);
|
||||
}
|
||||
|
||||
/* If -p is used there must be additional arguments. Getopt(3) wouldn't
|
||||
/* If -p is used there must be additional arguments. getopt(3) wouldn't
|
||||
* work for this because optarg would have to be one string to give to
|
||||
* system(3) or an equivalent and it would be a mess of parsing and
|
||||
* security issues. Any intended usage works with this slightly funkier
|
||||
* argument parsing, unintended usages work as happy coincidence. */
|
||||
if((argc > optind) == (outputs[2] == 0)){
|
||||
usage: fprintf(stderr, "Usage: %s (-1enot)"
|
||||
" (-p [program [arguments...]])\n",
|
||||
argv[0] == NULL ? program_name : argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
if((argc > optind) == (outputs[2] == 0))
|
||||
return usage(argv[0]);
|
||||
|
||||
if(outputs[2] != 0)
|
||||
switch(fork()){
|
||||
case 0:
|
||||
if(close(p[1]) == 0
|
||||
&& dup2(p[0], STDIN_FILENO)
|
||||
== STDIN_FILENO)
|
||||
execvp(argv[optind], argv + optind);
|
||||
if(close(p[1]) == 0 && dup2(p[0], STDIN_FILENO) == STDIN_FILENO)
|
||||
execvp(argv[optind], &argv[optind]);
|
||||
case -1:
|
||||
goto die;
|
||||
return oserr(argv[0]);
|
||||
default:
|
||||
if(close(p[0]) != 0)
|
||||
goto die;
|
||||
return oserr(argv[0]);
|
||||
}
|
||||
|
||||
/* terminal echo */
|
||||
@ -84,11 +82,10 @@ usage: fprintf(stderr, "Usage: %s (-1enot)"
|
||||
|
||||
do{ if((c = getc(stdin)) != eof || include_eof)
|
||||
for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i)
|
||||
if(outputs[i] != NULL
|
||||
&& putc(c, outputs[i]) == EOF){
|
||||
if(i == 2)
|
||||
if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){
|
||||
if(outputs[i] != stdout && outputs[i] != stderr)
|
||||
fclose(outputs[i]);
|
||||
outputs[i] = 0;
|
||||
outputs[i] = NULL;
|
||||
}
|
||||
}while(c != eof);
|
||||
|
||||
@ -97,7 +94,4 @@ usage: fprintf(stderr, "Usage: %s (-1enot)"
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||
|
||||
return EX_OK;
|
||||
|
||||
die: fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user