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