summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorawy <awy@awy.one>2025-08-25 19:38:44 +0300
committerawy <awy@awy.one>2025-08-25 19:38:44 +0300
commit4300e4022314514c982ef401b36f3061aa926ca1 (patch)
treed50da7fcb7775b0e2da9751091714c681a0610e8
parentda3ed11be83694d32285f1773de5232cd51d8b4b (diff)
updates
-rw-r--r--Makefile6
-rw-r--r--lib/cjson/cJSON.c (renamed from src/cjson/cJSON.c)0
-rw-r--r--lib/cjson/cJSON.h (renamed from src/cjson/cJSON.h)0
-rw-r--r--lib/util.c25
-rw-r--r--lib/util.h1
-rw-r--r--src/stclock.c13
-rw-r--r--src/stmail.c44
-rw-r--r--src/stmemory.c31
-rw-r--r--src/sttorrent.c2
-rw-r--r--src/stweath.c2
10 files changed, 77 insertions, 47 deletions
diff --git a/Makefile b/Makefile
index f965e66..a212b16 100644
--- a/Makefile
+++ b/Makefile
@@ -18,13 +18,13 @@ $(BINDIR)/stmusic: $(SRCDIR)/stmusic.c | $(BINDIR)
$(CC) $(CFLAGS) -o $@ $< -lmpdclient
$(BINDIR)/sttorrent: $(SRCDIR)/sttorrent.c | $(BINDIR)
- $(CC) $(CFLAGS) -o $@ $(SRCDIR)/cjson/cJSON.c $< -lcurl
+ $(CC) $(CFLAGS) -o $@ lib/cjson/cJSON.c $< -lcurl
$(BINDIR)/stweath: $(SRCDIR)/stweath.c | $(BINDIR)
- $(CC) $(CFLAGS) -o $@ $(SRCDIR)/cjson/cJSON.c $< -lcurl
+ $(CC) $(CFLAGS) -o $@ lib/cjson/cJSON.c $< -lcurl
$(BINDIR)/%: $(SRCDIR)/%.c | $(BINDIR)
- $(CC) $(CFLAGS) -o $@ $<
+ $(CC) $(CFLAGS) -o $@ lib/util.c $<
clean:
rm -f $(PROGS)
diff --git a/src/cjson/cJSON.c b/lib/cjson/cJSON.c
index ca824f0..ca824f0 100644
--- a/src/cjson/cJSON.c
+++ b/lib/cjson/cJSON.c
diff --git a/src/cjson/cJSON.h b/lib/cjson/cJSON.h
index 3edf8bc..3edf8bc 100644
--- a/src/cjson/cJSON.h
+++ b/lib/cjson/cJSON.h
diff --git a/lib/util.c b/lib/util.c
new file mode 100644
index 0000000..1fe30da
--- /dev/null
+++ b/lib/util.c
@@ -0,0 +1,25 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+void
+die(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
+ fputc(' ', stderr);
+ perror(NULL);
+ } else {
+ fputc('\n', stderr);
+ }
+
+ exit(1);
+}
diff --git a/lib/util.h b/lib/util.h
new file mode 100644
index 0000000..9ecc499
--- /dev/null
+++ b/lib/util.h
@@ -0,0 +1 @@
+void die(const char *, ...);
diff --git a/src/stclock.c b/src/stclock.c
index 7591825..22dae6a 100644
--- a/src/stclock.c
+++ b/src/stclock.c
@@ -17,11 +17,18 @@
#include <time.h>
#include <stdio.h>
+#include "../lib/util.h"
+
int
main(void)
{
- time_t t = time(NULL);
- struct tm *tm = localtime(&t);
- printf("%02d:%02d", tm->tm_hour, tm->tm_min);
+ time_t t;
+ char buf[64];
+
+ t = time(NULL);
+ if (!strftime(buf, sizeof(buf), "%F %T", localtime(&t)))
+ die("strftime: Result string exceeds buffer size");
+
+ printf("%s", buf);
return 0;
}
diff --git a/src/stmail.c b/src/stmail.c
index 4c66bc9..f151d4a 100644
--- a/src/stmail.c
+++ b/src/stmail.c
@@ -21,6 +21,8 @@
#include <stdio.h>
#include <unistd.h>
+#include "../lib/util.h"
+
int
newmsg(char path[1024])
{
@@ -28,11 +30,10 @@ newmsg(char path[1024])
int count;
struct dirent *entry;
- count = 0;
-
- dir = opendir(path);
- if (!dir) { return 1; }
+ if (!(dir = opendir(path)))
+ die("failed to open dir");
+ count = 0;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
@@ -40,7 +41,6 @@ newmsg(char path[1024])
}
}
}
-
return count;
}
@@ -49,28 +49,28 @@ main(void)
{
DIR *dir;
int count;
- char *path;
- char fullpath[1024];
+ char *env;
+ char buf[1024];
+ char path[1024];
struct dirent *entry;
- path = getenv("XDG_DATA_HOME");
- if (!path) { puts("XDG_DATA_HOME is not set"); return 1; }
+ env = getenv("XDG_DATA_HOME");
+ if (!env)
+ die("XDG_DATA_HOME is not set");
- strcat(path, "/mail");
+ snprintf(buf, sizeof(buf), "%s/mail/", env);
- dir = opendir(path);
- if (!dir) { return 1; }
+ if (!(dir = opendir(buf)))
+ die("failed to open dir");
count = 0;
-
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") && strcmp(entry->d_name, ".notmuch")) {
- strcpy(fullpath, path);
- strcat(fullpath, "/");
- strcat(fullpath, entry->d_name);
- strcat(fullpath, "/INBOX/new/");
- count += newmsg(fullpath);
+ strcpy(path, buf);
+ strcat(path, entry->d_name);
+ strcat(path, "/INBOX/new/");
+ count += newmsg(path);
}
}
}
@@ -79,11 +79,13 @@ main(void)
if (access("/tmp/mailupdate", F_OK) != -1) {
printf("");
- if (count != 0) { printf(" "); }
+ if (count != 0)
+ printf(" ");
}
- if (count == 0) { return 0; }
- printf(" %d", count);
+ if (count == 0)
+ return 0;
+ printf(" %d", count);
return 0;
}
diff --git a/src/stmemory.c b/src/stmemory.c
index 3553563..4f1435a 100644
--- a/src/stmemory.c
+++ b/src/stmemory.c
@@ -16,32 +16,27 @@
#include <stdio.h>
-// TODO: Memory hogs on left click using signals
+#include "../lib/util.h"
int
main(void)
{
- FILE *meminfo;
- char buff[100];
- long memtotal, memavail = 0;
+ FILE *fp;
+ char line[256];
+ long total, free;
- meminfo = fopen("/proc/meminfo", "r");
+ if (!(fp = fopen("/proc/meminfo", "r")))
+ die("failed to open: /proc/meminfo");
- if (meminfo == NULL) {
- puts("Error opening file");
- return 1;
- }
+ fgets(line, sizeof(line), fp);
+ sscanf(line + 9, "%ld", &total);
- fgets(buff, sizeof(buff), meminfo);
- sscanf(buff + 9, "%ld", &memtotal);
+ fgets(line, sizeof(line), fp);
+ fgets(line, sizeof(line), fp);
+ sscanf(line + 13, "%ld", &free);
- fgets(buff, sizeof(buff), meminfo);
- fgets(buff, sizeof(buff), meminfo);
- sscanf(buff + 13, "%ld", &memavail);
-
- printf("%ldMB", (memtotal - memavail) / 1024 );
-
- fclose(meminfo);
+ printf("%ldMB", (total - free) / 1024 );
+ fclose(fp);
return 0;
}
diff --git a/src/sttorrent.c b/src/sttorrent.c
index b112d5d..3bd513f 100644
--- a/src/sttorrent.c
+++ b/src/sttorrent.c
@@ -18,7 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "cjson/cJSON.h"
+#include "../lib/cjson/cJSON.h"
struct MemoryStruct {
char *memory;
diff --git a/src/stweath.c b/src/stweath.c
index c28f48d..e1b3e51 100644
--- a/src/stweath.c
+++ b/src/stweath.c
@@ -21,7 +21,7 @@
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
-#include "cjson/cJSON.h"
+#include "../lib/cjson/cJSON.h"
typedef struct {
int code;