/*
* 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
* 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 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;
/*
* 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
{
int c, narg;
int ch;
int new_argc;
int w;
extern int optind;
extern char *optarg;
#if !defined(TEXT_DOMAIN)
#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.
*/
exit(1);
fold = w; /* Update with new width */
} else {
/* Build the new command line */
/*
* 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.
*/
": option requires an argument -- w\n"));
Usage();
exit(1);
}
}
}
}
switch (ch) {
case 'b':
bflg++;
break;
case 's':
sflg++;
break;
case 'w':
wflg++;
/* No required arg ? */
(*optarg == '-'))) {
": option requires an argument -- w\n"));
Usage();
exit(1);
}
/* Bad number ? */
exit(1);
fold = w;
break;
default:
/*
* Errors should be filtered in previous
* pass.
*/
Usage();
exit(1);
} /* switch */
} /* while */
do {
Usage();
exit(1);
}
optind++;
}
for (;;) {
if (c == EOF)
break;
(void) putch(c);
lastc = c;
}
if (col != 0) newline_init();
return (0);
}
static void
putch(int c)
{
switch (c) {
case '\n':
ncol = 0;
break;
case '\t':
if (bflg)
else
break;
case '\b':
if (bflg)
else
break;
case '\r':
if (bflg)
else
ncol = 0;
break;
default:
if (bflg)
else
}
/*
* 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.
*/
/*
* Need to check the last position for blank
*/
/*
* Save the output buffer
* as NULL has to be insert into the last
* sp position.
*/
(void) putwchar('\n');
/*
* Restore the output buffer to stuff
* NULL into the last sp position
* for the new line.
*/
} else {
(void) newline_init();
(void) putwchar('\n');
}
}
/* Output buffer is full ? */
/* Reach buffer limit */
if (col > 0) {
}
}
/* Store in output buffer */
switch (c) {
case '\n':
(void) newline_init();
break;
case '\t':
if (bflg)
else
}
break;
case '\b':
if (bflg)
else {
if (col)
col--;
}
break;
case '\r':
col = 0;
break;
default:
}
if (bflg)
else
break;
}
}
static
void
Usage()
{
"Usage: fold [-bs] [-w width | -width ] [file...]\n"));
}
static
void
{
}
static int
chr_width(c)
register int c;
{
register int n;
return (n > 0 ? n : 0);
}
static int
char *toptarg;
int *width;
{
char *p;
if (!toptarg)
goto badno;
*width = 0;
errno = 0;
if (*width == -1)
goto badno;
if (*p)
goto badno;
if (!*width)
goto badno;
return (0);
/* fold error message */
"Bad number for fold\n"));
Usage();
return (-1);
}