8d4d736ef83c037a1ea857cd7658a12594c6eb96
[oweals/busybox.git] / libbb / mode_string.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mode_string implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include <assert.h>
24 #include <sys/stat.h>
25
26 #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
27  || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
28  || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
29  || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
30 #error permission bitflag value assumption(s) violated!
31 #endif
32
33 #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
34  || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
35  || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
36  || ( S_IFIFO != 0010000 )
37 #warning mode type bitflag value assumption(s) violated! falling back to larger version
38
39 #if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
40 #undef mode_t
41 #define mode_t unsigned short
42 #endif
43
44 static const mode_t mode_flags[] = {
45         S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
46         S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
47         S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
48 };
49
50 /* The static const char arrays below are duplicated for the two cases
51  * because moving them ahead of the mode_flags declaration cause a text
52  * size increase with the gcc version I'm using. */
53
54 /* The previous version used "0pcCd?bB-?l?s???".  However, the '0', 'C',
55  * and 'B' types don't appear to be available on linux.  So I removed them. */
56 static const char type_chars[16] = "?pc?d?b?-?l?s???";
57 /*                                  0123456789abcdef */
58 static const char mode_chars[7] = "rwxSTst";
59
60 const char *bb_mode_string(int mode)
61 {
62         static char buf[12];
63         char *p = buf;
64
65         int i, j, k;
66
67         *p = type_chars[ (mode >> 12) & 0xf ];
68         i = 0;
69         do {
70                 j = k = 0;
71                 do {
72                         *++p = '-';
73                         if (mode & mode_flags[i+j]) {
74                                 *p = mode_chars[j];
75                                 k = j;
76                         }
77                 } while (++j < 3);
78                 if (mode & mode_flags[i+j]) {
79                         *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
80                 }
81                 i += 4;
82         } while (i < 12);
83
84         /* Note: We don't bother with nul termination because bss initialization
85          * should have taken care of that for us.  If the user scribbled in buf
86          * memory, they deserve whatever happens.  But we'll at least assert. */
87         assert(buf[10] == 0);
88
89         return buf;
90 }
91
92 #else
93
94 /* The previous version used "0pcCd?bB-?l?s???".  However, the '0', 'C',
95  * and 'B' types don't appear to be available on linux.  So I removed them. */
96 static const char type_chars[16] = "?pc?d?b?-?l?s???";
97 /*                                  0123456789abcdef */
98 static const char mode_chars[7] = "rwxSTst";
99
100 const char *bb_mode_string(int mode)
101 {
102         static char buf[12];
103         char *p = buf;
104
105         int i, j, k, m;
106
107         *p = type_chars[ (mode >> 12) & 0xf ];
108         i = 0;
109         m = 0400;
110         do {
111                 j = k = 0;
112                 do {
113                         *++p = '-';
114                         if (mode & m) {
115                                 *p = mode_chars[j];
116                                 k = j;
117                         }
118                         m >>= 1;
119                 } while (++j < 3);
120                 ++i;
121                 if (mode & (010000 >> i)) {
122                         *p = mode_chars[3 + k + (i >> 1)];
123                 }
124         } while (i < 3);
125
126         /* Note: We don't bother with nul termination because bss initialization
127          * should have taken care of that for us.  If the user scribbled in buf
128          * memory, they deserve whatever happens.  But we'll at least assert. */
129         assert(buf[10] == 0);
130
131         return buf;
132 }
133
134 #endif