-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaix-cat.c
543 lines (505 loc) · 13 KB
/
aix-cat.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
static char sccsid[] = "@(#)33 1.34 src/bos/usr/bin/cat/cat.c, cmdscan, bos41J, 9521A_all 5/23/95 17:07:32";
/*
* COMPONENT_NAME: (CMDSCAN) commands that scan files
*
* FUNCTIONS:
*
* ORIGINS: 3, 26, 27, 71
*
* This module contains IBM CONFIDENTIAL code. -- (IBM
* Confidential Restricted when combined with the aggregated
* modules for this product)
* SOURCE MATERIALS
* (C) COPYRIGHT International Business Machines Corp. 1989, 1994
* All Rights Reserved
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*
* Copyright 1976, Bell Telephone Laboratories, Inc.
*
* (c) Copyright 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC.
* ALL RIGHTS RESERVED
*
* OSF/1 1.1
*/
#define _ILS_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <errno.h>
#include <nl_types.h>
#include <wchar.h>
#include <ctype.h>
#include "cat_msg.h"
#define BUFSIZE BUFSIZ /* BUFSIZ buffer for all others */
#define MSGSTR(num,str) catgets(catd,MS_CAT,num,str) /*MSG*/
static nl_catd catd;
static char buffer[BUFSIZE];
static int silent = 0;
static int bflag = 0; /* Don't count blank lines. */
static int eflag = 0; /* Put $ at the end of lines */
static int nflag = 0; /* Number lines. */
static int sflag = 0; /* Squeeze blank lines to one*/
static int tflag = 0; /* display tabs as ^I. */
static int vflag = 0; /* display all control chars.*/
static int spaced = 0;
static int inline = 0;
static void copyopt();
static void copyopt_ascii();
static void copyopt_mb();
/*
* There are just a few changes to make cat faster under certain
* circumstances. The standard code (scat) is called, if the option
* are used or input to cat is stdin. Switching between standard
* and the fast code is done by 'main()'!
*/
/*
* NAME: fcat
*
* FUNCTION:
* Faster cat when no options are specified (except
* -q for quiet mode).
*
* RETURN VALUE DESCRIPTION:
*
* Exits with status 0 if successful, 2 if failure.
*/
static void
fcat(argc,argv)
int argc;
char **argv;
{
char *file;
register int cnt, fd, args;
int status = 0;
int dev, ino = -1;
struct stat statb;
int tmpI;
if (fstat((int)fileno(stdout), &statb) < 0) {
if (!silent)
fprintf(stderr, MSGSTR(ESTATOUT,"cat: cannot stat stdout\n"));
exit(status = 2);
}
statb.st_mode &= S_IFMT;
if (statb.st_mode != S_IFCHR && statb.st_mode != S_IFBLK) {
dev = statb.st_dev;
ino = statb.st_ino;
}
for (args = 1; args < argc; args++) {
if ((argv[args][0] == '-') && (argv[args][1] == 'q')) {
silent++;
continue;
}
file = argv[args];
if (((fd = open(file,O_RDONLY)) == -1) && (!silent)) {
fprintf(stderr,MSGSTR(EOPEN,"cat: cannot open %s\n"),file);
status = 2;
continue;
}
if (fstat(fd, &statb) < 0) {
if (!silent)
fprintf(stderr,MSGSTR(ESTAT,"cat: cannot stat %s\n"),file);
status = 2;
continue;
}
while ((cnt = read(fd, buffer, BUFSIZE)) > 0) {
if ( cnt != (tmpI = write(fileno(stdout), buffer, cnt)) ) {
if (!silent) {
fprintf(stderr,MSGSTR(EOUTPUT,"cat: output error\n"));
perror("");
}
status = 2;
break;
}
}
if (cnt<0) {
fputs("cat: ",stderr);
perror(file);
status = 2;
/* we need to close the input file, so fall through */
}
if ((close(fd) != 0) && (!silent)) {
fprintf(stderr,MSGSTR(ECLOSE,"cat: close error\n"));
status = 2;
}
}
/* Defect 44200 */
errno = 0;
fclose(stdout);
if (errno == ENOSPC || errno == EDQUOT)
{
perror("cat");
status = 2;
}
exit(status);
}
/*
* NAME: scat
*
* FUNCTION:
* Concatenates files together. If options besides -q are
* used, this code will handle it. Otherwise fcat is called.
*
* RETURN VALUE DESCRIPTION:
*
* Exit 0 upon successfull completion, two otherwise.
*/
static void
scat(argc, argv)
int argc;
char **argv;
{
FILE *fi;
register int c;
int stdinflg = 0;
int status = 0;
int dev, ino = -1;
struct stat statb;
int asciipath;
char * loc_val;
int uflag=0;
#ifdef STANDALONE
if (argv[0][0] == '\0')
argc = getargv("cat", &argv, 0);
#endif
while ((c = getopt(argc, argv, "uSsrqvbnte")) != EOF ) {
switch (c) {
case 'u':
#ifndef STANDALONE
setbuf(stdout, (char *) NULL);
uflag=1;
#endif
continue;
case 'S': /* squeeze */
case 'r':
sflag++;
continue;
case 's':
case 'q':
silent++;
continue;
case 'v':
vflag++;
continue;
case 'b':
bflag++;
case 'n':
nflag++;
continue;
case 't':
tflag++;
vflag++;
continue;
case 'e':
eflag++;
vflag++;
continue;
case '?':
if (!silent)
fprintf(stderr,MSGSTR(EUSAGE,"usage: cat [-urSsq] [-n[b]] [-v[te]] [-|file] ...\n"));
exit(2);
}
break;
}
if (fstat((int)fileno(stdout), &statb) < 0) {
if (!silent)
fprintf(stderr, MSGSTR(ESTATOUT,"cat: cannot stat stdout\n"));
exit(2);
}
/* if we are in C or POSIX locales, we can make various */
/* assumptions to speed things up. */
loc_val = setlocale(LC_CTYPE,NULL);
if ( strcmp(loc_val,"C")==0 || ((*loc_val=='P') && (strcmp(loc_val,"POSIX")==0)) )
asciipath=1;
else
asciipath=0;
statb.st_mode &= S_IFMT;
if (statb.st_mode != S_IFCHR && statb.st_mode != S_IFBLK) {
dev = statb.st_dev;
ino = statb.st_ino;
}
if (optind == argc) {
argc++;
stdinflg++;
}
for (argv = &argv[optind];
optind < argc && !ferror(stdout); optind++, argv++) {
if (stdinflg || (*argv)[0] == '-' && (*argv)[1] == '\0')
fi = stdin;
else {
if ((fi = fopen(*argv, "r")) == NULL) {
if (!silent)
fprintf(stderr,MSGSTR(EOPEN,"cat: cannot open %s\n"),*argv);
status = 2;
continue;
}
}
if (fstat((int)fileno(fi), &statb) < 0) {
if (!silent)
fprintf(stderr,MSGSTR(ESTAT,"cat: cannot stat %s\n"),*argv);
status = 2;
continue;
}
if (vflag||nflag||sflag) {
if (asciipath)
copyopt_ascii(fi);
else
if (MB_CUR_MAX==1)
copyopt(fi);
else
copyopt_mb(fi);
}
else
if (uflag) {
while ((c = getc(fi)) != EOF)
if (putchar(c) == EOF && ferror(stdout)) {
if (!silent) {
fprintf(stderr,MSGSTR(EOUTPUT,"cat: output error\n"));
perror("");
}
status = 2;
break;
}
}
else
{
int cnt, tmpI;
while ((cnt = read(fileno(fi), buffer, BUFSIZE)) > 0) {
if ( cnt != (tmpI = write(fileno(stdout), buffer, cnt)) ) {
if (!silent) {
fprintf(stderr,MSGSTR(EOUTPUT,"cat: output error\n"));
perror("");
}
status = 2;
break;
}
}
if (cnt<0) {
fputs("cat: ",stderr);
perror(*argv);
status = 2;
}
}
if (fi != stdin) {
fflush(stdout);
if ((fclose(fi) != 0) && (!silent))
fprintf(stderr,MSGSTR(ECLOSE,"cat: close error\n"));
}
}
fflush(stdout);
if (ferror(stdout)) {
if (!silent)
fprintf(stderr,MSGSTR(EOUTPUT,"cat: output error\n"));
status = 2;
}
/* Defect 44200 */
errno = 0;
fclose(stdout);
if (errno == ENOSPC || errno == EDQUOT)
{
perror("cat");
status = 2;
}
exit(status);
}
/*
* NAME: cat
*
* FUNCTION:
* Depending on the option passed, main() switches between the
* standard cat (scat) and the faster version (fcat).
*
* RETURN:
* fcat and scat do not return.
*
*/
main(argc, argv)
int argc;
char **argv;
{
int i;
(void) setlocale (LC_ALL,"");
catd = catopen(MF_CAT, NL_CAT_LOCALE);
for (i = 1; i < argc ; i++)
if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
i = -1;
break;
}
if ((i != -1) && ((argc >= 2) && (argv[1][0] != '-')) ||
((argc > 2) && (argv[1][1] == 'q') && (argv[1][2] == '\0') &&
(argv[2][0] != '-')))
fcat(argc, argv);
else
scat(argc, argv);
exit(3); /* should never get here */
}
/*
* NAME: copyopt
*
* FUNCTION:
* Read file f, character by character and copy it to standard output.
* Translate special characters and number lines as specified by
* flags:
* -n number lines.
* -b don't count spaces.
* -s squeeze multiple blank lines together.
* -q don't print out errors.
* -t print tabs as ^I.
* -v print all control characters.
* -e signify endofline with a $
*
* RETURN VALUE DESCRIPTION:
* Subroutine, nothing returned.
*
* modifications to copyopt() must also be made to copyopt_ascii()
* and vis versa.
*/
static void
copyopt(f)
register FILE *f;
{
static lno = 1;
register int c;
while ((c=getc(f)) != EOF)
{
if (c == '\n') {
if (inline == 0) {
if (sflag && spaced)
continue;
spaced = 1;
}
if (nflag && bflag==0 && inline == 0)
printf("%6d\t", lno++);
if (eflag)
putchar('$');
putchar('\n');
inline = 0;
continue;
}
if (nflag && inline == 0)
printf("%6d\t", lno++);
inline = 1;
if (vflag) {
if (isprint(c))
putchar(c);
else if (iscntrl(c)) {
if (c == '\t' && tflag == 0)
putchar(c);
else
printf("^%c", c == '\177'
? '?' : c | 0100);
}
else if (iscntrl(c = toascii(c)))
printf("M-^%c", c == '\177'
? '?' : c | 0100);
else
printf("M-%c", c);
} else
putchar(c);
spaced = 0;
}
}
static void
copyopt_mb(f)
register FILE *f;
{
static lno = 1;
wint_t c;
while ((c=getwc(f)) != WEOF)
{
if (c == L'\n') {
if (inline == 0) {
if (sflag && spaced)
continue;
spaced = 1;
}
if (nflag && bflag==0 && inline == 0)
printf("%6d\t", lno++);
if (eflag)
putchar('$');
putchar('\n');
inline = 0;
continue;
}
if (nflag && inline == 0)
printf("%6d\t", lno++);
inline = 1;
if (vflag) {
if (iswprint(c))
putwchar(c);
else if (iswcntrl(c)) {
if (c == L'\t' && tflag == 0)
putwchar(c);
else
printf("^%C", c == L'\177'
? '?' : c | 0100);
}
else if (iswcntrl(c = (wchar_t)toascii(c)))
printf("M-^%C", c == '\177'
? '?' : c | 0100);
else
printf("M-%C", c);
} else
putwchar(c);
spaced = 0;
}
}
static void
copyopt_ascii(f)
register FILE *f;
{
static lno = 1;
register int c;
while ((c=getc(f)) != EOF)
{
if (c == '\n') {
if (inline == 0) {
if (sflag && spaced)
continue;
spaced = 1;
}
if (nflag && bflag==0 && inline == 0)
printf("%6d\t", lno++);
if (eflag)
putchar('$');
putchar('\n');
inline = 0;
continue;
}
if (nflag && inline == 0)
printf("%6d\t", lno++);
inline = 1;
if (vflag) {
if (c < ' ') {
if (c == '\t' && tflag == 0)
putchar(c);
else
printf("^%c", c | 0100);
}
else if (c < 0177)
putchar(c);
else if (c > 0177) {
fputs("M-",stdout);
c &= 0177;
if (c < ' ')
printf("^%c", c | 0100);
else if (c == 0177)
fputs("^?",stdout);
else
putchar(c);
}
else if (c == 0177)
fputs("^?",stdout);
} else
putchar(c);
spaced = 0;
}
}