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 <stdbool.h> 32 #include <stddef.h> 33 #include <stdint.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <time.h> 38 #include <unistd.h> 39 40 #ifdef _WIN32 41 #include <fcntl.h> 42 #include <windows.h> 43 #endif 44 45 const char* info = "" 46 "countdown [options...] [seconds/duration...]\n" 47 "\n" 48 "\n" 49 "Count-down the number of seconds given, or 60 seconds by default.\n" 50 "\n" 51 "The duration can be either a number (of seconds) or a string with no\n" 52 "spaces and numbered units `h`, `m`, `s`, which can be mixed together.\n" 53 "\n" 54 "Decimal dots and negative numbers aren't allowed." 55 "\n" 56 "\n" 57 "Options, all of which can start with either 1 or 2 dashes:\n" 58 "\n" 59 "\n" 60 " -h show this help message\n" 61 " -help show this help message\n" 62 "\n" 63 "\n" 64 "Examples" 65 "\n" 66 "\n" 67 " countdown 33 # 33 seconds\n" 68 " countdown 3h45m # 3 hours and 45 minutes\n" 69 " countdown 3h45s # 3 hours and 45 seconds\n" 70 " countdown 3h4m5s # 3 hours 4 minutes and 5 seconds\n" 71 ""; 72 73 void countdown(int seconds) { 74 struct timespec now; 75 clock_gettime(CLOCK_REALTIME, &now); 76 const int64_t ns2sec = 1e9; 77 const int64_t end = ns2sec * (now.tv_sec + seconds) + now.tv_nsec; 78 79 struct timespec delay; 80 delay.tv_sec = 0; 81 delay.tv_nsec = 100 * 1e6; 82 83 int64_t t = ns2sec * now.tv_sec + now.tv_nsec; 84 while (t < end) { 85 const int64_t diff = end - t; 86 const int64_t round_up = (diff % ns2sec) > (ns2sec / 5); 87 const int64_t left = diff / ns2sec + round_up; 88 const int h = left / 3600; 89 const int m = (left / 60) % 60; 90 const int s = left % 60; 91 fprintf(stderr, "\r%02d:%02d:%02d", h, m, s); 92 93 nanosleep(&delay, NULL); 94 clock_gettime(CLOCK_REALTIME, &now); 95 t = ns2sec * now.tv_sec + now.tv_nsec; 96 } 97 98 fputs("\r \r", stderr); 99 } 100 101 // is_help_option simplifies control-flow for func main 102 bool is_help_option(const char* s) { 103 return (s[0] == '-') && ( 104 strcmp(s, "-h") == 0 || 105 strcmp(s, "-help") == 0 || 106 strcmp(s, "--h") == 0 || 107 strcmp(s, "--help") == 0 108 ); 109 } 110 111 // bool parse_duration(char* s, int* seconds) { 112 // char* end; 113 // int v = strtol(argv[1], &end, 10); 114 // if (*end == 0) { 115 // *seconds = v; 116 // return true; 117 // } 118 // return false; 119 // } 120 121 bool parse_colon_style_duration(char* s, int* seconds) { 122 int accum = 0; 123 int digits = 0; 124 125 int parts[4] = {0, 0, 0, 0}; 126 const int n = sizeof(parts) / sizeof(parts[0]); 127 const int multipliers[4] = {1, 60, 60 * 60, 24 * 60 * 60}; 128 int which = 0; 129 130 for (; *s != 0; s++) { 131 const int b = *s; 132 133 if (b == ':') { 134 if (digits < 1) { 135 return false; 136 } 137 138 parts[which] = accum; 139 accum = 0; 140 digits = 0; 141 142 which++; 143 if (which >= n) { 144 return false; 145 } 146 147 continue; 148 } 149 150 if ('0' <= b && b <= '9') { 151 digits++; 152 accum *= 10; 153 accum += b - '0'; 154 continue; 155 } 156 157 return false; 158 } 159 160 if (digits < 1) { 161 return false; 162 } 163 164 parts[which] = accum; 165 166 int total = 0; 167 for (int i = which, j = 0; i >= 0; i--, j++) { 168 total += multipliers[j] * parts[i]; 169 } 170 *seconds = total; 171 return true; 172 } 173 174 bool parse_duration(char* s, int* seconds) { 175 int total = 0; 176 int accum = 0; 177 178 if (parse_colon_style_duration(s, seconds)) { 179 return true; 180 } 181 182 for (; *s != 0; s++) { 183 const int b = *s; 184 185 if ('0' <= b && b <= '9') { 186 accum *= 10; 187 accum += b - '0'; 188 continue; 189 } 190 191 switch (b) { 192 case 'h': 193 accum *= 3600; 194 total += accum; 195 accum = 0; 196 break; 197 198 case 'm': 199 accum *= 60; 200 total += accum; 201 accum = 0; 202 break; 203 204 case 's': 205 total += accum; 206 accum = 0; 207 break; 208 209 default: 210 return false; 211 } 212 } 213 214 total += accum; 215 *seconds = total; 216 return true; 217 } 218 219 int main(int argc, char** argv) { 220 #ifdef _WIN32 221 setmode(fileno(stdin), O_BINARY); 222 // ensure output lines end in LF instead of CRLF on windows 223 setmode(fileno(stdout), O_BINARY); 224 setmode(fileno(stderr), O_BINARY); 225 #endif 226 227 // handle any of the help options, if given 228 if (argc > 1 && is_help_option(argv[1])) { 229 puts(info); 230 return 0; 231 } 232 233 int seconds = 60; 234 if (argc > 1) { 235 if (!parse_duration(argv[1], &seconds)) { 236 const char* fmt = "\x1b[31minvalid duration '%s'\x1b[0m\n"; 237 fprintf(stderr, fmt, argv[1]); 238 return 1; 239 } 240 } 241 242 if (seconds > 0) { 243 countdown(seconds); 244 } 245 246 printf("done waiting for %d seconds\n", seconds); 247 return 0; 248 }