home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / GopherTools / securegopher / more / more.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-05  |  1.8 KB  |  119 lines

  1. /* more program by therat@ucscb.ucsc.edu */
  2.  
  3. #include <stdio.h>
  4. #include <sys/file.h>
  5. #include <sgtty.h>
  6.  
  7. #define ZMORE 0
  8.  
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char **argv;
  13. {
  14.  int c=1;
  15.  
  16.     while (c < argc)
  17.         doc(argv[c++]);
  18. }
  19.  
  20. doc(s)
  21. char *s;
  22. {
  23.     char filename[128], tmpfile[128];
  24.     int fd, pid;
  25.     long t;
  26.  
  27.     sprintf(filename, "%s", s);
  28.     /* could stat the file here to make sure it's there */
  29.  
  30.     time(&t);
  31.     sprintf(tmpfile, "/usr/tmp/#GBtrash%05d", (t >> 4) & 0x7fff);
  32.     fd = open(tmpfile, O_CREAT | O_WRONLY | O_TRUNC, 0600);
  33.     if (fd < 0) {
  34.         fprintf(stderr, "doc(): %s: ", filename);
  35.         perror("open failed");
  36.         return;
  37.     }
  38.  
  39.     switch (pid = fork()) {
  40.         case -1 :
  41.             perror("doc(): fork for zcat failed");
  42.             break;
  43.  
  44.         case 0 :
  45.             dup2(fd, 1);
  46. #            if (ZMORE)
  47.               execl("/usr/ucb/zcat", "zcat", filename, 0);
  48.               perror("doc(): execl for zcat failed");
  49. #            else
  50.               execl("/bin/cat", "cat", filename, 0);
  51. #            endif
  52.  
  53.         default :
  54.             while (wait(0) != pid)
  55.                 ;
  56.     }
  57.  
  58.     close(fd);
  59.     page(tmpfile);
  60.     unlink(tmpfile);
  61. }
  62.  
  63. page(file)
  64. char *file;
  65. {
  66.     char buf[4096];
  67.     FILE *fp;
  68.     int line;
  69.  
  70.     fp = fopen(file, "r");
  71.     if (!fp) {
  72.         fprintf(stderr, "page(): %s: ", file);
  73.         perror("open failed");
  74.         return;
  75.     }
  76.  
  77.     while (fgets(buf, sizeof(buf), fp)) {
  78.         ++line;
  79.         fputs(buf, stdout);
  80.         if (line == 23) {
  81.             char ch;
  82.  
  83.             fputs(" -- Hit SPACEBAR for more -- ", stdout);
  84.             fflush(stdout);
  85.             do
  86.                 ch = get_a_char();
  87.             while (ch != ' ' && ch!='q');
  88.             fputs("\b\b\b\b\b\b\b\b\b\b\b\b            \b\b\b\b\b\b\b\b\b\b\b\b", stdout);
  89.             if (ch=='q') {
  90.                  fclose (fp);
  91.                 return;
  92.             }
  93.             line = 0;
  94.         }
  95.     }
  96.     fclose(fp);
  97. }
  98.  
  99. get_a_char()
  100. {
  101.     struct sgttyb foo, bar;
  102.     char c;
  103.  
  104.     ioctl(0, TIOCGETP, &foo);
  105.     bar = foo;
  106.     foo.sg_flags |= CBREAK;
  107.     foo.sg_flags &= ~ECHO;
  108.     ioctl(0, TIOCSETN, &foo);
  109.  
  110.     c = getchar();
  111. /*     while (!read(0, &c, 1)) */
  112. /*         ; */
  113.  
  114.  
  115.     ioctl(0, TIOCSETN, &bar);
  116.  
  117.     return c;
  118. }
  119.