Repository navigation
Expand file tree
/
Copy pathpr.c
More file actions
executable file
·504 lines (445 loc) · 12 KB
/
Copy pathpr.c
File metadata and controls
executable file
·504 lines (445 loc) · 12 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* pr - print files Author: Michiel Huisjes */
/* Pr - print files
*
* Author: Michiel Huisjes.
* Modified: Jacob P. Bunschoten. (30 nov 87)
* When "columns" is not given and numbering is on:
* line numbers are correlated with input lines.
* (try pr [-1] -n file )
* tabs are accounted for.
* When numbering is turned on, width know this.
* automatic line-folding. -f to get the original program.
* backspaces are accounted for. -b to disable this.
* multi-column mode changed.
* header can be given and used.
* format changed may occur between printing of several files:
* pr -l30 file1 -w75 file2
*
* Modified: Rick Thomas. (Sept 12, 1988)
* added "-M" option to cover functionality of old "-n" option,
* and made "-n" option behavior compatible with system V.
*
* Usage: pr [+page] [-columns] [-h header] [-wwidth] [-llength] [-ntm] [files]
* -t : Do not print the 5 line header and trailer at the page.
* -n : Turn on line numbering.
* -M : Use "Minix" style line numbering -- Each page begins at
* a line number that is an even multiple of the page length.
* Like the listings in Appendix E of the book.
* +page : Start printing at page n.
* -columns : Print files in n-columns.
* -l length: Take the length of the page to be n instead of 66
* -h header: Take next argument as page header.
* -w width : Take the width of the page to be n instead of default 79
* -f : do not fold lines.
*
* Modified: Lars Fredriksen (Jan 19, 1990)
* fixed the program so that
* pr -n *.c
* would work. The clobal variable 'width' was decremented
* by NUM_WIDTH, for each file, resulting in width finally
* being so small that nothing was printed. Used the local
* variable 'w' for the width adjustment (in print())
*
* Modified: Kenneth J. Hendrickson (10 April 1991)
* date in header should be last modification date for files,
* and the current time for stdin.
*
* Modified: Kees J. Bot (5 October 1992)
* Use localtime(3) to get the date, it knows TZ.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define DEF_LENGTH 66
#define DEF_WIDTH 79
#define NUM_WIDTH 8
#define TAB_WIDTH 8 /* fixed tab_width */
/* Used to compute next (fixed) tabstop */
#define TO_TAB(x) (( (x) + TAB_WIDTH ) & ~07 )
typedef char BOOL;
#define FALSE 0
#define TRUE 1
/* EAT: eat rest of input line */
#define EAT(fp) while((c=getc(fp))!='\n' && c!=EOF)
/* L_BUF: calculate address of pointer to char (string) used in format */
#define L_BUF(i,j) * (char **) (line_buf + (i + j*length)*sizeof(char *))
char *header;
BOOL no_header;
BOOL number = FALSE;
BOOL minix_number = FALSE;
BOOL ext_header_set = FALSE; /* external header given */
BOOL back_space = TRUE; /* back space correction in line width */
BOOL dont_fold = FALSE; /* original. If the line does not fit eat it. */
short columns;
short cwidth;
short start_page = 1;
short width = DEF_WIDTH;
short length = DEF_LENGTH;
short linenr;
char *line_buf; /* used in format for multi-column output */
char output[1024];
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(static char *myalloc, (size_t size));
_PROTOTYPE(char skip_page, (int lines, int width, FILE * filep));
_PROTOTYPE(void format, (FILE * filep));
_PROTOTYPE(void print_page, (int pagenr, int maxcol));
_PROTOTYPE(void print, (FILE * filep));
_PROTOTYPE(void out_header, (int page));
_PROTOTYPE(void print_time, (time_t t));
int main(argc, argv)
int argc;
char *argv[];
{
FILE *file;
char *ptr;
int index = 1; /* index is one ahead of argc */
int line, col;
setbuf(stdout, output);
do {
if (argc == index) /* No arguments (left) */
goto pr_files;
ptr = argv[index++];
if (*ptr == '+') {
start_page = atoi(++ptr);
continue;
}
if (*ptr != '-') { /* no flags */
index--;
goto pr_files;
}
if (*++ptr >= '0' && *ptr <= '9') {
columns = atoi(ptr);
if (columns <= 0) columns = 1;
continue; /* Fetch next flag */
}
while (*ptr) switch (*ptr++) {
case 't': no_header = TRUE; break;
case 'n':
number = TRUE;
minix_number = FALSE;
break;
case 'M':
number = TRUE;
minix_number = TRUE;
break;
case 'h':
header = argv[index++];
ext_header_set = TRUE;
break;
case 'w':
if ((width = atoi(ptr)) <= 0) width = DEF_WIDTH;
*ptr = '\0';
break;
case 'l':
if ((length = atoi(ptr)) <= 0) length = DEF_LENGTH;
*ptr = '\0';
break;
case 'b': /* back_space correction off */
back_space = FALSE;
break;
case 'f': /* do not fold lines */
dont_fold = TRUE;
break;
default:
fprintf(stderr, "Usage: %s [+page] [-columns] [-h header] [-w<width>] [-l<length>] [-nMt] [files]\n", argv[0]);
exit(1);
}
continue; /* Scan for next flags */
/* ============== flags are read. Print the file(s) ========= */
pr_files:
if (!no_header) length -= 10;
if (columns) {
cwidth = width / columns + 1;
if (columns > width) {
fprintf(stderr, "Too many columns for page width.\n");
exit(1);
}
/* Allocate piece of mem to hold some pointers */
line_buf = myalloc(length * columns * sizeof(char *));
}
for (line = 0; line < length; line++)
for (col = 0; col < columns; col++)
L_BUF(line, col) = NULL;
if (length <= 0) {
fprintf(stderr, "Minimal length should be %d\n", no_header ?
1 : 11);
exit(1);
}
while (index <= argc) { /* print all files, including stdin */
if (index < argc && (*argv[index] == '-' || *argv[index] == '+'))
break; /* Format change */
if (argc == index) { /* no file specified, so stdin */
if (!ext_header_set) header = "";
file = stdin;
} else {
if ((file = fopen(argv[index], "r")) == (FILE *) 0) {
fprintf(stderr, "Cannot open %s\n", argv[index++]);
continue;
}
if (!ext_header_set) header = argv[index];
}
if (columns)
format(file);
else
print(file);
fclose(file);
if (++index >= argc)
break; /* all files (including stdin) done */
}
if (index >= argc) break;
/* When control comes here. format changes are to be done.
* reinitialize some variables */
if (!no_header) length += 10;
start_page = 1;
ext_header_set = FALSE;
if (columns) free(line_buf);
} while (index <= argc); /* "pr -l60" should work too */
(void) fflush(stdout);
return(0);
}
char skip_page(lines, width, filep)
int lines, width;
FILE *filep;
{
short c;
int char_cnt;
int w;
do {
w = width;
if (number) /* first lines are shorter */
if (!columns || /* called from print(file) */
!(lines % columns)) /* called from format(file) */
w -= NUM_WIDTH;
char_cnt = 0;
while ((c = getc(filep)) != '\n' && c != EOF && char_cnt < w) {
/* Calculate if this line is longer than "width (w)"
* characters */
if (c == '\b' && back_space) {
if (--char_cnt < 0) char_cnt = 0;
} else if (c == '\t')
char_cnt = TO_TAB(char_cnt);
else
char_cnt++;
}
if (dont_fold && c != '\n' && c != EOF) EAT(filep);
lines--;
if (c == '\n') linenr++;
} while (lines > 0 && c != EOF);
return c; /* last char read */
}
void format(filep)
FILE *filep;
{
char buf[512];
short c = '\0';
short index, lines, i;
short page_number = 0;
short maxcol = columns;
short wdth;
short line, col;
do {
/* Check printing of page */
page_number++;
if (page_number < start_page && c != EOF) {
c = (char) skip_page(columns * length, cwidth, filep);
continue;
}
if (c == EOF) return;
lines = columns * length;
for (line = 0; line < length; line++)
for (col = 0; col < columns; col++) {
if (L_BUF(line, col) != NULL)
free(L_BUF(line, col));
L_BUF(line, col) = (char *) NULL;
}
line = 0;
col = 0;
do {
index = 0;
wdth = cwidth - 1;
if (number && !col) /* need room for numbers */
wdth -= NUM_WIDTH;
/* Intermidiate colums are shortened by 1 char */
/* Last column not */
if (col + 1 == columns) wdth++;
for (i = 0; i < wdth - 1; i++) {
c = getc(filep);
if (c == '\n' || c == EOF) break;
if (c == '\b' && back_space) {
buf[index++] = '\b';
if (--i < 0) { /* just in case ... */
i = 0;
index = 0;
}
} else if (c == '\t') {
int cnt, max;
max = TO_TAB(i);
for (cnt = i; cnt < max; cnt++)
buf[index++] = ' ';
i = max - 1;
} else
buf[index++] = (char) c;
}
buf[index++] = '\0';
/* Collected enough chars (or eoln, or EOF) */
/* First char is EOF */
if (i == 0 && lines == columns * length && c == EOF) return;
/* Alloc mem to hold this (sub) string */
L_BUF(line, col) = myalloc(index * sizeof(char));
strcpy(L_BUF(line, col), buf);
line++;
line %= length;
if (line == 0) {
col++;
col %= columns;
}
if (dont_fold && c != '\n' && c != EOF) EAT(filep);
lines--; /* line ready for output */
if (c == EOF) {
maxcol = columns - lines / length;
}
} while (c != EOF && lines);
print_page(page_number, maxcol);
} while (c != EOF);
}
void print_page(pagenr, maxcol)
short pagenr, maxcol;
{
short pad, i, j;
short width;
char *p;
if (minix_number)
linenr = (pagenr - 1) * length + 1;
else
linenr = 1;
if (!no_header) out_header(pagenr);
for (i = 0; i < length; i++) {
for (j = 0; j < maxcol; j++) {
width = cwidth;
if (number && j == 0) { /* first columns */
printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
width -= NUM_WIDTH;
}
pad = 0;
if (p = (char *) L_BUF(i, j))
for (; pad < width - 1 && *p; pad++) putchar(*p++);<
AD78
/div>
if (j < maxcol - 1) while (pad++ < width - 1)
putchar(' ');
}
putchar('\n');
}
if (!no_header) printf("\n\n\n\n\n");
}
void print(filep)
FILE *filep;
{
short c = '\0';
short page_number = 0;
short lines;
short cnt;
short w = width;
BOOL pr_number = TRUE; /* only real lines are numbered, not folded
* parts */
linenr = 1;
if (number) w -= NUM_WIDTH;
do {
/* Check printing of page */
page_number++;
if (page_number < start_page && c != EOF) {
pr_number = FALSE;
c = skip_page(length, w, filep);
if (c == '\n') pr_number = TRUE;
continue;
}
if (c == EOF) return;
if (minix_number) linenr = (page_number - 1) * length + 1;
if (page_number == start_page) c = getc(filep);
/* Print the page */
lines = length;
while (lines && c != EOF) {
if (lines == length && !no_header) out_header(page_number);
if (number)
if (pr_number)
printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
else
printf("%7c ", ' '); /* 7 == NUM_WIDTH-1 */
pr_number = FALSE;
cnt = 0;
while (c != '\n' && c != EOF && cnt < w) {
if (c == '\t') {
int i, max;
max = TO_TAB(cnt);
for (i = cnt; i < max; i++) putchar(' ');
cnt = max - 1;
} else if (c == '\b' && back_space) {
putchar('\b');
cnt--;
} else
putchar(c);
c = getc(filep);
cnt++;
}
putchar('\n');
if (dont_fold && c != '\n' && c != EOF) EAT(filep);
lines--;
if (c == '\n') {
c = getc(filep);
pr_number = TRUE;
}
}
if (lines == length) /* We never printed anything on this
* page -- */
return; /* even the header, so dont try to fill it up */
if (!no_header) /* print the trailer -- 5 blank lines */
printf("\n\n\n\n\n");
} while (c != EOF);
/* Fill last page */
if (page_number >= start_page) {
while (lines--) putchar('\n');
}
}
static char *myalloc(size)
size_t size; /* How many bytes */
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "malloc returned NULL\n");
exit(1);
}
return(char *) ptr;
}
void out_header(page)
short page;
{
time_t t;
struct stat buf;
if (strlen(header)) {
stat(header, &buf); /* use last modify time for file */
t = buf.st_mtime;
} else
(void) time(&t); /* use current time for stdin */
print_time(t);
printf(" %s Page %d\n\n\n", header, page);
}
char *moname[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* Print the date. */
void print_time(t)
time_t t;
{
struct tm *tm;
tm = localtime(&t);
printf("\n\n%s %2d %2d:%02d %d",
moname[tm->tm_mon],
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
1900 + tm->tm_year
);
}