summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authorsobomax <sobomax@FreeBSD.org>2010-03-03 19:25:28 +0000
committersobomax <sobomax@FreeBSD.org>2010-03-03 19:25:28 +0000
commit8d2851aaa183c7a82819ef35816ab8ff778e5448 (patch)
tree45c5266f94dde28cf34cb8b8ce3c04603f26a46b /usr.bin
parent4d38d86e0ce6ea363a78a3745b1e487051b1fa6e (diff)
downloadFreeBSD-src-8d2851aaa183c7a82819ef35816ab8ff778e5448.zip
FreeBSD-src-8d2851aaa183c7a82819ef35816ab8ff778e5448.tar.gz
Use expand_number(3) from libutil instead of home-grown function to parse
human-friendly power-of-two numbers (i.e. 2k, 5M etc). Suggested by: many MFC after: 1 week
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/truncate/Makefile2
-rw-r--r--usr.bin/truncate/truncate.c67
2 files changed, 7 insertions, 62 deletions
diff --git a/usr.bin/truncate/Makefile b/usr.bin/truncate/Makefile
index 4752c5c..1b24d35 100644
--- a/usr.bin/truncate/Makefile
+++ b/usr.bin/truncate/Makefile
@@ -1,5 +1,7 @@
# $FreeBSD$
PROG= truncate
+DPADD= ${LIBUTIL}
+LDADD= -lutil
.include <bsd.prog.mk>
diff --git a/usr.bin/truncate/truncate.c b/usr.bin/truncate/truncate.c
index 3ab068b..12b81af 100644
--- a/usr.bin/truncate/truncate.c
+++ b/usr.bin/truncate/truncate.c
@@ -40,7 +40,8 @@ static const char rcsid[] =
#include <stdlib.h>
#include <unistd.h>
-static int parselength(char *, off_t *);
+#include <libutil.h>
+
static void usage(void);
static int no_create;
@@ -53,7 +54,8 @@ main(int argc, char **argv)
{
struct stat sb;
mode_t omode;
- off_t oflow, rsize, sz, tsize;
+ off_t oflow, rsize, tsize;
+ int64_t sz;
int ch, error, fd, oflags;
char *fname, *rname;
@@ -71,7 +73,7 @@ main(int argc, char **argv)
rname = optarg;
break;
case 's':
- if (parselength(optarg, &sz) == -1)
+ if (expand_number(optarg, &sz) == -1)
errx(EXIT_FAILURE,
"invalid size argument `%s'", optarg);
if (*optarg == '+' || *optarg == '-')
@@ -148,65 +150,6 @@ main(int argc, char **argv)
return error ? EXIT_FAILURE : EXIT_SUCCESS;
}
-/*
- * Return the numeric value of a string given in the form [+-][0-9]+[GMKT]
- * or -1 on format error or overflow.
- */
-static int
-parselength(char *ls, off_t *sz)
-{
- off_t length, oflow;
- int lsign;
-
- length = 0;
- lsign = 1;
-
- switch (*ls) {
- case '-':
- lsign = -1;
- case '+':
- ls++;
- }
-
-#define ASSIGN_CHK_OFLOW(x, y) if (x < y) return -1; y = x
- /*
- * Calculate the value of the decimal digit string, failing
- * on overflow.
- */
- while (isdigit(*ls)) {
- oflow = length * 10 + *ls++ - '0';
- ASSIGN_CHK_OFLOW(oflow, length);
- }
-
- switch (*ls) {
- case 'T':
- case 't':
- oflow = length * 1024;
- ASSIGN_CHK_OFLOW(oflow, length);
- case 'G':
- case 'g':
- oflow = length * 1024;
- ASSIGN_CHK_OFLOW(oflow, length);
- case 'M':
- case 'm':
- oflow = length * 1024;
- ASSIGN_CHK_OFLOW(oflow, length);
- case 'K':
- case 'k':
- if (ls[1] != '\0')
- return -1;
- oflow = length * 1024;
- ASSIGN_CHK_OFLOW(oflow, length);
- case '\0':
- break;
- default:
- return -1;
- }
-
- *sz = length * lsign;
- return 0;
-}
-
static void
usage(void)
{
OpenPOWER on IntegriCloud