peek(1): strip down to bare essentials, rewrite man page to match
This commit is contained in:
72
src/peek.c
72
src/peek.c
@@ -17,75 +17,66 @@
|
||||
*/
|
||||
|
||||
#include <signal.h> /* sigaction(2), signal(2), struct sigaction, SIGINT */
|
||||
#include <stdio.h> /* fclose(3), fdopen(3), fprintf(3), getc(3), perror(3),
|
||||
* putc(3), stderr, stdin, stdout, EOF, NULL */
|
||||
#include <stdlib.h> /* exit(3), size_t, EXIT_FAILURE */
|
||||
#include <string.h> /* strerror(3) */
|
||||
#include <stdio.h> /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin,
|
||||
* stdout, EOF, NULL */
|
||||
#include <stdlib.h> /* exit(3), EXIT_FAILURE */
|
||||
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
|
||||
#include <unistd.h> /* getopt(3), isatty(3), STDIN_FILENO */
|
||||
|
||||
static int oserr(char *s){ perror(s); return EX_OSERR; }
|
||||
|
||||
static char *program_name = "peek";
|
||||
|
||||
static int usage(char *s){
|
||||
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n",
|
||||
s == NULL ? program_name : s);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
/* Restores terminal echo; otherwise when a user ^Cs the terminal would
|
||||
* continue to not display typed text. If sig isn't zero, this will terminate
|
||||
* the program. */
|
||||
void restore_echo(int sig){
|
||||
static void restore_echo(int sig){
|
||||
static struct termios t;
|
||||
|
||||
tcgetattr(STDIN_FILENO, &t);
|
||||
t.c_lflag |= ECHO;
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||
|
||||
/* If, for whatever ungodly reason, exit(3) returns, the user will notice
|
||||
* their typed characters on the screen. */
|
||||
if(sig != 0)
|
||||
exit(EXIT_FAILURE);
|
||||
else
|
||||
return;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int oserr(char *s){ perror(s); return EX_OSERR; }
|
||||
static int ioerr(char *s){ perror(s); restore_echo(0); return EX_IOERR; }
|
||||
static int usage(char *s){
|
||||
fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n", s);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int eof;
|
||||
char include_eof;
|
||||
FILE *outputs[] = {
|
||||
NULL /* stdout */,
|
||||
NULL /* stderr */
|
||||
};
|
||||
|
||||
if(argc < 1)
|
||||
return usage(argv[0]);
|
||||
return usage(program_name);
|
||||
|
||||
eof = EOF;
|
||||
include_eof = 0;
|
||||
{ /* options parsing */
|
||||
char allow_nonterminals;
|
||||
int c;
|
||||
|
||||
while((c = getopt(argc, argv, "1enot")) != -1)
|
||||
allow_nonterminals = 0;
|
||||
while((c = getopt(argc, argv, "i")) != -1)
|
||||
switch(c){
|
||||
case '1': eof = '\n'; break;
|
||||
case 'n': include_eof = 1; break;
|
||||
case 'o': outputs[0] = stdout; break;
|
||||
case 'e': outputs[1] = stderr; break;
|
||||
case 't':
|
||||
if(isatty(STDIN_FILENO) != 1){
|
||||
fprintf(stderr, "%s: Must be run in a terminal"
|
||||
" (option -t specified)\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
case 'i': allow_nonterminals = 1; break;
|
||||
default: return usage(argv[0]);
|
||||
}
|
||||
|
||||
if(argc > optind)
|
||||
return usage(argv[0]);
|
||||
|
||||
if(!allow_nonterminals && isatty(STDIN_FILENO) != 1){
|
||||
fprintf(stderr, "%s: Must be run in a terminal"
|
||||
" (option -i skips this check)\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
{ /* install signal handler */
|
||||
@@ -116,15 +107,10 @@ int main(int argc, char *argv[]){
|
||||
|
||||
{ /* actual input loop */
|
||||
int c;
|
||||
size_t i;
|
||||
|
||||
do{ if((c = getc(stdin)) != eof || include_eof)
|
||||
for(i = 0; i < (sizeof outputs)/(sizeof *outputs); ++i)
|
||||
if(outputs[i] != NULL && putc(c, outputs[i]) == EOF){
|
||||
perror(argv[0]);
|
||||
outputs[i] = NULL;
|
||||
}
|
||||
}while(c != eof);
|
||||
while((c = getc(stdin)) != EOF)
|
||||
if(putc(c, stdout) == EOF)
|
||||
return ioerr(argv[0]);
|
||||
}
|
||||
|
||||
restore_echo(0);
|
||||
|
||||
Reference in New Issue
Block a user