aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemsen2012-05-17 19:27:54 +0200
committerGravatar Tom Willemsen2012-05-17 19:27:54 +0200
commitf49fe5af6701285ddb926518efd2c087a0fdc7af (patch)
tree248199356f0934a8821321ca48cbd438b3884f54
downloadbaps1-f49fe5af6701285ddb926518efd2c087a0fdc7af.tar.gz
baps1-f49fe5af6701285ddb926518efd2c087a0fdc7af.zip
Initial commit
-rw-r--r--src/Makefile2
-rw-r--r--src/main.c136
2 files changed, 138 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..b9611a0
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,2 @@
+baps1: main.c
+ $(CC) -Wall -Wextra -lm main.c -o baps1
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..c1b18d4
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,136 @@
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static char *
+get_tty_name(void)
+{
+ return ttyname(STDIN_FILENO);
+}
+
+static char *
+get_filename(void)
+{
+ char *filename = malloc(sizeof(char) * 255);
+ char *tty = get_tty_name();
+ char *pch;
+
+ while ((pch = strchr(tty, '/')))
+ *pch = '_';
+
+ sprintf(filename, "/tmp/baps1_%u_%s", getuid(), tty);
+
+ return filename;
+}
+
+static time_t
+load_previous_time(char *filename)
+{
+ FILE *timefile = fopen(filename, "r");
+ time_t prev;
+
+ fscanf(timefile, "%ld", &prev);
+ fclose(timefile);
+
+ return prev;
+}
+
+static time_t
+get_previous_time(char *filename)
+{
+ time_t prev;
+
+ if (access(filename, R_OK) == 0) {
+ prev = load_previous_time(filename);
+ }
+ else {
+ prev = 0;
+ }
+
+ return prev;
+}
+
+static int
+get_tty_number(void)
+{
+ char *tty = get_tty_name();
+ char *num = strrchr(tty, '/') + 1;
+
+ return atoi(num);
+}
+
+static char
+get_tty_type(void)
+{
+ char *tty = get_tty_name();
+
+ strtok(tty, "/"); /* dev */
+
+ return strtok(NULL, "/")[0];
+}
+
+static int
+save_current_time(char *filename)
+{
+ FILE *timefile = fopen(filename, "w");
+ time_t now;
+
+ if (!timefile)
+ return 0;
+
+ time(&now);
+ fprintf(timefile, "%ld", now);
+ fclose(timefile);
+
+ return 1;
+}
+
+static void
+print_time_since(time_t lasttime)
+{
+ double h, m, s;
+ time_t now;
+
+ time(&now);
+ s = difftime(now, lasttime);
+
+ if (s >= 60.0) {
+ m = floor(s / 60.0);
+ s -= m * 60;
+
+ if (m >= 60.0) {
+ h = floor(m / 60.0);
+ m -= h * 60;
+
+ printf("%.0lfh", h);
+ }
+
+ printf("%.0lfm", m);
+ }
+
+ printf("%.0lfs", s);
+}
+
+int
+main()
+{
+ char *filename = get_filename();
+ time_t lasttime = get_previous_time(filename);
+
+ printf("%c%d:", get_tty_type(), get_tty_number());
+
+ if (lasttime > 0) {
+ print_time_since(lasttime);
+ }
+ else {
+ puts("new");
+ }
+
+ save_current_time(filename);
+ free(filename);
+
+ return EXIT_SUCCESS;
+}