peek(1): remove -p

This commit is contained in:
dtb 2024-04-18 09:08:08 -06:00
parent 3b774cab27
commit 23521bade5
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

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