]> granicus.if.org Git - procps-ng/commitdiff
free -c option uses strutils
authorCraig Small <csmall@enc.com.au>
Tue, 3 Jan 2012 22:53:51 +0000 (09:53 +1100)
committerCraig Small <csmall@enc.com.au>
Tue, 3 Jan 2012 22:53:51 +0000 (09:53 +1100)
The handling of the -c (count) option in free uses the standard string
handling and error utils.  The program also checks for negative counts
and errors on these.

Makefile.am
free.c

index aa9503824c3c1314fc176ac750485c2a6f63dc36..2b7c3b4197a37996611c1b2a2b92b31bc89d9d1c 100644 (file)
@@ -70,6 +70,7 @@ skill_SOURCES = skill.c $(top_srcdir)/lib/strutils.c
 snice_SOURCES = skill.c $(top_srcdir)/lib/strutils.c
 tload_SOURCES = tload.c $(top_srcdir)/lib/strutils.c
 pkill_SOURCES = pgrep.c
+free_SOURCES = free.c $(top_srcdir)/lib/strutils.c
 vmstat_SOURCES = vmstat.c $(top_srcdir)/lib/strutils.c
 
 sysconf_DATA = sysctl.conf
diff --git a/free.c b/free.c
index cd1b1455f1690d2a65ebb3491c31c52bdbaca8a1..dd2d9eb746dde8cfbf3d0bb019831214cd608bf4 100644 (file)
--- a/free.c
+++ b/free.c
  * Copyright 2004 Albert Cahalan
  */
 
+#include "config.h"
 #include "proc/sysinfo.h"
 #include "proc/version.h"
 #include "c.h"
 #include "nls.h"
+#include "strutils.h"
 
 #include <locale.h>
 #include <errno.h>
@@ -255,10 +257,11 @@ int main(int argc, char **argv)
                case 'c':
                        flags |= FREE_REPEAT;
                        flags |= FREE_REPEATCOUNT;
-                       args.repeat_counter = strtoul(optarg, &endptr, 10);
-                       if (errno || optarg == endptr || (endptr && *endptr))
-                               xerrx(EXIT_FAILURE, _("count argument `%s' failed"), optarg);
-
+                       args.repeat_counter = strtol_or_err(optarg,
+                               _("failed to parse count argument"));
+                       if (args.repeat_counter > ULONG_MAX/2)
+                         error(EXIT_FAILURE, ERANGE,
+                                 _("failed to parse count argument: '%s'"), optarg);
                        break;
                case HELP_OPTION:
                        usage(stdout);