Subject: new and improved mkdir(1) (#149)
Index:	bin/mkdir.c 2.11BSD

Description:
	Mkdir(1) in 2.11BSD is missing the '-p' option.

Repeat-By:
	Examination.

Fix:
	I had a need to make directories where the intermediate
	path did not already exist.  I noticed that newer versions
	of 'mkdir' present beginning in 4.3Reno had a '-p' option to
	perform this task.  Luckily the program is small enough that
	it compiled straight away without any porting effort.  The
	same, alas, was not true of the manpage - i manually converted
	the man page from 'mandoc' to 'man' macros.

	1) mv /usr/src/bin/mkdir.c /usr/src/bin/mkdir.c.old
	2) mv /usr/src/man/man1/mkdir.1 /usr/src/man/man1/mkdir.1.old
	   [if you are sufficiently trusting you can simply rm the old files]
	3) unshar the file below
	4) cd /usr/src/bin
	   make mkdir
	   install -s mkdir /bin/mkdir
	5) /usr/man/manroff /usr/src/man/man1/mkdir.1 > /usr/man/cat1/mkdir.0
===================================cut here===========================
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	/usr/src/bin/mkdir.c
#	/usr/src/man/man1/mkdir.1
# This archive created: Mon Aug 16 20:18:53 1993
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f '/usr/src/bin/mkdir.c'
then
	echo shar: "will not over-write existing file '/usr/src/bin/mkdir.c'"
else
sed 's/^X//' << \SHAR_EOF > '/usr/src/bin/mkdir.c'
X/*
X * Copyright (c) 1983 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X *    must display the following acknowledgement:
X *	This product includes software developed by the University of
X *	California, Berkeley and its contributors.
X * 4. Neither the name of the University nor the names of its contributors
X *    may be used to endorse or promote products derived from this software
X *    without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X */
X
X#if	!defined(lint) && defined(DOSCCS)
Xchar copyright[] =
X"@(#) Copyright (c) 1983 Regents of the University of California.\n\
X All rights reserved.\n";
X
Xstatic char sccsid[] = "@(#)mkdir.c	5.7 (Berkeley) 5/31/90";
X#endif /* not lint */
X
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <errno.h>
X#include <stdio.h>
X#include <string.h>
X
Xextern int errno;
X
Xmain(argc, argv)
X	int argc;
X	char **argv;
X{
X	extern int optind;
X	int ch, exitval, pflag;
X
X	pflag = 0;
X	while ((ch = getopt(argc, argv, "p")) != EOF)
X		switch(ch) {
X		case 'p':
X			pflag = 1;
X			break;
X		case '?':
X		default:
X			usage();
X		}
X
X	if (!*(argv += optind))
X		usage();
X
X	for (exitval = 0; *argv; ++argv)
X		if (pflag)
X			exitval |= build(*argv);
X		else if (mkdir(*argv, 0777) < 0) {
X			(void)fprintf(stderr, "mkdir: %s: %s\n",
X			    *argv, strerror(errno));
X			exitval = 1;
X		}
X	exit(exitval);
X}
X
Xbuild(path)
X	char *path;
X{
X	register char *p;
X	struct stat sb;
X	int create, ch;
X
X	for (create = 0, p = path;; ++p)
X		if (!*p || *p  == '/') {
X			ch = *p;
X			*p = '\0';
X			if (stat(path, &sb)) {
X				if (errno != ENOENT || mkdir(path, 0777) < 0) {
X					(void)fprintf(stderr, "mkdir: %s: %s\n",
X					    path, strerror(errno));
X					return(1);
X				}
X				create = 1;
X			}
X			if (!(*p = ch))
X				break;
X		}
X	if (!create) {
X		(void)fprintf(stderr, "mkdir: %s: %s\n", path,
X		    strerror(EEXIST));
X		return(1);
X	}
X	return(0);
X}
X
Xusage()
X{
X	(void)fprintf(stderr, "usage: mkdir [-p] dirname ...\n");
X	exit(1);
X}
SHAR_EOF
fi
if test -f '/usr/src/man/man1/mkdir.1'
then
	echo shar: "will not over-write existing file '/usr/src/man/man1/mkdir.1'"
else
sed 's/^X//' << \SHAR_EOF > '/usr/src/man/man1/mkdir.1'
X.\" Copyright (c) 1989, 1990 The Regents of the University of California.
X.\" All rights reserved.
X.\"
X.\" This code is derived from software contributed to Berkeley by
X.\" the Institute of Electrical and Electronics Engineers, Inc.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. All advertising materials mentioning features or use of this software
X.\"    must display the following acknowledgement:
X.\"	This product includes software developed by the University of
X.\"	California, Berkeley and its contributors.
X.\" 4. Neither the name of the University nor the names of its contributors
X.\"    may be used to endorse or promote products derived from this software
X.\"    without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X.\" SUCH DAMAGE.
X.\"
X.\"	@(#)mkdir.1	6.10 (Berkeley) 7/27/91
X.\"
X.TH MKDIR 1 "August 16, 1993"
X.UC 2
X.SH NAME
Xmkdir \- make directories
X.SH SYNOPSIS
Xmkdir [ \-p \fBdirectory_name\fP ... ]
X.SH DESCRIPTION
XMkdir
Xcreates the directories named as operands, in the order specified,
Xusing mode \&0777 modified by the current umask(2).
X.PP
XThe options are as follows:
X.TP
X\-p
XCreate intermediate directories as required.  If this option is not
Xspecified, the full path prefix of each operand must already exist.
X.TP
XThe user must have write permission in the parent directory.
X.PP
XMkdir
Xexits 0 if successful, and >0 if an error occurred.
X.SH SEE ALSO
Xrmdir(1)
X.SH STANDARDS
XMkdir is POSIX 1003.2 compliant.
XThis manual page is derived from the POSIX 1003.2 manual page.
SHAR_EOF
fi
exit 0
#	End of shell archive
