Created
December 1, 2014 21:05
-
-
Save arkq/7e1a726a30e0d082dd63 to your computer and use it in GitHub Desktop.
Simple console countdown utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* counter.c - Simple console countdown utility | |
* Copyright (c) 2014 Arkadiusz Bokowy | |
* | |
* This projected is licensed under the terms of the MIT license. | |
* | |
* Compilation: | |
* gcc -Wall -Wextra -o counter counter.c | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <getopt.h> | |
#include <time.h> | |
/* Print formated output to the stdout. */ | |
static void printtimer(time_t current, time_t start, int verbose) { | |
unsigned int time_h, time_m, time_s; | |
time_h = current / 3600; | |
time_m = (current - time_h * 3600) / 60; | |
time_s = current - time_h * 3600 - time_m * 60; | |
if (!verbose) | |
printf("\rETA: %u:%02u:%02u\r", time_h, time_m, time_s); | |
else { | |
unsigned int etime_h, etime_m, etime_s; | |
etime_h = (start - current) / 3600; | |
etime_m = (start - current - etime_h * 3600) / 60; | |
etime_s = start - current - etime_h * 3600 - etime_m * 60; | |
printf("\rETA: %u:%02u:%02u (elapsed: %u:%02u:%02u)\r", | |
time_h, time_m, time_s, etime_h, etime_m, etime_s); | |
} | |
fflush(stdout); | |
} | |
int main(int argc, char *argv[]) { | |
int opt; | |
int beep = 0; | |
int verbose = 0; | |
char *command = NULL; | |
while ((opt = getopt(argc, argv, "hbvc:")) != -1) | |
switch (opt) { | |
case 'h': | |
return_usage: | |
printf("usage: %s [options] [[hh:]mm:]ss\n" | |
" -v\t\tprint more verbose output\n" | |
" -b\t\tplay beep sound on time out\n" | |
" -c CMD\trun given command on exit\n", | |
argv[0]); | |
return EXIT_SUCCESS; | |
case 'b': | |
beep = 1; | |
break; | |
case 'v': | |
verbose = 1; | |
break; | |
case 'c': | |
command = optarg; | |
break; | |
default: | |
printf("Try `%s -h` for more informations.\n", argv[0]); | |
return EXIT_FAILURE; | |
} | |
/* it should be only one argument (time) */ | |
if (optind + 1 != argc) | |
goto return_usage; | |
/* try to parse time argument */ | |
unsigned int time_h, time_m, time_s; | |
if (sscanf(argv[optind], "%u:%u:%u", &time_h, &time_m, &time_s) != 3) { | |
time_h = 0; | |
if (sscanf(argv[optind], "%u:%u", &time_m, &time_s) != 2) { | |
time_m = 0; | |
if (sscanf(argv[optind], "%u", &time_s) != 1) { | |
printf("error: unable to parse time\n"); | |
return EXIT_FAILURE; | |
} | |
} | |
} | |
/* convert given time to seconds */ | |
time_t input_time, counter; | |
input_time = time_s + time_m * 60 + time_h * 3600; | |
counter = input_time; | |
/* count-down loop */ | |
printtimer(counter, input_time, verbose); | |
while (counter--) { | |
usleep(1000000); | |
printtimer(counter, input_time, verbose); | |
/* play beep sound on last 3 seconds */ | |
if (beep && counter < 4) | |
printf("\a"); | |
} | |
printf("\n"); | |
/* play quick 3 beeps on exit */ | |
if (beep) { | |
int i = 3; | |
while (i--) { | |
usleep(100000); | |
printf("\a"); | |
} | |
} | |
if (command) | |
/* run given command on exit */ | |
return system(command); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment