#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; }