URI:
       tp.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tp.c (1494B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 
            5 #define        DEF        22        /* lines in chunk: 3*DEF == 66, #lines per nroff page */
            6 
            7 Biobuf *cons;
            8 Biobuf bout;
            9 
           10 int pglen = DEF;
           11 
           12 void printfile(int);
           13 
           14 void
           15 main(int argc, char *argv[])
           16 {
           17         int n;
           18         int f;
           19 
           20         if((cons = Bopen("/dev/tty", OREAD)) == 0) {
           21                 fprint(2, "p: can't open /dev/tty\n");
           22                 exits("missing /dev/tty");
           23         }
           24         Binit(&bout, 1, OWRITE);
           25         n = 0;
           26         while(argc > 1) {
           27                 --argc; argv++;
           28                 if(*argv[0] == '-'){
           29                         pglen = atoi(&argv[0][1]);
           30                         if(pglen <= 0)
           31                                 pglen = DEF;
           32                 } else {
           33                         n++;
           34                         f = open(argv[0], OREAD);
           35                         if(f < 0){
           36                                 fprint(2, "p: can't open %s\n", argv[0]);
           37                                 continue;
           38                         }
           39                         printfile(f);
           40                         close(f);
           41                 }
           42         }
           43         if(n == 0)
           44                 printfile(0);
           45         exits(0);
           46 }
           47 
           48 void
           49 printfile(int f)
           50 {
           51         int i, j, n;
           52         char *s, *cmd;
           53         Biobuf *b;
           54 
           55         b = malloc(sizeof(Biobuf));
           56         Binit(b, f, OREAD);
           57         for(;;){
           58                 for(i=1; i <= pglen; i++) {
           59                         s = Brdline(b, '\n');
           60                         if(s == 0){
           61                                 n = Blinelen(b);
           62                                 if(n > 0)        /* line too long for Brdline */
           63                                         for(j=0; j<n; j++)
           64                                                 Bputc(&bout, Bgetc(b));
           65                                 else{                /* true EOF */
           66                                         free(b);
           67                                         return;
           68                                 }
           69                         }else{
           70                                 Bwrite(&bout, s, Blinelen(b)-1);
           71                                 if(i < pglen)
           72                                         Bwrite(&bout, "\n", 1);
           73                         }
           74                 }
           75                 Bflush(&bout);
           76             getcmd:
           77                 cmd = Brdline(cons, '\n');
           78                 if(cmd == 0 || *cmd == 'q')
           79                         exits(0);
           80                 cmd[Blinelen(cons)-1] = 0;
           81                 if(*cmd == '!'){
           82                         if(fork() == 0){
           83                                 dup(Bfildes(cons), 0);
           84                                 execl("/bin/rc", "rc", "-c", cmd+1, 0);
           85                         }
           86                         waitpid();
           87                         goto getcmd;
           88                 }
           89         }
           90 }