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. */
20 /* Get isblank from GNU libc. */
25 #include <sys/types.h>
33 /* The name this program was run with. */
36 static int adjust_column ();
37 static int fold_file ();
39 /* If nonzero, try to break on whitespace. */
40 static int break_spaces;
42 /* If nonzero, count bytes, not column positions. */
43 static int count_bytes;
45 /* If nonzero, at least one of the files we read was standard input. */
46 static int have_read_stdin;
48 /* If non-zero, display usage information and exit. */
51 /* If non-zero, print the version on standard error. */
52 static int flag_version;
54 static struct option const longopts[] =
56 {"bytes", no_argument, NULL, 'b'},
57 {"spaces", no_argument, NULL, 's'},
58 {"width", required_argument, NULL, 'w'},
59 {"help", no_argument, &flag_help, 1},
60 {"version", no_argument, &flag_version, 1},
68 Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width]\n\
69 [--help] [--version] [file...]\n",
84 program_name = argv[0];
85 break_spaces = count_bytes = have_read_stdin = 0;
87 /* Turn any numeric options into -w options. */
88 for (i = 1; i < argc; i++)
90 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
94 s = xmalloc (strlen (argv[i]) + 2);
97 strcpy (s + 2, argv[i] + 1);
102 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
110 case 'b': /* Count bytes rather than columns. */
114 case 's': /* Break at word boundaries. */
118 case 'w': /* Line width. */
119 width = atoi (optarg);
121 error (1, 0, "%s: invalid line width", optarg);
131 fprintf (stderr, "%s\n", version_string);
139 errs |= fold_file ("-", width);
141 for (i = optind; i < argc; i++)
142 errs |= fold_file (argv[i], width);
144 if (have_read_stdin && fclose (stdin) == EOF)
145 error (1, errno, "-");
146 if (fclose (stdout) == EOF)
147 error (1, errno, "write error");
152 /* Fold file FILENAME, or standard input if FILENAME is "-",
153 to stdout, with maximum line length WIDTH.
154 Return 0 if successful, 1 if an error occurs. */
157 fold_file (filename, width)
163 int column = 0; /* Screen column where next char will go. */
164 int offset_out = 0; /* Index in `line_out' for next char. */
165 static char *line_out = NULL;
166 static size_t allocated_out = 0;
168 if (!strcmp (filename, "-"))
174 istream = fopen (filename, "r");
178 error (0, errno, "%s", filename);
182 while ((c = getc (istream)) != EOF)
184 if (offset_out + 1 >= allocated_out)
186 allocated_out += 1024;
187 line_out = xrealloc (line_out, allocated_out);
192 line_out[offset_out++] = c;
193 fwrite (line_out, sizeof (char), offset_out, stdout);
194 column = offset_out = 0;
199 column = adjust_column (column, c);
203 /* This character would make the line too long.
204 Print the line plus a newline, and make this character
205 start the next line. */
208 /* Look for the last blank. */
211 for (logical_end = offset_out - 1; logical_end >= 0;
213 if (ISBLANK (line_out[logical_end]))
215 if (logical_end >= 0)
219 /* Found a blank. Don't output the part after it. */
221 fwrite (line_out, sizeof (char), logical_end, stdout);
223 /* Move the remainder to the beginning of the next line.
224 The areas being copied here might overlap. */
225 bcopy (line_out + logical_end, line_out,
226 offset_out - logical_end);
227 offset_out -= logical_end;
228 for (column = i = 0; i < offset_out; i++)
229 column = adjust_column (column, line_out[i]);
233 line_out[offset_out++] = '\n';
234 fwrite (line_out, sizeof (char), offset_out, stdout);
235 column = offset_out = 0;
239 line_out[offset_out++] = c;
243 fwrite (line_out, sizeof (char), offset_out, stdout);
245 if (ferror (istream))
247 error (0, errno, "%s", filename);
248 if (strcmp (filename, "-"))
252 if (strcmp (filename, "-") && fclose (istream) == EOF)
254 error (0, errno, "%s", filename);
260 error (0, errno, "write error");
267 /* Assuming the current column is COLUMN, return the column that
268 printing C will move the cursor to.
269 The first column is 0. */
272 adjust_column (column, c)
286 column = column + 8 - column % 8;
287 else /* if (isprint (c)) */