commit 28b4618122eb17819ebeb2344774c6a47733519b
parent 37dc9cbfdf01c7af51c09c1645d07de844c97ee1
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Tue, 23 Dec 2025 00:54:53 +0000
parse integer from string for sleep argument
Diffstat:
| M | sleep.cpp | | | 35 | +++++++++++++++++++++++++++++++++-- |
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/sleep.cpp b/sleep.cpp
@@ -1,7 +1,38 @@
#include "linux.hpp"
-int main()
+bool parse_digit(char c, uint64_t& out)
{
- timespec duration{1, 0};
+ switch (c)
+ {
+ case '0' ... '9':
+ out = c - '0';
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool parse_u64(char const* p, uint64_t& out)
+{
+ if (!parse_digit(*p, out))
+ return false;
+
+ while (*++p)
+ {
+ uint64_t d;
+ if (not parse_digit(*p, d))
+ return false;
+ out = out * 10 + d;
+ }
+ return true;
+}
+
+int main(int argc, char* argv[])
+{
+ uint64_t n;
+ if (argc != 2 or not parse_u64(argv[1], n))
+ return 1;
+
+ timespec duration{(time_t)n, 0};
nanosleep(duration, nullptr);
}