1 // SPDX-License-Identifier: GPL-2.0+
3 * mode_string implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
9 * Fix a bug reported by junkio@cox.net involving the mode_chars index.
14 #include <linux/stat.h>
16 #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
17 || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
18 || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
19 || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
20 #error permission bitflag value assumption(s) violated!
23 #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
24 || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
25 || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
26 || ( S_IFIFO != 0010000 )
27 #warning mode type bitflag value assumption(s) violated! falling back to larger version
29 #if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
31 #define mode_t unsigned short
34 static const mode_t mode_flags[] = {
35 S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
36 S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
37 S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
40 /* The static const char arrays below are duplicated for the two cases
41 * because moving them ahead of the mode_flags declaration cause a text
42 * size increase with the gcc version I'm using. */
44 /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
45 * and 'B' types don't appear to be available on linux. So I removed them. */
46 static const char type_chars[16] = "?pc?d?b?-?l?s???";
47 /* 0123456789abcdef */
48 static const char mode_chars[7] = "rwxSTst";
50 const char *bb_mode_string(int mode)
57 *p = type_chars[ (mode >> 12) & 0xf ];
63 if (mode & mode_flags[i+j]) {
68 if (mode & mode_flags[i+j]) {
69 *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
74 /* Note: We don't bother with nul termination because bss initialization
75 * should have taken care of that for us. If the user scribbled in buf
76 * memory, they deserve whatever happens. But we'll at least assert. */
77 if (buf[10] != 0) return NULL;
84 /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
85 * and 'B' types don't appear to be available on linux. So I removed them. */
86 static const char type_chars[16] = "?pc?d?b?-?l?s???";
87 /* 0123456789abcdef */
88 static const char mode_chars[7] = "rwxSTst";
90 const char *bb_mode_string(int mode)
97 *p = type_chars[ (mode >> 12) & 0xf ];
111 if (mode & (010000 >> i)) {
112 *p = mode_chars[3 + (k & 2) + (i == 3)];
116 /* Note: We don't bother with nul termination because bss initialization
117 * should have taken care of that for us. If the user scribbled in buf
118 * memory, they deserve whatever happens. But we'll at least assert. */
119 if (buf[10] != 0) return NULL;