From ccd46ce2a784fffb6e7280c00da1f8c4cd809f5b Mon Sep 17 00:00:00 2001 From: dtb Date: Sun, 15 Jan 2023 18:54:58 -0500 Subject: [PATCH] p(1) --- p/Makefile | 2 ++ p/p.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 p/Makefile create mode 100644 p/p.c diff --git a/p/Makefile b/p/Makefile new file mode 100644 index 0000000..a5c66ed --- /dev/null +++ b/p/Makefile @@ -0,0 +1,2 @@ +p: p.c + $(CC) $(CFLAGS) -o p p.c diff --git a/p/p.c b/p/p.c new file mode 100644 index 0000000..5cb02a1 --- /dev/null +++ b/p/p.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#if !defined EX_USAGE || !defined EX_SOFTWARE +# include +#endif + +/* Done while looking at plan9ports' src/cmd/p.c. + * Taken from plan9 are the interface (minus p(1)'s shell escaping and the + * ability to page multiple files) and the practice of reading /dev/tty to get + * keyboard input without going through stdin. Also, a newline is now printed + * before querying the keyboard. */ + +static size_t l = 22; /* plan9 default */ + +static char *program_name = "p"; + +static int print_lines(FILE *f, size_t l){ + int c; + + while((c = getc(f)) != EOF) + if((c = putc(c, stdout)) == EOF || (l -= (c == '\n')) == 0) + break; + + return c; +} + +int main(int argc, char *argv[]){ + unsigned char cmd[99+1]; + char *p; + FILE *t; + + if(argc == 0) + argv = &program_name; + + if(argc > 1){ + fprintf(stderr, "Usage: %s\n", argv[0]); + return EX_USAGE; + } + + if((t = fopen("/dev/tty", "rb")) == NULL){ + fprintf(stderr, "%s: Could not open /dev/tty\n", argv[0]); + return EX_SOFTWARE; + } + + while(print_lines(stdin, l) != EOF){ + fgets(cmd, (sizeof cmd) / (sizeof *cmd), t); + for(p = cmd; *p != '\0' && strchr(" \t", *p) != NULL; ++p); + if(feof(t) || (*p | 32) == 'q') + break; + } + +exit: fclose(t); + return 0; +}