xref: /openbmc/u-boot/fs/reiserfs/mode_string.c (revision ad5b5801)
1 /*
2  * mode_string implementation for busybox
3  *
4  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /* Aug 13, 2003
10  * Fix a bug reported by junkio@cox.net involving the mode_chars index.
11  */
12 
13 
14 #include <common.h>
15 #include <linux/stat.h>
16 
17 #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
18  || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
19  || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
20  || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
21 #error permission bitflag value assumption(s) violated!
22 #endif
23 
24 #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
25  || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
26  || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
27  || ( S_IFIFO != 0010000 )
28 #warning mode type bitflag value assumption(s) violated! falling back to larger version
29 
30 #if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
31 #undef mode_t
32 #define mode_t unsigned short
33 #endif
34 
35 static const mode_t mode_flags[] = {
36 	S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
37 	S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
38 	S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
39 };
40 
41 /* The static const char arrays below are duplicated for the two cases
42  * because moving them ahead of the mode_flags declaration cause a text
43  * size increase with the gcc version I'm using. */
44 
45 /* The previous version used "0pcCd?bB-?l?s???".  However, the '0', 'C',
46  * and 'B' types don't appear to be available on linux.  So I removed them. */
47 static const char type_chars[16] = "?pc?d?b?-?l?s???";
48 /*                                  0123456789abcdef */
49 static const char mode_chars[7] = "rwxSTst";
50 
51 const char *bb_mode_string(int mode)
52 {
53 	static char buf[12];
54 	char *p = buf;
55 
56 	int i, j, k;
57 
58 	*p = type_chars[ (mode >> 12) & 0xf ];
59 	i = 0;
60 	do {
61 		j = k = 0;
62 		do {
63 			*++p = '-';
64 			if (mode & mode_flags[i+j]) {
65 				*p = mode_chars[j];
66 				k = j;
67 			}
68 		} while (++j < 3);
69 		if (mode & mode_flags[i+j]) {
70 			*p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
71 		}
72 		i += 4;
73 	} while (i < 12);
74 
75 	/* Note: We don't bother with nul termination because bss initialization
76 	 * should have taken care of that for us.  If the user scribbled in buf
77 	 * memory, they deserve whatever happens.  But we'll at least assert. */
78 	if (buf[10] != 0) return NULL;
79 
80 	return buf;
81 }
82 
83 #else
84 
85 /* The previous version used "0pcCd?bB-?l?s???".  However, the '0', 'C',
86  * and 'B' types don't appear to be available on linux.  So I removed them. */
87 static const char type_chars[16] = "?pc?d?b?-?l?s???";
88 /*                                  0123456789abcdef */
89 static const char mode_chars[7] = "rwxSTst";
90 
91 const char *bb_mode_string(int mode)
92 {
93 	static char buf[12];
94 	char *p = buf;
95 
96 	int i, j, k, m;
97 
98 	*p = type_chars[ (mode >> 12) & 0xf ];
99 	i = 0;
100 	m = 0400;
101 	do {
102 		j = k = 0;
103 		do {
104 			*++p = '-';
105 			if (mode & m) {
106 				*p = mode_chars[j];
107 				k = j;
108 			}
109 			m >>= 1;
110 		} while (++j < 3);
111 		++i;
112 		if (mode & (010000 >> i)) {
113 			*p = mode_chars[3 + (k & 2) + (i == 3)];
114 		}
115 	} while (i < 3);
116 
117 	/* Note: We don't bother with nul termination because bss initialization
118 	 * should have taken care of that for us.  If the user scribbled in buf
119 	 * memory, they deserve whatever happens.  But we'll at least assert. */
120 	if (buf[10] != 0) return NULL;
121 
122 	return buf;
123 }
124 
125 #endif
126