xref: /freebsd/usr.bin/patch/mkpath.c (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
12dd076b8SGabor Kovesdan /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
42dd076b8SGabor Kovesdan  * Copyright (c) 1983, 1992, 1993
52dd076b8SGabor Kovesdan  *	The Regents of the University of California.  All rights reserved.
62dd076b8SGabor Kovesdan  *
72dd076b8SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
82dd076b8SGabor Kovesdan  * modification, are permitted provided that the following conditions
92dd076b8SGabor Kovesdan  * are met:
102dd076b8SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
112dd076b8SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
122dd076b8SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
132dd076b8SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
142dd076b8SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
152dd076b8SGabor Kovesdan  * 3. Neither the name of the University nor the names of its contributors
162dd076b8SGabor Kovesdan  *    may be used to endorse or promote products derived from this software
172dd076b8SGabor Kovesdan  *    without specific prior written permission.
182dd076b8SGabor Kovesdan  *
192dd076b8SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202dd076b8SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212dd076b8SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222dd076b8SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232dd076b8SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242dd076b8SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252dd076b8SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262dd076b8SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272dd076b8SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282dd076b8SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292dd076b8SGabor Kovesdan  * SUCH DAMAGE.
302dd076b8SGabor Kovesdan  *
312dd076b8SGabor Kovesdan  * $OpenBSD: mkpath.c,v 1.2 2005/06/20 07:14:06 otto Exp $
322dd076b8SGabor Kovesdan  */
332dd076b8SGabor Kovesdan 
342dd076b8SGabor Kovesdan #include <sys/types.h>
352dd076b8SGabor Kovesdan #include <sys/stat.h>
362dd076b8SGabor Kovesdan #include <err.h>
372dd076b8SGabor Kovesdan #include <errno.h>
382dd076b8SGabor Kovesdan #include <string.h>
392dd076b8SGabor Kovesdan 
402dd076b8SGabor Kovesdan int	mkpath(char *);
412dd076b8SGabor Kovesdan 
422dd076b8SGabor Kovesdan /* Code taken directly from mkdir(1).
432dd076b8SGabor Kovesdan 
442dd076b8SGabor Kovesdan  * mkpath -- create directories.
452dd076b8SGabor Kovesdan  *	path     - path
462dd076b8SGabor Kovesdan  */
472dd076b8SGabor Kovesdan int
mkpath(char * path)482dd076b8SGabor Kovesdan mkpath(char *path)
492dd076b8SGabor Kovesdan {
502dd076b8SGabor Kovesdan 	struct stat sb;
512dd076b8SGabor Kovesdan 	char *slash;
522dd076b8SGabor Kovesdan 	int done = 0;
532dd076b8SGabor Kovesdan 
542dd076b8SGabor Kovesdan 	slash = path;
552dd076b8SGabor Kovesdan 
562dd076b8SGabor Kovesdan 	while (!done) {
572dd076b8SGabor Kovesdan 		slash += strspn(slash, "/");
582dd076b8SGabor Kovesdan 		slash += strcspn(slash, "/");
592dd076b8SGabor Kovesdan 
602dd076b8SGabor Kovesdan 		done = (*slash == '\0');
612dd076b8SGabor Kovesdan 		*slash = '\0';
622dd076b8SGabor Kovesdan 
632dd076b8SGabor Kovesdan 		if (stat(path, &sb)) {
642dd076b8SGabor Kovesdan 			if (errno != ENOENT || (mkdir(path, 0777) &&
652dd076b8SGabor Kovesdan 			    errno != EEXIST)) {
662dd076b8SGabor Kovesdan 				warn("%s", path);
672dd076b8SGabor Kovesdan 				return (-1);
682dd076b8SGabor Kovesdan 			}
692dd076b8SGabor Kovesdan 		} else if (!S_ISDIR(sb.st_mode)) {
702dd076b8SGabor Kovesdan 			warnx("%s: %s", path, strerror(ENOTDIR));
712dd076b8SGabor Kovesdan 			return (-1);
722dd076b8SGabor Kovesdan 		}
732dd076b8SGabor Kovesdan 
742dd076b8SGabor Kovesdan 		*slash = '/';
752dd076b8SGabor Kovesdan 	}
762dd076b8SGabor Kovesdan 
772dd076b8SGabor Kovesdan 	return (0);
782dd076b8SGabor Kovesdan }
792dd076b8SGabor Kovesdan 
80