1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <linux/stat.h>
15
16#if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
17 || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
18 || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
19 || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
20#error permission bitflag value assumption(s) violated!
21#endif
22
23#if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
24 || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
25 || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
26 || ( S_IFIFO != 0010000 )
27#warning mode type bitflag value assumption(s) violated! falling back to larger version
28
29#if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
30#undef mode_t
31#define mode_t unsigned short
32#endif
33
34static const mode_t mode_flags[] = {
35 S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
36 S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
37 S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
38};
39
40
41
42
43
44
45
46static const char type_chars[16] = "?pc?d?b?-?l?s???";
47
48static const char mode_chars[7] = "rwxSTst";
49
50const char *bb_mode_string(int mode)
51{
52 static char buf[12];
53 char *p = buf;
54
55 int i, j, k;
56
57 *p = type_chars[ (mode >> 12) & 0xf ];
58 i = 0;
59 do {
60 j = k = 0;
61 do {
62 *++p = '-';
63 if (mode & mode_flags[i+j]) {
64 *p = mode_chars[j];
65 k = j;
66 }
67 } while (++j < 3);
68 if (mode & mode_flags[i+j]) {
69 *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
70 }
71 i += 4;
72 } while (i < 12);
73
74
75
76
77 if (buf[10] != 0) return NULL;
78
79 return buf;
80}
81
82#else
83
84
85
86static const char type_chars[16] = "?pc?d?b?-?l?s???";
87
88static const char mode_chars[7] = "rwxSTst";
89
90const char *bb_mode_string(int mode)
91{
92 static char buf[12];
93 char *p = buf;
94
95 int i, j, k, m;
96
97 *p = type_chars[ (mode >> 12) & 0xf ];
98 i = 0;
99 m = 0400;
100 do {
101 j = k = 0;
102 do {
103 *++p = '-';
104 if (mode & m) {
105 *p = mode_chars[j];
106 k = j;
107 }
108 m >>= 1;
109 } while (++j < 3);
110 ++i;
111 if (mode & (010000 >> i)) {
112 *p = mode_chars[3 + (k & 2) + (i == 3)];
113 }
114 } while (i < 3);
115
116
117
118
119 if (buf[10] != 0) return NULL;
120
121 return buf;
122}
123
124#endif
125