1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 91, 1995-2002 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>
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "fold"
35 #define AUTHORS "David MacKenzie"
37 /* The name this program was run with. */
40 /* If nonzero, try to break on whitespace. */
41 static int break_spaces
;
43 /* If nonzero, count bytes, not column positions. */
44 static int count_bytes
;
46 /* If nonzero, at least one of the files we read was standard input. */
47 static int have_read_stdin
;
49 static struct option
const longopts
[] =
51 {"bytes", no_argument
, NULL
, 'b'},
52 {"spaces", no_argument
, NULL
, 's'},
53 {"width", required_argument
, NULL
, 'w'},
54 {GETOPT_HELP_OPTION_DECL
},
55 {GETOPT_VERSION_OPTION_DECL
},
63 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
68 Usage: %s [OPTION]... [FILE]...\n\
72 Wrap input lines in each FILE (standard input by default), writing to\n\
77 Mandatory arguments to long options are mandatory for short options too.\n\
80 -b, --bytes count bytes rather than columns\n\
81 -s, --spaces break at spaces\n\
82 -w, --width=WIDTH use WIDTH columns instead of 80\n\
84 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
85 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
86 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
88 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
91 /* Assuming the current column is COLUMN, return the column that
92 printing C will move the cursor to.
93 The first column is 0. */
96 adjust_column (int column
, char c
)
108 column
= column
+ 8 - column
% 8;
109 else /* if (isprint (c)) */
117 /* Fold file FILENAME, or standard input if FILENAME is "-",
118 to stdout, with maximum line length WIDTH.
119 Return 0 if successful, 1 if an error occurs. */
122 fold_file (char *filename
, int width
)
126 int column
= 0; /* Screen column where next char will go. */
127 int offset_out
= 0; /* Index in `line_out' for next char. */
128 static char *line_out
= NULL
;
129 static int allocated_out
= 0;
131 if (STREQ (filename
, "-"))
137 istream
= fopen (filename
, "r");
141 error (0, errno
, "%s", filename
);
145 while ((c
= getc (istream
)) != EOF
)
147 if (offset_out
+ 1 >= allocated_out
)
149 allocated_out
+= 1024;
150 line_out
= xrealloc (line_out
, allocated_out
);
155 line_out
[offset_out
++] = c
;
156 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
157 column
= offset_out
= 0;
162 column
= adjust_column (column
, c
);
166 /* This character would make the line too long.
167 Print the line plus a newline, and make this character
168 start the next line. */
171 /* Look for the last blank. */
174 for (logical_end
= offset_out
- 1; logical_end
>= 0;
176 if (ISBLANK (line_out
[logical_end
]))
178 if (logical_end
>= 0)
182 /* Found a blank. Don't output the part after it. */
184 fwrite (line_out
, sizeof (char), (size_t) logical_end
,
187 /* Move the remainder to the beginning of the next line.
188 The areas being copied here might overlap. */
189 memmove (line_out
, line_out
+ logical_end
,
190 offset_out
- logical_end
);
191 offset_out
-= logical_end
;
192 for (column
= i
= 0; i
< offset_out
; i
++)
193 column
= adjust_column (column
, line_out
[i
]);
201 line_out
[offset_out
++] = c
;
205 line_out
[offset_out
++] = '\n';
206 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
207 column
= offset_out
= 0;
211 line_out
[offset_out
++] = c
;
215 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
217 if (ferror (istream
))
219 error (0, errno
, "%s", filename
);
220 if (!STREQ (filename
, "-"))
224 if (!STREQ (filename
, "-") && fclose (istream
) == EOF
)
226 error (0, errno
, "%s", filename
);
234 main (int argc
, char **argv
)
241 program_name
= argv
[0];
242 setlocale (LC_ALL
, "");
243 bindtextdomain (PACKAGE
, LOCALEDIR
);
244 textdomain (PACKAGE
);
246 atexit (close_stdout
);
248 break_spaces
= count_bytes
= have_read_stdin
= 0;
250 /* Turn any numeric options into -w options. */
251 for (i
= 1; i
< argc
; i
++)
253 char const *a
= argv
[i
];
256 if (a
[1] == '-' && ! a
[2])
260 char *s
= xmalloc (strlen (a
) + 2);
263 strcpy (s
+ 2, a
+ 1);
265 if (200112 <= posix2_version ())
267 error (0, 0, _("`%s' option is obsolete; use `%s'"), a
, s
);
268 usage (EXIT_FAILURE
);
274 while ((optc
= getopt_long (argc
, argv
, "bsw:", longopts
, NULL
)) != -1)
281 case 'b': /* Count bytes rather than columns. */
285 case 's': /* Break at word boundaries. */
289 case 'w': /* Line width. */
292 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
293 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
294 error (EXIT_FAILURE
, 0,
295 _("invalid number of columns: `%s'"), optarg
);
296 width
= (int) tmp_long
;
300 case_GETOPT_HELP_CHAR
;
302 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
310 errs
|= fold_file ("-", width
);
312 for (i
= optind
; i
< argc
; i
++)
313 errs
|= fold_file (argv
[i
], width
);
315 if (have_read_stdin
&& fclose (stdin
) == EOF
)
316 error (EXIT_FAILURE
, errno
, "-");
318 exit (errs
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);