1*2dd076b8SGabor Kovesdan /* $FreeBSD$ */ 2*2dd076b8SGabor Kovesdan /*- 3*2dd076b8SGabor Kovesdan * Copyright (c) 1983, 1992, 1993 4*2dd076b8SGabor Kovesdan * The Regents of the University of California. All rights reserved. 5*2dd076b8SGabor Kovesdan * 6*2dd076b8SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 7*2dd076b8SGabor Kovesdan * modification, are permitted provided that the following conditions 8*2dd076b8SGabor Kovesdan * are met: 9*2dd076b8SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 10*2dd076b8SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 11*2dd076b8SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 12*2dd076b8SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 13*2dd076b8SGabor Kovesdan * documentation and/or other materials provided with the distribution. 14*2dd076b8SGabor Kovesdan * 3. Neither the name of the University nor the names of its contributors 15*2dd076b8SGabor Kovesdan * may be used to endorse or promote products derived from this software 16*2dd076b8SGabor Kovesdan * without specific prior written permission. 17*2dd076b8SGabor Kovesdan * 18*2dd076b8SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19*2dd076b8SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*2dd076b8SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*2dd076b8SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22*2dd076b8SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*2dd076b8SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*2dd076b8SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*2dd076b8SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*2dd076b8SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*2dd076b8SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*2dd076b8SGabor Kovesdan * SUCH DAMAGE. 29*2dd076b8SGabor Kovesdan * 30*2dd076b8SGabor Kovesdan * $OpenBSD: mkpath.c,v 1.2 2005/06/20 07:14:06 otto Exp $ 31*2dd076b8SGabor Kovesdan * $DragonFly: src/usr.bin/patch/mkpath.c,v 1.1 2007/09/29 23:11:10 swildner Exp $ 32*2dd076b8SGabor Kovesdan */ 33*2dd076b8SGabor Kovesdan 34*2dd076b8SGabor Kovesdan #include <sys/types.h> 35*2dd076b8SGabor Kovesdan #include <sys/stat.h> 36*2dd076b8SGabor Kovesdan #include <err.h> 37*2dd076b8SGabor Kovesdan #include <errno.h> 38*2dd076b8SGabor Kovesdan #include <string.h> 39*2dd076b8SGabor Kovesdan 40*2dd076b8SGabor Kovesdan int mkpath(char *); 41*2dd076b8SGabor Kovesdan 42*2dd076b8SGabor Kovesdan /* Code taken directly from mkdir(1). 43*2dd076b8SGabor Kovesdan 44*2dd076b8SGabor Kovesdan * mkpath -- create directories. 45*2dd076b8SGabor Kovesdan * path - path 46*2dd076b8SGabor Kovesdan */ 47*2dd076b8SGabor Kovesdan int 48*2dd076b8SGabor Kovesdan mkpath(char *path) 49*2dd076b8SGabor Kovesdan { 50*2dd076b8SGabor Kovesdan struct stat sb; 51*2dd076b8SGabor Kovesdan char *slash; 52*2dd076b8SGabor Kovesdan int done = 0; 53*2dd076b8SGabor Kovesdan 54*2dd076b8SGabor Kovesdan slash = path; 55*2dd076b8SGabor Kovesdan 56*2dd076b8SGabor Kovesdan while (!done) { 57*2dd076b8SGabor Kovesdan slash += strspn(slash, "/"); 58*2dd076b8SGabor Kovesdan slash += strcspn(slash, "/"); 59*2dd076b8SGabor Kovesdan 60*2dd076b8SGabor Kovesdan done = (*slash == '\0'); 61*2dd076b8SGabor Kovesdan *slash = '\0'; 62*2dd076b8SGabor Kovesdan 63*2dd076b8SGabor Kovesdan if (stat(path, &sb)) { 64*2dd076b8SGabor Kovesdan if (errno != ENOENT || (mkdir(path, 0777) && 65*2dd076b8SGabor Kovesdan errno != EEXIST)) { 66*2dd076b8SGabor Kovesdan warn("%s", path); 67*2dd076b8SGabor Kovesdan return (-1); 68*2dd076b8SGabor Kovesdan } 69*2dd076b8SGabor Kovesdan } else if (!S_ISDIR(sb.st_mode)) { 70*2dd076b8SGabor Kovesdan warnx("%s: %s", path, strerror(ENOTDIR)); 71*2dd076b8SGabor Kovesdan return (-1); 72*2dd076b8SGabor Kovesdan } 73*2dd076b8SGabor Kovesdan 74*2dd076b8SGabor Kovesdan *slash = '/'; 75*2dd076b8SGabor Kovesdan } 76*2dd076b8SGabor Kovesdan 77*2dd076b8SGabor Kovesdan return (0); 78*2dd076b8SGabor Kovesdan } 79*2dd076b8SGabor Kovesdan 80