cdesktopenv/cde/programs/dtksh/ksh93/src/lib/libcmd/mkdir.c

174 lines
4.9 KiB
C

/*
* CDE - Common Desktop Environment
*
* Copyright (c) 1993-2012, The Open Group. All rights reserved.
*
* These libraries and programs are free software; you can
* redistribute them and/or modify them under the terms of the GNU
* Lesser General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* These libraries and programs are distributed in the hope that
* they will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with these librararies and programs; if not, write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301 USA
*/
/* $XConsortium: mkdir.c /main/3 1995/11/01 19:06:47 rswiston $ */
/***************************************************************
* *
* AT&T - PROPRIETARY *
* *
* THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
* AT&T CORP. *
* *
* Copyright (c) 1995 AT&T Corp. *
* All Rights Reserved *
* *
* This software is licensed by AT&T Corp. *
* under the terms and conditions of the license in *
* http://www.research.att.com/orgs/ssr/book/reuse *
* *
* This software was created by the *
* Software Engineering Research Department *
* AT&T Bell Laboratories *
* *
* For further information contact *
* gsf@research.att.com *
* *
***************************************************************/
/* : : generated by proto : : */
#if !defined(__PROTO__)
#if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
#if defined(__cplusplus)
#define __MANGLE__ "C"
#else
#define __MANGLE__
#endif
#define __STDARG__
#define __PROTO__(x) x
#define __OTORP__(x)
#define __PARAM__(n,o) n
#if !defined(__STDC__) && !defined(__cplusplus)
#if !defined(c_plusplus)
#define const
#endif
#define signed
#define void int
#define volatile
#define __V_ char
#else
#define __V_ void
#endif
#else
#define __PROTO__(x) ()
#define __OTORP__(x) x
#define __PARAM__(n,o) o
#define __MANGLE__
#define __V_ char
#define const
#define signed
#define void int
#define volatile
#endif
#if defined(__cplusplus) || defined(c_plusplus)
#define __VARARG__ ...
#else
#define __VARARG__
#endif
#if defined(__STDARG__)
#define __VA_START__(p,a) va_start(p,a)
#else
#define __VA_START__(p,a) va_start(p)
#endif
#endif
static const char id[] = "\n@(#)mkdir (AT&T Bell Laboratories) 02/14/95\0\n";
#include <cmdlib.h>
#include <ls.h>
#define DIRMODE (S_IRWXU|S_IRWXG|S_IRWXO)
int
b_mkdir __PARAM__((int argc, char *argv[]), (argc, argv)) __OTORP__(int argc; char *argv[];){
register char *arg;
register mode_t mode=DIRMODE, mask;
register int n, mflag=0, pflag=0;
mode_t dmode;
NoP(argc);
NoP(id[0]);
cmdinit(argv);
while (n = optget(argv, "pm:[mode] directory ...")) switch (n)
{
case 'p':
pflag=1;
break;
case 'm':
mflag=1;
mode = strperm(arg=opt_info.arg,&opt_info.arg,mode);
if(*opt_info.arg)
error(ERROR_exit(0),"%s: invalid mode",arg);
break;
case ':':
error(2, opt_info.arg);
break;
case '?':
error(ERROR_usage(2), opt_info.arg);
break;
}
argv += opt_info.index;
if(error_info.errors || !*argv)
error(ERROR_usage(2),optusage(NiL));
if(mflag || pflag)
{
umask(mask = umask(0));
dmode = (DIRMODE&~mask)|S_IWUSR|S_IXUSR;
}
while(arg = *argv++)
{
if(mkdir(arg,mode) < 0)
{
char *name;
if(!pflag || !(errno==ENOENT || errno==EEXIST))
{
error(ERROR_system(0),"%s:",arg);
continue;
}
if(errno==EEXIST)
continue;
/* -p option, preserve intermediates */
/* first eliminate trailing /'s */
n = strlen(arg);
while(n>0 && arg[--n]=='/');
arg[n+1]=0;
for(name=arg,n= *arg;n;)
{
/* skip over slashes */
while(*arg=='/')
arg++;
/* skip to next component */
while((n= *arg) && n!='/')
arg++;
*arg = 0;
if(access(name, X_OK) && mkdir(name,n?dmode:mode))
{
*arg = n;
error(ERROR_system(0),"%s:",name);
break;
}
*arg = n;
}
}
}
return(error_info.errors!=0);
}