Audit mkdir(1)
1) No need for strchr() in mkdirp or a while-loop. Rewrite it in a sane and readable way. 2) fix usage according to the manpage. 3) order includes, don't align local variables. 4) argc-style-fix. 5) BUGFIX: Don't try to chmod() *argv when mkdir() / mkdirp() failed. 6) Add newline before return in two places.
This commit is contained in:
parent
cb04864692
commit
d9fa4b3ae7
2
README
2
README
|
@ -44,7 +44,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
||||||
=* logname yes none
|
=* logname yes none
|
||||||
= ls no (-C), -S, -f, -m, -s, -x
|
= ls no (-C), -S, -f, -m, -s, -x
|
||||||
=*| md5sum non-posix none
|
=*| md5sum non-posix none
|
||||||
=* mkdir yes none
|
=*| mkdir yes none
|
||||||
=*| mkfifo yes none
|
=*| mkfifo yes none
|
||||||
=* mktemp non-posix none
|
=* mktemp non-posix none
|
||||||
=*| mv yes none (-i)
|
=*| mv yes none (-i)
|
||||||
|
|
40
mkdir.c
40
mkdir.c
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -12,33 +11,31 @@ mkdirp(char *path)
|
||||||
{
|
{
|
||||||
char *p = path;
|
char *p = path;
|
||||||
|
|
||||||
do {
|
for (p = path; *p; p++) {
|
||||||
if (*p && (p = strchr(&p[1], '/')))
|
if (*p != '/')
|
||||||
|
continue;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) < 0 && errno != EEXIST) {
|
if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
|
||||||
weprintf("mkdir %s:", path);
|
weprintf("mkdir %s:", path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (p)
|
|
||||||
*p = '/';
|
*p = '/';
|
||||||
} while (p);
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-p] [-m mode] directory ...\n", argv0);
|
eprintf("usage: %s [-p] [-m mode] name ...\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
mode_t mode = 0;
|
mode_t mode = 0, mask;
|
||||||
mode_t mask;
|
int pflag = 0, mflag = 0, ret = 0;
|
||||||
int pflag = 0;
|
|
||||||
int mflag = 0;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'p':
|
case 'p':
|
||||||
|
@ -53,23 +50,20 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc < 1)
|
if (!argc)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (; argc > 0; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if (pflag) {
|
if (pflag && mkdirp(*argv) < 0) {
|
||||||
if (mkdirp(argv[0]) < 0)
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
} else if (mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) < 0) {
|
} else if (!pflag && mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
|
||||||
weprintf("mkdir %s:", argv[0]);
|
weprintf("mkdir %s:", *argv);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
} else if (mflag && chmod(*argv, mode) < 0) {
|
||||||
if (mflag) {
|
weprintf("chmod %s:", *argv);
|
||||||
if (chmod(argv[0], mode) < 0) {
|
|
||||||
weprintf("chmod %s:", argv[0]);
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user