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. */
23 #define isblank(c) ((c) == ' ' || (c) == '\t')
27 #include <sys/types.h>
33 static int adjust_column ();
34 static int fold_file ();
36 /* If nonzero, try to break on whitespace. */
37 static int break_spaces;
39 /* If nonzero, count bytes, not column positions. */
40 static int count_bytes;
42 /* If nonzero, at least one of the files we read was standard input. */
43 static int have_read_stdin;
45 /* The name this program was run with. */
48 static struct option const longopts[] =
50 {"bytes", 0, NULL, 'b'},
51 {"spaces", 0, NULL, 's'},
52 {"width", 1, NULL, 'w'},
66 program_name = argv[0];
67 break_spaces = count_bytes = have_read_stdin = 0;
69 while ((optc = getopt_long (argc, argv, "bsw:", longopts, (int *) 0))
74 case 'b': /* Count bytes rather than columns. */
78 case 's': /* Break at word boundaries. */
82 case 'w': /* Line width. */
83 width = atoi (optarg);
85 error (1, 0, "%s: invalid line width", optarg);
90 Usage: %s [-bs] [-w width] [--bytes] [--spaces] [--width=width] [file...]\n",
97 errs |= fold_file ("-", width);
99 for (i = optind; i < argc; i++)
100 errs |= fold_file (argv[i], width);
102 if (have_read_stdin && fclose (stdin) == EOF)
103 error (1, errno, "-");
104 if (fclose (stdout) == EOF)
105 error (1, errno, "write error");
110 /* Fold file FILENAME, or standard input if FILENAME is "-",
111 to stdout, with maximum line length WIDTH.
112 Return 0 if successful, 1 if an error occurs. */
115 fold_file (filename, width)
121 int column = 0; /* Screen column where next char will go. */
122 int offset_out = 0; /* Index in `line_out' for next char. */
123 static char *line_out = NULL;
124 static size_t allocated_out = 0;
126 if (!strcmp (filename, "-"))
132 istream = fopen (filename, "r");
136 error (0, errno, "%s", filename);
140 while ((c = getc (istream)) != EOF)
142 if (offset_out + 1 >= allocated_out)
144 allocated_out += 1024;
145 line_out = xrealloc (line_out, allocated_out);
150 line_out[offset_out++] = c;
151 fwrite (line_out, sizeof (char), offset_out, stdout);
152 column = offset_out = 0;
157 column = adjust_column (column, c);
161 /* This character would make the line too long.
162 Print the line plus a newline, and make this character
163 start the next line. */
166 /* Look for the last blank. */
169 for (logical_end = offset_out - 1; logical_end >= 0;
171 if (isblank (line_out[logical_end]))
173 if (logical_end >= 0)
177 /* Found a blank. Don't output the part after it. */
179 fwrite (line_out, sizeof (char), logical_end, stdout);
181 /* Move the remainder to the beginning of the next line.
182 The areas being copied here might overlap. */
183 bcopy (line_out + logical_end, line_out,
184 offset_out - logical_end);
185 offset_out -= logical_end;
186 for (column = i = 0; i < offset_out; i++)
187 column = adjust_column (column, line_out[i]);
191 line_out[offset_out++] = '\n';
192 fwrite (line_out, sizeof (char), offset_out, stdout);
193 column = offset_out = 0;
197 line_out[offset_out++] = c;
201 fwrite (line_out, sizeof (char), offset_out, stdout);
203 if (ferror (istream))
205 error (0, errno, "%s", filename);
206 if (strcmp (filename, "-"))
210 if (strcmp (filename, "-") && fclose (istream) == EOF)
212 error (0, errno, "%s", filename);
218 error (0, errno, "write error");
225 /* Assuming the current column is COLUMN, return the column that
226 printing C will move the cursor to.
227 The first column is 0. */
230 adjust_column (column, c)
244 column = column + 8 - column % 8;
245 else /* if (isprint (c)) */