1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 95, 96, 1997, 1998 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. */
22 /* Get isblank from GNU libc. */
27 #include <sys/types.h>
33 /* The name this program was run with. */
36 /* If nonzero, try to break on whitespace. */
37 static int break_spaces;
39 /* If nonzero, count bytes, not column positions. */
40 static int count_bytes;
42 /* If nonzero, at least one of the files we read was standard input. */
43 static int have_read_stdin;
45 /* If nonzero, display usage information and exit. */
48 /* If nonzero, print the version on standard output then exit. */
49 static int show_version;
51 static struct option const longopts[] =
53 {"bytes", no_argument, NULL, 'b'},
54 {"spaces", no_argument, NULL, 's'},
55 {"width", required_argument, NULL, 'w'},
56 {"help", no_argument, &show_help, 1},
57 {"version", no_argument, &show_version, 1},
65 fprintf (stderr, _("Try `%s --help' for more information.\n"),
70 Usage: %s [OPTION]... [FILE]...\n\
74 Wrap input lines in each FILE (standard input by default), writing to\n\
77 -b, --bytes count bytes rather than columns\n\
78 -s, --spaces break at spaces\n\
79 -w, --width=WIDTH use WIDTH columns instead of 80\n\
81 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
83 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
86 /* Assuming the current column is COLUMN, return the column that
87 printing C will move the cursor to.
88 The first column is 0. */
91 adjust_column (int column, char c)
103 column = column + 8 - column % 8;
104 else /* if (isprint (c)) */
112 /* Fold file FILENAME, or standard input if FILENAME is "-",
113 to stdout, with maximum line length WIDTH.
114 Return 0 if successful, 1 if an error occurs. */
117 fold_file (char *filename, int width)
121 int column = 0; /* Screen column where next char will go. */
122 int offset_out = 0; /* Index in `line_out' for next char. */
123 static char *line_out = NULL;
124 static int allocated_out = 0;
126 if (STREQ (filename, "-"))
132 istream = fopen (filename, "r");
136 error (0, errno, "%s", filename);
140 while ((c = getc (istream)) != EOF)
142 if (offset_out + 1 >= allocated_out)
144 allocated_out += 1024;
145 line_out = xrealloc (line_out, allocated_out);
150 line_out[offset_out++] = c;
151 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
152 column = offset_out = 0;
157 column = adjust_column (column, c);
161 /* This character would make the line too long.
162 Print the line plus a newline, and make this character
163 start the next line. */
166 /* Look for the last blank. */
169 for (logical_end = offset_out - 1; logical_end >= 0;
171 if (ISBLANK (line_out[logical_end]))
173 if (logical_end >= 0)
177 /* Found a blank. Don't output the part after it. */
179 fwrite (line_out, sizeof (char), (size_t) logical_end,
182 /* Move the remainder to the beginning of the next line.
183 The areas being copied here might overlap. */
184 memmove (line_out, line_out + logical_end,
185 offset_out - logical_end);
186 offset_out -= logical_end;
187 for (column = i = 0; i < offset_out; i++)
188 column = adjust_column (column, line_out[i]);
196 line_out[offset_out++] = c;
200 line_out[offset_out++] = '\n';
201 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
202 column = offset_out = 0;
206 line_out[offset_out++] = c;
210 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
212 if (ferror (istream))
214 error (0, errno, "%s", filename);
215 if (!STREQ (filename, "-"))
219 if (!STREQ (filename, "-") && fclose (istream) == EOF)
221 error (0, errno, "%s", filename);
227 error (0, errno, _("write error"));
235 main (int argc, char **argv)
242 program_name = argv[0];
243 setlocale (LC_ALL, "");
244 bindtextdomain (PACKAGE, LOCALEDIR);
245 textdomain (PACKAGE);
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 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
256 s = xmalloc (strlen (argv[i]) + 2);
259 strcpy (s + 2, argv[i] + 1);
264 while ((optc = getopt_long (argc, argv, "bsw:", longopts, NULL)) != -1)
271 case 'b': /* Count bytes rather than columns. */
275 case 's': /* Break at word boundaries. */
279 case 'w': /* Line width. */
282 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
283 || tmp_long <= 0 || tmp_long > INT_MAX)
284 error (EXIT_FAILURE, 0,
285 _("invalid number of columns: `%s'"), optarg);
286 width = (int) tmp_long;
297 printf ("fold (%s) %s\n", GNU_PACKAGE, VERSION);
305 errs |= fold_file ("-", width);
307 for (i = optind; i < argc; i++)
308 errs |= fold_file (argv[i], width);
310 if (have_read_stdin && fclose (stdin) == EOF)
311 error (EXIT_FAILURE, errno, "-");
312 if (fclose (stdout) == EOF)
313 error (EXIT_FAILURE, errno, _("write error"));
315 exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);