fold.c revision 27270
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Kevin Ruddy.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3827270Scharnierstatic const char copyright[] =
391590Srgrimes"@(#) Copyright (c) 1990, 1993\n\
401590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411590Srgrimes#endif /* not lint */
421590Srgrimes
431590Srgrimes#ifndef lint
4427270Scharnier#if 0
451590Srgrimesstatic char sccsid[] = "@(#)fold.c	8.1 (Berkeley) 6/6/93";
4627270Scharnier#endif
4727270Scharnierstatic const char rcsid[] =
4827270Scharnier	"$Id$";
491590Srgrimes#endif /* not lint */
501590Srgrimes
5127270Scharnier#include <err.h>
521590Srgrimes#include <stdio.h>
5327270Scharnier#include <stdlib.h>
541590Srgrimes#include <string.h>
5527270Scharnier#include <unistd.h>
561590Srgrimes
571590Srgrimes#define	DEFLINEWIDTH	80
581590Srgrimes
5927270Scharniervoid fold __P((int));
6027270Scharnierstatic void usage __P((void));
6127270Scharnier
6227270Scharnierint
631590Srgrimesmain(argc, argv)
641590Srgrimes	int argc;
651590Srgrimes	char **argv;
661590Srgrimes{
6727270Scharnier	extern int optind;
681590Srgrimes	extern char *optarg;
691590Srgrimes	register int ch;
701590Srgrimes	int width;
711590Srgrimes	char *p;
721590Srgrimes
731590Srgrimes	width = -1;
7424360Simp	while ((ch = getopt(argc, argv, "0123456789w:")) != -1)
751590Srgrimes		switch (ch) {
761590Srgrimes		case 'w':
771590Srgrimes			if ((width = atoi(optarg)) <= 0) {
7827270Scharnier				errx(1, "illegal width value");
791590Srgrimes			}
801590Srgrimes			break;
811590Srgrimes		case '0': case '1': case '2': case '3': case '4':
821590Srgrimes		case '5': case '6': case '7': case '8': case '9':
831590Srgrimes			if (width == -1) {
841590Srgrimes				p = argv[optind - 1];
851590Srgrimes				if (p[0] == '-' && p[1] == ch && !p[2])
861590Srgrimes					width = atoi(++p);
871590Srgrimes				else
881590Srgrimes					width = atoi(argv[optind] + 1);
891590Srgrimes			}
901590Srgrimes			break;
911590Srgrimes		default:
9227270Scharnier			usage();
931590Srgrimes		}
941590Srgrimes	argv += optind;
951590Srgrimes	argc -= optind;
961590Srgrimes
971590Srgrimes	if (width == -1)
981590Srgrimes		width = DEFLINEWIDTH;
991590Srgrimes	if (!*argv)
1001590Srgrimes		fold(width);
1011590Srgrimes	else for (; *argv; ++argv)
1021590Srgrimes		if (!freopen(*argv, "r", stdin)) {
10327270Scharnier			err(1, "%s", *argv);
1041590Srgrimes		} else
1051590Srgrimes			fold(width);
1061590Srgrimes	exit(0);
1071590Srgrimes}
1081590Srgrimes
10927270Scharnierstatic void
11027270Scharnierusage()
11127270Scharnier{
11227270Scharnier	(void)fprintf(stderr, "usage: fold [-w width] [file ...]\n");
11327270Scharnier	exit(1);
11427270Scharnier}
11527270Scharnier
11627270Scharniervoid
1171590Srgrimesfold(width)
1181590Srgrimes	register int width;
1191590Srgrimes{
1201590Srgrimes	register int ch, col, new;
1211590Srgrimes
1221590Srgrimes	for (col = 0;;) {
1231590Srgrimes		switch (ch = getchar()) {
1241590Srgrimes		case EOF:
1251590Srgrimes			return;
1261590Srgrimes		case '\b':
1271590Srgrimes			new = col ? col - 1 : 0;
1281590Srgrimes			break;
1291590Srgrimes		case '\n':
1301590Srgrimes		case '\r':
1311590Srgrimes			new = 0;
1321590Srgrimes			break;
1331590Srgrimes		case '\t':
1341590Srgrimes			new = (col + 8) & ~7;
1351590Srgrimes			break;
1361590Srgrimes		default:
1371590Srgrimes			new = col + 1;
1381590Srgrimes			break;
1391590Srgrimes		}
1401590Srgrimes
1411590Srgrimes		if (new > width) {
1421590Srgrimes			putchar('\n');
1431590Srgrimes			col = 0;
1441590Srgrimes		}
1451590Srgrimes		putchar(ch);
1461590Srgrimes
1471590Srgrimes		switch (ch) {
1481590Srgrimes		case '\b':
1491590Srgrimes			if (col > 0)
1501590Srgrimes				--col;
1511590Srgrimes			break;
1521590Srgrimes		case '\n':
1531590Srgrimes		case '\r':
1541590Srgrimes			col = 0;
1551590Srgrimes			break;
1561590Srgrimes		case '\t':
1571590Srgrimes			col += 8;
1581590Srgrimes			col &= ~7;
1591590Srgrimes			break;
1601590Srgrimes		default:
1611590Srgrimes			++col;
1621590Srgrimes			break;
1631590Srgrimes		}
1641590Srgrimes	}
1651590Srgrimes}
166