GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/mkdir/mkdir.c Lines: 42 56 75.0 %
Date: 2017-11-07 Branches: 26 39 66.7 %

Line Branch Exec Source
1
/*	$OpenBSD: mkdir.c,v 1.30 2016/10/19 18:20:25 schwarze Exp $	*/
2
/*	$NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $	*/
3
4
/*
5
 * Copyright (c) 1983, 1992, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/types.h>
34
#include <sys/stat.h>
35
36
#include <err.h>
37
#include <errno.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
extern char *__progname;
44
45
int	mkpath(char *, mode_t, mode_t);
46
static void __dead usage(void);
47
48
int
49
main(int argc, char *argv[])
50
{
51
	int ch, rv, exitval, pflag;
52
	void *set;
53
	mode_t mode, dir_mode;
54
55
	/*
56
	 * The default file mode is a=rwx (0777) with selected permissions
57
	 * removed in accordance with the file mode creation mask.  For
58
	 * intermediate path name components, the mode is the default modified
59
	 * by u+wx so that the subdirectories can always be created.
60
	 */
61
3554
	mode = 0777 & ~umask(0);
62
1777
	dir_mode = mode | S_IWUSR | S_IXUSR;
63
64
	pflag = 0;
65
5134
	while ((ch = getopt(argc, argv, "m:p")) != -1)
66
1580
		switch(ch) {
67
		case 'p':
68
			pflag = 1;
69
1556
			break;
70
		case 'm':
71
24
			if ((set = setmode(optarg)) == NULL)
72
				errx(1, "invalid file mode: %s", optarg);
73
24
			mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
74
24
			free(set);
75
24
			break;
76
		default:
77
			usage();
78
		}
79
1777
	argc -= optind;
80
1777
	argv += optind;
81
82
1777
	if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) == 0) {
83
1777
		if (pledge("stdio rpath cpath fattr flock wpath", NULL) == -1)
84
			err(1, "pledge");
85
	}
86
87
1777
	if (*argv == NULL)
88
		usage();
89
90
7120
	for (exitval = 0; *argv != NULL; ++argv) {
91
		char *slash;
92
93
		/* Remove trailing slashes, per POSIX. */
94
1783
		slash = strrchr(*argv, '\0');
95

5306
		while (--slash > *argv && *slash == '/')
96
			*slash = '\0';
97
98
1783
		if (pflag) {
99
1558
			rv = mkpath(*argv, mode, dir_mode);
100
1558
		} else {
101
225
			rv = mkdir(*argv, mode);
102
			/*
103
			 * The mkdir() and umask() calls both honor only the
104
			 * low nine bits, so if you try to set a mode including
105
			 * the sticky, setuid, setgid bits you lose them. Don't
106
			 * do this unless the user has specifically requested
107
			 * a mode as chmod will (obviously) ignore the umask.
108
			 */
109
225
			if (rv == 0 && mode > 0777)
110
				rv = chmod(*argv, mode);
111
		}
112
1783
		if (rv < 0) {
113
79
			warn("%s", *argv);
114
			exitval = 1;
115
79
		}
116
	}
117
1777
	return exitval;
118
}
119
120
/*
121
 * mkpath -- create directories.
122
 *	path     - path
123
 *	mode     - file mode of terminal directory
124
 *	dir_mode - file mode of intermediate directories
125
 */
126
int
127
mkpath(char *path, mode_t mode, mode_t dir_mode)
128
{
129
3116
	struct stat sb;
130
	char *slash;
131
	int done;
132
133
	slash = path;
134
135
2463
	for (;;) {
136
2463
		slash += strspn(slash, "/");
137
2463
		slash += strcspn(slash, "/");
138
139
2463
		done = (*slash == '\0');
140
2463
		*slash = '\0';
141
142
2463
		if (mkdir(path, done ? mode : dir_mode) == 0) {
143

2001
			if (mode > 0777 && chmod(path, mode) < 0)
144
				return (-1);
145
		} else {
146
462
			int mkdir_errno = errno;
147
148
462
			if (stat(path, &sb)) {
149
				/* Not there; use mkdir()s errno */
150
				errno = mkdir_errno;
151
				return (-1);
152
			}
153
462
			if (!S_ISDIR(sb.st_mode)) {
154
				/* Is there, but isn't a directory */
155
				errno = ENOTDIR;
156
				return (-1);
157
			}
158
462
		}
159
160
2463
		if (done)
161
			break;
162
163
905
		*slash = '/';
164
	}
165
166
1558
	return (0);
167
1558
}
168
169
static void __dead
170
usage(void)
171
{
172
	(void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n",
173
	    __progname);
174
	exit(1);
175
}