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)
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. */
24 #include <sys/types.h>
28 #include "long-options.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "fold"
34 /* The name this program was run with. */
37 /* If nonzero, try to break on whitespace. */
38 static int break_spaces;
40 /* If nonzero, count bytes, not column positions. */
41 static int count_bytes;
43 /* If nonzero, at least one of the files we read was standard input. */
44 static int have_read_stdin;
46 static struct option const longopts[] =
48 {"bytes", no_argument, NULL, 'b'},
49 {"spaces", no_argument, NULL, 's'},
50 {"width", required_argument, NULL, 'w'},
58 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 Usage: %s [OPTION]... [FILE]...\n\
67 Wrap input lines in each FILE (standard input by default), writing to\n\
70 -b, --bytes count bytes rather than columns\n\
71 -s, --spaces break at spaces\n\
72 -w, --width=WIDTH use WIDTH columns instead of 80\n\
73 --help display this help and exit\n\
74 --version output version information and exit\n\
76 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
78 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
81 /* Assuming the current column is COLUMN, return the column that
82 printing C will move the cursor to.
83 The first column is 0. */
86 adjust_column (int column, char c)
98 column = column + 8 - column % 8;
99 else /* if (isprint (c)) */
107 /* Fold file FILENAME, or standard input if FILENAME is "-",
108 to stdout, with maximum line length WIDTH.
109 Return 0 if successful, 1 if an error occurs. */
112 fold_file (char *filename, int width)
116 int column = 0; /* Screen column where next char will go. */
117 int offset_out = 0; /* Index in `line_out' for next char. */
118 static char *line_out = NULL;
119 static int allocated_out = 0;
121 if (STREQ (filename, "-"))
127 istream = fopen (filename, "r");
131 error (0, errno, "%s", filename);
135 while ((c = getc (istream)) != EOF)
137 if (offset_out + 1 >= allocated_out)
139 allocated_out += 1024;
140 line_out = xrealloc (line_out, allocated_out);
145 line_out[offset_out++] = c;
146 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
147 column = offset_out = 0;
152 column = adjust_column (column, c);
156 /* This character would make the line too long.
157 Print the line plus a newline, and make this character
158 start the next line. */
161 /* Look for the last blank. */
164 for (logical_end = offset_out - 1; logical_end >= 0;
166 if (ISBLANK (line_out[logical_end]))
168 if (logical_end >= 0)
172 /* Found a blank. Don't output the part after it. */
174 fwrite (line_out, sizeof (char), (size_t) logical_end,
177 /* Move the remainder to the beginning of the next line.
178 The areas being copied here might overlap. */
179 memmove (line_out, line_out + logical_end,
180 offset_out - logical_end);
181 offset_out -= logical_end;
182 for (column = i = 0; i < offset_out; i++)
183 column = adjust_column (column, line_out[i]);
191 line_out[offset_out++] = c;
195 line_out[offset_out++] = '\n';
196 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
197 column = offset_out = 0;
201 line_out[offset_out++] = c;
205 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
207 if (ferror (istream))
209 error (0, errno, "%s", filename);
210 if (!STREQ (filename, "-"))
214 if (!STREQ (filename, "-") && fclose (istream) == EOF)
216 error (0, errno, "%s", filename);
222 error (0, errno, _("write error"));
230 main (int argc, char **argv)
237 program_name = argv[0];
238 setlocale (LC_ALL, "");
239 bindtextdomain (PACKAGE, LOCALEDIR);
240 textdomain (PACKAGE);
242 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
243 "David MacKenzie", usage);
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]))
254 s = xmalloc (strlen (argv[i]) + 2);
257 strcpy (s + 2, argv[i] + 1);
262 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
269 case 'b': /* Count bytes rather than columns. */
273 case 's': /* Break at word boundaries. */
277 case 'w': /* Line width. */
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;
294 errs |= fold_file ("-", width);
296 for (i = optind; i < argc; i++)
297 errs |= fold_file (argv[i], width);
299 if (have_read_stdin && fclose (stdin) == EOF)
300 error (EXIT_FAILURE, errno, "-");
301 if (fclose (stdout) == EOF)
302 error (EXIT_FAILURE, errno, _("write error"));
304 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);