1 /* $NetBSD: mkdirp.c,v 1.1 2009/03/26 22:11:44 ad Exp $ */
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License, Version 1.0 only
8 * (the "License"). You may not use this file except in compliance
11 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12 * or http://www.opensolaris.org/os/licensing.
13 * See the License for the specific language governing permissions
14 * and limitations under the License.
16 * When distributing Covered Code, include this CDDL HEADER in each
17 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18 * If applicable, add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your own identifying
20 * information: Portions Copyright [yyyy] [name of copyright owner]
25 /* Copyright (c) 1988 AT&T */
26 /* All Rights Reserved */
29 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
33 #pragma ident "@(#)mkdirp.c 1.15 06/01/04 SMI"
36 * Creates directory and it's parents if the parents do not
39 * Returns -1 if fails for reasons other than non-existing
41 * Does NOT simplify pathnames with . or .. in them.
44 #include <sys/types.h>
52 static char *simplify(const char *str
);
55 mkdirp(const char *d
, mode_t mode
)
57 char *endptr
, *ptr
, *slash
, *str
;
61 /* If space couldn't be allocated for the simplified names, return. */
66 /* Try to make the directory */
68 if (mkdir(str
, mode
) == 0) {
72 if (errno
!= ENOENT
) {
76 endptr
= strrchr(str
, '\0');
77 slash
= strrchr(str
, '/');
79 /* Search upward for the non-existing parent */
81 while (slash
!= NULL
) {
86 /* If reached an existing parent, break */
88 if (access(str
, F_OK
) == 0)
91 /* If non-existing parent */
94 slash
= strrchr(str
, '/');
96 /* If under / or current directory, make it. */
98 if (slash
== NULL
|| slash
== str
) {
99 if (mkdir(str
, mode
) != 0 && errno
!= EEXIST
) {
108 /* Create directories starting from upmost non-existing parent */
110 while ((ptr
= strchr(str
, '\0')) != endptr
) {
112 if (mkdir(str
, mode
) != 0 && errno
!= EEXIST
) {
114 * If the mkdir fails because str already
115 * exists (EEXIST), then str has the form
116 * "existing-dir/..", and this is really
117 * ok. (Remember, this loop is creating the
118 * portion of the path that didn't exist)
129 * simplify - given a pathname, simplify that path by removing
130 * duplicate contiguous slashes.
132 * A simplified copy of the argument is returned to the
133 * caller, or NULL is returned on error.
135 * The caller should handle error reporting based upon the
136 * returned vlaue, and should free the returned value,
141 simplify(const char *str
)
144 size_t mbPathlen
; /* length of multi-byte path */
145 size_t wcPathlen
; /* length of wide-character path */
146 wchar_t *wptr
; /* scratch pointer */
147 wchar_t *wcPath
; /* wide-character version of the path */
148 char *mbPath
; /* The copy fo the path to be returned */
151 * bail out if there is nothing there.
158 * Get a copy of the argument.
161 if ((mbPath
= strdup(str
)) == NULL
) {
166 * convert the multi-byte version of the path to a
167 * wide-character rendering, for doing our figuring.
170 mbPathlen
= strlen(mbPath
);
172 if ((wcPath
= calloc(sizeof (wchar_t), mbPathlen
+1)) == NULL
) {
177 if ((wcPathlen
= mbstowcs(wcPath
, mbPath
, mbPathlen
)) == (size_t)-1) {
184 * remove duplicate slashes first ("//../" -> "/")
187 for (wptr
= wcPath
, i
= 0; i
< wcPathlen
; i
++) {
190 if (wcPath
[i
] == '/') {
193 while (wcPath
[i
] == '/') {
204 * now convert back to the multi-byte format.
207 if (wcstombs(mbPath
, wcPath
, mbPathlen
) == (size_t)-1) {