peek(1): remove -p

This commit is contained in:
dtb 2024-04-18 09:08:08 -06:00
parent 4e5fc680e9
commit 46b607384a
Signed by: trinity
GPG Key ID: 31FF85CCB6DC7641

View File

@ -25,8 +25,7 @@
# include <sysexits.h> # include <sysexits.h>
#endif #endif
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */ #include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
#include <unistd.h> /* dup(2), execvp(3), fork(2), getopt(3), isatty(3), #include <unistd.h> /* getopt(3), isatty(3), STDIN_FILENO */
* pipe(2), STDIN_FILENO */
static int oserr(char *s){ perror(s); return EX_OSERR; } static int oserr(char *s){ perror(s); return EX_OSERR; }
@ -59,29 +58,23 @@ int main(int argc, char *argv[]){
char include_eof; char include_eof;
FILE *outputs[] = { FILE *outputs[] = {
NULL /* stdout */, NULL /* stdout */,
NULL /* stderr */, NULL /* stderr */
NULL /* -p */
}; };
int p[2] = {0, 0};
if(argc < 1) if(argc < 1)
usage(argv[0]); return usage(argv[0]);
eof = EOF; eof = EOF;
include_eof = 0; include_eof = 0;
{ /* options parsing */ { /* options parsing */
int c; int c;
while((c = getopt(argc, argv, "1enopt")) != -1) while((c = getopt(argc, argv, "1enot")) != -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':
if(pipe(p) != 0 || (outputs[2] = fdopen(p[1], "ab")) == NULL)
return oserr(argv[0]);
break;
case 't': case 't':
if(isatty(STDIN_FILENO) != 1){ if(isatty(STDIN_FILENO) != 1){
fprintf(stderr, "%s: Must be run in a terminal" fprintf(stderr, "%s: Must be run in a terminal"
@ -90,26 +83,9 @@ int main(int argc, char *argv[]){
} }
default: return usage(argv[0]); default: return usage(argv[0]);
} }
}
/* If -p is used there must be additional arguments. getopt(3) wouldn't if(argc > optind)
* 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))
return usage(argv[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]);
case -1:
return oserr(argv[0]);
default:
if(close(p[0]) != 0)
return oserr(argv[0]);
} }
{ /* install signal handler */ { /* install signal handler */
@ -146,8 +122,6 @@ int main(int argc, char *argv[]){
for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i) for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i)
if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){ if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){
perror(argv[0]); perror(argv[0]);
if(outputs[i] != stdout && outputs[i] != stderr)
fclose(outputs[i]);
outputs[i] = NULL; outputs[i] = NULL;
} }
}while(c != eof); }while(c != eof);