home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / MORE.CMM < prev    next >
Text File  |  1995-12-04  |  2KB  |  92 lines

  1. /*
  2.  * More.cmm
  3.  *
  4.  * Display a file, pausing after each screenful.
  5.  */
  6.  
  7. #include "netware.lib"
  8.  
  9. usage()
  10. {
  11.   printf("Use the MORE command to list the contents of a file.\n");
  12.   printf("Syntax:\n");
  13.   printf("  MORE [drive:][path]filename\n");
  14.   printf("where:\n");
  15.   printf("  drive:/path/filename   Specifies the file to display.\n");
  16.   exit(EXIT_FAILURE);
  17. }
  18.  
  19. main(argc,argv)
  20. {
  21.   if( argc>2 ) usage();
  22.   if( !strcmp(argv[1],"/?") ) usage();
  23.  
  24.   height = ScreenSize().row;
  25.   width = ScreenSize().col;
  26.  
  27.   if( argc==1 )
  28.     {
  29.       fp = stdin;
  30.       total = 0;
  31.     } else {
  32.       fp = fopen(argv[1],"r");
  33.       if( fp==NULL )
  34.         {
  35.           printf("No such file \"%s\".\n",argv[1]);
  36.           exit(EXIT_FAILURE);
  37.         }
  38.       fseek(fp,0,SEEK_END);
  39.       total = ftell(fp);
  40.       fseek(fp,0,SEEK_SET);
  41.     }
  42.  
  43.   mored = 0;
  44.   die = 0;
  45.   line = 0;
  46.   buf = ""; SetArraySpan(buf,256);
  47.   while( 1 )
  48.     {
  49.       if( fgets(buf,256,fp)==NULL )
  50.         {
  51.           die = 1;
  52.         } else {
  53.           if( buf[0]=='\f' && buf[1]=='\n' && buf[2]=='\0' )
  54.             {
  55.               do { printf("\n"); } while( ++line<height-1 );
  56.             } else {
  57.               printf("%s",buf);
  58.               line += 1+(strlen(buf)-1)/width;
  59.             }
  60.           mored = 0;
  61.         }
  62.       if( !mored && (line>=height-1 || die) )
  63.     {
  64.           mored = 1;
  65.           if( defined(_UNIX_) ) printf("\x1b[7m");
  66.           if( total )
  67.             {
  68.               printf("--MORE--(%d%%)",100 * ftell(fp)/total);
  69.             } else {
  70.               printf("--MORE--");
  71.             }
  72.           if( defined(_UNIX_) ) printf("\x1b[0m");
  73.       ch = getch();
  74.       printf("\r               \r");
  75.       if( toupper(ch)=='Q' ) break;
  76.       if( ch==' ' || ch=='\r' || ch=='\n' )
  77.         {
  78. // Why set the line to 1 you ask? Well, we want to print 1 less than the
  79. // maximum number of lines. This keeps the bottom line on the screen and
  80. // gives the reader some continuity. It's a little thing, but it matters...
  81.           if( ch==' ' )
  82.         line = 1;
  83.           else
  84.         line--;
  85.         }
  86.     }
  87.       if( die ) break;
  88.     }
  89.  
  90.   if( argc!=1 ) fclose(fp);
  91. }
  92.