1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate #include <limits.h>
32*7c478bd9Sstevel@tonic-gate #include <locale.h>
33*7c478bd9Sstevel@tonic-gate #include <nl_types.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <errno.h>
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #define FF '\f'
41*7c478bd9Sstevel@tonic-gate #define NL '\n'
42*7c478bd9Sstevel@tonic-gate #define NUL '\0'
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate static void usage(void);
45*7c478bd9Sstevel@tonic-gate static void disp_file(FILE *f, char *filename);
46*7c478bd9Sstevel@tonic-gate static FILE *get_next_file(int, char *, char *[], int);
47*7c478bd9Sstevel@tonic-gate static void finish(int need_a_newline);
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate int estatus = 0; /* exit status */
50*7c478bd9Sstevel@tonic-gate int i; /* argv index */
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])53*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate int c;
56*7c478bd9Sstevel@tonic-gate int form_feeds = 0;
57*7c478bd9Sstevel@tonic-gate int need_a_newline = 0;
58*7c478bd9Sstevel@tonic-gate char *filename = NULL;
59*7c478bd9Sstevel@tonic-gate FILE *f;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
62*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
63*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it were not */
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "f")) != EOF) {
69*7c478bd9Sstevel@tonic-gate switch (c) {
70*7c478bd9Sstevel@tonic-gate case 'f':
71*7c478bd9Sstevel@tonic-gate form_feeds = 1;
72*7c478bd9Sstevel@tonic-gate break;
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate case '?':
75*7c478bd9Sstevel@tonic-gate usage();
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate i = optind;
80*7c478bd9Sstevel@tonic-gate if (argc <= i) {
81*7c478bd9Sstevel@tonic-gate filename = NULL;
82*7c478bd9Sstevel@tonic-gate f = stdin;
83*7c478bd9Sstevel@tonic-gate } else {
84*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename, argv, argc);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
88*7c478bd9Sstevel@tonic-gate for (;;) {
89*7c478bd9Sstevel@tonic-gate /* interpret the first character in the line */
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate c = getc(f);
92*7c478bd9Sstevel@tonic-gate switch (c) {
93*7c478bd9Sstevel@tonic-gate case EOF:
94*7c478bd9Sstevel@tonic-gate disp_file(f, filename);
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate if (i >= argc)
97*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename, argv, argc);
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate if (need_a_newline) {
102*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
103*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate if (form_feeds)
107*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate continue;
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate case NL:
112*7c478bd9Sstevel@tonic-gate if (need_a_newline)
113*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
114*7c478bd9Sstevel@tonic-gate need_a_newline = 1;
115*7c478bd9Sstevel@tonic-gate continue;
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate case '+':
118*7c478bd9Sstevel@tonic-gate if (need_a_newline)
119*7c478bd9Sstevel@tonic-gate (void) putchar('\r');
120*7c478bd9Sstevel@tonic-gate break;
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate case '0':
123*7c478bd9Sstevel@tonic-gate if (need_a_newline)
124*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
125*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
126*7c478bd9Sstevel@tonic-gate break;
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate case '1':
129*7c478bd9Sstevel@tonic-gate if (need_a_newline)
130*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
131*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
132*7c478bd9Sstevel@tonic-gate break;
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate case ' ':
135*7c478bd9Sstevel@tonic-gate default:
136*7c478bd9Sstevel@tonic-gate if (need_a_newline)
137*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
138*7c478bd9Sstevel@tonic-gate break;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate for (;;) {
144*7c478bd9Sstevel@tonic-gate c = getc(f);
145*7c478bd9Sstevel@tonic-gate if (c == NL) {
146*7c478bd9Sstevel@tonic-gate need_a_newline = 1;
147*7c478bd9Sstevel@tonic-gate break;
148*7c478bd9Sstevel@tonic-gate } else if (c == EOF) {
149*7c478bd9Sstevel@tonic-gate disp_file(f, filename);
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate if (i >= argc)
152*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename,
155*7c478bd9Sstevel@tonic-gate argv, argc);
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gate if (form_feeds) {
158*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
159*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
160*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
161*7c478bd9Sstevel@tonic-gate break;
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate } else {
164*7c478bd9Sstevel@tonic-gate (void) putchar(c);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
169*7c478bd9Sstevel@tonic-gate return (0);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate static void
usage(void)173*7c478bd9Sstevel@tonic-gate usage(void)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: asa [-f] [-|file...]\n"));
176*7c478bd9Sstevel@tonic-gate exit(1);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate static void
disp_file(FILE * f,char * filename)181*7c478bd9Sstevel@tonic-gate disp_file(FILE *f, char *filename)
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate if (ferror(f)) {
185*7c478bd9Sstevel@tonic-gate int serror = errno;
186*7c478bd9Sstevel@tonic-gate if (filename) {
187*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
188*7c478bd9Sstevel@tonic-gate "asa: read error on file %s\n"), filename);
189*7c478bd9Sstevel@tonic-gate } else {
190*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
191*7c478bd9Sstevel@tonic-gate "asa: read error on standard input\n"));
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate errno = serror;
194*7c478bd9Sstevel@tonic-gate perror("");
195*7c478bd9Sstevel@tonic-gate estatus = 1;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate (void) fclose(f);
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate static FILE *
get_next_file(int need_a_newline,char * filename,char * argv[],int argc)203*7c478bd9Sstevel@tonic-gate get_next_file(int need_a_newline, char *filename, char *argv[], int argc)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate FILE *f;
206*7c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
207*7c478bd9Sstevel@tonic-gate filename = NULL;
208*7c478bd9Sstevel@tonic-gate f = stdin;
209*7c478bd9Sstevel@tonic-gate } else {
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate * Process each file operand. If unsuccessful, affect the
212*7c478bd9Sstevel@tonic-gate * exit status and continue processing the next operand.
213*7c478bd9Sstevel@tonic-gate */
214*7c478bd9Sstevel@tonic-gate filename = argv[i];
215*7c478bd9Sstevel@tonic-gate while ((f = fopen(filename, "r")) == NULL) {
216*7c478bd9Sstevel@tonic-gate int serror = errno;
217*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
218*7c478bd9Sstevel@tonic-gate gettext("asa: cannot open %s:"), filename);
219*7c478bd9Sstevel@tonic-gate errno = serror;
220*7c478bd9Sstevel@tonic-gate perror("");
221*7c478bd9Sstevel@tonic-gate estatus = 1;
222*7c478bd9Sstevel@tonic-gate if (++i < argc) {
223*7c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
224*7c478bd9Sstevel@tonic-gate filename = NULL;
225*7c478bd9Sstevel@tonic-gate f = stdin;
226*7c478bd9Sstevel@tonic-gate break;
227*7c478bd9Sstevel@tonic-gate } else {
228*7c478bd9Sstevel@tonic-gate filename = argv[i];
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate } else {
231*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate ++i;
236*7c478bd9Sstevel@tonic-gate return (f);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate static void
finish(int need_a_newline)240*7c478bd9Sstevel@tonic-gate finish(int need_a_newline)
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate if (need_a_newline)
243*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
244*7c478bd9Sstevel@tonic-gate exit(estatus);
245*7c478bd9Sstevel@tonic-gate }
246