saturday work
This commit is contained in:
parent
02353836ca
commit
b971bb1a40
@ -1,18 +1,21 @@
|
|||||||
|
/* #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 */
|
||||||
#if !defined EX_OK || !defined EX_USAGE
|
#if !defined EX_OK || !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), STDIN_FILENO */
|
#include <unistd.h> /* getopt(3), write(2), STDERR_FILENO, STDIN_FILENO */
|
||||||
|
|
||||||
static char *program_name = "peek";
|
static char *program_name = "peek";
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int c;
|
int c;
|
||||||
int eof;
|
int eof;
|
||||||
|
size_t i;
|
||||||
char include_eof;
|
char include_eof;
|
||||||
char to_stderr;
|
size_t j;
|
||||||
char to_stdout;
|
int outputs[] = {0 /* stdout */, 0 /* stderr */, 0 /* -p */, 0};
|
||||||
struct termios t;
|
struct termios t;
|
||||||
|
|
||||||
/* disable terminal echo */
|
/* disable terminal echo */
|
||||||
@ -27,9 +30,9 @@ int main(int argc, char *argv[]){
|
|||||||
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 'e': to_stderr = 1; break;
|
|
||||||
case 'n': include_eof = 1; break;
|
case 'n': include_eof = 1; break;
|
||||||
case 'o': to_stdout = 1; break;
|
case 'o': outputs[0] = STDIN_FILENO; break;
|
||||||
|
case 'e': outputs[1] = STDERR_FILENO; break;
|
||||||
case 'p': printf("not yet implemented\n");
|
case 'p': printf("not yet implemented\n");
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
|
fprintf(stderr, "Usage: %s (-1eno) (-p [program])\n",
|
||||||
@ -37,18 +40,18 @@ int main(int argc, char *argv[]){
|
|||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
while((c = getc(stdin)) != eof){
|
/* permute non-zero outputs to front */
|
||||||
if(to_stdout && putc(c, stdout) != c)
|
for(i = 0; i < (sizeof outputs) / (sizeof *outputs) - 1; ++i)
|
||||||
return EX_OK; /* broken pipe */
|
if(outputs[i] == 0)
|
||||||
if(to_stderr)
|
for(j = 1; j < (sizeof outputs) / (sizeof *outputs);
|
||||||
putc(c, stderr);
|
++j)
|
||||||
}
|
outputs[j - 1] = outputs[j];
|
||||||
|
|
||||||
if(include_eof && to_stdout)
|
do{ if((c = getc(stdin)) != eof || include_eof)
|
||||||
putc(c, stdout);
|
for(i = 0; outputs[i] != 0; ++i);
|
||||||
|
if(write(outputs[i], &c, 1) != 1)
|
||||||
if(include_eof && to_stderr)
|
{/* errro */}
|
||||||
putc(c, stderr);
|
}while(c != eof);
|
||||||
|
|
||||||
tcgetattr(STDIN_FILENO, &t);
|
tcgetattr(STDIN_FILENO, &t);
|
||||||
t.c_lflag |= ECHO;
|
t.c_lflag |= ECHO;
|
||||||
|
Loading…
Reference in New Issue
Block a user