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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <euc.h>
#include <stdlib.h> /* XCU4 */
#include <limits.h>
#include <libintl.h>
#include <langinfo.h>
#include <utime.h>
#include <widec.h>
#include <wctype.h>
#include <errno.h>
/*
* fold - fold long lines for finite output devices
*/
static int fold = 80;
static int bflg = 0;
static int sflg = 0;
static int wflg = 0;
static int lastc = 0;
static int col = 0;
static int ncol = 0;
static int spcol = 0;
static wchar_t line[LINE_MAX];
static wchar_t *lastout = line;
static wchar_t *curc = line;
static wchar_t *lastsp = NULL;
#define MAXARG _POSIX_ARG_MAX
/*
* Fix lint errors
*/
void exit();
static void Usage();
static void putch();
static void newline_init();
static int chr_width();
extern int errno;
static int get_foldw();
int
main(int argc, char *argv[])
{
int c, narg;
int ch;
char *cmdline[MAXARG];
int new_argc;
int w;
extern int optind;
extern char *optarg;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
/*
* Parse -width separately and build
* the new command line without -width.
* Next, use getopt() to parse this
* new command line.
*/
for (narg = new_argc = 0; narg < argc; narg ++) {
if (argv[narg][0] == '-' &&
isdigit(argv[narg][1])) {
if (get_foldw((char *)&argv[narg][1], &w) < 0)
exit(1);
fold = w; /* Update with new width */
} else {
/* Build the new command line */
cmdline[new_argc++] = argv[narg];
/*
* Check to make sure the option with
* required arg should have arg.
* This would check errors introduced in
* mixing non-getopt() options and that of
* getopt()'s due to stripping non-getopt
* options.
*/
if ((argv[narg][0] == '-') && (argv[narg][1] == 'w')) {
if (((narg+1) < argc) &&
(argv[narg+1][0] == '-')) {
(void) fprintf(stderr, "fold");
(void) fprintf(stderr, gettext(
": option requires an argument -- w\n"));
Usage();
exit(1);
}
}
}
}
while ((ch = getopt(new_argc, cmdline, "w:bs")) != EOF) {
switch (ch) {
case 'b':
bflg++;
break;
case 's':
sflg++;
break;
case 'w':
wflg++;
/* No required arg ? */
if ((optarg == (char *)NULL) ||
((optarg != (char *)NULL) &&
(*optarg == '-'))) {
(void) fprintf(stderr, "fold");
(void) fprintf(stderr, gettext(
": option requires an argument -- w\n"));
Usage();
exit(1);
}
/* Bad number ? */
if (get_foldw(optarg, &w) < 0)
exit(1);
fold = w;
break;
default:
/*
* Errors should be filtered in previous
* pass.
*/
Usage();
exit(1);
} /* switch */
} /* while */
do {
if (new_argc > optind) {
if (freopen(cmdline[optind], "r", stdin) == NULL) {
perror(cmdline[optind]);
Usage();
exit(1);
}
optind++;
}
for (;;) {
c = getwc(stdin);
if (c == EOF)
break;
(void) putch(c);
lastc = c;
}
if (col != 0) newline_init();
} while (new_argc > optind);
return (0);
}
static void
putch(int c)
{
wchar_t tline[LINE_MAX];
switch (c) {
case '\n':
ncol = 0;
break;
case '\t':
if (bflg)
ncol = col + chr_width(c);
else
ncol = (col + 8) &~ 7;
break;
case '\b':
if (bflg)
ncol = col + chr_width(c);
else
ncol = col ? col - 1 : 0;
break;
case '\r':
if (bflg)
ncol = col + chr_width(c);
else
ncol = 0;
break;
default:
if (bflg)
ncol = col + chr_width(c);
else
ncol = col + wcwidth(c);
}
/*
* Special processing when -b is not specified
* for backspace, and carriage return.
* No newline is inseted before or after the
* special character: backspace, or cr.
* See man page for the handling of backspace
* and CR when there is no -b.
*/
if ((ncol > fold) && (bflg ||
(!bflg && (lastc != '\b') && (c != '\b') &&
(lastc != '\n') && (c != '\n')))) {
/*
* Need to check the last position for blank
*/
if (sflg && lastsp) {
/*
* Save the output buffer
* as NULL has to be insert into the last
* sp position.
*/
(void) wscpy(tline, line);
*lastsp = (wchar_t)NULL;
(void) fputws(lastout, stdout);
(void) putwchar('\n');
/*
* Restore the output buffer to stuff
* NULL into the last sp position
* for the new line.
*/
(void) wscpy(line, tline);
lastout = lastsp;
lastsp = NULL; /* Reset the last sp */
ncol -= spcol; /* Reset column positions */
col -= spcol;
} else {
(void) newline_init();
(void) putwchar('\n');
lastout = curc;
}
}
/* Output buffer is full ? */
if ((curc + 1) >= (line + LINE_MAX)) {
/* Reach buffer limit */
if (col > 0) {
*curc = (wchar_t)NULL;
(void) fputws(lastout, stdout);
lastsp = NULL;
}
curc = lastout = line;
}
/* Store in output buffer */
*curc++ = (wchar_t)c;
switch (c) {
case '\n':
(void) newline_init();
curc = lastout = line;
break;
case '\t':
if (bflg)
col = col + chr_width(c);
else
col = (col + 8) &~ 7;
if (sflg && iswspace(c)) {
lastsp = curc;
spcol = ncol;
}
break;
case '\b':
if (bflg)
col = ncol;
else {
if (col)
col--;
}
break;
case '\r':
col = 0;
break;
default:
if (sflg && iswspace(c)) {
lastsp = curc;
spcol = ncol;
}
if (bflg)
col += chr_width(c);
else
col += wcwidth(c);
break;
}
}
static
void
Usage()
{
(void) fprintf(stderr, gettext(
"Usage: fold [-bs] [-w width | -width ] [file...]\n"));
}
static
void
newline_init()
{
*curc = (wchar_t)NULL;
(void) fputws(lastout, stdout);
ncol = col = spcol = 0;
lastsp = NULL;
}
static int
chr_width(c)
register int c;
{
char chr[MB_LEN_MAX+1];
register int n;
n = wctomb(chr, (wchar_t)c);
return (n > 0 ? n : 0);
}
static int
get_foldw(toptarg, width)
char *toptarg;
int *width;
{
char *p;
if (!toptarg)
goto badno;
*width = 0;
errno = 0;
*width = strtoul(toptarg, &p, 10);
if (*width == -1)
goto badno;
if (*p)
goto badno;
if (!*width)
goto badno;
return (0);
badno:
/* fold error message */
(void) fprintf(stderr, gettext(
"Bad number for fold\n"));
Usage();
return (-1);
}
|