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>
35 /* The name this program was run with. */
38 /* If nonzero, try to break on whitespace. */
39 static int break_spaces;
41 /* If nonzero, count bytes, not column positions. */
42 static int count_bytes;
44 /* If nonzero, at least one of the files we read was standard input. */
45 static int have_read_stdin;
47 /* If nonzero, display usage information and exit. */
50 /* If nonzero, print the version on standard output then exit. */
51 static int show_version;
53 static struct option const longopts[] =
55 {"bytes", no_argument, NULL, 'b'},
56 {"spaces", no_argument, NULL, 's'},
57 {"width", required_argument, NULL, 'w'},
58 {"help", no_argument, &show_help, 1},
59 {"version", no_argument, &show_version, 1},
67 fprintf (stderr, _("Try `%s --help' for more information.\n"),
72 Usage: %s [OPTION]... [FILE]...\n\
76 Wrap input lines in each FILE (standard input by default), writing to\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\
87 /* Assuming the current column is COLUMN, return the column that
88 printing C will move the cursor to.
89 The first column is 0. */
92 adjust_column (int column, char c)
104 column = column + 8 - column % 8;
105 else /* if (isprint (c)) */
113 /* Fold file FILENAME, or standard input if FILENAME is "-",
114 to stdout, with maximum line length WIDTH.
115 Return 0 if successful, 1 if an error occurs. */
118 fold_file (char *filename, int width)
122 int column = 0; /* Screen column where next char will go. */
123 size_t offset_out = 0; /* Index in `line_out' for next char. */
124 static char *line_out = NULL;
125 static size_t allocated_out = 0;
127 if (!strcmp (filename, "-"))
133 istream = fopen (filename, "r");
137 error (0, errno, "%s", filename);
141 while ((c = getc (istream)) != EOF)
143 if (offset_out + 1 >= allocated_out)
145 allocated_out += 1024;
146 line_out = xrealloc (line_out, allocated_out);
151 line_out[offset_out++] = c;
152 fwrite (line_out, sizeof (char), offset_out, stdout);
153 column = offset_out = 0;
158 column = adjust_column (column, c);
162 /* This character would make the line too long.
163 Print the line plus a newline, and make this character
164 start the next line. */
167 /* Look for the last blank. */
170 for (logical_end = offset_out - 1; logical_end >= 0;
172 if (ISBLANK (line_out[logical_end]))
174 if (logical_end >= 0)
178 /* Found a blank. Don't output the part after it. */
180 fwrite (line_out, sizeof (char), logical_end, stdout);
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), offset_out, stdout);
202 column = offset_out = 0;
206 line_out[offset_out++] = c;
210 fwrite (line_out, sizeof (char), offset_out, stdout);
212 if (ferror (istream))
214 error (0, errno, "%s", filename);
215 if (strcmp (filename, "-"))
219 if (strcmp (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 break_spaces = count_bytes = have_read_stdin = 0;
245 /* Turn any numeric options into -w options. */
246 for (i = 1; i < argc; i++)
248 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
252 s = xmalloc (strlen (argv[i]) + 2);
255 strcpy (s + 2, argv[i] + 1);
260 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
268 case 'b': /* Count bytes rather than columns. */
272 case 's': /* Break at word boundaries. */
276 case 'w': /* Line width. */
277 width = atoi (optarg);
279 error (1, 0, _("%s: invalid line width"), optarg);
289 printf ("fold - %s\n", version_string);
297 errs |= fold_file ("-", width);
299 for (i = optind; i < argc; i++)
300 errs |= fold_file (argv[i], width);
302 if (have_read_stdin && fclose (stdin) == EOF)
303 error (1, errno, "-");
304 if (fclose (stdout) == EOF)
305 error (1, errno, _("write error"));