1246074Sgabor/*-
2246074Sgabor * Copyright (c) 1983, 1992, 1993
3246074Sgabor *	The Regents of the University of California.  All rights reserved.
4246074Sgabor *
5246074Sgabor * Redistribution and use in source and binary forms, with or without
6246074Sgabor * modification, are permitted provided that the following conditions
7246074Sgabor * are met:
8246074Sgabor * 1. Redistributions of source code must retain the above copyright
9246074Sgabor *    notice, this list of conditions and the following disclaimer.
10246074Sgabor * 2. Redistributions in binary form must reproduce the above copyright
11246074Sgabor *    notice, this list of conditions and the following disclaimer in the
12246074Sgabor *    documentation and/or other materials provided with the distribution.
13246074Sgabor * 3. Neither the name of the University nor the names of its contributors
14246074Sgabor *    may be used to endorse or promote products derived from this software
15246074Sgabor *    without specific prior written permission.
16246074Sgabor *
17246074Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18246074Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19246074Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20246074Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21246074Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22246074Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23246074Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24246074Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25246074Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26246074Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27246074Sgabor * SUCH DAMAGE.
28246074Sgabor *
29246091Sdelphij * $OpenBSD: mkpath.c,v 1.2 2005/06/20 07:14:06 otto Exp $
30246091Sdelphij * $FreeBSD$
31246074Sgabor */
32246074Sgabor
33246074Sgabor#include <sys/types.h>
34246074Sgabor#include <sys/stat.h>
35246074Sgabor#include <err.h>
36246074Sgabor#include <errno.h>
37246074Sgabor#include <string.h>
38246074Sgabor
39246074Sgaborint	mkpath(char *);
40246074Sgabor
41246074Sgabor/* Code taken directly from mkdir(1).
42246074Sgabor
43246074Sgabor * mkpath -- create directories.
44246074Sgabor *	path     - path
45246074Sgabor */
46246074Sgaborint
47246074Sgabormkpath(char *path)
48246074Sgabor{
49246074Sgabor	struct stat sb;
50246074Sgabor	char *slash;
51246074Sgabor	int done = 0;
52246074Sgabor
53246074Sgabor	slash = path;
54246074Sgabor
55246074Sgabor	while (!done) {
56246074Sgabor		slash += strspn(slash, "/");
57246074Sgabor		slash += strcspn(slash, "/");
58246074Sgabor
59246074Sgabor		done = (*slash == '\0');
60246074Sgabor		*slash = '\0';
61246074Sgabor
62246074Sgabor		if (stat(path, &sb)) {
63246074Sgabor			if (errno != ENOENT || (mkdir(path, 0777) &&
64246074Sgabor			    errno != EEXIST)) {
65246074Sgabor				warn("%s", path);
66246074Sgabor				return (-1);
67246074Sgabor			}
68246074Sgabor		} else if (!S_ISDIR(sb.st_mode)) {
69246074Sgabor			warnx("%s: %s", path, strerror(ENOTDIR));
70246074Sgabor			return (-1);
71246074Sgabor		}
72246074Sgabor
73246074Sgabor		*slash = '/';
74246074Sgabor	}
75246074Sgabor
76246074Sgabor	return (0);
77246074Sgabor}
78246074Sgabor
79