-
Notifications
You must be signed in to change notification settings - Fork 2
/
sx_more.c
178 lines (122 loc) · 2.96 KB
/
sx_more.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* sx_more.c
MORE for Samarux.
Console output pager (filter).
Copyright (c) 2007 - 2015 Miguel I. Garcia Lopez.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Usage:
more [-num] [file ...]
File - refers to standard input.
Examples:
more
more myfile.doc
more -24 file.txt letter.doc
Changes:
30 Dec 2014 : 1.00 : 1st version.
31 Dec 2014 : 1.01 : Minor changes in console output.
02 Jan 2015 : 1.02 : Prints banner if there is more than one file to be displayed.
03 Jan 2015 : 1.03 : Gets tty size from Samarux variables.
26 Feb 2015 : 1.04 : Gets tty size from TermRow() and TermCol().
31 Mar 2015 : 1.05 : Change RETURN CODE behaviour.
29 Nov 2015 : 1.06 : Added support for builtin / external.
*/
/* Built-in or external
--------------------
*/
#ifdef SX_SAMARUX
#define SX_MORE
#else
#include "samarux.h"
#define MoreMain main
#endif
MoreMain(argc, argv)
int argc, argv[];
{
int i, argi, rows, cols, code;
char *p, *buf;
/* Defaults */
argi = 1; /* First file name */
rows = TermRows(); /* Screen rows */
cols = TermCols(); /* Screen columns */
/* Check arguments */
if(argc > 1)
{
p = argv[1];
if(*p == '-' && isdigit(p[1]))
{
if((rows = atoi(p + 1)) < 8)
return Error("Bad number");
++argi;
}
}
/* Buffer */
if((buf = malloc(cols)) == NULL)
return ErrorMem();
/* Process */
if(argi == argc)
code = MoreOut("-", rows, cols, buf, 0, 1);
else
{
for(i = argi; i < argc; ++i)
{
if((code = MoreOut(argv[i], rows, cols, buf, argi == argc - 1 ? 0 : 1, i == argc - 1 ? 1 : 0)))
break;
}
}
/* Free buffer */
free(buf);
/* Return success or failure */
return code;
}
MoreOut(fn, rows, cols, buf, banner, last)
char *fn; int rows, cols; char *buf; int banner, last;
{
FILE *fp; int k, lin, q;
/* Open */
if(*fn == '-' && !fn[1])
fp = stdin;
else if((fp = fopen(fn, "r")) == NULL)
return ErrorOpen();
lin = q = 0;
if(banner)
{
printf("-- File: %s --\n", fn); ++lin;
}
/* Print */
while(fgets(buf, cols, fp) != NULL)
{
putstr(buf);
k = strlen(buf);
if(buf[k - 1] != '\n')
putchar('\n');
if(++lin == rows - 1)
{
if((q = MorePause()))
break;
lin = 0;
}
}
/* Close */
if(fp != stdin)
fclose(fp);
if(banner && lin && !q && !last)
MorePause();
return 0;
}
MorePause()
{
int k;
putstr("-- More (q:quit) --");
k = getch();
putchar('\n');
return k == 'q';
}