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