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