diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
commit | 70e9163c9c18e995515598085cb824e554eb7ae7 (patch) | |
tree | a42dc8b2a6c031354bf31472de888bfc8a060132 /src/fold.c | |
parent | cbf5993c43f49281173f185863577d86bfac6eae (diff) | |
download | coreutils-tarball-70e9163c9c18e995515598085cb824e554eb7ae7.tar.gz |
coreutils-8.25HEADcoreutils-8.25master
Diffstat (limited to 'src/fold.c')
-rw-r--r-- | src/fold.c | 258 |
1 files changed, 124 insertions, 134 deletions
@@ -1,10 +1,10 @@ /* fold -- wrap each input line to fit in specified width. - Copyright (C) 91, 1995-2006 Free Software Foundation, Inc. + Copyright (C) 1991-2016 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -12,8 +12,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */ @@ -25,18 +24,15 @@ #include "system.h" #include "error.h" -#include "quote.h" -#include "xstrtol.h" +#include "fadvise.h" +#include "xdectoint.h" #define TAB_WIDTH 8 -/* The official name of this program (e.g., no `g' prefix). */ +/* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "fold" -#define AUTHORS "David MacKenzie" - -/* The name this program was run with. */ -char *program_name; +#define AUTHORS proper_name ("David MacKenzie") /* If nonzero, try to break on whitespace. */ static bool break_spaces; @@ -63,22 +59,20 @@ void usage (int status) { if (status != EXIT_SUCCESS) - fprintf (stderr, _("Try `%s --help' for more information.\n"), - program_name); + emit_try_help (); else { printf (_("\ Usage: %s [OPTION]... [FILE]...\n\ "), - program_name); - fputs (_("\ -Wrap input lines in each FILE (standard input by default), writing to\n\ -standard output.\n\ -\n\ -"), stdout); + program_name); fputs (_("\ -Mandatory arguments to long options are mandatory for short options too.\n\ +Wrap input lines in each FILE, writing to standard output.\n\ "), stdout); + + emit_stdin_note (); + emit_mandatory_arg_note (); + fputs (_("\ -b, --bytes count bytes rather than columns\n\ -s, --spaces break at spaces\n\ @@ -86,7 +80,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); - printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); + emit_ancillary_info (PROGRAM_NAME); } exit (status); } @@ -101,16 +95,16 @@ adjust_column (size_t column, char c) if (!count_bytes) { if (c == '\b') - { - if (column > 0) - column--; - } + { + if (column > 0) + column--; + } else if (c == '\r') - column = 0; + column = 0; else if (c == '\t') - column += TAB_WIDTH - column % TAB_WIDTH; + column += TAB_WIDTH - column % TAB_WIDTH; else /* if (isprint (c)) */ - column++; + column++; } else column++; @@ -127,7 +121,7 @@ fold_file (char const *filename, size_t width) FILE *istream; int c; size_t column = 0; /* Screen column where next char will go. */ - size_t offset_out = 0; /* Index in `line_out' for next char. */ + size_t offset_out = 0; /* Index in 'line_out' for next char. */ static char *line_out = NULL; static size_t allocated_out = 0; int saved_errno; @@ -142,78 +136,80 @@ fold_file (char const *filename, size_t width) if (istream == NULL) { - error (0, errno, "%s", filename); + error (0, errno, "%s", quotef (filename)); return false; } + fadvise (istream, FADVISE_SEQUENTIAL); + while ((c = getc (istream)) != EOF) { if (offset_out + 1 >= allocated_out) - line_out = X2REALLOC (line_out, &allocated_out); + line_out = X2REALLOC (line_out, &allocated_out); if (c == '\n') - { - line_out[offset_out++] = c; - fwrite (line_out, sizeof (char), offset_out, stdout); - column = offset_out = 0; - continue; - } + { + line_out[offset_out++] = c; + fwrite (line_out, sizeof (char), offset_out, stdout); + column = offset_out = 0; + continue; + } rescan: column = adjust_column (column, c); if (column > width) - { - /* This character would make the line too long. - Print the line plus a newline, and make this character - start the next line. */ - if (break_spaces) - { - bool found_blank = false; - size_t logical_end = offset_out; - - /* Look for the last blank. */ - while (logical_end) - { - --logical_end; - if (isblank (to_uchar (line_out[logical_end]))) - { - found_blank = true; - break; - } - } - - if (found_blank) - { - size_t i; - - /* Found a blank. Don't output the part after it. */ - logical_end++; - fwrite (line_out, sizeof (char), (size_t) logical_end, - stdout); - putchar ('\n'); - /* Move the remainder to the beginning of the next line. - The areas being copied here might overlap. */ - memmove (line_out, line_out + logical_end, - offset_out - logical_end); - offset_out -= logical_end; - for (column = i = 0; i < offset_out; i++) - column = adjust_column (column, line_out[i]); - goto rescan; - } - } - - if (offset_out == 0) - { - line_out[offset_out++] = c; - continue; - } - - line_out[offset_out++] = '\n'; - fwrite (line_out, sizeof (char), (size_t) offset_out, stdout); - column = offset_out = 0; - goto rescan; - } + { + /* This character would make the line too long. + Print the line plus a newline, and make this character + start the next line. */ + if (break_spaces) + { + bool found_blank = false; + size_t logical_end = offset_out; + + /* Look for the last blank. */ + while (logical_end) + { + --logical_end; + if (isblank (to_uchar (line_out[logical_end]))) + { + found_blank = true; + break; + } + } + + if (found_blank) + { + size_t i; + + /* Found a blank. Don't output the part after it. */ + logical_end++; + fwrite (line_out, sizeof (char), (size_t) logical_end, + stdout); + putchar ('\n'); + /* Move the remainder to the beginning of the next line. + The areas being copied here might overlap. */ + memmove (line_out, line_out + logical_end, + offset_out - logical_end); + offset_out -= logical_end; + for (column = i = 0; i < offset_out; i++) + column = adjust_column (column, line_out[i]); + goto rescan; + } + } + + if (offset_out == 0) + { + line_out[offset_out++] = c; + continue; + } + + line_out[offset_out++] = '\n'; + fwrite (line_out, sizeof (char), (size_t) offset_out, stdout); + column = offset_out = 0; + goto rescan; + } line_out[offset_out++] = c; } @@ -225,14 +221,14 @@ fold_file (char const *filename, size_t width) if (ferror (istream)) { - error (0, saved_errno, "%s", filename); + error (0, saved_errno, "%s", quotef (filename)); if (!STREQ (filename, "-")) - fclose (istream); + fclose (istream); return false; } if (!STREQ (filename, "-") && fclose (istream) == EOF) { - error (0, errno, "%s", filename); + error (0, errno, "%s", quotef (filename)); return false; } @@ -248,7 +244,7 @@ main (int argc, char **argv) bool ok; initialize_main (&argc, &argv); - program_name = argv[0]; + set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); @@ -262,44 +258,38 @@ main (int argc, char **argv) char optargbuf[2]; switch (optc) - { - case 'b': /* Count bytes rather than columns. */ - count_bytes = true; - break; - - case 's': /* Break at word boundaries. */ - break_spaces = true; - break; - - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - if (optarg) - optarg--; - else - { - optargbuf[0] = optc; - optargbuf[1] = '\0'; - optarg = optargbuf; - } - /* Fall through. */ - case 'w': /* Line width. */ - { - unsigned long int tmp_ulong; - if (! (xstrtoul (optarg, NULL, 10, &tmp_ulong, "") == LONGINT_OK - && 0 < tmp_ulong && tmp_ulong < SIZE_MAX - TAB_WIDTH)) - error (EXIT_FAILURE, 0, - _("invalid number of columns: %s"), quote (optarg)); - width = tmp_ulong; - } - break; - - case_GETOPT_HELP_CHAR; - - case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); - - default: - usage (EXIT_FAILURE); - } + { + case 'b': /* Count bytes rather than columns. */ + count_bytes = true; + break; + + case 's': /* Break at word boundaries. */ + break_spaces = true; + break; + + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + if (optarg) + optarg--; + else + { + optargbuf[0] = optc; + optargbuf[1] = '\0'; + optarg = optargbuf; + } + /* Fall through. */ + case 'w': /* Line width. */ + width = xdectoumax (optarg, 1, SIZE_MAX - TAB_WIDTH - 1, "", + _("invalid number of columns"), 0); + break; + + case_GETOPT_HELP_CHAR; + + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + + default: + usage (EXIT_FAILURE); + } } if (argc == optind) @@ -308,11 +298,11 @@ main (int argc, char **argv) { ok = true; for (i = optind; i < argc; i++) - ok &= fold_file (argv[i], width); + ok &= fold_file (argv[i], width); } if (have_read_stdin && fclose (stdin) == EOF) error (EXIT_FAILURE, errno, "-"); - exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); + return ok ? EXIT_SUCCESS : EXIT_FAILURE; } |