libremail: Add shared try_mkdir() function
Signed-off-by: Chloe M. <chloe@mirocom.org>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Copyright (c) 2026, Chloe Moffett
|
||||
# Provided under the BSD-3 clause
|
||||
#
|
||||
|
||||
CFILES = $(shell find . -name "*.c")
|
||||
OFILES = $(CFILES:.c=.o)
|
||||
DFILES = $(CFILES:.c=.d)
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = \
|
||||
-Wall \
|
||||
-pedantic \
|
||||
-MMD \
|
||||
-Iinc
|
||||
|
||||
.PHONY: all
|
||||
all: $(OFILES)
|
||||
|
||||
-include $(DFILES)
|
||||
%.o: %.c
|
||||
$(CC) -c $< $(CFLAGS) -o $@
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(OFILES) $(DFILES)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe Moffett
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include "libremail/file.h"
|
||||
|
||||
int
|
||||
try_mkdir(const char *path, mode_t mode)
|
||||
{
|
||||
if (path == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (access(path, F_OK) != 0) {
|
||||
return mkdir(path, mode);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe Moffett
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#ifndef LIBREMAIL_FILE_H
|
||||
#define LIBREMAIL_FILE_H 1
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
/*
|
||||
* Attempt to create a directory if it doesn't exist
|
||||
*
|
||||
* @path: Path to directory to create
|
||||
* @mode: Desired mode to assign
|
||||
*
|
||||
* Returns zeo on success
|
||||
*/
|
||||
int try_mkdir(const char *path, mode_t mode);
|
||||
|
||||
#endif /* !LIBREMAIL_FILE_H */
|
||||
Reference in New Issue
Block a user