summaryrefslogtreecommitdiffstats
path: root/sys-utils/flock.c
diff options
context:
space:
mode:
authorSami Kerola2011-09-25 12:03:30 +0200
committerSami Kerola2011-09-29 22:45:27 +0200
commit605325b23b36238c8f3ae165e37cab9064553cf7 (patch)
tree9d0e71f19f2c0a25f2bdb4bdb4e43a6ad2ea6c62 /sys-utils/flock.c
parentlib: [strutils] add strtod_or_err() function (diff)
downloadkernel-qcow2-util-linux-605325b23b36238c8f3ae165e37cab9064553cf7.tar.gz
kernel-qcow2-util-linux-605325b23b36238c8f3ae165e37cab9064553cf7.tar.xz
kernel-qcow2-util-linux-605325b23b36238c8f3ae165e37cab9064553cf7.zip
flock: simplify strtotimeval()
Retire private string to number conversion and use strtod_or_err() instead. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/flock.c')
-rw-r--r--sys-utils/flock.c34
1 files changed, 8 insertions, 26 deletions
diff --git a/sys-utils/flock.c b/sys-utils/flock.c
index cdfa6cfb1..f23651b4d 100644
--- a/sys-utils/flock.c
+++ b/sys-utils/flock.c
@@ -73,30 +73,15 @@ static void timeout_handler(int sig __attribute__((__unused__)))
timeout_expired = 1;
}
-static char *strtotimeval(const char *str, struct timeval *tv)
+static void strtotimeval(const char *str, struct timeval *tv)
{
- char *s;
- /* Fractional seconds */
- long fs;
- int i;
+ double user_input;
- tv->tv_sec = strtol(str, &s, 10);
- fs = 0;
- if (*s == '.') {
- s++;
- for (i = 0; i < 6; i++) {
- if (!isdigit(*s))
- break;
- fs *= 10;
- fs += *s++ - '0';
- }
- for (; i < 6; i++)
- fs *= 10;
- while (isdigit(*s))
- s++;
- }
- tv->tv_usec = fs;
- return s;
+ user_input = strtod_or_err(str, "bad number");
+ tv->tv_sec = (time_t) user_input;
+ tv->tv_usec = (long)((user_input - tv->tv_sec) * 1000000);
+ if ((tv->tv_sec + tv->tv_usec) == 0)
+ errx(EX_USAGE, _("timeout cannot be zero"));
}
int main(int argc, char *argv[])
@@ -110,7 +95,6 @@ int main(int argc, char *argv[])
int opt, ix;
int do_close = 0;
int status;
- char *eon;
char **cmd_argv = NULL, *sh_c_argv[4];
const char *filename = NULL;
struct sigaction sa, old_sa;
@@ -161,9 +145,7 @@ int main(int argc, char *argv[])
break;
case 'w':
have_timeout = 1;
- eon = strtotimeval(optarg, &timeout.it_value);
- if (*eon)
- usage(EX_USAGE);
+ strtotimeval(optarg, &timeout.it_value);
break;
case 'V':
printf("flock (%s)\n", PACKAGE_STRING);