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 static int adjust_column ();
39 static int fold_file ();
41 /* If nonzero, try to break on whitespace. */
42 static int break_spaces;
44 /* If nonzero, count bytes, not column positions. */
45 static int count_bytes;
47 /* If nonzero, at least one of the files we read was standard input. */
48 static int have_read_stdin;
50 /* If non-zero, display usage information and exit. */
53 /* If non-zero, print the version on standard output then exit. */
54 static int show_version;
56 static struct option const longopts[] =
58 {"bytes", no_argument, NULL, 'b'},
59 {"spaces", no_argument, NULL, 's'},
60 {"width", required_argument, NULL, 'w'},
61 {"help", no_argument, &show_help, 1},
62 {"version", no_argument, &show_version, 1},
71 fprintf (stderr, "Try `%s --help' for more information.\n",
76 Usage: %s [OPTION]... [FILE]...\n\
80 Wrap input lines in each FILE (standard input by default), writing to\n\
83 -b, --bytes count bytes rather than columns\n\
84 -s, --spaces break at word boundaries\n\
85 -w, --width=WIDTH use WIDTH columns instead of 80\n\
101 program_name = argv[0];
102 break_spaces = count_bytes = have_read_stdin = 0;
104 /* Turn any numeric options into -w options. */
105 for (i = 1; i < argc; i++)
107 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
111 s = xmalloc (strlen (argv[i]) + 2);
114 strcpy (s + 2, argv[i] + 1);
119 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
127 case 'b': /* Count bytes rather than columns. */
131 case 's': /* Break at word boundaries. */
135 case 'w': /* Line width. */
136 width = atoi (optarg);
138 error (1, 0, "%s: invalid line width", optarg);
148 printf ("fold - %s\n", version_string);
156 errs |= fold_file ("-", width);
158 for (i = optind; i < argc; i++)
159 errs |= fold_file (argv[i], width);
161 if (have_read_stdin && fclose (stdin) == EOF)
162 error (1, errno, "-");
163 if (fclose (stdout) == EOF)
164 error (1, errno, "write error");
169 /* Fold file FILENAME, or standard input if FILENAME is "-",
170 to stdout, with maximum line length WIDTH.
171 Return 0 if successful, 1 if an error occurs. */
174 fold_file (filename, width)
180 int column = 0; /* Screen column where next char will go. */
181 size_t offset_out = 0; /* Index in `line_out' for next char. */
182 static char *line_out = NULL;
183 static size_t allocated_out = 0;
185 if (!strcmp (filename, "-"))
191 istream = fopen (filename, "r");
195 error (0, errno, "%s", filename);
199 while ((c = getc (istream)) != EOF)
201 if (offset_out + 1 >= allocated_out)
203 allocated_out += 1024;
204 line_out = xrealloc (line_out, allocated_out);
209 line_out[offset_out++] = c;
210 fwrite (line_out, sizeof (char), offset_out, stdout);
211 column = offset_out = 0;
216 column = adjust_column (column, c);
220 /* This character would make the line too long.
221 Print the line plus a newline, and make this character
222 start the next line. */
225 /* Look for the last blank. */
228 for (logical_end = offset_out - 1; logical_end >= 0;
230 if (ISBLANK (line_out[logical_end]))
232 if (logical_end >= 0)
236 /* Found a blank. Don't output the part after it. */
238 fwrite (line_out, sizeof (char), logical_end, stdout);
240 /* Move the remainder to the beginning of the next line.
241 The areas being copied here might overlap. */
242 memmove (line_out, line_out + logical_end,
243 offset_out - logical_end);
244 offset_out -= logical_end;
245 for (column = i = 0; i < offset_out; i++)
246 column = adjust_column (column, line_out[i]);
254 line_out[offset_out++] = c;
258 line_out[offset_out++] = '\n';
259 fwrite (line_out, sizeof (char), offset_out, stdout);
260 column = offset_out = 0;
264 line_out[offset_out++] = c;
268 fwrite (line_out, sizeof (char), offset_out, stdout);
270 if (ferror (istream))
272 error (0, errno, "%s", filename);
273 if (strcmp (filename, "-"))
277 if (strcmp (filename, "-") && fclose (istream) == EOF)
279 error (0, errno, "%s", filename);
285 error (0, errno, "write error");
292 /* Assuming the current column is COLUMN, return the column that
293 printing C will move the cursor to.
294 The first column is 0. */
297 adjust_column (column, c)
311 column = column + 8 - column % 8;
312 else /* if (isprint (c)) */