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
|
||||
= ls no (-C), -S, -f, -m, -s, -x
|
||||
=*| md5sum non-posix none
|
||||
=* mkdir yes none
|
||||
=*| mkdir yes none
|
||||
=*| mkfifo yes none
|
||||
=* mktemp non-posix none
|
||||
=*| mv yes none (-i)
|
||||
|
48
mkdir.c
48
mkdir.c
@ -3,7 +3,6 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
@ -12,33 +11,31 @@ mkdirp(char *path)
|
||||
{
|
||||
char *p = path;
|
||||
|
||||
do {
|
||||
if (*p && (p = strchr(&p[1], '/')))
|
||||
*p = '\0';
|
||||
if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) < 0 && errno != EEXIST) {
|
||||
for (p = path; *p; p++) {
|
||||
if (*p != '/')
|
||||
continue;
|
||||
*p = '\0';
|
||||
if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
|
||||
weprintf("mkdir %s:", path);
|
||||
return -1;
|
||||
}
|
||||
if (p)
|
||||
*p = '/';
|
||||
} while (p);
|
||||
*p = '/';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s [-p] [-m mode] directory ...\n", argv0);
|
||||
eprintf("usage: %s [-p] [-m mode] name ...\n", argv0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
mode_t mode = 0;
|
||||
mode_t mask;
|
||||
int pflag = 0;
|
||||
int mflag = 0;
|
||||
int ret = 0;
|
||||
mode_t mode = 0, mask;
|
||||
int pflag = 0, mflag = 0, ret = 0;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'p':
|
||||
@ -53,23 +50,20 @@ main(int argc, char *argv[])
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc < 1)
|
||||
if (!argc)
|
||||
usage();
|
||||
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if (pflag) {
|
||||
if (mkdirp(argv[0]) < 0)
|
||||
ret = 1;
|
||||
} else if (mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) < 0) {
|
||||
weprintf("mkdir %s:", argv[0]);
|
||||
for (; *argv; argc--, argv++) {
|
||||
if (pflag && mkdirp(*argv) < 0) {
|
||||
ret = 1;
|
||||
} else if (!pflag && mkdir(*argv, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
|
||||
weprintf("mkdir %s:", *argv);
|
||||
ret = 1;
|
||||
} else if (mflag && chmod(*argv, mode) < 0) {
|
||||
weprintf("chmod %s:", *argv);
|
||||
ret = 1;
|
||||
}
|
||||
if (mflag) {
|
||||
if (chmod(argv[0], mode) < 0) {
|
||||
weprintf("chmod %s:", argv[0]);
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user