1
0

more02.c 1.8 KB

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