1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 1991, 1995 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
16 Foundation, 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>
34 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
38 # define INT_MAX ((int) (UINT_MAX >> 1))
49 /* The name this program was run with. */
52 /* If nonzero, try to break on whitespace. */
53 static int break_spaces;
55 /* If nonzero, count bytes, not column positions. */
56 static int count_bytes;
58 /* If nonzero, at least one of the files we read was standard input. */
59 static int have_read_stdin;
61 /* If nonzero, display usage information and exit. */
64 /* If nonzero, print the version on standard output then exit. */
65 static int show_version;
67 static struct option const longopts[] =
69 {"bytes", no_argument, NULL, 'b'},
70 {"spaces", no_argument, NULL, 's'},
71 {"width", required_argument, NULL, 'w'},
72 {"help", no_argument, &show_help, 1},
73 {"version", no_argument, &show_version, 1},
81 fprintf (stderr, _("Try `%s --help' for more information.\n"),
86 Usage: %s [OPTION]... [FILE]...\n\
90 Wrap input lines in each FILE (standard input by default), writing to\n\
93 -b, --bytes count bytes rather than columns\n\
94 -s, --spaces break at spaces\n\
95 -w, --width=WIDTH use WIDTH columns instead of 80\n\
101 /* Assuming the current column is COLUMN, return the column that
102 printing C will move the cursor to.
103 The first column is 0. */
106 adjust_column (int column, char c)
118 column = column + 8 - column % 8;
119 else /* if (isprint (c)) */
127 /* Fold file FILENAME, or standard input if FILENAME is "-",
128 to stdout, with maximum line length WIDTH.
129 Return 0 if successful, 1 if an error occurs. */
132 fold_file (char *filename, int width)
136 int column = 0; /* Screen column where next char will go. */
137 int offset_out = 0; /* Index in `line_out' for next char. */
138 static char *line_out = NULL;
139 static int allocated_out = 0;
141 if (!strcmp (filename, "-"))
147 istream = fopen (filename, "r");
151 error (0, errno, "%s", filename);
155 while ((c = getc (istream)) != EOF)
157 if (offset_out + 1 >= allocated_out)
159 allocated_out += 1024;
160 line_out = xrealloc (line_out, allocated_out);
165 line_out[offset_out++] = c;
166 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
167 column = offset_out = 0;
172 column = adjust_column (column, c);
176 /* This character would make the line too long.
177 Print the line plus a newline, and make this character
178 start the next line. */
181 /* Look for the last blank. */
184 for (logical_end = offset_out - 1; logical_end >= 0;
186 if (ISBLANK (line_out[logical_end]))
188 if (logical_end >= 0)
192 /* Found a blank. Don't output the part after it. */
194 fwrite (line_out, sizeof (char), (size_t) logical_end,
197 /* Move the remainder to the beginning of the next line.
198 The areas being copied here might overlap. */
199 memmove (line_out, line_out + logical_end,
200 offset_out - logical_end);
201 offset_out -= logical_end;
202 for (column = i = 0; i < offset_out; i++)
203 column = adjust_column (column, line_out[i]);
211 line_out[offset_out++] = c;
215 line_out[offset_out++] = '\n';
216 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
217 column = offset_out = 0;
221 line_out[offset_out++] = c;
225 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
227 if (ferror (istream))
229 error (0, errno, "%s", filename);
230 if (strcmp (filename, "-"))
234 if (strcmp (filename, "-") && fclose (istream) == EOF)
236 error (0, errno, "%s", filename);
242 error (0, errno, _("write error"));
250 main (int argc, char **argv)
257 program_name = argv[0];
258 setlocale (LC_ALL, "");
259 bindtextdomain (PACKAGE, LOCALEDIR);
260 textdomain (PACKAGE);
262 break_spaces = count_bytes = have_read_stdin = 0;
264 /* Turn any numeric options into -w options. */
265 for (i = 1; i < argc; i++)
267 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
271 s = xmalloc (strlen (argv[i]) + 2);
274 strcpy (s + 2, argv[i] + 1);
279 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
287 case 'b': /* Count bytes rather than columns. */
291 case 's': /* Break at word boundaries. */
295 case 'w': /* Line width. */
298 if (xstrtol (optarg, NULL, 10, &tmp_long, NULL) != LONGINT_OK
299 || tmp_long <= 0 || tmp_long > INT_MAX)
300 error (1, 0, _("invalid number of columns: `%s'"), optarg);
301 width = (int) tmp_long;
312 printf ("fold - %s\n", version_string);
320 errs |= fold_file ("-", width);
322 for (i = optind; i < argc; i++)
323 errs |= fold_file (argv[i], width);
325 if (have_read_stdin && fclose (stdin) == EOF)
326 error (1, errno, "-");
327 if (fclose (stdout) == EOF)
328 error (1, errno, _("write error"));