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