1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
31 /* Get isblank from GNU libc. */
36 #include <sys/types.h>
44 /* The name this program was run with. */
47 static int adjust_column ();
48 static int fold_file ();
50 /* If nonzero, try to break on whitespace. */
51 static int break_spaces;
53 /* If nonzero, count bytes, not column positions. */
54 static int count_bytes;
56 /* If nonzero, at least one of the files we read was standard input. */
57 static int have_read_stdin;
59 /* If non-zero, display usage information and exit. */
62 /* If non-zero, print the version on standard output then exit. */
63 static int show_version;
65 static struct option const longopts[] =
67 {"bytes", no_argument, NULL, 'b'},
68 {"spaces", no_argument, NULL, 's'},
69 {"width", required_argument, NULL, 'w'},
70 {"help", no_argument, &show_help, 1},
71 {"version", no_argument, &show_version, 1},
80 fprintf (stderr, "Try `%s --help' for more information.\n",
85 Usage: %s [OPTION]... [FILE]...\n\
90 -b, --bytes count bytes rather than columns\n\
91 -s, --spaces break at word boundaries\n\
92 -w, --width=WIDTH use WIDTH columns instead of 80\n\
108 program_name = argv[0];
109 break_spaces = count_bytes = have_read_stdin = 0;
111 /* Turn any numeric options into -w options. */
112 for (i = 1; i < argc; i++)
114 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
118 s = xmalloc (strlen (argv[i]) + 2);
121 strcpy (s + 2, argv[i] + 1);
126 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
134 case 'b': /* Count bytes rather than columns. */
138 case 's': /* Break at word boundaries. */
142 case 'w': /* Line width. */
143 width = atoi (optarg);
145 error (1, 0, "%s: invalid line width", optarg);
155 printf ("%s\n", version_string);
163 errs |= fold_file ("-", width);
165 for (i = optind; i < argc; i++)
166 errs |= fold_file (argv[i], width);
168 if (have_read_stdin && fclose (stdin) == EOF)
169 error (1, errno, "-");
170 if (fclose (stdout) == EOF)
171 error (1, errno, "write error");
176 /* Fold file FILENAME, or standard input if FILENAME is "-",
177 to stdout, with maximum line length WIDTH.
178 Return 0 if successful, 1 if an error occurs. */
181 fold_file (filename, width)
187 int column = 0; /* Screen column where next char will go. */
188 int offset_out = 0; /* Index in `line_out' for next char. */
189 static char *line_out = NULL;
190 static size_t allocated_out = 0;
192 if (!strcmp (filename, "-"))
198 istream = fopen (filename, "r");
202 error (0, errno, "%s", filename);
206 while ((c = getc (istream)) != EOF)
208 if (offset_out + 1 >= allocated_out)
210 allocated_out += 1024;
211 line_out = xrealloc (line_out, allocated_out);
216 line_out[offset_out++] = c;
217 fwrite (line_out, sizeof (char), offset_out, stdout);
218 column = offset_out = 0;
223 column = adjust_column (column, c);
227 /* This character would make the line too long.
228 Print the line plus a newline, and make this character
229 start the next line. */
232 /* Look for the last blank. */
235 for (logical_end = offset_out - 1; logical_end >= 0;
237 if (ISBLANK (line_out[logical_end]))
239 if (logical_end >= 0)
243 /* Found a blank. Don't output the part after it. */
245 fwrite (line_out, sizeof (char), logical_end, stdout);
247 /* Move the remainder to the beginning of the next line.
248 The areas being copied here might overlap. */
249 bcopy (line_out + logical_end, line_out,
250 offset_out - logical_end);
251 offset_out -= logical_end;
252 for (column = i = 0; i < offset_out; i++)
253 column = adjust_column (column, line_out[i]);
257 line_out[offset_out++] = '\n';
258 fwrite (line_out, sizeof (char), offset_out, stdout);
259 column = offset_out = 0;
263 line_out[offset_out++] = c;
267 fwrite (line_out, sizeof (char), offset_out, stdout);
269 if (ferror (istream))
271 error (0, errno, "%s", filename);
272 if (strcmp (filename, "-"))
276 if (strcmp (filename, "-") && fclose (istream) == EOF)
278 error (0, errno, "%s", filename);
284 error (0, errno, "write error");
291 /* Assuming the current column is COLUMN, return the column that
292 printing C will move the cursor to.
293 The first column is 0. */
296 adjust_column (column, c)
310 column = column + 8 - column % 8;
311 else /* if (isprint (c)) */