Merge branch 'master' of ssh://swildner@crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / usr.bin / fold / fold.c
blob9bf98f2afdafb7c09d81d6684c67cf04661cec56
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kevin Ruddy.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)fold.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/fold/fold.c,v 1.4.2.3 2002/07/11 01:01:44 tjr Exp $
39 * $DragonFly: src/usr.bin/fold/fold.c,v 1.5 2006/08/13 02:15:07 swildner Exp $
42 #include <ctype.h>
43 #include <err.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #define DEFLINEWIDTH 80
53 void fold(int);
54 static int newpos(int, int);
55 static void usage(void);
57 int bflag; /* Count bytes, not columns */
58 int sflag; /* Split on word boundaries */
60 int
61 main(int argc, char **argv)
63 int ch;
64 int rval, width;
65 char *p;
67 (void) setlocale(LC_CTYPE, "");
69 width = -1;
70 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
71 switch (ch) {
72 case 'b':
73 bflag = 1;
74 break;
75 case 's':
76 sflag = 1;
77 break;
78 case 'w':
79 if ((width = atoi(optarg)) <= 0) {
80 errx(1, "illegal width value");
82 break;
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 if (width == -1) {
86 p = argv[optind - 1];
87 if (p[0] == '-' && p[1] == ch && !p[2])
88 width = atoi(++p);
89 else
90 width = atoi(argv[optind] + 1);
92 break;
93 default:
94 usage();
96 argv += optind;
97 argc -= optind;
99 if (width == -1)
100 width = DEFLINEWIDTH;
101 rval = 0;
102 if (!*argv)
103 fold(width);
104 else for (; *argv; ++argv)
105 if (!freopen(*argv, "r", stdin)) {
106 warn("%s", *argv);
107 rval = 1;
108 } else
109 fold(width);
110 exit(rval);
113 static void
114 usage(void)
116 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
117 exit(1);
121 * Fold the contents of standard input to fit within WIDTH columns (or bytes)
122 * and write to standard output.
124 * If sflag is set, split the line at the last space character on the line.
125 * This flag necessitates storing the line in a buffer until the current
126 * column > width, or a newline or EOF is read.
128 * The buffer can grow larger than WIDTH due to backspaces and carriage
129 * returns embedded in the input stream.
131 void
132 fold(int width)
134 static char *buf;
135 static int buf_max;
136 int ch, col, i, indx, space;
138 col = indx = space = 0;
139 while ((ch = getchar()) != EOF) {
140 if (ch == '\n') {
141 if (indx != 0)
142 fwrite(buf, 1, indx, stdout);
143 putchar('\n');
144 col = indx = 0;
145 continue;
147 if ((col = newpos(col, ch)) > width) {
148 if (sflag) {
149 i = indx;
150 while (--i >= 0 && !isblank((unsigned char)buf[i]))
152 space = i;
154 if (sflag && space != -1) {
155 space++;
156 fwrite(buf, 1, space, stdout);
157 memmove(buf, buf + space, indx - space);
158 indx -= space;
159 col = 0;
160 for (i = 0; i < indx; i++)
161 col = newpos(col,
162 (unsigned char)buf[i]);
163 } else {
164 fwrite(buf, 1, indx, stdout);
165 col = indx = 0;
167 putchar('\n');
168 col = newpos(col, ch);
170 if (indx + 1 > buf_max) {
171 buf_max += LINE_MAX;
172 if ((buf = realloc(buf, buf_max)) == NULL)
173 err(1, "realloc()");
175 buf[indx++] = ch;
178 if (indx != 0)
179 fwrite(buf, 1, indx, stdout);
183 * Update the current column position for a character.
185 static int
186 newpos(int col, int ch)
189 if (bflag)
190 ++col;
191 else
192 switch (ch) {
193 case '\b':
194 if (col > 0)
195 --col;
196 break;
197 case '\r':
198 col = 0;
199 break;
200 case '\t':
201 col = (col + 8) & ~7;
202 break;
203 default:
204 if (isprint(ch))
205 ++col;
206 break;
209 return (col);