1
0
mirror of git://git.suckless.org/sbase synced 2024-12-22 23:13:44 +00:00
sbase/mkdir.c
Michael Forney 529e50a7ad mkdir: Fix created directory permissions
Previously, with -p, the specified directory and all of its parents
would be 0777&~filemask (regardless of the -m flag). POSIX says parent
directories must created as (0300|~filemask)&0777, and of course if -m
is set, the specified directory should be created with those
permissions.

Additionally, POSIX says that for symbolic_mode strings, + and - should
be interpretted relative to a default mode of 0777 (not 0).

Without -p, previously the directory would be created first with
0777&~filemask (before a chmod), but POSIX says that the directory shall
at no point in time have permissions less restrictive than the -m mode
argument.

Rather than dealing with mkdir removing the filemask bits by calling
chmod afterward, just clear the umask and remove the bits manually.
2017-07-03 21:03:11 +02:00

50 lines
739 B
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-p] [-m mode] name ...\n", argv0);
}
int
main(int argc, char *argv[])
{
mode_t mode, mask;
int pflag = 0, ret = 0;
mask = umask(0);
mode = 0777 & ~mask;
ARGBEGIN {
case 'p':
pflag = 1;
break;
case 'm':
mode = parsemode(EARGF(usage()), 0777, mask);
break;
default:
usage();
} ARGEND
if (!argc)
usage();
for (; *argv; argc--, argv++) {
if (pflag) {
if (mkdirp(*argv, mode, 0777 & (~mask | 0300)) < 0)
ret = 1;
} else if (mkdir(*argv, mode) < 0) {
weprintf("mkdir %s:", *argv);
ret = 1;
}
}
return ret;
}