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>
27 #include "xdectoint.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "fold"
34 #define AUTHORS proper_name ("David MacKenzie")
36 /* If nonzero, try to break on whitespace. */
37 static bool break_spaces
;
39 /* If nonzero, count bytes, not column positions. */
40 static bool count_bytes
;
42 /* If nonzero, at least one of the files we read was standard input. */
43 static bool have_read_stdin
;
45 static char const shortopts
[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
47 static struct option
const longopts
[] =
49 {"bytes", no_argument
, nullptr, 'b'},
50 {"spaces", no_argument
, nullptr, 's'},
51 {"width", required_argument
, nullptr, 'w'},
52 {GETOPT_HELP_OPTION_DECL
},
53 {GETOPT_VERSION_OPTION_DECL
},
54 {nullptr, 0, nullptr, 0}
60 if (status
!= EXIT_SUCCESS
)
65 Usage: %s [OPTION]... [FILE]...\n\
69 Wrap input lines in each FILE, writing to standard output.\n\
73 emit_mandatory_arg_note ();
76 -b, --bytes count bytes rather than columns\n\
77 -s, --spaces break at spaces\n\
78 -w, --width=WIDTH use WIDTH columns instead of 80\n\
80 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
81 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
82 emit_ancillary_info (PROGRAM_NAME
);
87 /* Assuming the current column is COLUMN, return the column that
88 printing C will move the cursor to.
89 The first column is 0. */
92 adjust_column (size_t column
, char c
)
104 column
+= TAB_WIDTH
- column
% TAB_WIDTH
;
105 else /* if (isprint (c)) */
113 /* Fold file FILENAME, or standard input if FILENAME is "-",
114 to stdout, with maximum line length WIDTH.
115 Return true if successful. */
118 fold_file (char const *filename
, size_t width
)
122 size_t column
= 0; /* Screen column where next char will go. */
123 size_t offset_out
= 0; /* Index in 'line_out' for next char. */
124 static char *line_out
= nullptr;
125 static size_t allocated_out
= 0;
128 if (STREQ (filename
, "-"))
131 have_read_stdin
= true;
134 istream
= fopen (filename
, "r");
136 if (istream
== nullptr)
138 error (0, errno
, "%s", quotef (filename
));
142 fadvise (istream
, FADVISE_SEQUENTIAL
);
144 while ((c
= getc (istream
)) != EOF
)
146 if (offset_out
+ 1 >= allocated_out
)
147 line_out
= X2REALLOC (line_out
, &allocated_out
);
151 line_out
[offset_out
++] = c
;
152 fwrite (line_out
, sizeof (char), offset_out
, stdout
);
153 column
= offset_out
= 0;
158 column
= adjust_column (column
, c
);
162 /* This character would make the line too long.
163 Print the line plus a newline, and make this character
164 start the next line. */
167 bool found_blank
= false;
168 size_t logical_end
= offset_out
;
170 /* Look for the last blank. */
174 if (isblank (to_uchar (line_out
[logical_end
])))
185 /* Found a blank. Don't output the part after it. */
187 fwrite (line_out
, sizeof (char), (size_t) logical_end
,
190 /* Move the remainder to the beginning of the next line.
191 The areas being copied here might overlap. */
192 memmove (line_out
, line_out
+ logical_end
,
193 offset_out
- logical_end
);
194 offset_out
-= logical_end
;
195 for (column
= i
= 0; i
< offset_out
; i
++)
196 column
= adjust_column (column
, line_out
[i
]);
203 line_out
[offset_out
++] = c
;
207 line_out
[offset_out
++] = '\n';
208 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
209 column
= offset_out
= 0;
213 line_out
[offset_out
++] = c
;
217 if (!ferror (istream
))
221 fwrite (line_out
, sizeof (char), (size_t) offset_out
, stdout
);
223 if (STREQ (filename
, "-"))
225 else if (fclose (istream
) != 0 && !saved_errno
)
230 error (0, saved_errno
, "%s", quotef (filename
));
238 main (int argc
, char **argv
)
245 initialize_main (&argc
, &argv
);
246 set_program_name (argv
[0]);
247 setlocale (LC_ALL
, "");
248 bindtextdomain (PACKAGE
, LOCALEDIR
);
249 textdomain (PACKAGE
);
251 atexit (close_stdout
);
253 break_spaces
= count_bytes
= have_read_stdin
= false;
255 while ((optc
= getopt_long (argc
, argv
, shortopts
, longopts
, nullptr)) != -1)
261 case 'b': /* Count bytes rather than columns. */
265 case 's': /* Break at word boundaries. */
269 case '0': case '1': case '2': case '3': case '4':
270 case '5': case '6': case '7': case '8': case '9':
280 case 'w': /* Line width. */
281 width
= xdectoumax (optarg
, 1, SIZE_MAX
- TAB_WIDTH
- 1, "",
282 _("invalid number of columns"), 0);
285 case_GETOPT_HELP_CHAR
;
287 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
290 usage (EXIT_FAILURE
);
295 ok
= fold_file ("-", width
);
299 for (i
= optind
; i
< argc
; i
++)
300 ok
&= fold_file (argv
[i
], width
);
303 if (have_read_stdin
&& fclose (stdin
) == EOF
)
304 error (EXIT_FAILURE
, errno
, "-");
306 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;