140768Sbostic /*-
2*61999Sbostic * Copyright (c) 1990, 1993
3*61999Sbostic * The Regents of the University of California. All rights reserved.
434911Sbostic *
540768Sbostic * This code is derived from software contributed to Berkeley by
640768Sbostic * Kevin Ruddy.
740768Sbostic *
842731Sbostic * %sccs.include.redist.c%
921555Sdist */
1021555Sdist
1121555Sdist #ifndef lint
12*61999Sbostic static char copyright[] =
13*61999Sbostic "@(#) Copyright (c) 1990, 1993\n\
14*61999Sbostic The Regents of the University of California. All rights reserved.\n";
1534911Sbostic #endif /* not lint */
1621555Sdist
1721555Sdist #ifndef lint
18*61999Sbostic static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 06/06/93";
1934911Sbostic #endif /* not lint */
2021555Sdist
211018Sbill #include <stdio.h>
2242050Sbostic #include <string.h>
231018Sbill
2440768Sbostic #define DEFLINEWIDTH 80
251018Sbill
main(argc,argv)261018Sbill main(argc, argv)
271018Sbill int argc;
2840768Sbostic char **argv;
291018Sbill {
3040768Sbostic extern int errno, optind;
3140768Sbostic extern char *optarg;
3240768Sbostic register int ch;
3340768Sbostic int width;
3440768Sbostic char *p;
351018Sbill
3640768Sbostic width = -1;
3740768Sbostic while ((ch = getopt(argc, argv, "0123456789w:")) != EOF)
3840768Sbostic switch (ch) {
3940768Sbostic case 'w':
4040768Sbostic if ((width = atoi(optarg)) <= 0) {
4140768Sbostic (void)fprintf(stderr,
4240768Sbostic "fold: illegal width value.\n");
431018Sbill exit(1);
441018Sbill }
4540768Sbostic break;
4640768Sbostic case '0': case '1': case '2': case '3': case '4':
4740768Sbostic case '5': case '6': case '7': case '8': case '9':
4840768Sbostic if (width == -1) {
4940768Sbostic p = argv[optind - 1];
5040768Sbostic if (p[0] == '-' && p[1] == ch && !p[2])
5140768Sbostic width = atoi(++p);
5240768Sbostic else
5340768Sbostic width = atoi(argv[optind] + 1);
5440768Sbostic }
5540768Sbostic break;
5640768Sbostic default:
5740768Sbostic (void)fprintf(stderr,
5840768Sbostic "usage: fold [-w width] [file ...]\n");
5940768Sbostic exit(1);
601018Sbill }
6140768Sbostic argv += optind;
6240768Sbostic argc -= optind;
6340768Sbostic
6440768Sbostic if (width == -1)
6540768Sbostic width = DEFLINEWIDTH;
6640768Sbostic if (!*argv)
6740768Sbostic fold(width);
6840768Sbostic else for (; *argv; ++argv)
6940768Sbostic if (!freopen(*argv, "r", stdin)) {
7040768Sbostic (void)fprintf(stderr,
7140768Sbostic "fold: %s: %s\n", *argv, strerror(errno));
7240768Sbostic exit(1);
7340768Sbostic } else
7440768Sbostic fold(width);
751018Sbill exit(0);
761018Sbill }
771018Sbill
fold(width)7840768Sbostic fold(width)
7940768Sbostic register int width;
801018Sbill {
8140768Sbostic register int ch, col, new;
821018Sbill
8340768Sbostic for (col = 0;;) {
8440768Sbostic switch (ch = getchar()) {
8540768Sbostic case EOF:
8640768Sbostic return;
8740768Sbostic case '\b':
8840768Sbostic new = col ? col - 1 : 0;
8940768Sbostic break;
901018Sbill case '\n':
9140768Sbostic case '\r':
9240768Sbostic new = 0;
931018Sbill break;
941018Sbill case '\t':
9540768Sbostic new = (col + 8) & ~7;
961018Sbill break;
9740768Sbostic default:
9840768Sbostic new = col + 1;
9940768Sbostic break;
10040768Sbostic }
10140768Sbostic
10240768Sbostic if (new > width) {
10340768Sbostic putchar('\n');
10440768Sbostic col = 0;
10540768Sbostic }
10640768Sbostic putchar(ch);
10740768Sbostic
10840768Sbostic switch (ch) {
1091018Sbill case '\b':
11040768Sbostic if (col > 0)
11140768Sbostic --col;
1121018Sbill break;
11340768Sbostic case '\n':
1141018Sbill case '\r':
1151018Sbill col = 0;
1161018Sbill break;
1171018Sbill case '\t':
11810821Ssam col += 8;
11910821Ssam col &= ~7;
1201018Sbill break;
1211018Sbill default:
12240768Sbostic ++col;
1231018Sbill break;
12440768Sbostic }
1251018Sbill }
1261018Sbill }
127