commit 44d4dee63c97ba9fd3d92f3cb67fccf0b9e5f518
parent f0df6e3a39a3b9386b57b8338802f337aa3e38a0
Author: cowmonk <cowmonk@based.pt>
Date: Tue, 23 Jun 2026 22:25:03 +0000
slog creating directories as needed
It's quite inconvienent to have to create the log directories by hand.
`mkdir_p()` is the new function created to be lazy and let slog do the
expected behavior.
Diffstat:
| M | slog.c | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/slog.c b/slog.c
@@ -21,6 +21,30 @@ usage(void)
eprintf("usage: %s [-d dir] [-s bytes] [-n count]\n", argv0);
}
+static void
+mkdir_p(const char *path)
+{
+ char tmp[PATH_MAX];
+ char *p;
+ size_t len;
+
+ strlcpy(tmp, path, sizeof(tmp));
+ len = strlen(tmp);
+ if (len > 0 && tmp[len - 1] == '/')
+ tmp[len - 1] = '\0';
+
+ for (p = tmp + 1; *p; p++) {
+ if (*p == '/') {
+ *p = '\0';
+ if (mkdir(tmp, 0755) < 0 && errno != EEXIST)
+ eprintf("mkdir %s:", tmp);
+ *p = '/';
+ }
+ }
+ if (mkdir(tmp, 0755) < 0 && errno != EEXIST)
+ eprintf("mkdir %s:", tmp);
+}
+
int
main(int argc, char *argv[])
{
@@ -42,6 +66,8 @@ main(int argc, char *argv[])
default: usage();
} ARGEND
+ mkdir_p();
+
snprintf(cur, sizeof(cur), "%s/current", dir);
fd = open(cur, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd < 0)