fold.c revision 1590
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
381590Srgrimesstatic 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
441590Srgrimesstatic char sccsid[] = "@(#)fold.c	8.1 (Berkeley) 6/6/93";
451590Srgrimes#endif /* not lint */
461590Srgrimes
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <string.h>
491590Srgrimes
501590Srgrimes#define	DEFLINEWIDTH	80
511590Srgrimes
521590Srgrimesmain(argc, argv)
531590Srgrimes	int argc;
541590Srgrimes	char **argv;
551590Srgrimes{
561590Srgrimes	extern int errno, optind;
571590Srgrimes	extern char *optarg;
581590Srgrimes	register int ch;
591590Srgrimes	int width;
601590Srgrimes	char *p;
611590Srgrimes
621590Srgrimes	width = -1;
631590Srgrimes	while ((ch = getopt(argc, argv, "0123456789w:")) != EOF)
641590Srgrimes		switch (ch) {
651590Srgrimes		case 'w':
661590Srgrimes			if ((width = atoi(optarg)) <= 0) {
671590Srgrimes				(void)fprintf(stderr,
681590Srgrimes				    "fold: illegal width value.\n");
691590Srgrimes				exit(1);
701590Srgrimes			}
711590Srgrimes			break;
721590Srgrimes		case '0': case '1': case '2': case '3': case '4':
731590Srgrimes		case '5': case '6': case '7': case '8': case '9':
741590Srgrimes			if (width == -1) {
751590Srgrimes				p = argv[optind - 1];
761590Srgrimes				if (p[0] == '-' && p[1] == ch && !p[2])
771590Srgrimes					width = atoi(++p);
781590Srgrimes				else
791590Srgrimes					width = atoi(argv[optind] + 1);
801590Srgrimes			}
811590Srgrimes			break;
821590Srgrimes		default:
831590Srgrimes			(void)fprintf(stderr,
841590Srgrimes			    "usage: fold [-w width] [file ...]\n");
851590Srgrimes			exit(1);
861590Srgrimes		}
871590Srgrimes	argv += optind;
881590Srgrimes	argc -= optind;
891590Srgrimes
901590Srgrimes	if (width == -1)
911590Srgrimes		width = DEFLINEWIDTH;
921590Srgrimes	if (!*argv)
931590Srgrimes		fold(width);
941590Srgrimes	else for (; *argv; ++argv)
951590Srgrimes		if (!freopen(*argv, "r", stdin)) {
961590Srgrimes			(void)fprintf(stderr,
971590Srgrimes			    "fold: %s: %s\n", *argv, strerror(errno));
981590Srgrimes			exit(1);
991590Srgrimes		} else
1001590Srgrimes			fold(width);
1011590Srgrimes	exit(0);
1021590Srgrimes}
1031590Srgrimes
1041590Srgrimesfold(width)
1051590Srgrimes	register int width;
1061590Srgrimes{
1071590Srgrimes	register int ch, col, new;
1081590Srgrimes
1091590Srgrimes	for (col = 0;;) {
1101590Srgrimes		switch (ch = getchar()) {
1111590Srgrimes		case EOF:
1121590Srgrimes			return;
1131590Srgrimes		case '\b':
1141590Srgrimes			new = col ? col - 1 : 0;
1151590Srgrimes			break;
1161590Srgrimes		case '\n':
1171590Srgrimes		case '\r':
1181590Srgrimes			new = 0;
1191590Srgrimes			break;
1201590Srgrimes		case '\t':
1211590Srgrimes			new = (col + 8) & ~7;
1221590Srgrimes			break;
1231590Srgrimes		default:
1241590Srgrimes			new = col + 1;
1251590Srgrimes			break;
1261590Srgrimes		}
1271590Srgrimes
1281590Srgrimes		if (new > width) {
1291590Srgrimes			putchar('\n');
1301590Srgrimes			col = 0;
1311590Srgrimes		}
1321590Srgrimes		putchar(ch);
1331590Srgrimes
1341590Srgrimes		switch (ch) {
1351590Srgrimes		case '\b':
1361590Srgrimes			if (col > 0)
1371590Srgrimes				--col;
1381590Srgrimes			break;
1391590Srgrimes		case '\n':
1401590Srgrimes		case '\r':
1411590Srgrimes			col = 0;
1421590Srgrimes			break;
1431590Srgrimes		case '\t':
1441590Srgrimes			col += 8;
1451590Srgrimes			col &= ~7;
1461590Srgrimes			break;
1471590Srgrimes		default:
1481590Srgrimes			++col;
1491590Srgrimes			break;
1501590Srgrimes		}
1511590Srgrimes	}
1521590Srgrimes}
153