more03.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include <string.h>
  8. #define PAGELEN 24
  9. #define LINELEN 512
  10. void do_more(FILE *);
  11. int see_more(FILE *);
  12. int main( int ac , char *av[] )
  13. {
  14. char flag[] = "-o";
  15. char *outname;
  16. char *inname;
  17. int c;
  18. FILE *fp;
  19. FILE *outfile;
  20. if ( ac == 4 )
  21. {
  22. for ( int i = 0; i < ac; i++)
  23. {
  24. if ( ! strcmp (av[i], flag) )
  25. {
  26. outname = av[i+1];
  27. printf ("%s\n", outname);
  28. if ( i == 1 )
  29. {
  30. inname = av[3];
  31. }
  32. if ( i == 2 )
  33. {
  34. inname = av[1];
  35. }
  36. printf ("%s\n", inname);
  37. }
  38. }
  39. fp = fopen (inname, "r");
  40. outfile = fopen (outname, "w");
  41. while ( (c = fgetc (fp) ) != EOF )
  42. {
  43. fputc( c, outfile );
  44. }
  45. fclose (outfile);
  46. fclose (fp);
  47. printf ("%s\n", "here");
  48. fp = fopen (inname, "r");
  49. do_more (fp);
  50. fclose (fp);
  51. printf ("%s\n", "here1");
  52. }
  53. if ( ac == 1 )
  54. {
  55. do_more( stdin );
  56. }
  57. if ( ac == 2 )
  58. {
  59. while ( --ac )
  60. if ( (fp = fopen( *++av , "r" )) != NULL )
  61. {
  62. do_more( fp );
  63. fclose( fp );
  64. }
  65. else
  66. exit(1);
  67. }
  68. return 0;
  69. }
  70. void do_more( FILE *fp )
  71. /*
  72. * read PAGELEN lines, then call see_more() for further instructions
  73. */
  74. {
  75. char line[LINELEN];
  76. int num_of_lines = 0;
  77. int see_more(FILE *), reply;
  78. FILE *fp_tty;
  79. fp_tty = fopen( "/dev/tty", "r" ); /* NEW: cmd stream */
  80. if ( fp_tty == NULL ) /* if open fails */
  81. exit(1); /* no use in running */
  82. while ( fgets( line, LINELEN, fp ) ){ /* more input */
  83. if ( num_of_lines == PAGELEN ) { /* full screen? */
  84. reply = see_more(fp_tty); /* NEW: pass FILE * */
  85. if ( reply == 0 ) /* n: done */
  86. break;
  87. num_of_lines -= reply; /* reset count */
  88. }
  89. if ( fputs( line, stdout ) == EOF ) /* show line */
  90. exit(1); /* or die */
  91. num_of_lines++; /* count it */
  92. }
  93. }
  94. int see_more(FILE *cmd) /* NEW: accepts arg */
  95. /*
  96. * print message, wait for response, return # of lines to advance
  97. * q means no, space means yes, CR means one line
  98. */
  99. {
  100. int c;
  101. printf("\033[7m more? \033[m"); /* reverse on a vt100 */
  102. while( (c=getc(cmd)) != EOF ) /* NEW: reads from tty */
  103. {
  104. if ( c == 'q' ) /* q -> N */
  105. return 0;
  106. if ( c == ' ' ) /* ' ' => next page */
  107. return PAGELEN; /* how many to show */
  108. if ( c == '\n' ) /* Enter key => 1 line */
  109. return 1;
  110. }
  111. return 0;
  112. }