1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 1995-2001 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>
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. */
39 /* If nonzero, try to break on whitespace. */
40 static int break_spaces;
42 /* If nonzero, count bytes, not column positions. */
43 static int count_bytes;
45 /* If nonzero, at least one of the files we read was standard input. */
46 static int 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},
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
67 Usage: %s [OPTION]... [FILE]...\n\
71 Wrap input lines in each FILE (standard input by default), writing to\n\
76 Mandatory arguments to long options are mandatory for short options too.\n\
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\
84 --help display this help and exit\n\
85 --version output version information and exit\n\
87 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
89 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
92 /* Assuming the current column is COLUMN, return the column that
93 printing C will move the cursor to.
94 The first column is 0. */
97 adjust_column (int column, char c)
109 column = column + 8 - column % 8;
110 else /* if (isprint (c)) */
118 /* Fold file FILENAME, or standard input if FILENAME is "-",
119 to stdout, with maximum line length WIDTH.
120 Return 0 if successful, 1 if an error occurs. */
123 fold_file (char *filename, int width)
127 int column = 0; /* Screen column where next char will go. */
128 int offset_out = 0; /* Index in `line_out' for next char. */
129 static char *line_out = NULL;
130 static int allocated_out = 0;
132 if (STREQ (filename, "-"))
138 istream = fopen (filename, "r");
142 error (0, errno, "%s", filename);
146 while ((c = getc (istream)) != EOF)
148 if (offset_out + 1 >= allocated_out)
150 allocated_out += 1024;
151 line_out = xrealloc (line_out, allocated_out);
156 line_out[offset_out++] = c;
157 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
158 column = offset_out = 0;
163 column = adjust_column (column, c);
167 /* This character would make the line too long.
168 Print the line plus a newline, and make this character
169 start the next line. */
172 /* Look for the last blank. */
175 for (logical_end = offset_out - 1; logical_end >= 0;
177 if (ISBLANK (line_out[logical_end]))
179 if (logical_end >= 0)
183 /* Found a blank. Don't output the part after it. */
185 fwrite (line_out, sizeof (char), (size_t) logical_end,
188 /* Move the remainder to the beginning of the next line.
189 The areas being copied here might overlap. */
190 memmove (line_out, line_out + logical_end,
191 offset_out - logical_end);
192 offset_out -= logical_end;
193 for (column = i = 0; i < offset_out; i++)
194 column = adjust_column (column, line_out[i]);
202 line_out[offset_out++] = c;
206 line_out[offset_out++] = '\n';
207 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
208 column = offset_out = 0;
212 line_out[offset_out++] = c;
216 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
218 if (ferror (istream))
220 error (0, errno, "%s", filename);
221 if (!STREQ (filename, "-"))
225 if (!STREQ (filename, "-") && fclose (istream) == EOF)
227 error (0, errno, "%s", filename);
235 main (int argc, char **argv)
242 program_name = argv[0];
243 setlocale (LC_ALL, "");
244 bindtextdomain (PACKAGE, LOCALEDIR);
245 textdomain (PACKAGE);
247 atexit (close_stdout);
249 break_spaces = count_bytes = have_read_stdin = 0;
251 /* Turn any numeric options into -w options. */
252 for (i = 1; i < argc; i++)
254 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
258 s = xmalloc (strlen (argv[i]) + 2);
261 strcpy (s + 2, argv[i] + 1);
266 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
273 case 'b': /* Count bytes rather than columns. */
277 case 's': /* Break at word boundaries. */
281 case 'w': /* Line width. */
284 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
285 || tmp_long <= 0 || tmp_long > INT_MAX)
286 error (EXIT_FAILURE, 0,
287 _("invalid number of columns: `%s'"), optarg);
288 width = (int) tmp_long;
292 case_GETOPT_HELP_CHAR;
294 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
302 errs |= fold_file ("-", width);
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, "-");
310 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);