xref: /onnv-gate/usr/src/lib/libshell/common/bltins/umask.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1982-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                  David Korn <dgk@research.att.com>                   *
184887Schin *                                                                      *
194887Schin ***********************************************************************/
204887Schin #pragma prototyped
214887Schin /*
224887Schin  * umask [-S] [mask]
234887Schin  *
244887Schin  *   David Korn
254887Schin  *   AT&T Labs
264887Schin  *   research!dgk
274887Schin  *
284887Schin  */
294887Schin 
304887Schin #include	<ast.h>
314887Schin #include	<sfio.h>
324887Schin #include	<error.h>
334887Schin #include	<ctype.h>
344887Schin #include	<ls.h>
354887Schin #include	<shell.h>
364887Schin #include	"builtins.h"
374887Schin #ifndef SH_DICT
384887Schin #   define SH_DICT	"libshell"
394887Schin #endif
404887Schin 
b_umask(int argc,char * argv[],void * extra)414887Schin int	b_umask(int argc,char *argv[],void *extra)
424887Schin {
434887Schin 	register char *mask;
444887Schin 	register int flag = 0, sflag = 0;
454887Schin 	NOT_USED(extra);
464887Schin 	while((argc = optget(argv,sh_optumask))) switch(argc)
474887Schin 	{
484887Schin 		case 'S':
494887Schin 			sflag++;
504887Schin 			break;
514887Schin 		case ':':
524887Schin 			errormsg(SH_DICT,2, "%s", opt_info.arg);
534887Schin 			break;
544887Schin 		case '?':
554887Schin 			errormsg(SH_DICT,ERROR_usage(2), "%s",opt_info.arg);
564887Schin 			break;
574887Schin 	}
584887Schin 	if(error_info.errors)
594887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
604887Schin 	argv += opt_info.index;
614887Schin 	if(mask = *argv)
624887Schin 	{
634887Schin 		register int c;
644887Schin 		if(isdigit(*mask))
654887Schin 		{
664887Schin 			while(c = *mask++)
674887Schin 			{
684887Schin 				if (c>='0' && c<='7')
694887Schin 					flag = (flag<<3) + (c-'0');
704887Schin 				else
714887Schin 					errormsg(SH_DICT,ERROR_exit(1),e_number,*argv);
724887Schin 			}
734887Schin 		}
744887Schin 		else
754887Schin 		{
764887Schin 			char *cp = mask;
774887Schin 			flag = umask(0);
788462SApril.Chin@Sun.COM 			c = strperm(cp,&cp,~flag&0777);
794887Schin 			if(*cp)
804887Schin 			{
814887Schin 				umask(flag);
824887Schin 				errormsg(SH_DICT,ERROR_exit(1),e_format,mask);
834887Schin 			}
844887Schin 			flag = (~c&0777);
854887Schin 		}
864887Schin 		umask(flag);
874887Schin 	}
884887Schin 	else
894887Schin 	{
904887Schin 		umask(flag=umask(0));
914887Schin 		if(sflag)
924887Schin 			sfprintf(sfstdout,"%s\n",fmtperm(~flag&0777));
934887Schin 		else
944887Schin 			sfprintf(sfstdout,"%0#4o\n",flag);
954887Schin 	}
964887Schin 	return(0);
974887Schin }
984887Schin 
99