more01.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* more01.c - version 0.1 of more
  2. * read and print 24 lines then pause for a few special commands
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define PAGELEN 24
  7. #define LINELEN 512
  8. void do_more(FILE *);
  9. int see_more();
  10. int main( int ac , char *av[] )
  11. {
  12. FILE *fp;
  13. if ( ac == 1 )
  14. do_more( stdin );
  15. else
  16. while ( --ac )
  17. if ( (fp = fopen( *++av , "r" )) != NULL )
  18. {
  19. do_more( fp ) ;
  20. fclose( fp );
  21. }
  22. else
  23. exit(1);
  24. return 0;
  25. }
  26. void do_more( FILE *fp )
  27. /*
  28. * read PAGELEN lines, then call see_more() for further instructions
  29. */
  30. {
  31. char line[LINELEN];
  32. int num_of_lines = 0;
  33. int see_more(), reply;
  34. while ( fgets( line, LINELEN, fp ) ){ /* more input */
  35. if ( num_of_lines == PAGELEN ) { /* full screen? */
  36. reply = see_more(); /* y: ask user */
  37. if ( reply == 0 ) /* n: done */
  38. break;
  39. num_of_lines -= reply; /* reset count */
  40. }
  41. if ( fputs( line, stdout ) == EOF ) /* show line */
  42. exit(1); /* or die */
  43. num_of_lines++; /* count it */
  44. }
  45. }
  46. int see_more()
  47. /*
  48. * print message, wait for response, return # of lines to advance
  49. * q means no, space means yes, CR means one line
  50. */
  51. {
  52. int c;
  53. printf("\033[7m more? \033[m"); /* reverse on a vt100 */
  54. while( (c=getchar()) != EOF ) /* get response */
  55. {
  56. if ( c == 'q' ) /* q -> N */
  57. return 0;
  58. if ( c == ' ' ) /* ' ' => next page */
  59. return PAGELEN; /* how many to show */
  60. if ( c == '\n' ) /* Enter key => 1 line */
  61. return 1;
  62. }
  63. return 0;
  64. }