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