summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1996-04-21 03:52:30 +0000
committerJim Meyering <jim@meyering.net>1996-04-21 03:52:30 +0000
commitc1f1f8ac3a0c2b8bb68bc0b9b629276bb745138f (patch)
treed93b45e2338f20a189709e748fbd7c953f7c2a76 /src
parent0aa556c39c3bb635a5850319bae7bad107b262b7 (diff)
downloadcoreutils-c1f1f8ac3a0c2b8bb68bc0b9b629276bb745138f.tar.xz
(decode_switches): Use xstrtol, not atoi.
Detect and ignore invalid value for COLUMNS environment variable. In error about invalid TABSIZE, don't reference optarg!
Diffstat (limited to 'src')
-rw-r--r--src/ls.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/ls.c b/src/ls.c
index 5da593def..1a1ebbc06 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -62,6 +62,7 @@
#include "ls.h"
#include "error.h"
#include "argmatch.h"
+#include "xstrtol.h"
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
@@ -645,6 +646,7 @@ decode_switches (int argc, char **argv)
register char *p;
int c;
int i;
+ long int tmp_long;
qmark_funny_chars = 0;
quote_funny_chars = 0;
@@ -701,7 +703,18 @@ decode_switches (int argc, char **argv)
quote_as_string = 0;
p = getenv ("COLUMNS");
- line_length = p ? atoi (p) : 80;
+ if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
+ && 0 < tmp_long && tmp_long <= INT_MAX)
+ {
+ line_length = (int) tmp_long;
+ }
+ else
+ {
+ error (0, 0,
+ _("ignoring invalid width in enironment variable COLUMNS: %s"),
+ p);
+ line_length = 80;
+ }
#ifdef TIOCGWINSZ
{
@@ -714,13 +727,17 @@ decode_switches (int argc, char **argv)
/* FIXME: reference TABSIZE iff !posix_pedantic. */
p = getenv ("TABSIZE");
- /* FIXME: use strtol here! */
- tabsize = p ? atoi (p) : 8;
- if (tabsize < 1)
+
+ if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
+ && 0 < tmp_long && tmp_long <= INT_MAX)
+ {
+ tabsize = (int) tmp_long;
+ }
+ else
{
error (0, 0,
_("ignoring invalid tab size in enironment variable TABSIZE: %s"),
- optarg);
+ p);
tabsize = 8;
}
@@ -1527,10 +1544,10 @@ print_long_format (struct fileinfo *f)
char modebuf[20];
char timebuf[40];
- /* 7 fields that may (worst case be 64-bit integral values) require 20 bytes,
- 10 character mode field,
- 24 characters for the time,
- 9 spaces following each of these fields,
+ /* 7 fields that may (worst case: 64-bit integral values) require 20 bytes,
+ 1 10-character mode string,
+ 1 24-character time string,
+ 9 spaces, one following each of these fields,
and 1 trailing NUL byte. */
char bigbuf[7 * 20 + 10 + 24 + 9 + 1];
char *p;