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