From 2ca65abe1cf7825184116982fedb6aa991540d24 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 11 Jun 2015 22:04:50 +0100 Subject: timer.c: Made years, months and days verbal. Years, months and days now display in a verbal fashion and only appear if the values are above 0. Additionally, plural and singular values are also displayed correctly. --- timer.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/timer.c b/timer.c index d32c9b1..875cb5c 100644 --- a/timer.c +++ b/timer.c @@ -88,17 +88,39 @@ void clear_line(void) void print_time(unsigned long total_sec, bool show_colon) { unsigned long years, months, days, hours, minutes, seconds; - char colon = show_colon ? ':' : ' '; + char colon = show_colon ? ':' : ' ', syears[128], smonths[16], + sdays[16]; years = total_sec / SEC_YEAR; total_sec %= SEC_YEAR; + if (years == 1) + snprintf(syears, sizeof syears, "%lu year, ", years); + else if (years > 1) + snprintf(syears, sizeof syears, "%lu years, ", years); + else + syears[0] = '\0'; + months = total_sec / SEC_MONTH; total_sec %= SEC_MONTH; + if (months == 1) + snprintf(smonths, sizeof smonths, "%lu month, ", months); + else if (months > 1) + snprintf(smonths, sizeof smonths, "%lu months, ", months); + else + smonths[0] = '\0'; + days = total_sec / SEC_DAY; total_sec %= SEC_DAY; + if (days == 1) + snprintf(sdays, sizeof sdays, "%lu day, ", days); + else if (days > 1) + snprintf(sdays, sizeof sdays, "%lu days, ", days); + else + sdays[0] = '\0'; + hours = total_sec / SEC_HOUR; total_sec %= SEC_HOUR; @@ -111,8 +133,8 @@ void print_time(unsigned long total_sec, bool show_colon) if (total_sec != 0) error(1, 0, "An error occured during time formatting"); - printf(" %luY %luM %luD %.2lu%c%.2lu%c%.2lu\r", years, months, - days, hours, colon, minutes, colon, seconds); + printf(" %s%s%s%.2lu%c%.2lu%c%.2lu\r", syears, smonths, sdays, hours, + colon, minutes, colon, seconds); if (fflush(stdout) != 0) error(1, errno, "Failed to flush stdout"); -- cgit v1.2.3-54-g00ecf From 0dc36e29dd1e65b22745ffe4b348047e36f38d1b Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 11 Jun 2015 22:37:34 +0100 Subject: timer.c: Use SIGWINCH handler to get term_width when changed. timer.c no longer spams ioctl TIOCGWINSZ 4 times a second, it now handles SIGWINCH and sets a file scope variable to the width. --- timer.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/timer.c b/timer.c index 875cb5c..87c90a3 100644 --- a/timer.c +++ b/timer.c @@ -30,6 +30,19 @@ #define SEC_YEAR 31556940 static const unsigned long interval_nsec = 1000000000 / 4; +unsigned short term_width = 0; /* Maybe volatile? */ + +void sigwinch(int sig) { + if (sig != SIGWINCH) + return; + + struct winsize ws; + + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) + error(1, errno, "Failed to get terminal width"); + + term_width = ws.ws_col; +} unsigned long int get_seconds(char *code) { @@ -67,15 +80,10 @@ unsigned long int get_seconds(char *code) void clear_line(void) { - struct winsize ws; - - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) - error(1, errno, "IOCTL failed"); - - if (ws.ws_col <= 1) + if (term_width <= 1) return; - for (unsigned short i = 0; i < ws.ws_col - 1; i++) + for (unsigned short i = 0; i < term_width - 1; i++) putchar(' '); putchar('\r'); @@ -152,6 +160,7 @@ int main(int argc, char **argv) sigset_t sigset; struct itimerspec its; struct sigevent sev; + struct sigaction sigact; timer_t timerid; unsigned long total_seconds = 0; @@ -165,6 +174,11 @@ int main(int argc, char **argv) for (int i = 1; i < argc; i++) total_seconds += get_seconds(argv[i]); + sigact.sa_handler = sigwinch; + + if (sigaction(SIGWINCH, &sigact, NULL) != 0) + error(1, errno, "Unable to set SIGWINCH signal action"); + sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN; sev.sigev_value.sival_ptr = &timerid; @@ -189,6 +203,8 @@ int main(int argc, char **argv) if (timer_settime(timerid, 0, &its, NULL) != 0) error(1, errno, "Could not set timer"); + sigwinch(SIGWINCH); + for (unsigned long i = 0; i < total_seconds; i++) for (int ii = 0; ii < 4; ii++) { if (sigwait(&sigset, &sig), sig != SIGRTMIN) -- cgit v1.2.3-54-g00ecf From a72bf3a44e8002181b2818ab557434a2a0a22c96 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 19 Jun 2015 19:15:27 +0100 Subject: nkey.c; time_diff.c --- nkey.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ time_diff.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 nkey.c create mode 100644 time_diff.c diff --git a/nkey.c b/nkey.c new file mode 100644 index 0000000..de92d66 --- /dev/null +++ b/nkey.c @@ -0,0 +1,51 @@ +#include +#include + +int getch_nodelay(void) +{ + int retval; + + nodelay(stdscr, TRUE); + retval = getch(); + nodelay(stdscr, FALSE); + + return retval; +} + +int main(void) +{ + cbreak(); + initscr(); + noecho(); + nonl(); + + keypad(stdscr, TRUE); + + while (1) { + char number[32]; + int c[5] = { + getch(), + getch_nodelay(), + getch_nodelay(), + getch_nodelay(), + getch_nodelay() + }; + + if (c[1] == ERR) + snprintf(number, 32, "%d ", c[0]); + else if (c[2] == ERR) + snprintf(number, 32, "%d-%d ", c[0], c[1]); + else if (c[3] == ERR) + snprintf(number, 32, "%d-%d-%d ", c[0], c[1], c[2]); + else if (c[4] == ERR) + snprintf(number, 32, "%d-%d-%d-%d ", c[0], c[1], c[2], + c[3]); + else if (c[5] == ERR) + snprintf(number, 32, "%d-%d-%d-%d-%d ", c[0], c[1], + c[2], c[3], c[4]); + + addstr(number); + } + + return 0; +} diff --git a/time_diff.c b/time_diff.c new file mode 100644 index 0000000..726a6cd --- /dev/null +++ b/time_diff.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include + +const char *progname; + +inline uint64_t get_time_ms(void) +{ + struct timespec timespec; + static const uint64_t invmil = 1 / 1000000; + + if (clock_gettime(CLOCK_MONOTONIC, ×pec) != 0) { + fprintf(stderr, "%s: Unable to get time delta: %s\n", + progname, strerror(errno)); + exit(EXIT_FAILURE); + } + + return timespec.tv_sec * 1000 + timespec.tv_nsec * invmil; +} + +uint64_t get_delta(void) +{ + static uint64_t oldtime = 0; + uint64_t timediff, newtime; + + newtime = get_time_ms(); + + timediff = newtime - oldtime; + + oldtime = newtime; + + return timediff; +} + +int main(int argc, char **argv) +{ + struct { + int a; + char b; + } teststruct = {1, 2}; + + uint64_t buildup = 0, count = 0; + + progname = argc != 0 && argv[0] != NULL ? argv[0] : ""; + + get_delta(); + + while (true) { + for (buildup += get_delta(); buildup >= 1000; buildup -= 1000) + printf("%lu, %lu\n", count++, buildup); + } + + printf("%d\n", teststruct.a); + + return 0; +} -- cgit v1.2.3-54-g00ecf From c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 21 Jun 2015 14:31:04 +0100 Subject: similarity.c --- similarity.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 similarity.c diff --git a/similarity.c b/similarity.c new file mode 100644 index 0000000..b1fbd06 --- /dev/null +++ b/similarity.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + unsigned long matches = 0, misses = 0; + char *strings[2]; + size_t lengths[2]; + size_t pos[2] = {0}; + unsigned current = 0; + + if (argc != 3) + error(1, 0, "Incorrect number of arguments %d", argc); + + strings[0] = argv[1]; + lengths[0] = strlen(strings[0]); + strings[1] = argv[2]; + lengths[1] = strlen(strings[1]); + + for (; pos[0] < lengths[0] && pos[1] < lengths[1];) { + if (strings[0][pos[0]] == strings[1][pos[1]]) { + matches++; + pos[0]++; + pos[1]++; + current = current ? 0 : 1; + } else { + pos[current ? 0 : 1]++; + misses++; + } + } + + if (lengths[0] > pos[0]) + misses += lengths[0] - pos[0] - 1; + + if (lengths[1] > pos[1]) + misses += lengths[1] - pos[1] - 1; + + printf("Matches: %lu, Misses: %lu\n", matches, misses); + + return 0; +} -- cgit v1.2.3-54-g00ecf