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