.
[coreutils.git] / src / fold.c
bloba36ce6c946abe5306cdb2bdc18c23848527bdde7
1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 1995-2004 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 "error.h"
28 #include "posixver.h"
29 #include "xstrtol.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "fold"
34 #define AUTHORS "David MacKenzie"
36 /* The name this program was run with. */
37 char *program_name;
39 /* If nonzero, try to break on whitespace. */
40 static bool break_spaces;
42 /* If nonzero, count bytes, not column positions. */
43 static bool count_bytes;
45 /* If nonzero, at least one of the files we read was standard input. */
46 static bool have_read_stdin;
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 {GETOPT_HELP_OPTION_DECL},
54 {GETOPT_VERSION_OPTION_DECL},
55 {NULL, 0, NULL, 0}
58 void
59 usage (int status)
61 if (status != EXIT_SUCCESS)
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 fputs (_("\
71 Wrap input lines in each FILE (standard input by default), writing to\n\
72 standard output.\n\
73 \n\
74 "), stdout);
75 fputs (_("\
76 Mandatory arguments to long options are mandatory for short options too.\n\
77 "), stdout);
78 fputs (_("\
79 -b, --bytes count bytes rather than columns\n\
80 -s, --spaces break at spaces\n\
81 -w, --width=WIDTH use WIDTH columns instead of 80\n\
82 "), stdout);
83 fputs (HELP_OPTION_DESCRIPTION, stdout);
84 fputs (VERSION_OPTION_DESCRIPTION, stdout);
85 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
87 exit (status);
90 /* Assuming the current column is COLUMN, return the column that
91 printing C will move the cursor to.
92 The first column is 0. */
94 static size_t
95 adjust_column (size_t column, char c)
97 if (!count_bytes)
99 if (c == '\b')
101 if (column > 0)
102 column--;
104 else if (c == '\r')
105 column = 0;
106 else if (c == '\t')
107 column = column + 8 - column % 8;
108 else /* if (isprint (c)) */
109 column++;
111 else
112 column++;
113 return column;
116 /* Fold file FILENAME, or standard input if FILENAME is "-",
117 to stdout, with maximum line length WIDTH.
118 Return 0 if successful, 1 if an error occurs. */
120 static int
121 fold_file (char *filename, int width)
123 FILE *istream;
124 register int c;
125 size_t column = 0; /* Screen column where next char will go. */
126 size_t offset_out = 0; /* Index in `line_out' for next char. */
127 static char *line_out = NULL;
128 static size_t allocated_out = 0;
129 int saved_errno;
131 if (STREQ (filename, "-"))
133 istream = stdin;
134 have_read_stdin = true;
136 else
137 istream = fopen (filename, "r");
139 if (istream == NULL)
141 error (0, errno, "%s", filename);
142 return 1;
145 while ((c = getc (istream)) != EOF)
147 if (offset_out + 1 >= allocated_out)
148 line_out = x2nrealloc (line_out, &allocated_out, sizeof *line_out);
150 if (c == '\n')
152 line_out[offset_out++] = c;
153 fwrite (line_out, sizeof (char), offset_out, stdout);
154 column = offset_out = 0;
155 continue;
158 rescan:
159 column = adjust_column (column, c);
161 if (column > width)
163 /* This character would make the line too long.
164 Print the line plus a newline, and make this character
165 start the next line. */
166 if (break_spaces)
168 bool found_blank = false;
169 size_t logical_end = offset_out;
171 /* Look for the last blank. */
172 while (logical_end)
174 --logical_end;
175 if (ISBLANK (line_out[logical_end]))
177 found_blank = true;
178 break;
182 if (found_blank)
184 size_t i;
186 /* Found a blank. Don't output the part after it. */
187 logical_end++;
188 fwrite (line_out, sizeof (char), (size_t) logical_end,
189 stdout);
190 putchar ('\n');
191 /* Move the remainder to the beginning of the next line.
192 The areas being copied here might overlap. */
193 memmove (line_out, line_out + logical_end,
194 offset_out - logical_end);
195 offset_out -= logical_end;
196 for (column = i = 0; i < offset_out; i++)
197 column = adjust_column (column, line_out[i]);
198 goto rescan;
202 if (offset_out == 0)
204 line_out[offset_out++] = c;
205 continue;
208 line_out[offset_out++] = '\n';
209 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
210 column = offset_out = 0;
211 goto rescan;
214 line_out[offset_out++] = c;
217 saved_errno = errno;
219 if (offset_out)
220 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
222 if (ferror (istream))
224 error (0, saved_errno, "%s", filename);
225 if (!STREQ (filename, "-"))
226 fclose (istream);
227 return 1;
229 if (!STREQ (filename, "-") && fclose (istream) == EOF)
231 error (0, errno, "%s", filename);
232 return 1;
235 return 0;
239 main (int argc, char **argv)
241 int width = 80;
242 int i;
243 int optc;
244 int errs = 0;
246 initialize_main (&argc, &argv);
247 program_name = argv[0];
248 setlocale (LC_ALL, "");
249 bindtextdomain (PACKAGE, LOCALEDIR);
250 textdomain (PACKAGE);
252 atexit (close_stdout);
254 break_spaces = count_bytes = have_read_stdin = false;
256 /* Turn any numeric options into -w options. */
257 for (i = 1; i < argc; i++)
259 char const *a = argv[i];
260 if (a[0] == '-')
262 if (a[1] == '-' && ! a[2])
263 break;
264 if (ISDIGIT (a[1]))
266 size_t len_a = strlen (a);
267 char *s = xmalloc (len_a + 2);
268 s[0] = '-';
269 s[1] = 'w';
270 memcpy (s + 2, a + 1, len_a);
271 argv[i] = s;
272 if (200112 <= posix2_version ())
274 error (0, 0, _("`%s' option is obsolete; use `%s'"), a, s);
275 usage (EXIT_FAILURE);
281 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
283 switch (optc)
285 case 0:
286 break;
288 case 'b': /* Count bytes rather than columns. */
289 count_bytes = true;
290 break;
292 case 's': /* Break at word boundaries. */
293 break_spaces = true;
294 break;
296 case 'w': /* Line width. */
298 long int tmp_long;
299 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
300 || tmp_long <= 0 || tmp_long > INT_MAX)
301 error (EXIT_FAILURE, 0,
302 _("invalid number of columns: `%s'"), optarg);
303 width = (int) tmp_long;
305 break;
307 case_GETOPT_HELP_CHAR;
309 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
311 default:
312 usage (EXIT_FAILURE);
316 if (argc == optind)
317 errs |= fold_file ("-", width);
318 else
319 for (i = optind; i < argc; i++)
320 errs |= fold_file (argv[i], width);
322 if (have_read_stdin && fclose (stdin) == EOF)
323 error (EXIT_FAILURE, errno, "-");
325 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);