1
0

sunday work

This commit is contained in:
dtb 2023-12-10 22:08:41 -07:00
parent b971bb1a40
commit 952aa9cfee

View File

@ -1,11 +1,13 @@
/* #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 */
#if !defined EX_OK || !defined EX_USAGE #include <string.h> /* strerror(3) */
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
# 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> /* getopt(3), write(2), STDERR_FILENO, STDIN_FILENO */ #include <unistd.h> /* fork(2), getopt(3), pipe(2), write(2), STDERR_FILENO,
* STDOUT_FILENO */
static char *program_name = "peek"; static char *program_name = "peek";
@ -14,43 +16,68 @@ int main(int argc, char *argv[]){
int eof; int eof;
size_t i; size_t i;
char include_eof; char include_eof;
size_t j; int outputs[] = {0 /* stdout */, 0 /* stderr */, 0 /* -p */};
int outputs[] = {0 /* stdout */, 0 /* stderr */, 0 /* -p */, 0}; int p[2] = {0, 0};
struct termios t; struct termios t;
/* disable terminal echo */ /* terminal echo */
tcgetattr(STDIN_FILENO, &t); tcgetattr(STDIN_FILENO, &t);
t.c_lflag ^= ECHO; t.c_lflag ^= ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
if(argc < 1)
argv[0] = program_name;
eof = EOF; eof = EOF;
include_eof = 0; include_eof = 0;
to_stderr = 0;
to_stdout = 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] = STDIN_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': printf("not yet implemented\n"); case 'p':
printf("not implemented\n"); return;
if(pipe(p) != 0){
fprintf(stderr, "%s: %s\n",
argv[0] == NULL
? program_name
: argv[0],
strerror(errno));
return EX_OSERR;
}else
outputs[2] = p[1];
break;
default: default:
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n", fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
argv[0] == NULL ? program_name : argv[0]); argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE; return EX_USAGE;
} }
/* permute non-zero outputs to front */ if(outputs[2] != 0){
for(i = 0; i < (sizeof outputs) / (sizeof *outputs) - 1; ++i) switch(fork()){
if(outputs[i] == 0) case -1:
for(j = 1; j < (sizeof outputs) / (sizeof *outputs); fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
++j) return EX_OSERR;
outputs[j - 1] = outputs[j]; case 0:
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 */
}
}
do{ if((c = getc(stdin)) != eof || include_eof) do{ if((c = getc(stdin)) != eof || include_eof)
for(i = 0; outputs[i] != 0; ++i); for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i)
if(write(outputs[i], &c, 1) != 1) if(outputs[i] != 0
{/* errro */} && write(outputs[i], &c, 1)
!= 1)
{/* errno */}
}while(c != eof); }while(c != eof);
tcgetattr(STDIN_FILENO, &t); tcgetattr(STDIN_FILENO, &t);