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. */
24 #define isblank(c) ((c) == ' ' || (c) == '\t')
28 #include <sys/types.h>
34 static int adjust_column ();
35 static int fold_file ();
37 /* If nonzero, try to break on whitespace. */
38 static int break_spaces;
40 /* If nonzero, count bytes, not column positions. */
41 static int count_bytes;
43 /* If nonzero, at least one of the files we read was standard input. */
44 static int have_read_stdin;
46 /* The name this program was run with. */
49 static struct option const longopts[] =
51 {"bytes", no_argument, NULL, 'b'},
52 {"spaces", no_argument, NULL, 's'},
53 {"width", required_argument, NULL, 'w'},
67 program_name = argv[0];
68 break_spaces = count_bytes = have_read_stdin = 0;
70 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
75 case 'b': /* Count bytes rather than columns. */
79 case 's': /* Break at word boundaries. */
83 case 'w': /* Line width. */
84 width = atoi (optarg);
86 error (1, 0, "%s: invalid line width", optarg);
91 Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width] [file...]\n",
98 errs |= fold_file ("-", width);
100 for (i = optind; i < argc; i++)
101 errs |= fold_file (argv[i], width);
103 if (have_read_stdin && fclose (stdin) == EOF)
104 error (1, errno, "-");
105 if (fclose (stdout) == EOF)
106 error (1, errno, "write error");
111 /* Fold file FILENAME, or standard input if FILENAME is "-",
112 to stdout, with maximum line length WIDTH.
113 Return 0 if successful, 1 if an error occurs. */
116 fold_file (filename, width)
122 int column = 0; /* Screen column where next char will go. */
123 int offset_out = 0; /* Index in `line_out' for next char. */
124 static char *line_out = NULL;
125 static size_t allocated_out = 0;
127 if (!strcmp (filename, "-"))
133 istream = fopen (filename, "r");
137 error (0, errno, "%s", filename);
141 while ((c = getc (istream)) != EOF)
143 if (offset_out + 1 >= allocated_out)
145 allocated_out += 1024;
146 line_out = xrealloc (line_out, allocated_out);
151 line_out[offset_out++] = c;
152 fwrite (line_out, sizeof (char), offset_out, stdout);
153 column = offset_out = 0;
158 column = adjust_column (column, c);
162 /* This character would make the line too long.
163 Print the line plus a newline, and make this character
164 start the next line. */
167 /* Look for the last blank. */
170 for (logical_end = offset_out - 1; logical_end >= 0;
172 if (isblank (line_out[logical_end]))
174 if (logical_end >= 0)
178 /* Found a blank. Don't output the part after it. */
180 fwrite (line_out, sizeof (char), logical_end, stdout);
182 /* Move the remainder to the beginning of the next line.
183 The areas being copied here might overlap. */
184 bcopy (line_out + logical_end, line_out,
185 offset_out - logical_end);
186 offset_out -= logical_end;
187 for (column = i = 0; i < offset_out; i++)
188 column = adjust_column (column, line_out[i]);
192 line_out[offset_out++] = '\n';
193 fwrite (line_out, sizeof (char), offset_out, stdout);
194 column = offset_out = 0;
198 line_out[offset_out++] = c;
202 fwrite (line_out, sizeof (char), offset_out, stdout);
204 if (ferror (istream))
206 error (0, errno, "%s", filename);
207 if (strcmp (filename, "-"))
211 if (strcmp (filename, "-") && fclose (istream) == EOF)
213 error (0, errno, "%s", filename);
219 error (0, errno, "write error");
226 /* Assuming the current column is COLUMN, return the column that
227 printing C will move the cursor to.
228 The first column is 0. */
231 adjust_column (column, c)
245 column = column + 8 - column % 8;
246 else /* if (isprint (c)) */