1
0

minimum implementation

This commit is contained in:
dtb 2023-12-08 13:56:49 -07:00
parent b776121952
commit 81cc1ef31b
2 changed files with 54 additions and 0 deletions

1
wip/peek/Makefile Normal file
View File

@ -0,0 +1 @@
peek: peek.c

53
wip/peek/peek.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h> /* fprintf(3), getc(3), putc(3), EOF */
#if !defined EX_OK || !defined EX_USAGE
# include <sysexits.h>
#endif
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
#include <unistd.h> /* getopt(3), STDIN_FILENO */
static char *program_name = "peek";
int main(int argc, char *argv[]){
int c;
int eof;
char include_eof;
char to_stderr;
char to_stdout;
struct termios t;
/* disable terminal echo */
tcgetattr(STDIN_FILENO, &t);
t.c_lflag &= ~(ECHO);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
eof = EOF;
include_eof = 0;
to_stderr = 0;
to_stdout = 0;
while((c = getopt(argc, argv, "1enop:")) != -1)
switch(c){
case '1': eof = '\n'; break;
case 'e': to_stderr = 1; break;
case 'n': include_eof = 1; break;
case 'o': to_stdout = 1; break;
default:
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE;
}
while((c = getc(stdin)) != eof){
if(to_stdout && putc(c, stdout) != c)
return EX_OK; /* broken pipe */
if(to_stderr)
putc(c, stderr);
}
if(include_eof && to_stdout)
putc(c, stdout);
if(include_eof && to_stderr)
putc(c, stderr);
return EX_OK;
}