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