(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / fold.c
bloba9b25f66243361d926cbaf6e05530da89aa404b5
1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 1995-1999 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 "xstrtol.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "fold"
33 #define AUTHORS "David MacKenzie"
35 /* The name this program was run with. */
36 char *program_name;
38 /* If nonzero, try to break on whitespace. */
39 static int break_spaces;
41 /* If nonzero, count bytes, not column positions. */
42 static int count_bytes;
44 /* If nonzero, at least one of the files we read was standard input. */
45 static int have_read_stdin;
47 static struct option const longopts[] =
49 {"bytes", no_argument, NULL, 'b'},
50 {"spaces", no_argument, NULL, 's'},
51 {"width", required_argument, NULL, 'w'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("\
66 Usage: %s [OPTION]... [FILE]...\n\
67 "),
68 program_name);
69 printf (_("\
70 Wrap input lines in each FILE (standard input by default), writing to\n\
71 standard output.\n\
72 \n\
73 -b, --bytes count bytes rather than columns\n\
74 -s, --spaces break at spaces\n\
75 -w, --width=WIDTH use WIDTH columns instead of 80\n\
76 --help display this help and exit\n\
77 --version output version information and exit\n\
78 "));
79 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
81 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
84 /* Assuming the current column is COLUMN, return the column that
85 printing C will move the cursor to.
86 The first column is 0. */
88 static int
89 adjust_column (int column, char c)
91 if (!count_bytes)
93 if (c == '\b')
95 if (column > 0)
96 column--;
98 else if (c == '\r')
99 column = 0;
100 else if (c == '\t')
101 column = column + 8 - column % 8;
102 else /* if (isprint (c)) */
103 column++;
105 else
106 column++;
107 return column;
110 /* Fold file FILENAME, or standard input if FILENAME is "-",
111 to stdout, with maximum line length WIDTH.
112 Return 0 if successful, 1 if an error occurs. */
114 static int
115 fold_file (char *filename, int width)
117 FILE *istream;
118 register int c;
119 int column = 0; /* Screen column where next char will go. */
120 int offset_out = 0; /* Index in `line_out' for next char. */
121 static char *line_out = NULL;
122 static int allocated_out = 0;
124 if (STREQ (filename, "-"))
126 istream = stdin;
127 have_read_stdin = 1;
129 else
130 istream = fopen (filename, "r");
132 if (istream == NULL)
134 error (0, errno, "%s", filename);
135 return 1;
138 while ((c = getc (istream)) != EOF)
140 if (offset_out + 1 >= allocated_out)
142 allocated_out += 1024;
143 line_out = xrealloc (line_out, allocated_out);
146 if (c == '\n')
148 line_out[offset_out++] = c;
149 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
150 column = offset_out = 0;
151 continue;
154 rescan:
155 column = adjust_column (column, c);
157 if (column > width)
159 /* This character would make the line too long.
160 Print the line plus a newline, and make this character
161 start the next line. */
162 if (break_spaces)
164 /* Look for the last blank. */
165 int logical_end;
167 for (logical_end = offset_out - 1; logical_end >= 0;
168 logical_end--)
169 if (ISBLANK (line_out[logical_end]))
170 break;
171 if (logical_end >= 0)
173 int i;
175 /* Found a blank. Don't output the part after it. */
176 logical_end++;
177 fwrite (line_out, sizeof (char), (size_t) logical_end,
178 stdout);
179 putchar ('\n');
180 /* Move the remainder to the beginning of the next line.
181 The areas being copied here might overlap. */
182 memmove (line_out, line_out + logical_end,
183 offset_out - logical_end);
184 offset_out -= logical_end;
185 for (column = i = 0; i < offset_out; i++)
186 column = adjust_column (column, line_out[i]);
187 goto rescan;
190 else
192 if (offset_out == 0)
194 line_out[offset_out++] = c;
195 continue;
198 line_out[offset_out++] = '\n';
199 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
200 column = offset_out = 0;
201 goto rescan;
204 line_out[offset_out++] = c;
207 if (offset_out)
208 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
210 if (ferror (istream))
212 error (0, errno, "%s", filename);
213 if (!STREQ (filename, "-"))
214 fclose (istream);
215 return 1;
217 if (!STREQ (filename, "-") && fclose (istream) == EOF)
219 error (0, errno, "%s", filename);
220 return 1;
223 if (ferror (stdout))
225 error (0, errno, _("write error"));
226 return 1;
229 return 0;
233 main (int argc, char **argv)
235 int width = 80;
236 int i;
237 int optc;
238 int errs = 0;
240 program_name = argv[0];
241 setlocale (LC_ALL, "");
242 bindtextdomain (PACKAGE, LOCALEDIR);
243 textdomain (PACKAGE);
245 break_spaces = count_bytes = have_read_stdin = 0;
247 /* Turn any numeric options into -w options. */
248 for (i = 1; i < argc; i++)
250 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
252 char *s;
254 s = xmalloc (strlen (argv[i]) + 2);
255 s[0] = '-';
256 s[1] = 'w';
257 strcpy (s + 2, argv[i] + 1);
258 argv[i] = s;
262 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
264 switch (optc)
266 case 0:
267 break;
269 case 'b': /* Count bytes rather than columns. */
270 count_bytes = 1;
271 break;
273 case 's': /* Break at word boundaries. */
274 break_spaces = 1;
275 break;
277 case 'w': /* Line width. */
279 long int tmp_long;
280 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
281 || tmp_long <= 0 || tmp_long > INT_MAX)
282 error (EXIT_FAILURE, 0,
283 _("invalid number of columns: `%s'"), optarg);
284 width = (int) tmp_long;
286 break;
288 case_GETOPT_HELP_CHAR;
290 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
292 default:
293 usage (1);
297 if (argc == optind)
298 errs |= fold_file ("-", width);
299 else
300 for (i = optind; i < argc; i++)
301 errs |= fold_file (argv[i], width);
303 if (have_read_stdin && fclose (stdin) == EOF)
304 error (EXIT_FAILURE, errno, "-");
305 if (fclose (stdout) == EOF)
306 error (EXIT_FAILURE, errno, _("write error"));
308 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);