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. */
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\
81 -b, --bytes count bytes rather than columns\n\
82 -s, --spaces break at word boundaries\n\
83 -w, --width=WIDTH use WIDTH columns instead of 80\n\
99 program_name = argv[0];
100 break_spaces = count_bytes = have_read_stdin = 0;
102 /* Turn any numeric options into -w options. */
103 for (i = 1; i < argc; i++)
105 if (argv[i][0] == '-' && ISDIGIT (argv[i][1]))
109 s = xmalloc (strlen (argv[i]) + 2);
112 strcpy (s + 2, argv[i] + 1);
117 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
125 case 'b': /* Count bytes rather than columns. */
129 case 's': /* Break at word boundaries. */
133 case 'w': /* Line width. */
134 width = atoi (optarg);
136 error (1, 0, "%s: invalid line width", optarg);
146 printf ("fold - %s\n", version_string);
154 errs |= fold_file ("-", width);
156 for (i = optind; i < argc; i++)
157 errs |= fold_file (argv[i], width);
159 if (have_read_stdin && fclose (stdin) == EOF)
160 error (1, errno, "-");
161 if (fclose (stdout) == EOF)
162 error (1, errno, "write error");
167 /* Fold file FILENAME, or standard input if FILENAME is "-",
168 to stdout, with maximum line length WIDTH.
169 Return 0 if successful, 1 if an error occurs. */
172 fold_file (filename, width)
178 int column = 0; /* Screen column where next char will go. */
179 int offset_out = 0; /* Index in `line_out' for next char. */
180 static char *line_out = NULL;
181 static size_t allocated_out = 0;
183 if (!strcmp (filename, "-"))
189 istream = fopen (filename, "r");
193 error (0, errno, "%s", filename);
197 while ((c = getc (istream)) != EOF)
199 if (offset_out + 1 >= allocated_out)
201 allocated_out += 1024;
202 line_out = xrealloc (line_out, allocated_out);
207 line_out[offset_out++] = c;
208 fwrite (line_out, sizeof (char), offset_out, stdout);
209 column = offset_out = 0;
214 column = adjust_column (column, c);
218 /* This character would make the line too long.
219 Print the line plus a newline, and make this character
220 start the next line. */
223 /* Look for the last blank. */
226 for (logical_end = offset_out - 1; logical_end >= 0;
228 if (ISBLANK (line_out[logical_end]))
230 if (logical_end >= 0)
234 /* Found a blank. Don't output the part after it. */
236 fwrite (line_out, sizeof (char), logical_end, stdout);
238 /* Move the remainder to the beginning of the next line.
239 The areas being copied here might overlap. */
240 bcopy (line_out + logical_end, line_out,
241 offset_out - logical_end);
242 offset_out -= logical_end;
243 for (column = i = 0; i < offset_out; i++)
244 column = adjust_column (column, line_out[i]);
252 line_out[offset_out++] = c;
256 line_out[offset_out++] = '\n';
257 fwrite (line_out, sizeof (char), offset_out, stdout);
258 column = offset_out = 0;
262 line_out[offset_out++] = c;
266 fwrite (line_out, sizeof (char), offset_out, stdout);
268 if (ferror (istream))
270 error (0, errno, "%s", filename);
271 if (strcmp (filename, "-"))
275 if (strcmp (filename, "-") && fclose (istream) == EOF)
277 error (0, errno, "%s", filename);
283 error (0, errno, "write error");
290 /* Assuming the current column is COLUMN, return the column that
291 printing C will move the cursor to.
292 The first column is 0. */
295 adjust_column (column, c)
309 column = column + 8 - column % 8;
310 else /* if (isprint (c)) */