1*5ad04d35Sguenther /* $OpenBSD: mkpath.c,v 1.4 2014/05/20 01:25:23 guenther Exp $ */
2a6dfabdaSespie /*
3a6dfabdaSespie * Copyright (c) 1983, 1992, 1993
4a6dfabdaSespie * The Regents of the University of California. All rights reserved.
5a6dfabdaSespie *
6a6dfabdaSespie * Redistribution and use in source and binary forms, with or without
7a6dfabdaSespie * modification, are permitted provided that the following conditions
8a6dfabdaSespie * are met:
9a6dfabdaSespie * 1. Redistributions of source code must retain the above copyright
10a6dfabdaSespie * notice, this list of conditions and the following disclaimer.
11a6dfabdaSespie * 2. Redistributions in binary form must reproduce the above copyright
12a6dfabdaSespie * notice, this list of conditions and the following disclaimer in the
13a6dfabdaSespie * documentation and/or other materials provided with the distribution.
14a6dfabdaSespie * 3. Neither the name of the University nor the names of its contributors
15a6dfabdaSespie * may be used to endorse or promote products derived from this software
16a6dfabdaSespie * without specific prior written permission.
17a6dfabdaSespie *
18a6dfabdaSespie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19a6dfabdaSespie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a6dfabdaSespie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a6dfabdaSespie * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22a6dfabdaSespie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a6dfabdaSespie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a6dfabdaSespie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a6dfabdaSespie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a6dfabdaSespie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a6dfabdaSespie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a6dfabdaSespie * SUCH DAMAGE.
29a6dfabdaSespie */
30a6dfabdaSespie
31a6dfabdaSespie #include <sys/types.h>
32a6dfabdaSespie #include <sys/stat.h>
33a6dfabdaSespie #include <err.h>
34a6dfabdaSespie #include <errno.h>
35a6dfabdaSespie #include <string.h>
36a6dfabdaSespie
3717769931Sderaadt #include "common.h"
3817769931Sderaadt #include "util.h"
3917769931Sderaadt
40a6dfabdaSespie /* Code taken directly from mkdir(1).
41a6dfabdaSespie
42a6dfabdaSespie * mkpath -- create directories.
43a6dfabdaSespie * path - path
44a6dfabdaSespie */
45a6dfabdaSespie int
mkpath(char * path)466fd3743fSotto mkpath(char *path)
47a6dfabdaSespie {
48a6dfabdaSespie struct stat sb;
49a6dfabdaSespie char *slash;
50a6dfabdaSespie int done = 0;
51a6dfabdaSespie
52a6dfabdaSespie slash = path;
53a6dfabdaSespie
54a6dfabdaSespie while (!done) {
55a6dfabdaSespie slash += strspn(slash, "/");
56a6dfabdaSespie slash += strcspn(slash, "/");
57a6dfabdaSespie
58a6dfabdaSespie done = (*slash == '\0');
59a6dfabdaSespie *slash = '\0';
60a6dfabdaSespie
61a6dfabdaSespie if (stat(path, &sb)) {
626fd3743fSotto if (errno != ENOENT || (mkdir(path, 0777) &&
63a6dfabdaSespie errno != EEXIST)) {
64a6dfabdaSespie warn("%s", path);
65a6dfabdaSespie return (-1);
66a6dfabdaSespie }
67a6dfabdaSespie } else if (!S_ISDIR(sb.st_mode)) {
68*5ad04d35Sguenther warnc(ENOTDIR, "%s", path);
69a6dfabdaSespie return (-1);
70a6dfabdaSespie }
71a6dfabdaSespie
72a6dfabdaSespie *slash = '/';
73a6dfabdaSespie }
74a6dfabdaSespie
75a6dfabdaSespie return (0);
76a6dfabdaSespie }
77a6dfabdaSespie
78