File: countdown.c 1 /* 2 The MIT License (MIT) 3 4 Copyright © 2020-2025 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the “Software”), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 You can build this command-line app by running 27 28 cc -Wall -s -O2 -o ./countdown ./countdown.c 29 */ 30 31 #include <fcntl.h> 32 #include <stdbool.h> 33 #include <stddef.h> 34 #include <stdint.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <time.h> 39 #include <unistd.h> 40 41 #ifdef _WIN32 42 #include <windows.h> 43 #endif 44 45 // info is the message shown when this app is given any of its help options 46 const char* info = "" 47 "countdown [options...] [seconds/duration...]\n" 48 "\n" 49 "\n" 50 "Count-down the number of seconds given, or 60 seconds by default.\n" 51 "\n" 52 "The duration can be either a number (of seconds) or a string with no\n" 53 "spaces and numbered units `h`, `m`, `s`, which can be mixed together.\n" 54 "\n" 55 "Decimal dots and negative numbers aren't allowed." 56 "\n" 57 "\n" 58 "Options, all of which can start with either 1 or 2 dashes:\n" 59 "\n" 60 "\n" 61 " -h show this help message\n" 62 " -help show this help message\n" 63 "\n" 64 "\n" 65 "Examples" 66 "\n" 67 "\n" 68 " countdown 33 # 33 seconds\n" 69 " countdown 3h45m # 3 hours and 45 minutes\n" 70 " countdown 3h45s # 3 hours and 45 seconds\n" 71 " countdown 3h4m5s # 3 hours 4 minutes and 5 seconds\n" 72 ""; 73 74 void countdown(int seconds) { 75 struct timespec now; 76 clock_gettime(CLOCK_REALTIME, &now); 77 const int64_t ns2sec = 1e9; 78 const int64_t end = ns2sec * (now.tv_sec + seconds) + now.tv_nsec; 79 80 struct timespec delay; 81 delay.tv_sec = 0; 82 delay.tv_nsec = 100 * 1e6; 83 84 int64_t t = ns2sec * now.tv_sec + now.tv_nsec; 85 while (t < end) { 86 const int64_t diff = end - t; 87 const int64_t round_up = (diff % ns2sec) > (ns2sec / 5); 88 const int64_t left = diff / ns2sec + round_up; 89 const int h = left / 3600; 90 const int m = (left / 60) % 60; 91 const int s = left % 60; 92 fprintf(stderr, "\r%02d:%02d:%02d", h, m, s); 93 94 nanosleep(&delay, NULL); 95 clock_gettime(CLOCK_REALTIME, &now); 96 t = ns2sec * now.tv_sec + now.tv_nsec; 97 } 98 99 fputs("\r \r", stderr); 100 } 101 102 // is_help_option simplifies control-flow for func main 103 bool is_help_option(char* s) { 104 return (s[0] == '-') && ( 105 strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || 106 strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 107 ); 108 } 109 110 // bool parse_duration(char* s, int* seconds) { 111 // char* end; 112 // int v = strtol(argv[1], &end, 10); 113 // if (*end == 0) { 114 // *seconds = v; 115 // return true; 116 // } 117 // return false; 118 // } 119 120 bool parse_duration(char* s, int* seconds) { 121 int total = 0; 122 int accum = 0; 123 124 for (; *s != 0; s++) { 125 const int b = *s; 126 127 if ('0' <= b && b <= '9') { 128 accum *= 10; 129 accum += b - '0'; 130 continue; 131 } 132 133 switch (b) { 134 case 'h': 135 accum *= 3600; 136 total += accum; 137 accum = 0; 138 break; 139 140 case 'm': 141 accum *= 60; 142 total += accum; 143 accum = 0; 144 break; 145 146 case 's': 147 total += accum; 148 accum = 0; 149 break; 150 151 default: 152 return false; 153 } 154 } 155 156 total += accum; 157 *seconds = total; 158 return true; 159 } 160 161 int main(int argc, char** argv) { 162 #ifdef _WIN32 163 setmode(fileno(stdin), O_BINARY); 164 // ensure output lines end in LF instead of CRLF on windows 165 setmode(fileno(stdout), O_BINARY); 166 setmode(fileno(stderr), O_BINARY); 167 #endif 168 169 // handle any of the help options, if given 170 if (argc > 1 && is_help_option(argv[1])) { 171 puts(info); 172 return 0; 173 } 174 175 int seconds = 60; 176 if (argc > 1) { 177 if (!parse_duration(argv[1], &seconds)) { 178 const char* fmt = "\x1b[31minvalid duration '%s'\x1b[0m\n"; 179 fprintf(stderr, fmt, argv[1]); 180 return 1; 181 } 182 } 183 184 if (seconds > 0) { 185 countdown(seconds); 186 } 187 188 printf("done waiting for %d seconds\n", seconds); 189 return 0; 190 }