From 857248325b4da39d75de53d3a4b6627add8fbbb5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 9 Nov 2015 17:09:41 +0100 Subject: parse arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/sleep-until.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/sleep-until.c diff --git a/src/sleep-until.c b/src/sleep-until.c new file mode 100644 index 0000000..1dcb3bb --- /dev/null +++ b/src/sleep-until.c @@ -0,0 +1,68 @@ +/** + * sleep-until – Sleeps until a specified time + * + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include + + + +int main(int argc, char* argv[]) +{ + char* argv0; + char float_part[10]; + struct itimerspec* values; + int i; + + if (argc < 2) + return 0; + + argv0 = argv[0]; + argc--, argv--; + + values = alloca(argc * sizeof(*values)); + memset(values, 0, argc * sizeof(*values)); + float_part[9] = '\0'; + for (i = 0; i < argc; i++) + { + char* p1 = argv[i]; + char* p2 = strchr(p1, '.'); + char excess; + size_t len; + if (p2) + *p2++ = '\0'; + + values[i].it_value.tv_sec = (time_t)atoll(p1); + + if (p2) + { + len = strlen(p2); + memset(float_part, '0', 9 * sizeof(char)); + excess = len > 9 ? p2[9] : '0'; + len = len > 9 ? 9 : len; + memcpy(float_part + 9 - len, p2, len * sizeof(char)); + values[i].it_value.tv_nsec = atol(float_part); + if ((excess >= '5') && (values[i].it_value.tv_nsec++ == 999999999L)) + values[i].it_value.tv_nsec = 0, values[i].it_value.tv_sec++; + } + } + + return 0; +} + -- cgit v1.2.3-70-g09d2