From 9b18c35ac946d538e237a5494fc1626be13db5c6 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sun, 5 Nov 2000 15:14:09 +0000 Subject: (main): Use make_dir instead of using mkdir directly. Diagnose as failure when mkdir tries to create (without the --parent (-p) option) a directory that already exists. --- src/mkdir.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/mkdir.c b/src/mkdir.c index 11487c8f2..bc5ad0e68 100644 --- a/src/mkdir.c +++ b/src/mkdir.c @@ -151,12 +151,22 @@ main (int argc, char **argv) if (fail == 0) { - fail = mkdir (argv[optind], newmode); - if (fail) - error (0, errno, _("cannot create directory %s"), - quote (argv[optind])); + const char *dir = argv[optind]; + int dir_created; + int t_errno; + fail = make_dir (dir, dir, newmode, &dir_created); + t_errno = errno; + /* If make_dir `succeeds' because the directory already exists, + then fail unless --parents (-p) was specified. */ + if (fail || + (!create_parents && !dir_created && (t_errno = EEXIST))) + { + error (0, t_errno, _("cannot create directory %s"), + quote (dir)); + fail = 1; + } else if (verbose_fmt_string) - error (0, 0, verbose_fmt_string, quote (argv[optind])); + error (0, 0, verbose_fmt_string, quote (dir)); /* mkdir(2) is required to honor only the file permission bits. In particular, it needn't do anything about `special' bits, @@ -166,10 +176,10 @@ main (int argc, char **argv) if (fail == 0 && specified_mode) { - fail = chmod (argv[optind], newmode); + fail = chmod (dir, newmode); if (fail) error (0, errno, _("cannot set permissions of directory %s"), - quote (argv[optind])); + quote (dir)); } } -- cgit v1.2.3-70-g09d2