aboutsummaryrefslogtreecommitdiff
path: root/util.c
blob: 6e6079ef13b7cdbe9314feb6e2fbed2a43e5d922 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <linux/limits.h>

#include "util.h"

int r_mkdir(const char *path, mode_t mode)
{
	int r;
	size_t len;
	char *p;
	char tmp[NAME_MAX + 1];

	strncpy(tmp, path, sizeof(tmp) - 1);
	len = strlen(tmp);
	if(tmp[len - 1] == '/')
		tmp[len - 1] = '\0';

	for (p = tmp + 1; *p; ++p) {
		if (*p == '/') {
			*p = '\0';

			r = mkdir(tmp, mode);
			if (r && !(errno & EEXIST))
				return r;

			*p = '/';
		}
	}

	return mkdir(path, mode);
}