home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / csh4.zip / MORE.C < prev    next >
C/C++ Source or Header  |  1985-09-03  |  4KB  |  238 lines

  1. #include <stdio.h>
  2. #include <sgtty.h>
  3. #include <debug.h>
  4. #include <ctype.h>
  5. #include <setjmp.h>
  6. #include <signal.h>
  7. void (*signal())();
  8. void (*moresig)();
  9.  
  10. long int size, position;
  11. char filename[30];
  12. char isaconsole = '\0';
  13. char isapipe = '\0';
  14. int tabsize = 8;
  15. #define STDIN 0
  16. #define STDOUT 1
  17. #define STDERR 2
  18. #define ESC '\033'
  19. void (*output)();
  20. extern int scr_putc();
  21. std_out(c)
  22. {
  23.     write(1,&c,1);
  24. }
  25. jmp_buf moreenv;
  26. moreintr()
  27. {
  28.     signal(SIGINT,moresig);
  29.     longjmp(moreenv,-1);
  30. }
  31.  
  32. more(argc,argv)
  33.     int argc;
  34.     char *argv[];
  35. {
  36.     FILE *fp, *fopen();
  37.     long ftell();
  38.     long int i;
  39.     long int fsize();
  40.  
  41.     if (-1 == setjmp(moreenv))
  42.     {
  43.         write(2,"Interrupted\r\n",13);
  44.         fclose(fp);
  45.         return -1;
  46.     }
  47.  
  48.     moresig = signal(SIGINT,moreintr);
  49.     isaconsole = isatty(STDOUT);
  50.     isapipe = !isatty(STDIN);
  51.     if (isaconsole)
  52.     {
  53.         output = scr_putc;
  54.         scr_echo(0);
  55.     }
  56.     else
  57.         output = std_out;
  58.     if ( (*(++argv))[0] == '-' && isdigit((*argv)[1]) )
  59.     {
  60.         tabsize = atoi( (*argv+1) );
  61.         --argc;
  62.     } else
  63.     {
  64.         --argv;
  65.     }
  66.     if (argc == 1)
  67.     {
  68.         if (NULL == (fp = fdopen(0,"r")))
  69.         {
  70.             return -1;
  71.         }
  72.         display(fp);
  73.         crlf();
  74.         return 0;
  75.     }
  76.     while(--argc) 
  77.     {
  78.         if (NULL == (fp = fopen(*(++argv),"r")) )
  79.         {
  80.             fprintf(stderr,"more - can't open %s\n",*argv);
  81.             continue;
  82.         }
  83.         strncpy(filename,*argv,30);
  84.         if (filename[29])
  85.             filename[29] = '\0';
  86.         if (!isapipe)
  87.             size = fsize(fp);
  88.         position = 0;
  89.         if (-2 == display(fp))
  90.             argc = 0;        /* force completion of command */
  91.         fclose(fp);
  92.     }
  93. bugout:
  94.     if (isaconsole)
  95.         scr_echo(0);
  96.     crlf();
  97.     tabsize = 8;
  98.     isaconsole = isapipe = '\0';
  99.     signal(SIGINT,moresig);
  100.     return 0;
  101. }
  102.  
  103. long fsize(fp) 
  104.     FILE *fp;
  105. {
  106.     long ftell();
  107.     long position, last;
  108.     position = ftell(fp);
  109.     if (-1 == fseek(fp,0L,2))
  110.     {
  111.         fprintf(stderr,"more - error on fseek\n");
  112.     }
  113.     last = (ftell(fp));
  114.     fseek(fp,position,0);
  115.     return last;
  116. }
  117. #define LBUFSIZE 160
  118. char linebuffer[LBUFSIZE];
  119. int lines;
  120.  
  121. display(fp)
  122.     FILE *fp;
  123. {
  124.     FILE *fgets();
  125.     long ftell();
  126.     char c;
  127.     lines = 1;
  128.     while ( NULL != fgets(linebuffer,LBUFSIZE,fp))
  129.     {
  130.         if (isaconsole)
  131.         {
  132.             if (lines == 1)        /* top of display */
  133.             {
  134.                 scr_clear();
  135.             }
  136.         }
  137.         lines += localputs(linebuffer);
  138.         if (isaconsole)
  139.         {
  140.             if (lines >=  24)        /* bottome of display */
  141.             {
  142.                 char tst;
  143.                 position = ftell(fp);
  144.                 scr_curs(24,0);
  145.                 if (!isapipe)
  146.                     scr_printf(
  147.                 "%s - %ld bytes - %d%% displayed - <ESC> = skip to next file",
  148.                     filename,size,percent(position,size) );
  149.                 else
  150.                     scr_printf("-more-");
  151.                 switch (scr_getc())
  152.                 {
  153.                 case ESC :
  154.                     return 0;
  155.                 case 3 :
  156.                     return -2;
  157.                 default:
  158.                     break;
  159.                 }
  160.  
  161.                 lines = 1;
  162.             }
  163.         }
  164.     }
  165.     if (isaconsole)
  166.     {
  167.         if (lines != 1)        /* bottome of display */
  168.         {
  169.  
  170.             scr_curs(24,0);
  171.             if (!isapipe)
  172.             {
  173.                 position = ftell(fp);
  174.                 scr_printf("%s - %ld characters - %d%% displayed",
  175.                     filename,size,percent(position,size) );
  176.             }
  177.             else
  178.                 scr_printf("-done-");
  179.             scr_getc();
  180.             lines = 1;
  181.         }
  182.     }
  183. }
  184.  
  185. percent(x,y)
  186.     long int x,y;
  187. {    /* returns integer percentage of x into y */
  188.     float xf,yf;
  189.     xf = x; yf = y;
  190.     x = ((xf/yf)*100);
  191.     return (x);
  192. }
  193.  
  194. localputs(lb)
  195.     register char *lb;
  196. {
  197.     int lines, pos, tabstop;
  198.     lines = 1;
  199.     pos = 0;
  200.     while (*lb)
  201.     {
  202.         switch (*lb)
  203.         {
  204.         case '\t':
  205.             tabstop = pos + (tabsize - (pos % tabsize));    
  206.             for (;pos <= tabstop; pos++)
  207.                 (*output)(' ');
  208.             break;
  209.         case '\n':
  210.             (*output)('\r');
  211.         default:
  212.             (*output)(*lb);
  213.             pos++;
  214.         }
  215.         if (pos == 79)
  216.         {
  217.             pos = 1;
  218.             (*output)('\r');
  219.             (*output)('\n');
  220.             ++lines;
  221.         } else if (pos > 79)
  222.         {
  223.             pos -= 80;
  224.             ++lines;
  225.         }
  226.         ++lb;
  227.     }
  228.     return lines;
  229. }
  230.  
  231. scr_printf(fmt,args)
  232. char *fmt; unsigned args;
  233. {
  234.     extern int scr_putc();
  235.  
  236.     format(scr_putc,fmt,&args);
  237. }
  238.