1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 1991, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
22 /* Get isblank from GNU libc. */
27 #include <sys/types.h>
34 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
38 # define INT_MAX ((int) (UINT_MAX >> 1))
48 /* The name this program was run with. */
51 /* If nonzero, try to break on whitespace. */
52 static int break_spaces
;
54 /* If nonzero, count bytes, not column positions. */
55 static int count_bytes
;
57 /* If nonzero, at least one of the files we read was standard input. */
58 static int have_read_stdin
;
60 /* If nonzero, display usage information and exit. */
63 /* If nonzero, print the version on standard output then exit. */
64 static int show_version
;
66 static struct option
const longopts
[] =
68 {"bytes", no_argument
, NULL
, 'b'},
69 {"spaces", no_argument
, NULL
, 's'},
70 {"width", required_argument
, NULL
, 'w'},
71 {"help", no_argument
, &show_help
, 1},
72 {"version", no_argument
, &show_version
, 1},
80 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
85 Usage: %s [OPTION]... [FILE]...\n\
89 Wrap input lines in each FILE (standard input by default), writing to\n\
92 -b, --bytes count bytes rather than columns\n\
93 -s, --spaces break at spaces\n\
94 -w, --width=WIDTH use WIDTH columns instead of 80\n\
100 /* Assuming the current column is COLUMN, return the column that
101 printing C will move the cursor to.
102 The first column is 0. */
105 adjust_column (int column
, char c
)
117 column
= column
+ 8 - column
% 8;
118 else /* if (isprint (c)) */
126 /* Fold file FILENAME, or standard input if FILENAME is "-",
127 to stdout, with maximum line length WIDTH.
128 Return 0 if successful, 1 if an error occurs. */
131 fold_file (char *filename
, int width
)
135 int column
= 0; /* Screen column where next char will go. */
136 int offset_out
= 0; /* Index in `line_out' for next char. */
137 static char *line_out
= NULL
;
138 static int allocated_out
= 0;
140 if (!strcmp (filename
, "-"))
146 istream
= fopen (filename
, "r");
150 error (0, errno
, "%s", filename
);
154 while ((c
= getc (istream
)) != EOF
)
156 if (offset_out
+ 1 >= allocated_out
)
158 allocated_out
+= 1024;
159 line_out
= xrealloc (line_out
, allocated_out
);
164 line_out
[offset_out
++] = c
;
165 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
166 column
= offset_out
= 0;
171 column
= adjust_column (column
, c
);
175 /* This character would make the line too long.
176 Print the line plus a newline, and make this character
177 start the next line. */
180 /* Look for the last blank. */
183 for (logical_end
= offset_out
- 1; logical_end
>= 0;
185 if (ISBLANK (line_out
[logical_end
]))
187 if (logical_end
>= 0)
191 /* Found a blank. Don't output the part after it. */
193 fwrite (line_out
, sizeof (char), (size_t) logical_end
,
196 /* Move the remainder to the beginning of the next line.
197 The areas being copied here might overlap. */
198 memmove (line_out
, line_out
+ logical_end
,
199 offset_out
- logical_end
);
200 offset_out
-= logical_end
;
201 for (column
= i
= 0; i
< offset_out
; i
++)
202 column
= adjust_column (column
, line_out
[i
]);
210 line_out
[offset_out
++] = c
;
214 line_out
[offset_out
++] = '\n';
215 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
216 column
= offset_out
= 0;
220 line_out
[offset_out
++] = c
;
224 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
226 if (ferror (istream
))
228 error (0, errno
, "%s", filename
);
229 if (strcmp (filename
, "-"))
233 if (strcmp (filename
, "-") && fclose (istream
) == EOF
)
235 error (0, errno
, "%s", filename
);
241 error (0, errno
, _("write error"));
249 main (int argc
, char **argv
)
256 program_name
= argv
[0];
257 setlocale (LC_ALL
, "");
258 bindtextdomain (PACKAGE
, LOCALEDIR
);
259 textdomain (PACKAGE
);
261 break_spaces
= count_bytes
= have_read_stdin
= 0;
263 /* Turn any numeric options into -w options. */
264 for (i
= 1; i
< argc
; i
++)
266 if (argv
[i
][0] == '-' && ISDIGIT (argv
[i
][1]))
270 s
= xmalloc (strlen (argv
[i
]) + 2);
273 strcpy (s
+ 2, argv
[i
] + 1);
278 while ((optc
= getopt_long (argc
, argv
, "bsw:", longopts
, (int *) 0))
286 case 'b': /* Count bytes rather than columns. */
290 case 's': /* Break at word boundaries. */
294 case 'w': /* Line width. */
297 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
298 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
299 error (1, 0, _("invalid number of columns: `%s'"), optarg
);
300 width
= (int) tmp_long
;
311 printf ("fold - %s\n", PACKAGE_VERSION
);
319 errs
|= fold_file ("-", width
);
321 for (i
= optind
; i
< argc
; i
++)
322 errs
|= fold_file (argv
[i
], width
);
324 if (have_read_stdin
&& fclose (stdin
) == EOF
)
325 error (1, errno
, "-");
326 if (fclose (stdout
) == EOF
)
327 error (1, errno
, _("write error"));