File: countdown.c
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2024 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...]\n"
  48     "\n"
  49     "\n"
  50     "Count-down the number of seconds given, or 60 seconds by default.\n"
  51     "\n"
  52     "\n"
  53     "Options, all of which can start with either 1 or 2 dashes:\n"
  54     "\n"
  55     "\n"
  56     "  -h          show this help message\n"
  57     "  -help       show this help message\n"
  58     "";
  59 
  60 void countdown(int seconds) {
  61     struct timespec now;
  62     clock_gettime(CLOCK_REALTIME, &now);
  63     const int64_t ns2sec = 1e9;
  64     const int64_t end = ns2sec * (now.tv_sec + seconds) + now.tv_nsec;
  65 
  66     struct timespec delay;
  67     delay.tv_sec = 0;
  68     delay.tv_nsec = 100 * 1e6;
  69 
  70     int64_t t = ns2sec * now.tv_sec + now.tv_nsec;
  71     while (t < end) {
  72         const int64_t diff = end - t;
  73         const int64_t round_up = (diff % ns2sec) > (ns2sec / 5);
  74         const int64_t left = diff / ns2sec + round_up;
  75         const int h = left / 3600;
  76         const int m = (left / 60) % 60;
  77         const int s = left % 60;
  78         fprintf(stderr, "\r%02d:%02d:%02d", h, m, s);
  79 
  80         nanosleep(&delay, NULL);
  81         clock_gettime(CLOCK_REALTIME, &now);
  82         t = ns2sec * now.tv_sec + now.tv_nsec;
  83     }
  84 
  85     fputs("\r        \r", stderr);
  86 }
  87 
  88 // is_help_option simplifies control-flow for func main
  89 bool is_help_option(char* s) {
  90     return (s[0] == '-') && (
  91         strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 ||
  92         strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0
  93     );
  94 }
  95 
  96 int main(int argc, char** argv) {
  97 #ifdef _WIN32
  98     setmode(fileno(stdin), O_BINARY);
  99     // ensure output lines end in LF instead of CRLF on windows
 100     setmode(fileno(stdout), O_BINARY);
 101     setmode(fileno(stderr), O_BINARY);
 102 #endif
 103 
 104     // handle any of the help options, if given
 105     if (argc > 1 && is_help_option(argv[1])) {
 106         puts(info);
 107         return 0;
 108     }
 109 
 110     int seconds = 60;
 111     if (argc > 1) {
 112         char* end;
 113         int v = strtol(argv[1], &end, 10);
 114         if (*end == 0) {
 115             seconds = v;
 116         }
 117     }
 118 
 119     if (seconds > 0) {
 120         countdown(seconds);
 121     }
 122     printf("done waiting for %d seconds\n", seconds);
 123     return 0;
 124 }