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