4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 /* Copyright (c) 1988 AT&T */
24 /* All Rights Reserved */
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "@(#)mkdirp.c 1.15 06/01/04 SMI"
34 * Creates directory and it's parents if the parents do not
37 * Returns -1 if fails for reasons other than non-existing
39 * Does NOT simplify pathnames with . or .. in them.
42 #include <sys/types.h>
50 static char *simplify(const char *str);
53 mkdirp(const char *d, mode_t mode)
55 char *endptr, *ptr, *slash, *str;
59 /* If space couldn't be allocated for the simplified names, return. */
64 /* Try to make the directory */
66 if (mkdir(str, mode) == 0) {
70 if (errno != ENOENT) {
74 endptr = strrchr(str, '\0');
75 slash = strrchr(str, '/');
77 /* Search upward for the non-existing parent */
79 while (slash != NULL) {
84 /* If reached an existing parent, break */
86 if (access(str, F_OK) == 0)
89 /* If non-existing parent */
92 slash = strrchr(str, '/');
94 /* If under / or current directory, make it. */
96 if (slash == NULL || slash == str) {
97 if (mkdir(str, mode) != 0 && errno != EEXIST) {
106 /* Create directories starting from upmost non-existing parent */
108 while ((ptr = strchr(str, '\0')) != endptr) {
110 if (mkdir(str, mode) != 0 && errno != EEXIST) {
112 * If the mkdir fails because str already
113 * exists (EEXIST), then str has the form
114 * "existing-dir/..", and this is really
115 * ok. (Remember, this loop is creating the
116 * portion of the path that didn't exist)
127 * simplify - given a pathname, simplify that path by removing
128 * duplicate contiguous slashes.
130 * A simplified copy of the argument is returned to the
131 * caller, or NULL is returned on error.
133 * The caller should handle error reporting based upon the
134 * returned vlaue, and should free the returned value,
139 simplify(const char *str)
142 size_t mbPathlen; /* length of multi-byte path */
143 size_t wcPathlen; /* length of wide-character path */
144 wchar_t *wptr; /* scratch pointer */
145 wchar_t *wcPath; /* wide-character version of the path */
146 char *mbPath; /* The copy fo the path to be returned */
149 * bail out if there is nothing there.
156 * Get a copy of the argument.
159 if ((mbPath = strdup(str)) == NULL) {
164 * convert the multi-byte version of the path to a
165 * wide-character rendering, for doing our figuring.
168 mbPathlen = strlen(mbPath);
170 if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
175 if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
182 * remove duplicate slashes first ("//../" -> "/")
185 for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
188 if (wcPath[i] == '/') {
191 while (wcPath[i] == '/') {
202 * now convert back to the multi-byte format.
205 if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {