xref: /illumos-gate/usr/src/cmd/fold/fold.c (revision 2a8bcb4e)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <locale.h>
42*7c478bd9Sstevel@tonic-gate #include <wchar.h>
43*7c478bd9Sstevel@tonic-gate #include <euc.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>		/* XCU4 */
45*7c478bd9Sstevel@tonic-gate #include <limits.h>
46*7c478bd9Sstevel@tonic-gate #include <libintl.h>
47*7c478bd9Sstevel@tonic-gate #include <langinfo.h>
48*7c478bd9Sstevel@tonic-gate #include <utime.h>
49*7c478bd9Sstevel@tonic-gate #include <widec.h>
50*7c478bd9Sstevel@tonic-gate #include <wctype.h>
51*7c478bd9Sstevel@tonic-gate #include <errno.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * fold - fold long lines for finite output devices
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static int fold =  80;
59*7c478bd9Sstevel@tonic-gate static int bflg = 0;
60*7c478bd9Sstevel@tonic-gate static int sflg = 0;
61*7c478bd9Sstevel@tonic-gate static int wflg = 0;
62*7c478bd9Sstevel@tonic-gate static int lastc = 0;
63*7c478bd9Sstevel@tonic-gate static int col = 0;
64*7c478bd9Sstevel@tonic-gate static int ncol = 0;
65*7c478bd9Sstevel@tonic-gate static int spcol = 0;
66*7c478bd9Sstevel@tonic-gate static wchar_t line[LINE_MAX];
67*7c478bd9Sstevel@tonic-gate static wchar_t *lastout = line;
68*7c478bd9Sstevel@tonic-gate static wchar_t *curc = line;
69*7c478bd9Sstevel@tonic-gate static wchar_t *lastsp = NULL;
70*7c478bd9Sstevel@tonic-gate #define	MAXARG _POSIX_ARG_MAX
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * Fix lint errors
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate void exit();
76*7c478bd9Sstevel@tonic-gate static void Usage();
77*7c478bd9Sstevel@tonic-gate static void putch();
78*7c478bd9Sstevel@tonic-gate static void newline_init();
79*7c478bd9Sstevel@tonic-gate static int chr_width();
80*7c478bd9Sstevel@tonic-gate extern int errno;
81*7c478bd9Sstevel@tonic-gate static int get_foldw();
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])85*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	int c, narg;
88*7c478bd9Sstevel@tonic-gate 	int ch;
89*7c478bd9Sstevel@tonic-gate 	char *cmdline[MAXARG];
90*7c478bd9Sstevel@tonic-gate 	int	new_argc;
91*7c478bd9Sstevel@tonic-gate 	int w;
92*7c478bd9Sstevel@tonic-gate 	extern int optind;
93*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
96*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
97*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
98*7c478bd9Sstevel@tonic-gate #endif
99*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * Parse -width separately and build
103*7c478bd9Sstevel@tonic-gate 	 * the new command line without -width.
104*7c478bd9Sstevel@tonic-gate 	 * Next, use getopt() to parse this
105*7c478bd9Sstevel@tonic-gate 	 * new command line.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	for (narg = new_argc = 0; narg < argc; narg ++) {
109*7c478bd9Sstevel@tonic-gate 		if (argv[narg][0] == '-' &&
110*7c478bd9Sstevel@tonic-gate 			isdigit(argv[narg][1])) {
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 			if (get_foldw((char *)&argv[narg][1], &w) < 0)
113*7c478bd9Sstevel@tonic-gate 				exit(1);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 			fold = w;	/* Update with new width */
116*7c478bd9Sstevel@tonic-gate 		} else {
117*7c478bd9Sstevel@tonic-gate 			/* Build the new command line */
118*7c478bd9Sstevel@tonic-gate 			cmdline[new_argc++] = argv[narg];
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 			/*
121*7c478bd9Sstevel@tonic-gate 			 * Check to make sure the option with
122*7c478bd9Sstevel@tonic-gate 			 * required arg should have arg.
123*7c478bd9Sstevel@tonic-gate 			 * This would check errors introduced in
124*7c478bd9Sstevel@tonic-gate 			 * mixing non-getopt() options and that of
125*7c478bd9Sstevel@tonic-gate 			 * getopt()'s due to stripping non-getopt
126*7c478bd9Sstevel@tonic-gate 			 * options.
127*7c478bd9Sstevel@tonic-gate 			 */
128*7c478bd9Sstevel@tonic-gate 			if ((argv[narg][0] == '-') && (argv[narg][1] == 'w')) {
129*7c478bd9Sstevel@tonic-gate 				if (((narg+1) < argc) &&
130*7c478bd9Sstevel@tonic-gate 					(argv[narg+1][0] == '-')) {
131*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "fold");
132*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
133*7c478bd9Sstevel@tonic-gate 				": option requires an argument -- w\n"));
134*7c478bd9Sstevel@tonic-gate 					Usage();
135*7c478bd9Sstevel@tonic-gate 					exit(1);
136*7c478bd9Sstevel@tonic-gate 				}
137*7c478bd9Sstevel@tonic-gate 			}
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	while ((ch = getopt(new_argc, cmdline, "w:bs")) != EOF) {
142*7c478bd9Sstevel@tonic-gate 		switch (ch) {
143*7c478bd9Sstevel@tonic-gate 			case 'b':
144*7c478bd9Sstevel@tonic-gate 				bflg++;
145*7c478bd9Sstevel@tonic-gate 				break;
146*7c478bd9Sstevel@tonic-gate 			case 's':
147*7c478bd9Sstevel@tonic-gate 				sflg++;
148*7c478bd9Sstevel@tonic-gate 				break;
149*7c478bd9Sstevel@tonic-gate 			case 'w':
150*7c478bd9Sstevel@tonic-gate 				wflg++;
151*7c478bd9Sstevel@tonic-gate 				/* No required arg ? */
152*7c478bd9Sstevel@tonic-gate 				if ((optarg == (char *)NULL) ||
153*7c478bd9Sstevel@tonic-gate 					((optarg != (char *)NULL) &&
154*7c478bd9Sstevel@tonic-gate 					(*optarg == '-'))) {
155*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "fold");
156*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
157*7c478bd9Sstevel@tonic-gate 				": option requires an argument -- w\n"));
158*7c478bd9Sstevel@tonic-gate 						Usage();
159*7c478bd9Sstevel@tonic-gate 						exit(1);
160*7c478bd9Sstevel@tonic-gate 				}
161*7c478bd9Sstevel@tonic-gate 				/* Bad number ? */
162*7c478bd9Sstevel@tonic-gate 				if (get_foldw(optarg, &w) < 0)
163*7c478bd9Sstevel@tonic-gate 					exit(1);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 				fold = w;
166*7c478bd9Sstevel@tonic-gate 				break;
167*7c478bd9Sstevel@tonic-gate 			default:
168*7c478bd9Sstevel@tonic-gate 				/*
169*7c478bd9Sstevel@tonic-gate 				 * Errors should be filtered in previous
170*7c478bd9Sstevel@tonic-gate 				 * pass.
171*7c478bd9Sstevel@tonic-gate 				 */
172*7c478bd9Sstevel@tonic-gate 				Usage();
173*7c478bd9Sstevel@tonic-gate 				exit(1);
174*7c478bd9Sstevel@tonic-gate 		} /* switch */
175*7c478bd9Sstevel@tonic-gate 	} /* while */
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	do {
178*7c478bd9Sstevel@tonic-gate 		if (new_argc > optind) {
179*7c478bd9Sstevel@tonic-gate 			if (freopen(cmdline[optind], "r", stdin) == NULL) {
180*7c478bd9Sstevel@tonic-gate 				perror(cmdline[optind]);
181*7c478bd9Sstevel@tonic-gate 				Usage();
182*7c478bd9Sstevel@tonic-gate 				exit(1);
183*7c478bd9Sstevel@tonic-gate 			}
184*7c478bd9Sstevel@tonic-gate 			optind++;
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		for (;;) {
188*7c478bd9Sstevel@tonic-gate 			c = getwc(stdin);
189*7c478bd9Sstevel@tonic-gate 			if (c == EOF)
190*7c478bd9Sstevel@tonic-gate 				break;
191*7c478bd9Sstevel@tonic-gate 			(void) putch(c);
192*7c478bd9Sstevel@tonic-gate 			lastc = c;
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 		if (col != 0) newline_init();
195*7c478bd9Sstevel@tonic-gate 	} while (new_argc > optind);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	return (0);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate static void
putch(int c)201*7c478bd9Sstevel@tonic-gate putch(int c)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	wchar_t tline[LINE_MAX];
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	switch (c) {
206*7c478bd9Sstevel@tonic-gate 		case '\n':
207*7c478bd9Sstevel@tonic-gate 			ncol = 0;
208*7c478bd9Sstevel@tonic-gate 			break;
209*7c478bd9Sstevel@tonic-gate 		case '\t':
210*7c478bd9Sstevel@tonic-gate 			if (bflg)
211*7c478bd9Sstevel@tonic-gate 				ncol = col + chr_width(c);
212*7c478bd9Sstevel@tonic-gate 			else
213*7c478bd9Sstevel@tonic-gate 				ncol = (col + 8) &~ 7;
214*7c478bd9Sstevel@tonic-gate 			break;
215*7c478bd9Sstevel@tonic-gate 		case '\b':
216*7c478bd9Sstevel@tonic-gate 			if (bflg)
217*7c478bd9Sstevel@tonic-gate 				ncol = col + chr_width(c);
218*7c478bd9Sstevel@tonic-gate 			else
219*7c478bd9Sstevel@tonic-gate 				ncol = col ? col - 1 : 0;
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 		case '\r':
222*7c478bd9Sstevel@tonic-gate 			if (bflg)
223*7c478bd9Sstevel@tonic-gate 				ncol = col + chr_width(c);
224*7c478bd9Sstevel@tonic-gate 			else
225*7c478bd9Sstevel@tonic-gate 				ncol = 0;
226*7c478bd9Sstevel@tonic-gate 			break;
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 		default:
229*7c478bd9Sstevel@tonic-gate 			if (bflg)
230*7c478bd9Sstevel@tonic-gate 				ncol = col + chr_width(c);
231*7c478bd9Sstevel@tonic-gate 			else
232*7c478bd9Sstevel@tonic-gate 				ncol = col + wcwidth(c);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	/*
237*7c478bd9Sstevel@tonic-gate 	 * Special processing when -b is not specified
238*7c478bd9Sstevel@tonic-gate 	 * for backspace, and carriage return.
239*7c478bd9Sstevel@tonic-gate 	 * No newline is inseted before or after the
240*7c478bd9Sstevel@tonic-gate 	 * special character: backspace, or cr.
241*7c478bd9Sstevel@tonic-gate 	 * See man page for the handling of backspace
242*7c478bd9Sstevel@tonic-gate 	 * and CR when there is no -b.
243*7c478bd9Sstevel@tonic-gate 	 */
244*7c478bd9Sstevel@tonic-gate 	if ((ncol > fold) && (bflg ||
245*7c478bd9Sstevel@tonic-gate 		(!bflg && (lastc != '\b') && (c != '\b') &&
246*7c478bd9Sstevel@tonic-gate 		(lastc != '\n') && (c != '\n'))))  {
247*7c478bd9Sstevel@tonic-gate 		/*
248*7c478bd9Sstevel@tonic-gate 		 * Need to check the last position for blank
249*7c478bd9Sstevel@tonic-gate 		 */
250*7c478bd9Sstevel@tonic-gate 		if (sflg && lastsp) {
251*7c478bd9Sstevel@tonic-gate 			/*
252*7c478bd9Sstevel@tonic-gate 			 * Save the output buffer
253*7c478bd9Sstevel@tonic-gate 			 * as NULL has to be insert into the last
254*7c478bd9Sstevel@tonic-gate 			 * sp position.
255*7c478bd9Sstevel@tonic-gate 			 */
256*7c478bd9Sstevel@tonic-gate 			(void) wscpy(tline, line);
257*7c478bd9Sstevel@tonic-gate 			*lastsp = (wchar_t)NULL;
258*7c478bd9Sstevel@tonic-gate 			(void) fputws(lastout, stdout);
259*7c478bd9Sstevel@tonic-gate 			(void) putwchar('\n');
260*7c478bd9Sstevel@tonic-gate 			/*
261*7c478bd9Sstevel@tonic-gate 			 * Restore the output buffer to stuff
262*7c478bd9Sstevel@tonic-gate 			 * NULL into the last sp position
263*7c478bd9Sstevel@tonic-gate 			 * for the new line.
264*7c478bd9Sstevel@tonic-gate 			 */
265*7c478bd9Sstevel@tonic-gate 			(void) wscpy(line, tline);
266*7c478bd9Sstevel@tonic-gate 			lastout = lastsp;
267*7c478bd9Sstevel@tonic-gate 			lastsp = NULL;	/* Reset the last sp */
268*7c478bd9Sstevel@tonic-gate 			ncol -= spcol;	/* Reset column positions */
269*7c478bd9Sstevel@tonic-gate 			col -= spcol;
270*7c478bd9Sstevel@tonic-gate 		} else {
271*7c478bd9Sstevel@tonic-gate 			(void) newline_init();
272*7c478bd9Sstevel@tonic-gate 			(void) putwchar('\n');
273*7c478bd9Sstevel@tonic-gate 			lastout = curc;
274*7c478bd9Sstevel@tonic-gate 		}
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	/* Output buffer is full ? */
277*7c478bd9Sstevel@tonic-gate 	if ((curc + 1) >= (line + LINE_MAX)) {
278*7c478bd9Sstevel@tonic-gate 		/* Reach buffer limit */
279*7c478bd9Sstevel@tonic-gate 		if (col > 0) {
280*7c478bd9Sstevel@tonic-gate 			*curc = (wchar_t)NULL;
281*7c478bd9Sstevel@tonic-gate 			(void) fputws(lastout, stdout);
282*7c478bd9Sstevel@tonic-gate 			lastsp = NULL;
283*7c478bd9Sstevel@tonic-gate 		}
284*7c478bd9Sstevel@tonic-gate 		curc = lastout = line;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	/* Store in output buffer */
289*7c478bd9Sstevel@tonic-gate 	*curc++ = (wchar_t)c;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	switch (c) {
292*7c478bd9Sstevel@tonic-gate 		case '\n':
293*7c478bd9Sstevel@tonic-gate 			(void)  newline_init();
294*7c478bd9Sstevel@tonic-gate 			curc = lastout = line;
295*7c478bd9Sstevel@tonic-gate 			break;
296*7c478bd9Sstevel@tonic-gate 		case '\t':
297*7c478bd9Sstevel@tonic-gate 			if (bflg)
298*7c478bd9Sstevel@tonic-gate 				col = col + chr_width(c);
299*7c478bd9Sstevel@tonic-gate 			else
300*7c478bd9Sstevel@tonic-gate 				col = (col + 8) &~ 7;
301*7c478bd9Sstevel@tonic-gate 			if (sflg && iswspace(c)) {
302*7c478bd9Sstevel@tonic-gate 				lastsp = curc;
303*7c478bd9Sstevel@tonic-gate 				spcol = ncol;
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 			break;
307*7c478bd9Sstevel@tonic-gate 		case '\b':
308*7c478bd9Sstevel@tonic-gate 			if (bflg)
309*7c478bd9Sstevel@tonic-gate 				col = ncol;
310*7c478bd9Sstevel@tonic-gate 			else {
311*7c478bd9Sstevel@tonic-gate 				if (col)
312*7c478bd9Sstevel@tonic-gate 					col--;
313*7c478bd9Sstevel@tonic-gate 			}
314*7c478bd9Sstevel@tonic-gate 			break;
315*7c478bd9Sstevel@tonic-gate 		case '\r':
316*7c478bd9Sstevel@tonic-gate 			col = 0;
317*7c478bd9Sstevel@tonic-gate 			break;
318*7c478bd9Sstevel@tonic-gate 		default:
319*7c478bd9Sstevel@tonic-gate 			if (sflg && iswspace(c)) {
320*7c478bd9Sstevel@tonic-gate 				lastsp = curc;
321*7c478bd9Sstevel@tonic-gate 				spcol = ncol;
322*7c478bd9Sstevel@tonic-gate 			}
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 			if (bflg)
325*7c478bd9Sstevel@tonic-gate 				col += chr_width(c);
326*7c478bd9Sstevel@tonic-gate 			else
327*7c478bd9Sstevel@tonic-gate 				col += wcwidth(c);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 			break;
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate static
333*7c478bd9Sstevel@tonic-gate void
Usage()334*7c478bd9Sstevel@tonic-gate Usage()
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
337*7c478bd9Sstevel@tonic-gate 	    "Usage: fold [-bs] [-w width | -width ] [file...]\n"));
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate static
341*7c478bd9Sstevel@tonic-gate void
newline_init()342*7c478bd9Sstevel@tonic-gate newline_init()
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	*curc = (wchar_t)NULL;
345*7c478bd9Sstevel@tonic-gate 	(void) fputws(lastout, stdout);
346*7c478bd9Sstevel@tonic-gate 	ncol = col = spcol = 0;
347*7c478bd9Sstevel@tonic-gate 	lastsp = NULL;
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate static int
chr_width(c)351*7c478bd9Sstevel@tonic-gate chr_width(c)
352*7c478bd9Sstevel@tonic-gate register int c;
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	char chr[MB_LEN_MAX+1];
355*7c478bd9Sstevel@tonic-gate 	register int	n;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	n = wctomb(chr, (wchar_t)c);
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	return (n > 0 ? n : 0);
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate static int
get_foldw(toptarg,width)363*7c478bd9Sstevel@tonic-gate get_foldw(toptarg, width)
364*7c478bd9Sstevel@tonic-gate char	*toptarg;
365*7c478bd9Sstevel@tonic-gate int		*width;
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	char	*p;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	if (!toptarg)
370*7c478bd9Sstevel@tonic-gate 		goto badno;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	*width = 0;
373*7c478bd9Sstevel@tonic-gate 	errno = 0;
374*7c478bd9Sstevel@tonic-gate 	*width = strtoul(toptarg, &p, 10);
375*7c478bd9Sstevel@tonic-gate 	if (*width == -1)
376*7c478bd9Sstevel@tonic-gate 		goto badno;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	if (*p)
379*7c478bd9Sstevel@tonic-gate 		goto badno;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (!*width)
382*7c478bd9Sstevel@tonic-gate 		goto badno;
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	return (0);
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate badno:
387*7c478bd9Sstevel@tonic-gate 	/* fold error message */
388*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
389*7c478bd9Sstevel@tonic-gate 		"Bad number for fold\n"));
390*7c478bd9Sstevel@tonic-gate 	Usage();
391*7c478bd9Sstevel@tonic-gate 	return (-1);
392*7c478bd9Sstevel@tonic-gate }
393