1
0

saturday work

This commit is contained in:
dtb 2023-12-10 01:32:32 -07:00
parent 02353836ca
commit b971bb1a40

View File

@ -1,18 +1,21 @@
/* #include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), getc(3), putc(3), EOF */
#include <stdlib.h> /* size_t */
#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 */
#include <unistd.h> /* getopt(3), write(2), STDERR_FILENO, STDIN_FILENO */
static char *program_name = "peek";
int main(int argc, char *argv[]){
int c;
int eof;
size_t i;
char include_eof;
char to_stderr;
char to_stdout;
size_t j;
int outputs[] = {0 /* stdout */, 0 /* stderr */, 0 /* -p */, 0};
struct termios t;
/* disable terminal echo */
@ -26,10 +29,10 @@ int main(int argc, char *argv[]){
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;
case '1': eof = '\n'; break;
case 'n': include_eof = 1; break;
case 'o': outputs[0] = STDIN_FILENO; break;
case 'e': outputs[1] = STDERR_FILENO; break;
case 'p': printf("not yet implemented\n");
default:
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
@ -37,18 +40,18 @@ int main(int argc, char *argv[]){
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);
}
/* permute non-zero outputs to front */
for(i = 0; i < (sizeof outputs) / (sizeof *outputs) - 1; ++i)
if(outputs[i] == 0)
for(j = 1; j < (sizeof outputs) / (sizeof *outputs);
++j)
outputs[j - 1] = outputs[j];
if(include_eof && to_stdout)
putc(c, stdout);
if(include_eof && to_stderr)
putc(c, stderr);
do{ if((c = getc(stdin)) != eof || include_eof)
for(i = 0; outputs[i] != 0; ++i);
if(write(outputs[i], &c, 1) != 1)
{/* errro */}
}while(c != eof);
tcgetattr(STDIN_FILENO, &t);
t.c_lflag |= ECHO;