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.
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 "gen_synonyms.h"
45 #include <sys/types.h>
53 static char *simplify(const char *str);
56 mkdirp(const char *d, mode_t mode)
58 char *endptr, *ptr, *slash, *str;
62 /* If space couldn't be allocated for the simplified names, return. */
67 /* Try to make the directory */
69 if (mkdir(str, mode) == 0) {
73 if (errno != ENOENT) {
77 endptr = strrchr(str, '\0');
78 slash = strrchr(str, '/');
80 /* Search upward for the non-existing parent */
82 while (slash != NULL) {
87 /* If reached an existing parent, break */
89 if (access(str, F_OK) == 0)
92 /* If non-existing parent */
95 slash = strrchr(str, '/');
97 /* If under / or current directory, make it. */
99 if (slash == NULL || slash == str) {
100 if (mkdir(str, mode) != 0 && errno != EEXIST) {
109 /* Create directories starting from upmost non-existing parent */
111 while ((ptr = strchr(str, '\0')) != endptr) {
113 if (mkdir(str, mode) != 0 && errno != EEXIST) {
115 * If the mkdir fails because str already
116 * exists (EEXIST), then str has the form
117 * "existing-dir/..", and this is really
118 * ok. (Remember, this loop is creating the
119 * portion of the path that didn't exist)
130 * simplify - given a pathname, simplify that path by removing
131 * duplicate contiguous slashes.
133 * A simplified copy of the argument is returned to the
134 * caller, or NULL is returned on error.
136 * The caller should handle error reporting based upon the
137 * returned vlaue, and should free the returned value,
142 simplify(const char *str)
145 size_t mbPathlen; /* length of multi-byte path */
146 size_t wcPathlen; /* length of wide-character path */
147 wchar_t *wptr; /* scratch pointer */
148 wchar_t *wcPath; /* wide-character version of the path */
149 char *mbPath; /* The copy fo the path to be returned */
152 * bail out if there is nothing there.
159 * Get a copy of the argument.
162 if ((mbPath = strdup(str)) == NULL) {
167 * convert the multi-byte version of the path to a
168 * wide-character rendering, for doing our figuring.
171 mbPathlen = strlen(mbPath);
173 if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
178 if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
185 * remove duplicate slashes first ("//../" -> "/")
188 for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
191 if (wcPath[i] == '/') {
194 while (wcPath[i] == '/') {
205 * now convert back to the multi-byte format.
208 if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {