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