summaryrefslogtreecommitdiff
path: root/src/fold.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-08-03 06:16:05 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-08-03 06:16:05 +0000
commit3d3030da664c179fae27ab5859ab6ea3bb4e4de7 (patch)
tree7a620049d22b3e3e1a3ab5520699a51d28bc1de6 /src/fold.c
parentc8f4d13551487bb29eebc14df56f40f5b4d6ffaf (diff)
downloadcoreutils-3d3030da664c179fae27ab5859ab6ea3bb4e4de7.tar.gz
coreutils-3d3030da664c179fae27ab5859ab6ea3bb4e4de7.tar.bz2
coreutils-3d3030da664c179fae27ab5859ab6ea3bb4e4de7.zip
(TAB_WIDTH): New macro; use it instead of "8".
(fold_file, main): Use bool for booleans. (fold_file, main): Use size_t for sizes. (main): Allow -w options up to SIZE_MAX - TAB_WIDTH - 1, instead of prohibiting widths greater than INT_MAX.
Diffstat (limited to 'src/fold.c')
-rw-r--r--src/fold.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/fold.c b/src/fold.c
index a36ce6c94..8e343e787 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -28,6 +28,8 @@
#include "posixver.h"
#include "xstrtol.h"
+#define TAB_WIDTH 8
+
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "fold"
@@ -104,7 +106,7 @@ adjust_column (size_t column, char c)
else if (c == '\r')
column = 0;
else if (c == '\t')
- column = column + 8 - column % 8;
+ column += TAB_WIDTH - column % TAB_WIDTH;
else /* if (isprint (c)) */
column++;
}
@@ -115,10 +117,10 @@ adjust_column (size_t column, char c)
/* Fold file FILENAME, or standard input if FILENAME is "-",
to stdout, with maximum line length WIDTH.
- Return 0 if successful, 1 if an error occurs. */
+ Return true if successful. */
-static int
-fold_file (char *filename, int width)
+static bool
+fold_file (char *filename, size_t width)
{
FILE *istream;
register int c;
@@ -139,7 +141,7 @@ fold_file (char *filename, int width)
if (istream == NULL)
{
error (0, errno, "%s", filename);
- return 1;
+ return false;
}
while ((c = getc (istream)) != EOF)
@@ -224,24 +226,24 @@ fold_file (char *filename, int width)
error (0, saved_errno, "%s", filename);
if (!STREQ (filename, "-"))
fclose (istream);
- return 1;
+ return false;
}
if (!STREQ (filename, "-") && fclose (istream) == EOF)
{
error (0, errno, "%s", filename);
- return 1;
+ return false;
}
- return 0;
+ return true;
}
int
main (int argc, char **argv)
{
- int width = 80;
+ size_t width = 80;
int i;
int optc;
- int errs = 0;
+ bool ok;
initialize_main (&argc, &argv);
program_name = argv[0];
@@ -295,12 +297,12 @@ main (int argc, char **argv)
case 'w': /* Line width. */
{
- long int tmp_long;
- if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
- || tmp_long <= 0 || tmp_long > INT_MAX)
+ 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'"), optarg);
- width = (int) tmp_long;
+ width = tmp_ulong;
}
break;
@@ -314,13 +316,16 @@ main (int argc, char **argv)
}
if (argc == optind)
- errs |= fold_file ("-", width);
+ ok = fold_file ("-", width);
else
- for (i = optind; i < argc; i++)
- errs |= fold_file (argv[i], width);
+ {
+ ok = true;
+ for (i = optind; i < argc; i++)
+ ok &= fold_file (argv[i], width);
+ }
if (have_read_stdin && fclose (stdin) == EOF)
error (EXIT_FAILURE, errno, "-");
- exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+ exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}