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