xref: /csrg-svn/usr.bin/fold/fold.c (revision 42050)
140768Sbostic /*-
240768Sbostic  * Copyright (c) 1990 The Regents of the University of California.
334911Sbostic  * All rights reserved.
434911Sbostic  *
540768Sbostic  * This code is derived from software contributed to Berkeley by
640768Sbostic  * Kevin Ruddy.
740768Sbostic  *
834911Sbostic  * Redistribution and use in source and binary forms are permitted
934911Sbostic  * provided that the above copyright notice and this paragraph are
1034911Sbostic  * duplicated in all such forms and that any documentation,
1134911Sbostic  * advertising materials, and other materials related to such
1234911Sbostic  * distribution and use acknowledge that the software was developed
1334911Sbostic  * by the University of California, Berkeley.  The name of the
1434911Sbostic  * University may not be used to endorse or promote products derived
1534911Sbostic  * from this software without specific prior written permission.
1634911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1840768Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1921555Sdist  */
2021555Sdist 
2121555Sdist #ifndef lint
2221555Sdist char copyright[] =
2340768Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
2421555Sdist  All rights reserved.\n";
2534911Sbostic #endif /* not lint */
2621555Sdist 
2721555Sdist #ifndef lint
28*42050Sbostic static char sccsid[] = "@(#)fold.c	5.4 (Berkeley) 05/15/90";
2934911Sbostic #endif /* not lint */
3021555Sdist 
311018Sbill #include <stdio.h>
32*42050Sbostic #include <string.h>
331018Sbill 
3440768Sbostic #define	DEFLINEWIDTH	80
351018Sbill 
361018Sbill main(argc, argv)
371018Sbill 	int argc;
3840768Sbostic 	char **argv;
391018Sbill {
4040768Sbostic 	extern int errno, optind;
4140768Sbostic 	extern char *optarg;
4240768Sbostic 	register int ch;
4340768Sbostic 	int width;
4440768Sbostic 	char *p;
451018Sbill 
4640768Sbostic 	width = -1;
4740768Sbostic 	while ((ch = getopt(argc, argv, "0123456789w:")) != EOF)
4840768Sbostic 		switch (ch) {
4940768Sbostic 		case 'w':
5040768Sbostic 			if ((width = atoi(optarg)) <= 0) {
5140768Sbostic 				(void)fprintf(stderr,
5240768Sbostic 				    "fold: illegal width value.\n");
531018Sbill 				exit(1);
541018Sbill 			}
5540768Sbostic 			break;
5640768Sbostic 		case '0': case '1': case '2': case '3': case '4':
5740768Sbostic 		case '5': case '6': case '7': case '8': case '9':
5840768Sbostic 			if (width == -1) {
5940768Sbostic 				p = argv[optind - 1];
6040768Sbostic 				if (p[0] == '-' && p[1] == ch && !p[2])
6140768Sbostic 					width = atoi(++p);
6240768Sbostic 				else
6340768Sbostic 					width = atoi(argv[optind] + 1);
6440768Sbostic 			}
6540768Sbostic 			break;
6640768Sbostic 		default:
6740768Sbostic 			(void)fprintf(stderr,
6840768Sbostic 			    "usage: fold [-w width] [file ...]\n");
6940768Sbostic 			exit(1);
701018Sbill 		}
7140768Sbostic 	argv += optind;
7240768Sbostic 	argc -= optind;
7340768Sbostic 
7440768Sbostic 	if (width == -1)
7540768Sbostic 		width = DEFLINEWIDTH;
7640768Sbostic 	if (!*argv)
7740768Sbostic 		fold(width);
7840768Sbostic 	else for (; *argv; ++argv)
7940768Sbostic 		if (!freopen(*argv, "r", stdin)) {
8040768Sbostic 			(void)fprintf(stderr,
8140768Sbostic 			    "fold: %s: %s\n", *argv, strerror(errno));
8240768Sbostic 			exit(1);
8340768Sbostic 		} else
8440768Sbostic 			fold(width);
851018Sbill 	exit(0);
861018Sbill }
871018Sbill 
8840768Sbostic fold(width)
8940768Sbostic 	register int width;
901018Sbill {
9140768Sbostic 	register int ch, col, new;
921018Sbill 
9340768Sbostic 	for (col = 0;;) {
9440768Sbostic 		switch (ch = getchar()) {
9540768Sbostic 		case EOF:
9640768Sbostic 			return;
9740768Sbostic 		case '\b':
9840768Sbostic 			new = col ? col - 1 : 0;
9940768Sbostic 			break;
1001018Sbill 		case '\n':
10140768Sbostic 		case '\r':
10240768Sbostic 			new = 0;
1031018Sbill 			break;
1041018Sbill 		case '\t':
10540768Sbostic 			new = (col + 8) & ~7;
1061018Sbill 			break;
10740768Sbostic 		default:
10840768Sbostic 			new = col + 1;
10940768Sbostic 			break;
11040768Sbostic 		}
11140768Sbostic 
11240768Sbostic 		if (new > width) {
11340768Sbostic 			putchar('\n');
11440768Sbostic 			col = 0;
11540768Sbostic 		}
11640768Sbostic 		putchar(ch);
11740768Sbostic 
11840768Sbostic 		switch (ch) {
1191018Sbill 		case '\b':
12040768Sbostic 			if (col > 0)
12140768Sbostic 				--col;
1221018Sbill 			break;
12340768Sbostic 		case '\n':
1241018Sbill 		case '\r':
1251018Sbill 			col = 0;
1261018Sbill 			break;
1271018Sbill 		case '\t':
12810821Ssam 			col += 8;
12910821Ssam 			col &= ~7;
1301018Sbill 			break;
1311018Sbill 		default:
13240768Sbostic 			++col;
1331018Sbill 			break;
13440768Sbostic 		}
1351018Sbill 	}
1361018Sbill }
137