mkpath.c revision 246074
1246074Sgabor/* $FreeBSD: head/usr.bin/patch/mkpath.c 246074 2013-01-29 17:03:18Z gabor $ */ 2246074Sgabor/*- 3246074Sgabor * Copyright (c) 1983, 1992, 1993 4246074Sgabor * The Regents of the University of California. All rights reserved. 5246074Sgabor * 6246074Sgabor * Redistribution and use in source and binary forms, with or without 7246074Sgabor * modification, are permitted provided that the following conditions 8246074Sgabor * are met: 9246074Sgabor * 1. Redistributions of source code must retain the above copyright 10246074Sgabor * notice, this list of conditions and the following disclaimer. 11246074Sgabor * 2. Redistributions in binary form must reproduce the above copyright 12246074Sgabor * notice, this list of conditions and the following disclaimer in the 13246074Sgabor * documentation and/or other materials provided with the distribution. 14246074Sgabor * 3. Neither the name of the University nor the names of its contributors 15246074Sgabor * may be used to endorse or promote products derived from this software 16246074Sgabor * without specific prior written permission. 17246074Sgabor * 18246074Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19246074Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20246074Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21246074Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22246074Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23246074Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24246074Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25246074Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26246074Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27246074Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28246074Sgabor * SUCH DAMAGE. 29246074Sgabor * 30246074Sgabor * $OpenBSD: mkpath.c,v 1.2 2005/06/20 07:14:06 otto Exp $ 31246074Sgabor * $DragonFly: src/usr.bin/patch/mkpath.c,v 1.1 2007/09/29 23:11:10 swildner Exp $ 32246074Sgabor */ 33246074Sgabor 34246074Sgabor#include <sys/types.h> 35246074Sgabor#include <sys/stat.h> 36246074Sgabor#include <err.h> 37246074Sgabor#include <errno.h> 38246074Sgabor#include <string.h> 39246074Sgabor 40246074Sgaborint mkpath(char *); 41246074Sgabor 42246074Sgabor/* Code taken directly from mkdir(1). 43246074Sgabor 44246074Sgabor * mkpath -- create directories. 45246074Sgabor * path - path 46246074Sgabor */ 47246074Sgaborint 48246074Sgabormkpath(char *path) 49246074Sgabor{ 50246074Sgabor struct stat sb; 51246074Sgabor char *slash; 52246074Sgabor int done = 0; 53246074Sgabor 54246074Sgabor slash = path; 55246074Sgabor 56246074Sgabor while (!done) { 57246074Sgabor slash += strspn(slash, "/"); 58246074Sgabor slash += strcspn(slash, "/"); 59246074Sgabor 60246074Sgabor done = (*slash == '\0'); 61246074Sgabor *slash = '\0'; 62246074Sgabor 63246074Sgabor if (stat(path, &sb)) { 64246074Sgabor if (errno != ENOENT || (mkdir(path, 0777) && 65246074Sgabor errno != EEXIST)) { 66246074Sgabor warn("%s", path); 67246074Sgabor return (-1); 68246074Sgabor } 69246074Sgabor } else if (!S_ISDIR(sb.st_mode)) { 70246074Sgabor warnx("%s: %s", path, strerror(ENOTDIR)); 71246074Sgabor return (-1); 72246074Sgabor } 73246074Sgabor 74246074Sgabor *slash = '/'; 75246074Sgabor } 76246074Sgabor 77246074Sgabor return (0); 78246074Sgabor} 79246074Sgabor 80