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},
79 Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width]\n\
80 [--help] [--version] [file...]\n",
95 program_name = argv[0];
96 break_spaces = count_bytes = have_read_stdin = 0;
98 /* Turn any numeric options into -w options. */
99 for (i = 1; i < argc; i++)
101 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
105 s = xmalloc (strlen (argv[i]) + 2);
108 strcpy (s + 2, argv[i] + 1);
113 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
121 case 'b': /* Count bytes rather than columns. */
125 case 's': /* Break at word boundaries. */
129 case 'w': /* Line width. */
130 width = atoi (optarg);
132 error (1, 0, "%s: invalid line width", optarg);
142 printf ("%s\n", version_string);
150 errs |= fold_file ("-", width);
152 for (i = optind; i < argc; i++)
153 errs |= fold_file (argv[i], width);
155 if (have_read_stdin && fclose (stdin) == EOF)
156 error (1, errno, "-");
157 if (fclose (stdout) == EOF)
158 error (1, errno, "write error");
163 /* Fold file FILENAME, or standard input if FILENAME is "-",
164 to stdout, with maximum line length WIDTH.
165 Return 0 if successful, 1 if an error occurs. */
168 fold_file (filename, width)
174 int column = 0; /* Screen column where next char will go. */
175 int offset_out = 0; /* Index in `line_out' for next char. */
176 static char *line_out = NULL;
177 static size_t allocated_out = 0;
179 if (!strcmp (filename, "-"))
185 istream = fopen (filename, "r");
189 error (0, errno, "%s", filename);
193 while ((c = getc (istream)) != EOF)
195 if (offset_out + 1 >= allocated_out)
197 allocated_out += 1024;
198 line_out = xrealloc (line_out, allocated_out);
203 line_out[offset_out++] = c;
204 fwrite (line_out, sizeof (char), offset_out, stdout);
205 column = offset_out = 0;
210 column = adjust_column (column, c);
214 /* This character would make the line too long.
215 Print the line plus a newline, and make this character
216 start the next line. */
219 /* Look for the last blank. */
222 for (logical_end = offset_out - 1; logical_end >= 0;
224 if (ISBLANK (line_out[logical_end]))
226 if (logical_end >= 0)
230 /* Found a blank. Don't output the part after it. */
232 fwrite (line_out, sizeof (char), logical_end, stdout);
234 /* Move the remainder to the beginning of the next line.
235 The areas being copied here might overlap. */
236 bcopy (line_out + logical_end, line_out,
237 offset_out - logical_end);
238 offset_out -= logical_end;
239 for (column = i = 0; i < offset_out; i++)
240 column = adjust_column (column, line_out[i]);
244 line_out[offset_out++] = '\n';
245 fwrite (line_out, sizeof (char), offset_out, stdout);
246 column = offset_out = 0;
250 line_out[offset_out++] = c;
254 fwrite (line_out, sizeof (char), offset_out, stdout);
256 if (ferror (istream))
258 error (0, errno, "%s", filename);
259 if (strcmp (filename, "-"))
263 if (strcmp (filename, "-") && fclose (istream) == EOF)
265 error (0, errno, "%s", filename);
271 error (0, errno, "write error");
278 /* Assuming the current column is COLUMN, return the column that
279 printing C will move the cursor to.
280 The first column is 0. */
283 adjust_column (column, c)
297 column = column + 8 - column % 8;
298 else /* if (isprint (c)) */