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