*** empty log message ***
[coreutils.git] / src / fold.c
bloba8699c66b1d1941d240637a4d71182b36c90b98e
1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 95, 96, 1997, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
20 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
26 #include "system.h"
27 #include "xstrtol.h"
28 #include "error.h"
30 /* The name this program was run with. */
31 char *program_name;
33 /* If nonzero, try to break on whitespace. */
34 static int break_spaces;
36 /* If nonzero, count bytes, not column positions. */
37 static int count_bytes;
39 /* If nonzero, at least one of the files we read was standard input. */
40 static int have_read_stdin;
42 /* If nonzero, display usage information and exit. */
43 static int show_help;
45 /* If nonzero, print the version on standard output then exit. */
46 static int show_version;
48 static struct option const longopts[] =
50 {"bytes", no_argument, NULL, 'b'},
51 {"spaces", no_argument, NULL, 's'},
52 {"width", required_argument, NULL, 'w'},
53 {"help", no_argument, &show_help, 1},
54 {"version", no_argument, &show_version, 1},
55 {NULL, 0, NULL, 0}
58 static void
59 usage (int status)
61 if (status != 0)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("\
67 Usage: %s [OPTION]... [FILE]...\n\
68 "),
69 program_name);
70 printf (_("\
71 Wrap input lines in each FILE (standard input by default), writing to\n\
72 standard output.\n\
73 \n\
74 -b, --bytes count bytes rather than columns\n\
75 -s, --spaces break at spaces\n\
76 -w, --width=WIDTH use WIDTH columns instead of 80\n\
77 "));
78 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
80 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
83 /* Assuming the current column is COLUMN, return the column that
84 printing C will move the cursor to.
85 The first column is 0. */
87 static int
88 adjust_column (int column, char c)
90 if (!count_bytes)
92 if (c == '\b')
94 if (column > 0)
95 column--;
97 else if (c == '\r')
98 column = 0;
99 else if (c == '\t')
100 column = column + 8 - column % 8;
101 else /* if (isprint (c)) */
102 column++;
104 else
105 column++;
106 return column;
109 /* Fold file FILENAME, or standard input if FILENAME is "-",
110 to stdout, with maximum line length WIDTH.
111 Return 0 if successful, 1 if an error occurs. */
113 static int
114 fold_file (char *filename, int width)
116 FILE *istream;
117 register int c;
118 int column = 0; /* Screen column where next char will go. */
119 int offset_out = 0; /* Index in `line_out' for next char. */
120 static char *line_out = NULL;
121 static int allocated_out = 0;
123 if (STREQ (filename, "-"))
125 istream = stdin;
126 have_read_stdin = 1;
128 else
129 istream = fopen (filename, "r");
131 if (istream == NULL)
133 error (0, errno, "%s", filename);
134 return 1;
137 while ((c = getc (istream)) != EOF)
139 if (offset_out + 1 >= allocated_out)
141 allocated_out += 1024;
142 line_out = xrealloc (line_out, allocated_out);
145 if (c == '\n')
147 line_out[offset_out++] = c;
148 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
149 column = offset_out = 0;
150 continue;
153 rescan:
154 column = adjust_column (column, c);
156 if (column > width)
158 /* This character would make the line too long.
159 Print the line plus a newline, and make this character
160 start the next line. */
161 if (break_spaces)
163 /* Look for the last blank. */
164 int logical_end;
166 for (logical_end = offset_out - 1; logical_end >= 0;
167 logical_end--)
168 if (ISBLANK (line_out[logical_end]))
169 break;
170 if (logical_end >= 0)
172 int i;
174 /* Found a blank. Don't output the part after it. */
175 logical_end++;
176 fwrite (line_out, sizeof (char), (size_t) logical_end,
177 stdout);
178 putchar ('\n');
179 /* Move the remainder to the beginning of the next line.
180 The areas being copied here might overlap. */
181 memmove (line_out, line_out + logical_end,
182 offset_out - logical_end);
183 offset_out -= logical_end;
184 for (column = i = 0; i < offset_out; i++)
185 column = adjust_column (column, line_out[i]);
186 goto rescan;
189 else
191 if (offset_out == 0)
193 line_out[offset_out++] = c;
194 continue;
197 line_out[offset_out++] = '\n';
198 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
199 column = offset_out = 0;
200 goto rescan;
203 line_out[offset_out++] = c;
206 if (offset_out)
207 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
209 if (ferror (istream))
211 error (0, errno, "%s", filename);
212 if (!STREQ (filename, "-"))
213 fclose (istream);
214 return 1;
216 if (!STREQ (filename, "-") && fclose (istream) == EOF)
218 error (0, errno, "%s", filename);
219 return 1;
222 if (ferror (stdout))
224 error (0, errno, _("write error"));
225 return 1;
228 return 0;
232 main (int argc, char **argv)
234 int width = 80;
235 int i;
236 int optc;
237 int errs = 0;
239 program_name = argv[0];
240 setlocale (LC_ALL, "");
241 bindtextdomain (PACKAGE, LOCALEDIR);
242 textdomain (PACKAGE);
244 break_spaces = count_bytes = have_read_stdin = 0;
246 /* Turn any numeric options into -w options. */
247 for (i = 1; i < argc; i++)
249 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
251 char *s;
253 s = xmalloc (strlen (argv[i]) + 2);
254 s[0] = '-';
255 s[1] = 'w';
256 strcpy (s + 2, argv[i] + 1);
257 argv[i] = s;
261 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
263 switch (optc)
265 case 0:
266 break;
268 case 'b': /* Count bytes rather than columns. */
269 count_bytes = 1;
270 break;
272 case 's': /* Break at word boundaries. */
273 break_spaces = 1;
274 break;
276 case 'w': /* Line width. */
278 long int tmp_long;
279 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
280 || tmp_long <= 0 || tmp_long > INT_MAX)
281 error (EXIT_FAILURE, 0,
282 _("invalid number of columns: `%s'"), optarg);
283 width = (int) tmp_long;
285 break;
287 default:
288 usage (1);
292 if (show_version)
294 printf ("fold (%s) %s\n", GNU_PACKAGE, VERSION);
295 exit (EXIT_SUCCESS);
298 if (show_help)
299 usage (0);
301 if (argc == optind)
302 errs |= fold_file ("-", width);
303 else
304 for (i = optind; i < argc; i++)
305 errs |= fold_file (argv[i], width);
307 if (have_read_stdin && fclose (stdin) == EOF)
308 error (EXIT_FAILURE, errno, "-");
309 if (fclose (stdout) == EOF)
310 error (EXIT_FAILURE, errno, _("write error"));
312 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);