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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 9 | #include <assert.h> |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \ |
| 13 | || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \ |
| 14 | || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \ |
| 15 | || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 ) |
| 16 | #error permission bitflag value assumption(s) violated! |
| 17 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | |
Denys Vlasenko | 6279aec | 2021-09-17 17:10:38 +0200 | [diff] [blame] | 19 | /* Generate ls-style "mode string" like "-rwsr-xr-x" or "drwxrwxrwt" */ |
| 20 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \ |
| 22 | || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \ |
| 23 | || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \ |
| 24 | || ( S_IFIFO != 0010000 ) |
Denys Vlasenko | 6279aec | 2021-09-17 17:10:38 +0200 | [diff] [blame] | 25 | # 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] | 26 | |
Denys Vlasenko | 6279aec | 2021-09-17 17:10:38 +0200 | [diff] [blame] | 27 | # if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777 |
| 28 | # undef mode_t |
| 29 | # define mode_t unsigned short |
| 30 | # endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 32 | static const mode_t mode_flags[] ALIGN4 = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID, |
| 34 | S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID, |
| 35 | S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C', |
| 39 | * 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] | 40 | static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???"; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 41 | /***************************************** 0123456789abcdef */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 42 | static const char mode_chars[7] ALIGN1 = "rwxSTst"; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | 6279aec | 2021-09-17 17:10:38 +0200 | [diff] [blame] | 44 | char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | char *p = buf; |
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 | int i, j, k; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 49 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | *p = type_chars[ (mode >> 12) & 0xf ]; |
| 51 | i = 0; |
| 52 | do { |
| 53 | j = k = 0; |
| 54 | do { |
| 55 | *++p = '-'; |
| 56 | if (mode & mode_flags[i+j]) { |
| 57 | *p = mode_chars[j]; |
| 58 | k = j; |
| 59 | } |
| 60 | } while (++j < 3); |
| 61 | if (mode & mode_flags[i+j]) { |
| 62 | *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)]; |
| 63 | } |
| 64 | i += 4; |
| 65 | } while (i < 12); |
| 66 | |
Ron Yorston | 3a65435 | 2021-09-17 10:16:06 +0100 | [diff] [blame] | 67 | buf[10] = '\0'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 69 | return buf; |
| 70 | } |
| 71 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | #else |
| 73 | |
| 74 | /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C', |
| 75 | * and 'B' types don't appear to be available on linux. So I removed them. */ |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 76 | static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???"; |
Ron Yorston | 3a65435 | 2021-09-17 10:16:06 +0100 | [diff] [blame] | 77 | /***************************************** 0123456789abcdef */ |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 78 | static const char mode_chars[7] ALIGN1 = "rwxSTst"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | |
Denys Vlasenko | 6279aec | 2021-09-17 17:10:38 +0200 | [diff] [blame] | 80 | char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | char *p = buf; |
| 83 | |
| 84 | int i, j, k, m; |
| 85 | |
| 86 | *p = type_chars[ (mode >> 12) & 0xf ]; |
| 87 | i = 0; |
| 88 | m = 0400; |
| 89 | do { |
| 90 | j = k = 0; |
| 91 | do { |
| 92 | *++p = '-'; |
| 93 | if (mode & m) { |
| 94 | *p = mode_chars[j]; |
| 95 | k = j; |
| 96 | } |
| 97 | m >>= 1; |
| 98 | } while (++j < 3); |
| 99 | ++i; |
| 100 | if (mode & (010000 >> i)) { |
Manuel Novoa III | 062913f | 2003-08-14 02:28:49 +0000 | [diff] [blame] | 101 | *p = mode_chars[3 + (k & 2) + (i == 3)]; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | } |
| 103 | } while (i < 3); |
| 104 | |
Ron Yorston | 3a65435 | 2021-09-17 10:16:06 +0100 | [diff] [blame] | 105 | buf[10] = '\0'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | |
| 107 | return buf; |
| 108 | } |
| 109 | |
| 110 | #endif |