summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-12-15 03:19:51 +0000
committerJim Meyering <jim@meyering.net>1995-12-15 03:19:51 +0000
commit1bcceb8ba484d0c596efcc849eba99c8766bb13a (patch)
treeaf6b9836ca7c252f96bf6082b75c2ead5b2fcc26 /src
parente0bcf6c00dbf6aae87e52edfd364f1f6e43a6ac8 (diff)
downloadcoreutils-1bcceb8ba484d0c596efcc849eba99c8766bb13a.tar.xz
(main): Use xstrtol instead of atoi.
(fold_file): Make a couple locals `int', not size_t, and cast fwrite's arg3 to size_t.
Diffstat (limited to 'src')
-rw-r--r--src/fold.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/src/fold.c b/src/fold.c
index 7a9605d4d..c0d02423b 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -25,8 +25,22 @@
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
+
+#if HAVE_LIMITS_H
+# include <limits.h>
+#endif
+
+#ifndef UINT_MAX
+# define UINT_MAX ((unsigned int) ~(unsigned int) 0)
+#endif
+
+#ifndef INT_MAX
+# define INT_MAX ((int) (UINT_MAX >> 1))
+#endif
+
#include "system.h"
#include "version.h"
+#include "xstrtol.h"
#include "error.h"
char *xrealloc ();
@@ -120,9 +134,9 @@ fold_file (char *filename, int width)
FILE *istream;
register int c;
int column = 0; /* Screen column where next char will go. */
- size_t offset_out = 0; /* Index in `line_out' for next char. */
+ int offset_out = 0; /* Index in `line_out' for next char. */
static char *line_out = NULL;
- static size_t allocated_out = 0;
+ static int allocated_out = 0;
if (!strcmp (filename, "-"))
{
@@ -149,7 +163,7 @@ fold_file (char *filename, int width)
if (c == '\n')
{
line_out[offset_out++] = c;
- fwrite (line_out, sizeof (char), offset_out, stdout);
+ fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
column = offset_out = 0;
continue;
}
@@ -177,7 +191,8 @@ fold_file (char *filename, int width)
/* Found a blank. Don't output the part after it. */
logical_end++;
- fwrite (line_out, sizeof (char), logical_end, stdout);
+ 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. */
@@ -198,7 +213,7 @@ fold_file (char *filename, int width)
}
}
line_out[offset_out++] = '\n';
- fwrite (line_out, sizeof (char), offset_out, stdout);
+ fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
column = offset_out = 0;
goto rescan;
}
@@ -207,7 +222,7 @@ fold_file (char *filename, int width)
}
if (offset_out)
- fwrite (line_out, sizeof (char), offset_out, stdout);
+ fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
if (ferror (istream))
{
@@ -274,9 +289,13 @@ main (int argc, char **argv)
break;
case 'w': /* Line width. */
- width = atoi (optarg);
- if (width < 1)
- error (1, 0, _("%s: invalid line width"), optarg);
+ {
+ long int tmp_long;
+ if (xstrtol (optarg, NULL, 10, &tmp_long, NULL) != LONGINT_OK
+ || tmp_long <= 0 || tmp_long > INT_MAX)
+ error (1, 0, _("invalid number of columns: `%s'"), optarg);
+ width = (int) tmp_long;
+ }
break;
default: