Note that fcntl(2) returns EINVAL for invalid command.
[netbsd-mini2440.git] / usr.bin / fold / fold.c
blob7359a71b6d5d874ab26262dd2d9d360339e566d7
1 /* $NetBSD: fold.c,v 1.14 2008/07/21 14:19:22 lukem Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kevin Ruddy.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93";
44 #endif
45 __RCSID("$NetBSD: fold.c,v 1.14 2008/07/21 14:19:22 lukem Exp $");
46 #endif /* not lint */
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <err.h>
54 #define DEFLINEWIDTH 80
56 int main(int, char **);
57 static void fold(int);
58 static int new_column_position(int, int);
59 static void usage(void);
61 int count_bytes = 0;
62 int split_words = 0;
64 int
65 main(int argc, char **argv)
67 int ch;
68 int width;
69 char *p;
71 width = -1;
72 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
73 switch (ch) {
74 case 'b':
75 count_bytes = 1;
76 break;
77 case 's':
78 split_words = 1;
79 break;
80 case 'w':
81 if ((width = atoi(optarg)) <= 0)
82 errx(1, "illegal width value");
83 break;
84 case '0': case '1': case '2': case '3': case '4':
85 case '5': case '6': case '7': case '8': case '9':
86 if (width == -1) {
87 p = argv[optind - 1];
88 if (p[0] == '-' && p[1] == ch && !p[2])
89 width = atoi(++p);
90 else
91 width = atoi(argv[optind] + 1);
93 break;
94 default:
95 usage();
97 argv += optind;
98 argc -= optind;
100 if (width == -1)
101 width = DEFLINEWIDTH;
103 if (!*argv)
104 fold(width);
105 else for (; *argv; ++argv)
106 if (!freopen(*argv, "r", stdin)) {
107 err (1, "%s", *argv);
108 /* NOTREACHED */
109 } else
110 fold(width);
111 exit(0);
115 * Fold the contents of standard input to fit within WIDTH columns
116 * (or bytes) and write to standard output.
118 * If split_words is set, split the line at the last space character
119 * on the line. This flag necessitates storing the line in a buffer
120 * until the current column > width, or a newline or EOF is read.
122 * The buffer can grow larger than WIDTH due to backspaces and carriage
123 * returns embedded in the input stream.
125 static void
126 fold(int width)
128 static char *buf = NULL;
129 char *nbuf;
130 static int buf_max = 0;
131 int ch, col;
132 int indx;
134 col = indx = 0;
135 while ((ch = getchar()) != EOF) {
136 if (ch == '\n') {
137 if (indx != 0)
138 fwrite (buf, 1, indx, stdout);
139 putchar('\n');
140 col = indx = 0;
141 continue;
144 col = new_column_position (col, ch);
145 if (col > width) {
146 int i, last_space;
148 #ifdef __GNUC__
149 last_space = 0; /* XXX gcc */
150 #endif
151 if (split_words) {
152 for (i = 0, last_space = -1; i < indx; i++)
153 if (buf[i] == ' ')
154 last_space = i;
157 if (split_words && last_space != -1) {
158 fwrite (buf, 1, last_space, stdout);
160 /* increase last_space here, so we skip trailing whitespace */
161 last_space++;
162 memmove (buf, buf+last_space, indx-last_space);
164 indx -= last_space;
165 col = 0;
166 for (i = 0; i < indx; i++) {
167 col = new_column_position (col, buf[i]);
169 } else {
170 fwrite (buf, 1, indx, stdout);
171 col = indx = 0;
173 putchar('\n');
175 /* calculate the column position for the next line. */
176 col = new_column_position (col, ch);
179 if (indx + 1 > buf_max) {
180 /* Allocate buffer in LINE_MAX increments */
181 if ((nbuf = realloc (buf, buf_max + 2048)) == NULL) {
182 err (1, "realloc");
183 /* NOTREACHED */
185 buf = nbuf;
186 buf_max += 2048;
188 buf[indx++] = ch;
191 if (indx != 0)
192 fwrite (buf, 1, indx, stdout);
196 * calculate the column position
198 static int
199 new_column_position (int col, int ch)
201 if (!count_bytes) {
202 switch (ch) {
203 case '\b':
204 if (col > 0)
205 --col;
206 break;
207 case '\r':
208 col = 0;
209 break;
210 case '\t':
211 col = (col + 8) & ~7;
212 break;
213 default:
214 ++col;
215 break;
217 } else {
218 ++col;
221 return col;
224 static void
225 usage(void)
227 (void)fprintf(stderr,
228 "usage: fold [-bs] [-w width] [file ...]\n");
229 exit(1);