1
0

it is finished

This commit is contained in:
dtb 2023-12-12 20:14:33 -07:00
parent a3f3be0c88
commit 05bc6b08a8

View File

@ -1,4 +1,4 @@
/* #include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), getc(3), putc(3), EOF */ #include <stdio.h> /* fprintf(3), getc(3), putc(3), EOF */
#include <stdlib.h> /* size_t */ #include <stdlib.h> /* size_t */
#include <string.h> /* strerror(3) */ #include <string.h> /* strerror(3) */
@ -6,8 +6,8 @@
# 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> /* fork(2), getopt(3), pipe(2), write(2), STDERR_FILENO, #include <unistd.h> /* dup(2), execvp(3), fork(2), getopt(3), pipe(2),
* STDOUT_FILENO */ * write(2), STDERR_FILENO, STDOUT_FILENO */
static char *program_name = "peek"; static char *program_name = "peek";
@ -26,58 +26,58 @@ int main(int argc, char *argv[]){
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
if(argc < 1) if(argc < 1)
argv[0] = program_name; goto usage;
eof = EOF; eof = EOF;
include_eof = 0; include_eof = 0;
while((c = getopt(argc, argv, "1enop:")) != -1) while((c = getopt(argc, argv, "1enop")) != -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_FILENO; break; case 'o': outputs[0] = STDOUT_FILENO; break;
case 'e': outputs[1] = STDERR_FILENO; break; case 'e': outputs[1] = STDERR_FILENO; break;
case 'p': case 'p':
printf("not implemented\n"); return;
if(pipe(p) != 0){ if(pipe(p) != 0){
fprintf(stderr, "%s: %s\n", fprintf(stderr, "%s: %s\n",
argv[0] == NULL argv[0], strerror(errno));
? program_name
: argv[0],
strerror(errno));
return EX_OSERR; return EX_OSERR;
}else }else
outputs[2] = p[1]; outputs[2] = p[1];
break; break;
default: default: goto usage;
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE;
} }
if(outputs[2] != 0){ if((argc > optind && outputs[2] == 0)
switch(fork()){ || (!(argc > optind) && outputs[2] != 0)){
case -1: usage: fprintf(stderr, "Usage: %s (-1eno)"
fprintf(stderr, "%s: %s\n", argv[0], strerror(errno)); " (-p [program [arguments...]])\n",
return EX_OSERR; argv[0] == NULL ? program_name : argv[0]);
case 0: return EX_USAGE;
if(dup2(p[0], STDIN_FILENO) != STDIN_FILENO){
fprintf(stderr, "%s: %s\n",
argv[0] == NULL
? program_name
: argv[0],
strerror(errno));
return EX_OSERR;
}
/* exec */
}
} }
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:
goto die;
default:
if(close(p[0]) != 0)
goto die;
}
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] != 0 if(outputs[i] != 0
&& write(outputs[i], &c, 1) && write(outputs[i], &c, 1)
!= 1) == -1){
{/* errno */} if(i == 2)
close(outputs[i]);
outputs[i] = 0;
}
}while(c != eof); }while(c != eof);
tcgetattr(STDIN_FILENO, &t); tcgetattr(STDIN_FILENO, &t);
@ -85,4 +85,7 @@ int main(int argc, char *argv[]){
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_USAGE;
} }