aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-12-23 16:17:28 -0500
committerB. Watson <urchlay@slackware.uk>2024-12-23 16:17:28 -0500
commit6b4b1907c96b9070fd81c29c771f3da0510ff22c (patch)
tree944c0799312a76b052d5efb82000c8f36cde1f90
parente739d2ccb7a7b488b7dcc15313b0ffaedd8c0a77 (diff)
downloaduxd-6b4b1907c96b9070fd81c29c771f3da0510ff22c.tar.gz
allow decimal (floating point) for numeric args
-rw-r--r--uxd.16
-rw-r--r--uxd.c26
-rw-r--r--uxd.rst6
3 files changed, 23 insertions, 15 deletions
diff --git a/uxd.1 b/uxd.1
index 41f01c4..d541d71 100644
--- a/uxd.1
+++ b/uxd.1
@@ -61,10 +61,12 @@ Options can be bundled: \fB\-ubc1234\fP is the same as \fB\-u\fP \fB\-b\fP \fB\-
by itself.
.sp
The options that accept numbers (\fB\-l\fP, \fB\-o\fP, \fB\-s\fP, and \fB\-S\fP)
-allow decimal, hex (with \fI0x\fP prefix), or octal (with \fI0\fP prefix).
-Also, you can use the suffixes \fIk\fP, \fIm\fP, \fIg\fP, and \fIt\fP for power\-of\-2 based
+allow decimal, or hex (with \fI0x\fP prefix).
+You can use the suffixes \fIk\fP, \fIm\fP, \fIg\fP, and \fIt\fP for power\-of\-2 based
kilobytes, megabytes, gigabytes, or terabytes (e.g. \fI1k\fP is 1024 bytes), as well
as \fIK\fP, \fIM\fP, \fIG\fP, and \fIT\fP for power\-of\-10 based (e.g. \fI1K\fP is 1000 bytes).
+Also, a decimal point can be used: \fB1.5K\fP is 1500 bytes, \fB1.5k\fP is
+1536 bytes.
.\" the comments are turned into the --help message by mkusage.pl.
.
.INDENT 0.0
diff --git a/uxd.c b/uxd.c
index 9e402dc..80bf910 100644
--- a/uxd.c
+++ b/uxd.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <limits.h>
/* UTF-8 spec summary, taken from Wikipedia and elsewhere, kept here
for locality of reference.
@@ -237,9 +238,9 @@ void number_err(int opt) {
long parse_number(int opt, const char *s) {
char *e;
- long result;
+ double result;
- result = strtol(s, &e, 0);
+ result = strtod(s, &e);
/* require at least one digit (otherwise -sk would be allowed) */
if(e == s)
@@ -251,14 +252,14 @@ long parse_number(int opt, const char *s) {
case 'B':
if(e[1]) number_err(opt);
break; /* allow & ignore b/B for "bytes" */
- case 'k': result *= 1024L; break;
- case 'm': result *= 1048576L; break;
- case 'g': result *= 1073741824L; break;
- case 't': result *= 1099511627776L; break;
- case 'K': result *= 1000L; break;
- case 'M': result *= 1000000L; break;
- case 'G': result *= 1000000000L; break;
- case 'T': result *= 1000000000000L; break;
+ case 'k': result *= 1024.0L; break;
+ case 'm': result *= 1048576.0L; break;
+ case 'g': result *= 1073741824.0L; break;
+ case 't': result *= 1099511627776.0L; break;
+ case 'K': result *= 1000.0L; break;
+ case 'M': result *= 1000000.0L; break;
+ case 'G': result *= 1000000000.0L; break;
+ case 'T': result *= 1000000000000.0L; break;
default:
number_err(opt);
}
@@ -267,7 +268,10 @@ long parse_number(int opt, const char *s) {
if(e[0] && e[1] && e[1] != 'b' && e[1] != 'B')
number_err(opt);
- return result;
+ if(result < LONG_MIN || result > LONG_MAX)
+ number_err(opt);
+
+ return (long)result;
}
void parse_args(int argc, char **argv) {
diff --git a/uxd.rst b/uxd.rst
index 7190c20..d5dbf7d 100644
--- a/uxd.rst
+++ b/uxd.rst
@@ -51,10 +51,12 @@ Options can be bundled: **-ubc1234** is the same as **-u** **-b** **-c
by itself.
The options that accept numbers (**-l**, **-o**, **-s**, and **-S**)
-allow decimal, hex (with *0x* prefix), or octal (with *0* prefix).
-Also, you can use the suffixes *k*, *m*, *g*, and *t* for power-of-2 based
+allow decimal, or hex (with *0x* prefix).
+You can use the suffixes *k*, *m*, *g*, and *t* for power-of-2 based
kilobytes, megabytes, gigabytes, or terabytes (e.g. *1k* is 1024 bytes), as well
as *K*, *M*, *G*, and *T* for power-of-10 based (e.g. *1K* is 1000 bytes).
+Also, a decimal point can be used: **1.5K** is 1500 bytes, **1.5k** is
+1536 bytes.
.. the comments are turned into the --help message by mkusage.pl.