1 /* fold -- wrap each input line to fit in specified width.
2 Copyright (C) 1991-2023 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 3 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
23 #include <sys/types.h>
29 #include "xdectoint.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "fold"
36 #define AUTHORS proper_name ("David MacKenzie")
38 /* If nonzero, try to break on whitespace. */
39 static bool break_spaces
;
41 /* If nonzero, count bytes, not column positions. */
42 static bool count_bytes
;
44 /* If nonzero, at least one of the files we read was standard input. */
45 static bool have_read_stdin
;
47 static char const shortopts
[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
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
},
62 if (status
!= EXIT_SUCCESS
)
67 Usage: %s [OPTION]... [FILE]...\n\
71 Wrap input lines in each FILE, writing to standard output.\n\
75 emit_mandatory_arg_note ();
78 -b, --bytes count bytes rather than columns\n\
79 -s, --spaces break at spaces\n\
80 -w, --width=WIDTH use WIDTH columns instead of 80\n\
82 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
83 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
84 emit_ancillary_info (PROGRAM_NAME
);
89 /* Assuming the current column is COLUMN, return the column that
90 printing C will move the cursor to.
91 The first column is 0. */
94 adjust_column (size_t column
, char c
)
106 column
+= TAB_WIDTH
- column
% TAB_WIDTH
;
107 else /* if (isprint (c)) */
115 /* Fold file FILENAME, or standard input if FILENAME is "-",
116 to stdout, with maximum line length WIDTH.
117 Return true if successful. */
120 fold_file (char const *filename
, size_t width
)
124 size_t column
= 0; /* Screen column where next char will go. */
125 size_t offset_out
= 0; /* Index in 'line_out' for next char. */
126 static char *line_out
= NULL
;
127 static size_t allocated_out
= 0;
130 if (STREQ (filename
, "-"))
133 have_read_stdin
= true;
136 istream
= fopen (filename
, "r");
140 error (0, errno
, "%s", quotef (filename
));
144 fadvise (istream
, FADVISE_SEQUENTIAL
);
146 while ((c
= getc (istream
)) != EOF
)
148 if (offset_out
+ 1 >= allocated_out
)
149 line_out
= X2REALLOC (line_out
, &allocated_out
);
153 line_out
[offset_out
++] = c
;
154 fwrite (line_out
, sizeof (char), offset_out
, stdout
);
155 column
= offset_out
= 0;
160 column
= adjust_column (column
, c
);
164 /* This character would make the line too long.
165 Print the line plus a newline, and make this character
166 start the next line. */
169 bool found_blank
= false;
170 size_t logical_end
= offset_out
;
172 /* Look for the last blank. */
176 if (isblank (to_uchar (line_out
[logical_end
])))
187 /* Found a blank. Don't output the part after it. */
189 fwrite (line_out
, sizeof (char), (size_t) logical_end
,
192 /* Move the remainder to the beginning of the next line.
193 The areas being copied here might overlap. */
194 memmove (line_out
, line_out
+ logical_end
,
195 offset_out
- logical_end
);
196 offset_out
-= logical_end
;
197 for (column
= i
= 0; i
< offset_out
; i
++)
198 column
= adjust_column (column
, line_out
[i
]);
205 line_out
[offset_out
++] = c
;
209 line_out
[offset_out
++] = '\n';
210 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
211 column
= offset_out
= 0;
215 line_out
[offset_out
++] = c
;
219 if (!ferror (istream
))
223 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
225 if (STREQ (filename
, "-"))
227 else if (fclose (istream
) != 0 && !saved_errno
)
232 error (0, saved_errno
, "%s", quotef (filename
));
240 main (int argc
, char **argv
)
247 initialize_main (&argc
, &argv
);
248 set_program_name (argv
[0]);
249 setlocale (LC_ALL
, "");
250 bindtextdomain (PACKAGE
, LOCALEDIR
);
251 textdomain (PACKAGE
);
253 atexit (close_stdout
);
255 break_spaces
= count_bytes
= have_read_stdin
= false;
257 while ((optc
= getopt_long (argc
, argv
, shortopts
, longopts
, NULL
)) != -1)
263 case 'b': /* Count bytes rather than columns. */
267 case 's': /* Break at word boundaries. */
271 case '0': case '1': case '2': case '3': case '4':
272 case '5': case '6': case '7': case '8': case '9':
282 case 'w': /* Line width. */
283 width
= xdectoumax (optarg
, 1, SIZE_MAX
- TAB_WIDTH
- 1, "",
284 _("invalid number of columns"), 0);
287 case_GETOPT_HELP_CHAR
;
289 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
292 usage (EXIT_FAILURE
);
297 ok
= fold_file ("-", width
);
301 for (i
= optind
; i
< argc
; i
++)
302 ok
&= fold_file (argv
[i
], width
);
305 if (have_read_stdin
&& fclose (stdin
) == EOF
)
306 die (EXIT_FAILURE
, errno
, "-");
308 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;