1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 95, 96, 1997, 1998 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
24 #include <sys/types.h>
30 /* The name this program was run with. */
33 /* If nonzero, try to break on whitespace. */
34 static int break_spaces
;
36 /* If nonzero, count bytes, not column positions. */
37 static int count_bytes
;
39 /* If nonzero, at least one of the files we read was standard input. */
40 static int have_read_stdin
;
42 /* If nonzero, display usage information and exit. */
45 /* If nonzero, print the version on standard output then exit. */
46 static int show_version
;
48 static struct option
const longopts
[] =
50 {"bytes", no_argument
, NULL
, 'b'},
51 {"spaces", no_argument
, NULL
, 's'},
52 {"width", required_argument
, NULL
, 'w'},
53 {"help", no_argument
, &show_help
, 1},
54 {"version", no_argument
, &show_version
, 1},
62 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
67 Usage: %s [OPTION]... [FILE]...\n\
71 Wrap input lines in each FILE (standard input by default), writing to\n\
74 -b, --bytes count bytes rather than columns\n\
75 -s, --spaces break at spaces\n\
76 -w, --width=WIDTH use WIDTH columns instead of 80\n\
78 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
80 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
83 /* Assuming the current column is COLUMN, return the column that
84 printing C will move the cursor to.
85 The first column is 0. */
88 adjust_column (int column
, char c
)
100 column
= column
+ 8 - column
% 8;
101 else /* if (isprint (c)) */
109 /* Fold file FILENAME, or standard input if FILENAME is "-",
110 to stdout, with maximum line length WIDTH.
111 Return 0 if successful, 1 if an error occurs. */
114 fold_file (char *filename
, int width
)
118 int column
= 0; /* Screen column where next char will go. */
119 int offset_out
= 0; /* Index in `line_out' for next char. */
120 static char *line_out
= NULL
;
121 static int allocated_out
= 0;
123 if (STREQ (filename
, "-"))
129 istream
= fopen (filename
, "r");
133 error (0, errno
, "%s", filename
);
137 while ((c
= getc (istream
)) != EOF
)
139 if (offset_out
+ 1 >= allocated_out
)
141 allocated_out
+= 1024;
142 line_out
= xrealloc (line_out
, allocated_out
);
147 line_out
[offset_out
++] = c
;
148 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
149 column
= offset_out
= 0;
154 column
= adjust_column (column
, c
);
158 /* This character would make the line too long.
159 Print the line plus a newline, and make this character
160 start the next line. */
163 /* Look for the last blank. */
166 for (logical_end
= offset_out
- 1; logical_end
>= 0;
168 if (ISBLANK (line_out
[logical_end
]))
170 if (logical_end
>= 0)
174 /* Found a blank. Don't output the part after it. */
176 fwrite (line_out
, sizeof (char), (size_t) logical_end
,
179 /* Move the remainder to the beginning of the next line.
180 The areas being copied here might overlap. */
181 memmove (line_out
, line_out
+ logical_end
,
182 offset_out
- logical_end
);
183 offset_out
-= logical_end
;
184 for (column
= i
= 0; i
< offset_out
; i
++)
185 column
= adjust_column (column
, line_out
[i
]);
193 line_out
[offset_out
++] = c
;
197 line_out
[offset_out
++] = '\n';
198 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
199 column
= offset_out
= 0;
203 line_out
[offset_out
++] = c
;
207 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
209 if (ferror (istream
))
211 error (0, errno
, "%s", filename
);
212 if (!STREQ (filename
, "-"))
216 if (!STREQ (filename
, "-") && fclose (istream
) == EOF
)
218 error (0, errno
, "%s", filename
);
224 error (0, errno
, _("write error"));
232 main (int argc
, char **argv
)
239 program_name
= argv
[0];
240 setlocale (LC_ALL
, "");
241 bindtextdomain (PACKAGE
, LOCALEDIR
);
242 textdomain (PACKAGE
);
244 break_spaces
= count_bytes
= have_read_stdin
= 0;
246 /* Turn any numeric options into -w options. */
247 for (i
= 1; i
< argc
; i
++)
249 if (argv
[i
][0] == '-' && ISDIGIT (argv
[i
][1]))
253 s
= xmalloc (strlen (argv
[i
]) + 2);
256 strcpy (s
+ 2, argv
[i
] + 1);
261 while ((optc
= getopt_long (argc
, argv
, "bsw:", longopts
, NULL
)) != -1)
268 case 'b': /* Count bytes rather than columns. */
272 case 's': /* Break at word boundaries. */
276 case 'w': /* Line width. */
279 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
280 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
281 error (EXIT_FAILURE
, 0,
282 _("invalid number of columns: `%s'"), optarg
);
283 width
= (int) tmp_long
;
294 printf ("fold (%s) %s\n", GNU_PACKAGE
, VERSION
);
302 errs
|= fold_file ("-", width
);
304 for (i
= optind
; i
< argc
; i
++)
305 errs
|= fold_file (argv
[i
], width
);
307 if (have_read_stdin
&& fclose (stdin
) == EOF
)
308 error (EXIT_FAILURE
, errno
, "-");
309 if (fclose (stdout
) == EOF
)
310 error (EXIT_FAILURE
, errno
, _("write error"));
312 exit (errs
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);