/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./countdown ./countdown.c */ #include #include #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif // info is the message shown when this app is given any of its help options const char* info = "" "countdown [options...] [seconds/duration...]\n" "\n" "\n" "Count-down the number of seconds given, or 60 seconds by default.\n" "\n" "The duration can be either a number (of seconds) or a string with no\n" "spaces and numbered units `h`, `m`, `s`, which can be mixed together.\n" "\n" "Decimal dots and negative numbers aren't allowed." "\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\n" "\n" " -h show this help message\n" " -help show this help message\n" "\n" "\n" "Examples" "\n" "\n" " countdown 33 # 33 seconds\n" " countdown 3h45m # 3 hours and 45 minutes\n" " countdown 3h45s # 3 hours and 45 seconds\n" " countdown 3h4m5s # 3 hours 4 minutes and 5 seconds\n" ""; void countdown(int seconds) { struct timespec now; clock_gettime(CLOCK_REALTIME, &now); const int64_t ns2sec = 1e9; const int64_t end = ns2sec * (now.tv_sec + seconds) + now.tv_nsec; struct timespec delay; delay.tv_sec = 0; delay.tv_nsec = 100 * 1e6; int64_t t = ns2sec * now.tv_sec + now.tv_nsec; while (t < end) { const int64_t diff = end - t; const int64_t round_up = (diff % ns2sec) > (ns2sec / 5); const int64_t left = diff / ns2sec + round_up; const int h = left / 3600; const int m = (left / 60) % 60; const int s = left % 60; fprintf(stderr, "\r%02d:%02d:%02d", h, m, s); nanosleep(&delay, NULL); clock_gettime(CLOCK_REALTIME, &now); t = ns2sec * now.tv_sec + now.tv_nsec; } fputs("\r \r", stderr); } // is_help_option simplifies control-flow for func main bool is_help_option(char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } // bool parse_duration(char* s, int* seconds) { // char* end; // int v = strtol(argv[1], &end, 10); // if (*end == 0) { // *seconds = v; // return true; // } // return false; // } bool parse_duration(char* s, int* seconds) { int total = 0; int accum = 0; for (; *s != 0; s++) { const int b = *s; if ('0' <= b && b <= '9') { accum *= 10; accum += b - '0'; continue; } switch (b) { case 'h': accum *= 3600; total += accum; accum = 0; break; case 'm': accum *= 60; total += accum; accum = 0; break; case 's': total += accum; accum = 0; break; default: return false; } } total += accum; *seconds = total; return true; } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif // handle any of the help options, if given if (argc > 1 && is_help_option(argv[1])) { puts(info); return 0; } int seconds = 60; if (argc > 1) { if (!parse_duration(argv[1], &seconds)) { const char* fmt = "\x1b[31minvalid duration '%s'\x1b[0m\n"; fprintf(stderr, fmt, argv[1]); return 1; } } if (seconds > 0) { countdown(seconds); } printf("done waiting for %d seconds\n", seconds); return 0; }