1 /* @(#)permtostr.c	1.2 11/08/16 Copyright 2011 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)permtostr.c	1.2 11/08/16 Copyright 2011 J. Schilling";
6 #endif
7 /*
8  *	mkgmtime() is a complement to getperm()
9  *
10  *	Copyright (c) 2011 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include <schily/mconfig.h>
27 #include <schily/types.h>
28 #include <schily/stat.h>
29 #include <schily/schily.h>
30 
31 EXPORT	void	permtostr	__PR((mode_t mode, char *s));
32 
33 LOCAL mode_t modebits[9] = {
34 	S_IXOTH, S_IWOTH, S_IROTH,
35 	S_IXGRP, S_IWGRP, S_IRGRP,
36 	S_IXUSR, S_IWUSR, S_IRUSR
37 };
38 
39 /*
40  * The maximum length of the perm string (including the final null byte) is 24.
41  * If all permission related mode bits are set, the generated string will
42  * be: "u=srwx,g=srwx,o=rwx,a+t".
43  */
44 #ifdef	PROTOTYPES
45 EXPORT void
permtostr(register mode_t mode,char * s)46 permtostr(register mode_t mode, char *s)
47 #else
48 EXPORT void
49 permtostr(mode, s)
50 	register mode_t	mode;
51 	char		*s;
52 #endif
53 {
54 	register char	*mstr = "xwrxwrxwr";
55 	register char	*str = s;
56 	register int	i;
57 
58 	for (i = 9; --i >= 0; ) {
59 		if (i % 3 == 2) {
60 			if (str > s)
61 				*str++ = ',';
62 			*str++ = "ogu"[i/3];
63 			*str++ = '=';
64 			if (i == 8 && mode & S_ISUID)
65 				*str++ = 's';
66 			if (i == 5 && mode & S_ISGID)
67 				*str++ = 's';
68 		}
69 		if (mode & modebits[i])
70 			*str++ = mstr[i];
71 	}
72 	if (mode & S_ISVTX) {
73 		*str++ = ',';
74 		*str++ = 'a';
75 		*str++ = '+';
76 		*str++ = 't';
77 	}
78 	*str = '\0';
79 }
80