4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
31 * Creates directory and it's parents if the parents do not
34 * Returns -1 if fails for reasons other than non-existing
36 * Does NOT simplify pathnames with . or .. in them.
39 #include <sys/types.h>
47 static char *simplify(const char *str
);
50 mkdirp(const char *d
, mode_t mode
)
52 char *endptr
, *ptr
, *slash
, *str
;
56 /* If space couldn't be allocated for the simplified names, return. */
61 /* Try to make the directory */
63 if (mkdir(str
, mode
) == 0) {
67 if (errno
!= ENOENT
) {
71 endptr
= strrchr(str
, '\0');
72 slash
= strrchr(str
, '/');
74 /* Search upward for the non-existing parent */
76 while (slash
!= NULL
) {
81 /* If reached an existing parent, break */
83 if (access(str
, F_OK
) == 0)
86 /* If non-existing parent */
89 slash
= strrchr(str
, '/');
91 /* If under / or current directory, make it. */
93 if (slash
== NULL
|| slash
== str
) {
94 if (mkdir(str
, mode
) != 0 && errno
!= EEXIST
) {
103 /* Create directories starting from upmost non-existing parent */
105 while ((ptr
= strchr(str
, '\0')) != endptr
) {
107 if (mkdir(str
, mode
) != 0 && errno
!= EEXIST
) {
109 * If the mkdir fails because str already
110 * exists (EEXIST), then str has the form
111 * "existing-dir/..", and this is really
112 * ok. (Remember, this loop is creating the
113 * portion of the path that didn't exist)
124 * simplify - given a pathname, simplify that path by removing
125 * duplicate contiguous slashes.
127 * A simplified copy of the argument is returned to the
128 * caller, or NULL is returned on error.
130 * The caller should handle error reporting based upon the
131 * returned value, and should free the returned value,
136 simplify(const char *str
)
139 size_t mbPathlen
; /* length of multi-byte path */
140 size_t wcPathlen
; /* length of wide-character path */
141 wchar_t *wptr
; /* scratch pointer */
142 wchar_t *wcPath
; /* wide-character version of the path */
143 char *mbPath
; /* The copy fo the path to be returned */
146 * bail out if there is nothing there.
155 * Get a copy of the argument.
158 if ((mbPath
= strdup(str
)) == NULL
) {
163 * convert the multi-byte version of the path to a
164 * wide-character rendering, for doing our figuring.
167 mbPathlen
= strlen(mbPath
);
169 if ((wcPath
= calloc(mbPathlen
+1, sizeof (wchar_t))) == NULL
) {
174 if ((wcPathlen
= mbstowcs(wcPath
, mbPath
, mbPathlen
)) == (size_t)-1) {
181 * remove duplicate slashes first ("//../" -> "/")
184 for (wptr
= wcPath
, i
= 0; i
< wcPathlen
; i
++) {
187 if (wcPath
[i
] == '/') {
190 while (wcPath
[i
] == '/') {
201 * now convert back to the multi-byte format.
204 if (wcstombs(mbPath
, wcPath
, mbPathlen
) == (size_t)-1) {