aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
committerEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
commitf942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca (patch)
tree4a65e23e5052a6eece40768e9b332eb2f6c912c2
parentda87fcf25e0c94e57f00df84679cd6fadc56ed46 (diff)
parentc38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 (diff)
downloadc-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.gz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.xz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.zip
Merge branch 'master' of https://github.com/EliteTK/c-stuff
-rw-r--r--nkey.c51
-rw-r--r--similarity.c44
-rw-r--r--time_diff.c60
-rw-r--r--timer.c58
4 files changed, 203 insertions, 10 deletions
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 <ncurses.h>
+#include <stdio.h>
+
+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/similarity.c b/similarity.c
new file mode 100644
index 0000000..b1fbd06
--- /dev/null
+++ b/similarity.c
@@ -0,0 +1,44 @@
+#include <errno.h>
+#include <error.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+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;
+}
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 <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+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, &timespec) != 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;
+}
diff --git a/timer.c b/timer.c
index d32c9b1..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');
@@ -88,17 +96,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 +141,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");
@@ -130,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;
@@ -143,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;
@@ -167,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)