summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorawy <awy@awy.one>2025-08-25 20:03:28 +0300
committerawy <awy@awy.one>2025-08-25 20:03:28 +0300
commit013ac8d3a8e630a389242a527bcc4d140fba104a (patch)
treea5690823090a28ca673f1d4758ebc746916065b5
parent4300e4022314514c982ef401b36f3061aa926ca1 (diff)
updates
-rw-r--r--Makefile4
-rw-r--r--src/stmail.c3
-rw-r--r--src/stweath.c76
3 files changed, 44 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index a212b16..c51f3b4 100644
--- a/Makefile
+++ b/Makefile
@@ -18,10 +18,10 @@ $(BINDIR)/stmusic: $(SRCDIR)/stmusic.c | $(BINDIR)
$(CC) $(CFLAGS) -o $@ $< -lmpdclient
$(BINDIR)/sttorrent: $(SRCDIR)/sttorrent.c | $(BINDIR)
- $(CC) $(CFLAGS) -o $@ lib/cjson/cJSON.c $< -lcurl
+ $(CC) $(CFLAGS) -o $@ lib/util.c lib/cjson/cJSON.c $< -lcurl
$(BINDIR)/stweath: $(SRCDIR)/stweath.c | $(BINDIR)
- $(CC) $(CFLAGS) -o $@ lib/cjson/cJSON.c $< -lcurl
+ $(CC) $(CFLAGS) -o $@ lib/util.c lib/cjson/cJSON.c $< -lcurl
$(BINDIR)/%: $(SRCDIR)/%.c | $(BINDIR)
$(CC) $(CFLAGS) -o $@ lib/util.c $<
diff --git a/src/stmail.c b/src/stmail.c
index f151d4a..405f8bb 100644
--- a/src/stmail.c
+++ b/src/stmail.c
@@ -54,8 +54,7 @@ main(void)
char path[1024];
struct dirent *entry;
- env = getenv("XDG_DATA_HOME");
- if (!env)
+ if (!(env = getenv("XDG_DATA_HOME")))
die("XDG_DATA_HOME is not set");
snprintf(buf, sizeof(buf), "%s/mail/", env);
diff --git a/src/stweath.c b/src/stweath.c
index e1b3e51..e0cd154 100644
--- a/src/stweath.c
+++ b/src/stweath.c
@@ -21,7 +21,9 @@
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
+
#include "../lib/cjson/cJSON.h"
+#include "../lib/util.h"
typedef struct {
int code;
@@ -53,68 +55,69 @@ const char
}
int
-getforecast(char *path)
+getforecast(const char *path)
{
- FILE *report;
+ FILE *fp;
CURL* handle;
CURLcode res;
- report = fopen(path, "w");
- if (!report) { return 1; }
+ if (!(fp = fopen(path, "w")))
+ die("failed to open report:");
handle = curl_easy_init();
if (!handle) {
- fclose(report);
- return 1;
+ fclose(fp);
+ die("failed to create curl handle");
}
curl_easy_setopt(handle, CURLOPT_URL, "https://wttr.in/?format=j1");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
- curl_easy_setopt(handle, CURLOPT_WRITEDATA, report);
+ curl_easy_setopt(handle, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(handle);
if (res != CURLE_OK) {
- fclose(report);
- return 1;
+ fclose(fp);
+ die("curl failed");
}
curl_easy_cleanup(handle);
- fclose(report);
+ fclose(fp);
return 0;
}
int
-showforecast(char *path)
+showforecast(const char *path)
{
+ FILE *fp;
long size;
char *data;
const char *icon;
cJSON *root, *temp, *wcode, *rain;
- FILE *report;
- report = fopen(path, "rb");
- if (!report) { return 1; }
+ if (!(fp = fopen(path, "rb")))
+ die("fopen:");
- fseek(report, 0, SEEK_END);
- size = ftell(report);
- fseek(report, 0, SEEK_SET);
+ fseek(fp, 0, SEEK_END);
+ size = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
data = malloc(size + 1);
- if (!data) {
- perror("malloc");
- fclose(report);
- return 1;
+ if (!(data = malloc(size + 1))) {
+ fclose(fp);
+ die("failed to allocate memory");
}
- fread(data, 1, size, report);
+
+ fread(data, 1, size, fp);
data[size] = '\0';
- fclose(report);
+ fclose(fp);
- root = cJSON_Parse(data);
- free(data);
- if (!root) { return 1; }
+ if (!(root = cJSON_Parse(data))) {
+ free(data);
+ die("failed to parse json");
+ }
rain = cJSON_GetArrayItem(cJSON_GetObjectItem(root, "weather"), 0);
rain = cJSON_GetArrayItem(cJSON_GetObjectItem(rain, "hourly"), 0);
@@ -126,9 +129,9 @@ showforecast(char *path)
icon = geticon(atoi(wcode->valuestring));
- if (atoi(rain->valuestring) != 0) {
+ if (atoi(rain->valuestring) != 0)
printf(" %s%% ", rain->valuestring);
- }
+
printf("%s %s°C", icon, temp->valuestring);
cJSON_Delete(root);
@@ -136,14 +139,14 @@ showforecast(char *path)
}
int
-checkforecast(char *path)
+checkforecast(const char *path)
{
time_t now;
char *buff;
struct stat st;
int tries, force;
- /* Check if there is WEATHERFORCEUPDATE
+ /* check if there is WEATHERFORCEUPDATE
environment variable */
force = 0;
buff = getenv("WEATHERFORCEUPDATE");
@@ -154,11 +157,11 @@ checkforecast(char *path)
now = time(NULL);
if (now == ((time_t)-1)) { return 1; }
- /* Check if 30 mins passed
+ /* check if 30 mins passed
then get new forecast */
if (((now - st.st_mtime) >= 1800) || force) {
tries = 0;
- /* Try 100 fast retries */
+ /* try 100 fast retries */
while (tries < 100) {
if(getforecast(path) == 0) {
break;
@@ -175,10 +178,13 @@ checkforecast(char *path)
int
main(void)
{
- char *path;
+ char *env;
+ char path[1024];
+
+ if (!(env = getenv("XDG_CACHE_HOME")))
+ die("XDG_CACHE_HOME is not set");
- path = getenv("XDG_CACHE_HOME");
- if (!path) { puts("XDG_CACHE_HOME is not set"); return 1; }
+ strcpy(path, env);
strcat(path, "/weatherreport.json");
curl_global_init(CURL_GLOBAL_ALL);