Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / lib / libzfs / mkdirp.c
blob75c14c443747f9307e344cd2ed684948b50a6b51
1 /* $NetBSD: mkdirp.c,v 1.1 2009/03/26 22:11:44 ad Exp $ */
3 /*
4 * CDDL HEADER START
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
9 * with the License.
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]
22 * CDDL HEADER END
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
37 * exist yet.
39 * Returns -1 if fails for reasons other than non-existing
40 * parents.
41 * Does NOT simplify pathnames with . or .. in them.
44 #include <sys/types.h>
45 #include <libgen.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <sys/stat.h>
52 static char *simplify(const char *str);
54 int
55 mkdirp(const char *d, mode_t mode)
57 char *endptr, *ptr, *slash, *str;
59 str = simplify(d);
61 /* If space couldn't be allocated for the simplified names, return. */
63 if (str == NULL)
64 return (-1);
66 /* Try to make the directory */
68 if (mkdir(str, mode) == 0) {
69 free(str);
70 return (0);
72 if (errno != ENOENT) {
73 free(str);
74 return (-1);
76 endptr = strrchr(str, '\0');
77 slash = strrchr(str, '/');
79 /* Search upward for the non-existing parent */
81 while (slash != NULL) {
83 ptr = slash;
84 *ptr = '\0';
86 /* If reached an existing parent, break */
88 if (access(str, F_OK) == 0)
89 break;
91 /* If non-existing parent */
93 else {
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) {
100 free(str);
101 return (-1);
103 break;
108 /* Create directories starting from upmost non-existing parent */
110 while ((ptr = strchr(str, '\0')) != endptr) {
111 *ptr = '/';
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)
120 free(str);
121 return (-1);
124 free(str);
125 return (0);
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,
137 * when appropriate.
140 static char *
141 simplify(const char *str)
143 int i;
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.
154 if (!str)
155 return (NULL);
158 * Get a copy of the argument.
161 if ((mbPath = strdup(str)) == NULL) {
162 return (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) {
173 free(mbPath);
174 return (NULL);
177 if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
178 free(mbPath);
179 free(wcPath);
180 return (NULL);
184 * remove duplicate slashes first ("//../" -> "/")
187 for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
188 *wptr++ = wcPath[i];
190 if (wcPath[i] == '/') {
191 i++;
193 while (wcPath[i] == '/') {
194 i++;
197 i--;
201 *wptr = '\0';
204 * now convert back to the multi-byte format.
207 if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
208 free(mbPath);
209 free(wcPath);
210 return (NULL);
213 free(wcPath);
214 return (mbPath);