Make Posix 1003.2 (D11.2) compliant by adding -m (mode) option.
This commit is contained in:
parent
932d674def
commit
724f511de6
@ -34,9 +34,8 @@
|
||||
.\"
|
||||
.\" @(#)mkdir.1 6.9 (Berkeley) 6/27/91
|
||||
.\"
|
||||
.\" $Header: /cvsroot/src/bin/mkdir/mkdir.1,v 1.3 1993/03/23 00:26:14 cgd Exp $
|
||||
.\" $Header: /cvsroot/src/bin/mkdir/mkdir.1,v 1.4 1993/07/20 22:27:07 jtc Exp $
|
||||
.\"
|
||||
.Vx
|
||||
.Dd June 27, 1991
|
||||
.Dt MKDIR 1
|
||||
.Os
|
||||
@ -45,29 +44,47 @@
|
||||
.Nd make directories
|
||||
.Sh SYNOPSIS
|
||||
.Nm mkdir
|
||||
.Op Fl m Ar mode
|
||||
.Op Fl p
|
||||
.Ar directory_name ...
|
||||
.Sh DESCRIPTION
|
||||
.Nm Mkdir
|
||||
creates the directories named as operands, in the order specified,
|
||||
using mode
|
||||
creates the directories named as operands, in the order specified.
|
||||
The default mode of created directories is
|
||||
.Li \&0777
|
||||
modified by the current
|
||||
.Xr umask 2 .
|
||||
.Pp
|
||||
The options are as follows:
|
||||
.Tw Ds
|
||||
.Tp Fl p
|
||||
.Bl -tag -width Ds
|
||||
.It Fl m Ar mode
|
||||
Set the file permission bits of newly-created directories to
|
||||
.Ar mode .
|
||||
The
|
||||
.Ar mode
|
||||
is specified as in
|
||||
.Xr chmod 1 .
|
||||
In symbolic mode strings, the
|
||||
.Dq +
|
||||
and
|
||||
.Dq -
|
||||
operators are interpreted relative to an assumed initial mode of
|
||||
.Dq a=rwx .
|
||||
.It Fl p
|
||||
Create intermediate directories as required. If this option is not
|
||||
specified, the full path prefix of each operand must already exist.
|
||||
.Tp
|
||||
.El
|
||||
.Pp
|
||||
The user must have write permission in the parent directory.
|
||||
.Pp
|
||||
.Nm Mkdir
|
||||
exits 0 if successful, and >0 if an error occurred.
|
||||
.Sh SEE ALSO
|
||||
.Xr rmdir 1
|
||||
.Xr chmod 1 ,
|
||||
.Xr rmdir 1 ,
|
||||
.Xr umask 2
|
||||
.Sh STANDARDS
|
||||
Mkdir is POSIX 1003.2 compliant.
|
||||
This manual page is derived from the POSIX 1003.2 manual page.
|
||||
.Nm Mkdir
|
||||
is expected to be
|
||||
.St -p1003.2
|
||||
compatible.
|
||||
|
@ -39,7 +39,7 @@ char copyright[] =
|
||||
|
||||
#ifndef lint
|
||||
static char sccsid[] = "@(#)mkdir.c 5.7 (Berkeley) 5/31/90";
|
||||
static char rcsid[] = "$Header: /cvsroot/src/bin/mkdir/mkdir.c,v 1.3 1993/03/23 00:26:15 cgd Exp $";
|
||||
static char rcsid[] = "$Header: /cvsroot/src/bin/mkdir/mkdir.c,v 1.4 1993/07/20 22:27:08 jtc Exp $";
|
||||
#endif /* not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -47,22 +47,42 @@ static char rcsid[] = "$Header: /cvsroot/src/bin/mkdir/mkdir.c,v 1.3 1993/03/23
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern int errno;
|
||||
extern void *setmode();
|
||||
extern mode_t getmode();
|
||||
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
extern int optind;
|
||||
int ch, exitval, pflag;
|
||||
void *set;
|
||||
mode_t mode, dir_mode;
|
||||
|
||||
/* default file mode is a=rwx (777) with selected permissions
|
||||
removed in accordance with the file mode creation mask.
|
||||
For intermediate path name components, the mode is the default
|
||||
modified by u+wx so that the subdirectories can always be
|
||||
created. */
|
||||
mode = 0777 & ~umask(0);
|
||||
dir_mode = mode | S_IWUSR | S_IXUSR;
|
||||
|
||||
pflag = 0;
|
||||
while ((ch = getopt(argc, argv, "p")) != EOF)
|
||||
while ((ch = getopt(argc, argv, "pm:")) != EOF)
|
||||
switch(ch) {
|
||||
case 'p':
|
||||
pflag = 1;
|
||||
break;
|
||||
case 'm':
|
||||
if ((set = setmode(optarg)) == NULL) {
|
||||
(void)fprintf(stderr,
|
||||
"mkdir: invalid file mode.\n");
|
||||
exit(1);
|
||||
}
|
||||
mode = getmode (set, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
@ -70,50 +90,53 @@ main(argc, argv)
|
||||
|
||||
if (!*(argv += optind))
|
||||
usage();
|
||||
|
||||
for (exitval = 0; *argv; ++argv)
|
||||
|
||||
for (exitval = 0; *argv; ++argv) {
|
||||
if (pflag)
|
||||
exitval |= build(*argv);
|
||||
else if (mkdir(*argv, 0777) < 0) {
|
||||
exitval |= build(*argv, mode, dir_mode);
|
||||
else if (mkdir(*argv, mode) < 0) {
|
||||
(void)fprintf(stderr, "mkdir: %s: %s\n",
|
||||
*argv, strerror(errno));
|
||||
exitval = 1;
|
||||
}
|
||||
}
|
||||
exit(exitval);
|
||||
}
|
||||
|
||||
build(path)
|
||||
/*
|
||||
* build -- create directories.
|
||||
* mode - file mode of terminal directory
|
||||
* dir_mode - file mode of intermediate directories
|
||||
*/
|
||||
build(path, mode, dir_mode)
|
||||
char *path;
|
||||
mode_t mode;
|
||||
mode_t dir_mode;
|
||||
{
|
||||
register char *p;
|
||||
struct stat sb;
|
||||
int create, ch;
|
||||
int ch;
|
||||
|
||||
for (create = 0, p = path;; ++p)
|
||||
for (p = path;; ++p) {
|
||||
if (!*p || *p == '/') {
|
||||
ch = *p;
|
||||
*p = '\0';
|
||||
if (stat(path, &sb)) {
|
||||
if (errno != ENOENT || mkdir(path, 0777) < 0) {
|
||||
if (errno != ENOENT || mkdir(path, (ch) ? dir_mode : mode) < 0) {
|
||||
(void)fprintf(stderr, "mkdir: %s: %s\n",
|
||||
path, strerror(errno));
|
||||
return(1);
|
||||
}
|
||||
create = 1;
|
||||
}
|
||||
if (!(*p = ch))
|
||||
break;
|
||||
}
|
||||
if (!create) {
|
||||
(void)fprintf(stderr, "mkdir: %s: %s\n", path,
|
||||
strerror(EEXIST));
|
||||
return(1);
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
(void)fprintf(stderr, "usage: mkdir [-p] dirname ...\n");
|
||||
(void)fprintf(stderr, "usage: mkdir [-p] [-m mode] dirname ...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user