1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                  David Korn <dgk@research.att.com>                   *
18*4887Schin *                                                                      *
19*4887Schin ***********************************************************************/
20*4887Schin #pragma prototyped
21*4887Schin /*
22*4887Schin  * umask [-S] [mask]
23*4887Schin  *
24*4887Schin  *   David Korn
25*4887Schin  *   AT&T Labs
26*4887Schin  *   research!dgk
27*4887Schin  *
28*4887Schin  */
29*4887Schin 
30*4887Schin #include	<ast.h>
31*4887Schin #include	<sfio.h>
32*4887Schin #include	<error.h>
33*4887Schin #include	<ctype.h>
34*4887Schin #include	<ls.h>
35*4887Schin #include	<shell.h>
36*4887Schin #include	"builtins.h"
37*4887Schin #ifndef SH_DICT
38*4887Schin #   define SH_DICT	"libshell"
39*4887Schin #endif
40*4887Schin 
41*4887Schin int	b_umask(int argc,char *argv[],void *extra)
42*4887Schin {
43*4887Schin 	register char *mask;
44*4887Schin 	register int flag = 0, sflag = 0;
45*4887Schin 	NOT_USED(extra);
46*4887Schin 	while((argc = optget(argv,sh_optumask))) switch(argc)
47*4887Schin 	{
48*4887Schin 		case 'S':
49*4887Schin 			sflag++;
50*4887Schin 			break;
51*4887Schin 		case ':':
52*4887Schin 			errormsg(SH_DICT,2, "%s", opt_info.arg);
53*4887Schin 			break;
54*4887Schin 		case '?':
55*4887Schin 			errormsg(SH_DICT,ERROR_usage(2), "%s",opt_info.arg);
56*4887Schin 			break;
57*4887Schin 	}
58*4887Schin 	if(error_info.errors)
59*4887Schin 		errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0));
60*4887Schin 	argv += opt_info.index;
61*4887Schin 	if(mask = *argv)
62*4887Schin 	{
63*4887Schin 		register int c;
64*4887Schin 		if(isdigit(*mask))
65*4887Schin 		{
66*4887Schin 			while(c = *mask++)
67*4887Schin 			{
68*4887Schin 				if (c>='0' && c<='7')
69*4887Schin 					flag = (flag<<3) + (c-'0');
70*4887Schin 				else
71*4887Schin 					errormsg(SH_DICT,ERROR_exit(1),e_number,*argv);
72*4887Schin 			}
73*4887Schin 		}
74*4887Schin 		else
75*4887Schin 		{
76*4887Schin 			char *cp = mask;
77*4887Schin 			flag = umask(0);
78*4887Schin 			c = strperm(cp,&cp,~flag);
79*4887Schin 			if(*cp)
80*4887Schin 			{
81*4887Schin 				umask(flag);
82*4887Schin 				errormsg(SH_DICT,ERROR_exit(1),e_format,mask);
83*4887Schin 			}
84*4887Schin 			flag = (~c&0777);
85*4887Schin 		}
86*4887Schin 		umask(flag);
87*4887Schin 	}
88*4887Schin 	else
89*4887Schin 	{
90*4887Schin 		umask(flag=umask(0));
91*4887Schin 		if(sflag)
92*4887Schin 			sfprintf(sfstdout,"%s\n",fmtperm(~flag&0777));
93*4887Schin 		else
94*4887Schin 			sfprintf(sfstdout,"%0#4o\n",flag);
95*4887Schin 	}
96*4887Schin 	return(0);
97*4887Schin }
98*4887Schin 
99