commit 5430cf2a0f9d07b6f5084c60bb06821e9545d337
parent f4e0c794cf7f11b9e43606f557e10c0c85ef291b
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sat, 20 Jan 2024 09:11:55 -0800
Root socket path in con struct
This way the caller can chdir(2) without issue.
Diffstat:
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/lib/ap/con.c b/lib/ap/con.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2021 Jacob R. Edwards
+ * Copyright 2021, 2024 Jacob R. Edwards
*
* ap -- audio player
*
@@ -19,6 +19,8 @@
#include <sys/socket.h>
+#include <limits.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -26,6 +28,27 @@
#include <ap/ap.h>
char *
+root(char *path)
+{
+ char root[PATH_MAX];
+ char *newpath;
+ int len;
+
+ if (!getwd(root))
+ return NULL;
+
+ len = strlen(root) + 1 + strlen(path) + 1;
+ if (!(newpath = malloc(len)))
+ return NULL;
+
+ if (snprintf(newpath, len, "%s/%s", root, path) >= len) {
+ free(newpath);
+ return NULL;
+ }
+ return newpath;
+}
+
+char *
apfile(char *path)
{
if (path == NULL) {
@@ -33,6 +56,8 @@ apfile(char *path)
if (path == NULL)
path = "/tmp/aps";
}
+ if (*path != '/')
+ return root(path);
return strdup(path);
}