1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 1995-2003 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 WRITTEN_BY _("Written by 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\
83 fputs (HELP_OPTION_DESCRIPTION, stdout);
84 fputs (VERSION_OPTION_DESCRIPTION, stdout);
85 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
87 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
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. */
95 adjust_column (int column, char c)
107 column = column + 8 - column % 8;
108 else /* if (isprint (c)) */
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. */
121 fold_file (char *filename, int width)
125 int column = 0; /* Screen column where next char will go. */
126 int offset_out = 0; /* Index in `line_out' for next char. */
127 static char *line_out = NULL;
128 static int allocated_out = 0;
130 if (STREQ (filename, "-"))
136 istream = fopen (filename, "r");
140 error (0, errno, "%s", filename);
144 while ((c = getc (istream)) != EOF)
146 if (offset_out + 1 >= allocated_out)
148 allocated_out += 1024;
149 line_out = xrealloc (line_out, allocated_out);
154 line_out[offset_out++] = c;
155 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
156 column = offset_out = 0;
161 column = adjust_column (column, c);
165 /* This character would make the line too long.
166 Print the line plus a newline, and make this character
167 start the next line. */
170 /* Look for the last blank. */
173 for (logical_end = offset_out - 1; logical_end >= 0;
175 if (ISBLANK (line_out[logical_end]))
177 if (logical_end >= 0)
181 /* Found a blank. Don't output the part after it. */
183 fwrite (line_out, sizeof (char), (size_t) logical_end,
186 /* Move the remainder to the beginning of the next line.
187 The areas being copied here might overlap. */
188 memmove (line_out, line_out + logical_end,
189 offset_out - logical_end);
190 offset_out -= logical_end;
191 for (column = i = 0; i < offset_out; i++)
192 column = adjust_column (column, line_out[i]);
199 line_out[offset_out++] = c;
203 line_out[offset_out++] = '\n';
204 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
205 column = offset_out = 0;
209 line_out[offset_out++] = c;
213 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
215 if (ferror (istream))
217 error (0, errno, "%s", filename);
218 if (!STREQ (filename, "-"))
222 if (!STREQ (filename, "-") && fclose (istream) == EOF)
224 error (0, errno, "%s", filename);
232 main (int argc, char **argv)
239 initialize_main (&argc, &argv);
240 program_name = argv[0];
241 setlocale (LC_ALL, "");
242 bindtextdomain (PACKAGE, LOCALEDIR);
243 textdomain (PACKAGE);
245 atexit (close_stdout);
247 break_spaces = count_bytes = have_read_stdin = 0;
249 /* Turn any numeric options into -w options. */
250 for (i = 1; i < argc; i++)
252 char const *a = argv[i];
255 if (a[1] == '-' && ! a[2])
259 char *s = xmalloc (strlen (a) + 2);
262 strcpy (s + 2, a + 1);
264 if (200112 <= posix2_version ())
266 error (0, 0, _("`%s' option is obsolete; use `%s'"), a, s);
267 usage (EXIT_FAILURE);
273 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
280 case 'b': /* Count bytes rather than columns. */
284 case 's': /* Break at word boundaries. */
288 case 'w': /* Line width. */
291 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
292 || tmp_long <= 0 || tmp_long > INT_MAX)
293 error (EXIT_FAILURE, 0,
294 _("invalid number of columns: `%s'"), optarg);
295 width = (int) tmp_long;
299 case_GETOPT_HELP_CHAR;
301 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
304 usage (EXIT_FAILURE);
309 errs |= fold_file ("-", width);
311 for (i = optind; i < argc; i++)
312 errs |= fold_file (argv[i], width);
314 if (have_read_stdin && fclose (stdin) == EOF)
315 error (EXIT_FAILURE, errno, "-");
317 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);