p(1)
This commit is contained in:
parent
a256b92ce8
commit
ccd46ce2a7
2
p/Makefile
Normal file
2
p/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
p: p.c
|
||||
$(CC) $(CFLAGS) -o p p.c
|
56
p/p.c
Normal file
56
p/p.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined EX_USAGE || !defined EX_SOFTWARE
|
||||
# include <sysexits.h>
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user