/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: mkdir.c 1.5 90/03/19 $@(#)mkdir:mkdir.c	1.7"
/*
** make directory.
** If -m is used with a valid mode, directories will be
** created in that mode.  Otherwise, the default mode will
** be 777 possibly altered by the process's file mode creation
** mask.
** If -p is used, make the directory as well as
** its non-existing parent directories.
*/

#include	<signal.h>
#include	<stdio.h>
#include	<errno.h>
#include	<string.h>

extern int opterr, optind,  errno;
extern char *optarg,  *sys_errlist[];

char *strrchr();

main(argc, argv)
char *argv[];
int argc;
{
	int i, c, pflag, errflg, mflag, mode, status;
	char *d, *p, *endptr, *m, *ptr;
	char 	msgid[16];

	pflag = 0;
	mflag = 0;
	errflg = 0;
	mode = 0777;
	status = 0;

	while ((c=getopt(argc, argv, "m:p")) != EOF)
		switch (c) {
			case 'm':
				mflag++;
				m = optarg;
				for(i=0; *m != '\0'; m++)
					if(*m < '0' || *m > '7') {
						fprintf(stderr,
					gettxt("mkdir:1", "Invalid mode.\n"));
						exit(2);
						}
				umask(0);
				mode = (int) strtol(optarg, (char **)NULL, 8);
				break;
			case 'p':
				pflag++;
				break;
			case '?':
				errflg++;
				break;
		}
	if(argc < 2 || errflg) {
		fprintf(stderr,
    gettxt("mkdir:2", "mkdir: usage: mkdir [-m mode] [-p] dirname ...\n"));
		exit(2);
	}
	argc -= optind;
	argv = &argv[optind];
        while(argc--) {
		d = *argv++;

			/* Skip extra slashes at the end of path */

		while ((endptr=strrchr(d, '/')) != NULL) {
			p=endptr;
			p++;
			if (*p == '\0')
				*endptr='\0';
			else
				break;
		}

			/* When -p is set */
		if (pflag) {
			if (mkdirp(d,mode) <0) {
				sprintf(msgid,"syserr:%d",errno+1);
				fprintf(stderr,
				    gettxt("colon:37", "mkdir: \"%s\": %s\n"),
				    d, gettxt(msgid,sys_errlist[errno])); 
				status = 2;
				continue;
			}
			continue;
		}
			/* No -p. Make only one directory */

				/* Check write permission of parent*/

					/* Parent is current directory */

		if ((ptr=strrchr(d,'/')) == NULL)
			if (access(".",02)) {
				sprintf(msgid,"syserr:%d",errno+1);
				fprintf(stderr,
				    gettxt("colon:37", "mkdir: \"%s\": %s\n"),
				    d,gettxt(msgid,sys_errlist[errno])); 
				status = 2;
				continue;
			} else;
		else {
					/* Parent is / */
			if (ptr == d) {
				if (access("/",02)) {
					sprintf(msgid,"syserr:%d",errno+1);
					fprintf(stderr,
				    gettxt("colon:37", "mkdir: \"%s\": %s\n"),
					    d,gettxt(msgid,sys_errlist[errno]));
					status = 2;
					continue;
				} else;
					/* Parent is not / */
			} else {
				*ptr = '\0';
				if (access(d,02)) {
					*ptr='/';
					sprintf(msgid,"syserr:%d",errno+1);
					fprintf(stderr,
				    gettxt("colon:37", "mkdir: \"%s\": %s\n"),
					    d,gettxt(msgid,sys_errlist[errno]));
					status = 2;
					continue;
				}
				*ptr='/';
			}
		}
				/* Set id to real uid and gid */
 
		if (setuid(getuid()) || setgid(getgid())) {
			fprintf(stderr,
gettxt("mkdir:3", "mkdir:  Failed to set effective user/group ids to real user/group ids"));
			status = 2;
			continue;
		}
				/* Make the directory */

		if ((mkdir(d, mode)) < 0) {
			sprintf(msgid,"syserr:%d",errno+1);
                   	fprintf(stderr,
	    gettxt("mkdir:4", "mkdir:  Failed to make directory \"%s\"; %s\n"),
			    d, gettxt(msgid,sys_errlist[errno]));  
			status = 2;
			continue;
		}
	}
        exit(status);
}
