diff --git a/.gitignore b/.gitignore index 12c1930..0f20a9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.deb a.out dist +*.swp diff --git a/Makefile b/Makefile index 2034a81..aba159e 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,30 @@ all: djl-utils.deb dist: bin DEBIAN.control -bin: sign pasta suptime stopwatch timestamp +bin: sign pasta suptime countdown stopwatch timestamp -sign: src/sign +distdir: + mkdir -p dist/usr/bin + +sign: src/sign distdir cp src/sign dist/usr/bin -pasta: src/pasta +pasta: src/pasta distdir cp src/pasta dist/usr/bin -suptime: src/suptime.c +suptime: src/suptime.c distdir cc -o dist/usr/bin/suptime src/suptime.c -stopwatch: src/stopwatch.c +countdown: src/countdown.c distdir + cc -o dist/usr/bin/countdown src/countdown.c + +stopwatch: src/stopwatch.c distdir cc -o dist/usr/bin/stopwatch src/stopwatch.c -timestamp: src/timestamp +timestamp: src/timestamp distdir cp src/timestamp dist/usr/bin -DEBIAN.control: src/DEBIAN/control +DEBIAN.control: src/DEBIAN/control distdir rm -rf dist/DEBIAN cp -r src/DEBIAN dist/DEBIAN diff --git a/src/countdown.c b/src/countdown.c new file mode 100644 index 0000000..7d30b91 --- /dev/null +++ b/src/countdown.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020 Dylan Lom + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include "util/util.h" + +const char* argv0; + +void usage() { + fprintf(stderr, "usage: %s time\n", argv0); + exit(EXIT_FAILURE); +} + +int main(int argc, char* argv[]) { + SET_ARGV0(); + + if (argc < 1) usage(); + long time; + char* p; + time = strtol(argv[0], &p, 10); + if (errno != 0 || *p != '\0') { + fprintf(stderr, "strtol: %s\n", errno + ? strerror(errno) + : "Failed to parse time argument." + ); + exit(EXIT_FAILURE); + } + SHIFT_ARGS(); + + while (time) { + /* Clear stdout, otherwise going from e.g. 10->9 leaves a trailing 0 */ + printf("\r "); + printf("\r%d", time); + fflush(stdout); + sleep(1); + time--; + } + + puts("\n"); + return EXIT_SUCCESS; +} diff --git a/src/stopwatch.c b/src/stopwatch.c index d202e47..50528a1 100644 --- a/src/stopwatch.c +++ b/src/stopwatch.c @@ -18,6 +18,7 @@ #include #include #include +#include "util/util.h" const char* argv0; @@ -27,14 +28,12 @@ void usage() { } void printtime(int t) { - printf("\rstopwatch: %d", t); + printf("\r%d", t); fflush(stdout); } -#define SHIFT_ARGS() argv++; argc--; int main(int argc, char* argv[]) { - argv0 = argv[0]; - SHIFT_ARGS(); + SET_ARGV0(); if (argc > 0) usage(); time_t t = 0; diff --git a/src/util/util.h b/src/util/util.h new file mode 100644 index 0000000..a121f83 --- /dev/null +++ b/src/util/util.h @@ -0,0 +1,3 @@ +#define SHIFT_ARGS() argv++; argc--; +#define SET_ARGV0() argv0 = argv[0]; SHIFT_ARGS(); +