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 #include <ctype.h>
28*7c478bd9Sstevel@tonic-gate #include <limits.h>
29*7c478bd9Sstevel@tonic-gate #include <locale.h>
30*7c478bd9Sstevel@tonic-gate #include <nl_types.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #define FF '\f'
38*7c478bd9Sstevel@tonic-gate #define NL '\n'
39*7c478bd9Sstevel@tonic-gate #define NUL '\0'
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate static void usage(void);
42*7c478bd9Sstevel@tonic-gate static void disp_file(FILE *f, char *filename);
43*7c478bd9Sstevel@tonic-gate static FILE *get_next_file(int, char *, char *[], int);
44*7c478bd9Sstevel@tonic-gate static void finish(int need_a_newline);
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate int estatus = 0; /* exit status */
47*7c478bd9Sstevel@tonic-gate int i; /* argv index */
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])50*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate int c;
53*7c478bd9Sstevel@tonic-gate int form_feeds = 0;
54*7c478bd9Sstevel@tonic-gate int need_a_newline = 0;
55*7c478bd9Sstevel@tonic-gate char *filename = NULL;
56*7c478bd9Sstevel@tonic-gate FILE *f;
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
59*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
60*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it were not */
61*7c478bd9Sstevel@tonic-gate #endif
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "f")) != EOF) {
66*7c478bd9Sstevel@tonic-gate switch (c) {
67*7c478bd9Sstevel@tonic-gate case 'f':
68*7c478bd9Sstevel@tonic-gate form_feeds = 1;
69*7c478bd9Sstevel@tonic-gate break;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate case '?':
72*7c478bd9Sstevel@tonic-gate usage();
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate i = optind;
77*7c478bd9Sstevel@tonic-gate if (argc <= i) {
78*7c478bd9Sstevel@tonic-gate filename = NULL;
79*7c478bd9Sstevel@tonic-gate f = stdin;
80*7c478bd9Sstevel@tonic-gate } else {
81*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename, argv, argc);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
85*7c478bd9Sstevel@tonic-gate for (;;) {
86*7c478bd9Sstevel@tonic-gate /* interpret the first character in the line */
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate c = getc(f);
89*7c478bd9Sstevel@tonic-gate switch (c) {
90*7c478bd9Sstevel@tonic-gate case EOF:
91*7c478bd9Sstevel@tonic-gate disp_file(f, filename);
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate if (i >= argc)
94*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename, argv, argc);
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate if (need_a_newline) {
99*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
100*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate if (form_feeds)
104*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate continue;
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate case NL:
109*7c478bd9Sstevel@tonic-gate if (need_a_newline)
110*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
111*7c478bd9Sstevel@tonic-gate need_a_newline = 1;
112*7c478bd9Sstevel@tonic-gate continue;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate case '+':
115*7c478bd9Sstevel@tonic-gate if (need_a_newline)
116*7c478bd9Sstevel@tonic-gate (void) putchar('\r');
117*7c478bd9Sstevel@tonic-gate break;
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate case '0':
120*7c478bd9Sstevel@tonic-gate if (need_a_newline)
121*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
122*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
123*7c478bd9Sstevel@tonic-gate break;
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate case '1':
126*7c478bd9Sstevel@tonic-gate if (need_a_newline)
127*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
128*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
129*7c478bd9Sstevel@tonic-gate break;
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate case ' ':
132*7c478bd9Sstevel@tonic-gate default:
133*7c478bd9Sstevel@tonic-gate if (need_a_newline)
134*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
135*7c478bd9Sstevel@tonic-gate break;
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate for (;;) {
141*7c478bd9Sstevel@tonic-gate c = getc(f);
142*7c478bd9Sstevel@tonic-gate if (c == NL) {
143*7c478bd9Sstevel@tonic-gate need_a_newline = 1;
144*7c478bd9Sstevel@tonic-gate break;
145*7c478bd9Sstevel@tonic-gate } else if (c == EOF) {
146*7c478bd9Sstevel@tonic-gate disp_file(f, filename);
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate if (i >= argc)
149*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate f = get_next_file(need_a_newline, filename,
152*7c478bd9Sstevel@tonic-gate argv, argc);
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate if (form_feeds) {
155*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
156*7c478bd9Sstevel@tonic-gate (void) putchar(FF);
157*7c478bd9Sstevel@tonic-gate need_a_newline = 0;
158*7c478bd9Sstevel@tonic-gate break;
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate } else {
161*7c478bd9Sstevel@tonic-gate (void) putchar(c);
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
166*7c478bd9Sstevel@tonic-gate return (0);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate static void
usage(void)170*7c478bd9Sstevel@tonic-gate usage(void)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: asa [-f] [-|file...]\n"));
173*7c478bd9Sstevel@tonic-gate exit(1);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate static void
disp_file(FILE * f,char * filename)178*7c478bd9Sstevel@tonic-gate disp_file(FILE *f, char *filename)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate if (ferror(f)) {
182*7c478bd9Sstevel@tonic-gate int serror = errno;
183*7c478bd9Sstevel@tonic-gate if (filename) {
184*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
185*7c478bd9Sstevel@tonic-gate "asa: read error on file %s\n"), filename);
186*7c478bd9Sstevel@tonic-gate } else {
187*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
188*7c478bd9Sstevel@tonic-gate "asa: read error on standard input\n"));
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate errno = serror;
191*7c478bd9Sstevel@tonic-gate perror("");
192*7c478bd9Sstevel@tonic-gate estatus = 1;
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate (void) fclose(f);
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate static FILE *
get_next_file(int need_a_newline,char * filename,char * argv[],int argc)200*7c478bd9Sstevel@tonic-gate get_next_file(int need_a_newline, char *filename, char *argv[], int argc)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate FILE *f;
203*7c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
204*7c478bd9Sstevel@tonic-gate filename = NULL;
205*7c478bd9Sstevel@tonic-gate f = stdin;
206*7c478bd9Sstevel@tonic-gate } else {
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate * Process each file operand. If unsuccessful, affect the
209*7c478bd9Sstevel@tonic-gate * exit status and continue processing the next operand.
210*7c478bd9Sstevel@tonic-gate */
211*7c478bd9Sstevel@tonic-gate filename = argv[i];
212*7c478bd9Sstevel@tonic-gate while ((f = fopen(filename, "r")) == NULL) {
213*7c478bd9Sstevel@tonic-gate int serror = errno;
214*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
215*7c478bd9Sstevel@tonic-gate gettext("asa: cannot open %s:"), filename);
216*7c478bd9Sstevel@tonic-gate errno = serror;
217*7c478bd9Sstevel@tonic-gate perror("");
218*7c478bd9Sstevel@tonic-gate estatus = 1;
219*7c478bd9Sstevel@tonic-gate if (++i < argc) {
220*7c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-") == 0) {
221*7c478bd9Sstevel@tonic-gate filename = NULL;
222*7c478bd9Sstevel@tonic-gate f = stdin;
223*7c478bd9Sstevel@tonic-gate break;
224*7c478bd9Sstevel@tonic-gate } else {
225*7c478bd9Sstevel@tonic-gate filename = argv[i];
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate } else {
228*7c478bd9Sstevel@tonic-gate finish(need_a_newline);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate ++i;
233*7c478bd9Sstevel@tonic-gate return (f);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate static void
finish(int need_a_newline)237*7c478bd9Sstevel@tonic-gate finish(int need_a_newline)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate if (need_a_newline)
240*7c478bd9Sstevel@tonic-gate (void) putchar(NL);
241*7c478bd9Sstevel@tonic-gate exit(estatus);
242*7c478bd9Sstevel@tonic-gate }
243