Add countdown

Add util/util.h
Use util/util.h in stopwatch
Add distdir make target
This commit is contained in:
Dylan Lom 2020-11-29 16:35:39 +11:00
parent b1fc2508d3
commit ba1ffbb380
5 changed files with 77 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.deb
a.out
dist
*.swp

View File

@ -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

57
src/countdown.c Normal file
View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2020 Dylan Lom <djl@dylanlom.com>
*
* 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}

View File

@ -18,6 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#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;

3
src/util/util.h Normal file
View File

@ -0,0 +1,3 @@
#define SHIFT_ARGS() argv++; argc--;
#define SET_ARGV0() argv0 = argv[0]; SHIFT_ARGS();