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