File tree Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Original file line number Diff line number Diff line change 1- // list directory
1+ // print content of file
22#include <stdio.h>
33#include <string.h>
44
Original file line number Diff line number Diff line change 1+ // buffered print content of file
2+ #include <stdio.h>
3+ #include <string.h>
4+
5+ // assumption
6+ #define SCREEN_WIDTH 0x50
7+ #define SCREEN_HEIGHT 0x19
8+
9+ void process (char filename []) {
10+ FILE * handler = fopen (filename , "r" );
11+ if (handler == NULL ) {
12+ printf ("failed to open '%s' file.\n" , filename );
13+ return ;
14+ }
15+ char buffer [SCREEN_WIDTH + 1 ];
16+ int count_before_buffer = SCREEN_HEIGHT - 2 ;
17+ while (1 ) {
18+ if (!fgets (buffer , sizeof (buffer ), handler )) {
19+ int err = ferror (handler );
20+ if (err ) {
21+ printf ("Error: failed to read next chunk, code %d\n" , err );
22+ exit (err );
23+ }
24+ break ;
25+ }
26+ puts (buffer );
27+ if (count_before_buffer > 0 ) {
28+ count_before_buffer -- ;
29+ } else {
30+ // buffer
31+ char c = getch ();
32+ if (c == 'q' || c == 'Q' ) {
33+ exit (0 );
34+ }
35+ // continue
36+ }
37+ }
38+ fclose (handler );
39+ }
40+
41+ int print_usage () {
42+ printf ("Usage: more <filename>\n" );
43+ printf (" > buffer will start when output takes over the whole screen\n" );
44+ printf (" > press 'q' to exit\n" );
45+ printf (" > press any other key to scroll down\n" );
46+ return 0 ;
47+ }
48+
49+ int main (int argc , char * argv []) {
50+ if (argc != 2 ) {
51+ return print_usage ();
52+ }
53+ char * filename = argv [1 ];
54+ process (filename );
55+ return 0 ;
56+ }
You can’t perform that action at this time.
0 commit comments