diff options
| author | Drew DeVault <sir@cmpwn.com> | 2020-09-23 10:30:51 -0400 |
|---|---|---|
| committer | Drew DeVault <sir@cmpwn.com> | 2020-09-23 10:40:14 -0400 |
| commit | ccae8ffd2807b8b984b657b6321802fa00b52427 (patch) | |
| tree | 4869bbfc4de1d94e5b28b2835a2e18519e12c57c /src/util.c | |
| parent | 61b84a77c09959bfe4d2cfa50d1fb4578cb5fc08 (diff) | |
| download | gmnisrv-ccae8ffd2807b8b984b657b6321802fa00b52427.tar.gz gmnisrv-ccae8ffd2807b8b984b657b6321802fa00b52427.tar.xz gmnisrv-ccae8ffd2807b8b984b657b6321802fa00b52427.zip | |
Vendor in some support code
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..93c6e91 --- /dev/null +++ b/src/util.c @@ -0,0 +1,45 @@ +#include <assert.h> +#include <errno.h> +#include <libgen.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include "util.h" + +static void +posix_dirname(char *path, char *dname) +{ + char p[PATH_MAX+1]; + char *t; + + assert(strlen(path) <= PATH_MAX); + + strcpy(p, path); + t = dirname(path); + memmove(dname, t, strlen(t) + 1); + + /* restore the path if dirname worked in-place */ + if (t == path && path != dname) { + strcpy(path, p); + } +} + +/** Make directory and all of its parents */ +int +mkdirs(char *path, mode_t mode) +{ + char dname[PATH_MAX + 1]; + posix_dirname(path, dname); + if (strcmp(dname, "/") == 0) { + return 0; + } + if (mkdirs(dname, mode) != 0) { + return -1; + } + if (mkdir(path, mode) != 0 && errno != EEXIST) { + return -1; + } + errno = 0; + return 0; +} |
