aboutsummaryrefslogtreecommitdiff
path: root/libbb/mode_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/mode_string.c')
-rw-r--r--libbb/mode_string.c150
1 files changed, 103 insertions, 47 deletions
diff --git a/libbb/mode_string.c b/libbb/mode_string.c
index 12dc17966..8d4d736ef 100644
--- a/libbb/mode_string.c
+++ b/libbb/mode_string.c
@@ -1,78 +1,134 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * mode_string implementation for busybox
4 * 4 *
5 * Copyright (C) many different people. If you wrote this, please 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * acknowledge your work.
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 10 * (at your option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 15 * General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * USA 20 *
22 */ 21 */
23 22
24#include <stdio.h> 23#include <assert.h>
25#include "libbb.h" 24#include <sys/stat.h>
26 25
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
27 32
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
28 38
29#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) 39#if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
30#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) 40#undef mode_t
41#define mode_t unsigned short
42#endif
31 43
32/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ 44static const mode_t mode_flags[] = {
33static const mode_t SBIT[] = { 45 S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
34 0, 0, S_ISUID, 46 S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
35 0, 0, S_ISGID, 47 S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
36 0, 0, S_ISVTX
37}; 48};
38 49
39/* The 9 mode bits to test */ 50/* The static const char arrays below are duplicated for the two cases
40static const mode_t MBIT[] = { 51 * because moving them ahead of the mode_flags declaration cause a text
41 S_IRUSR, S_IWUSR, S_IXUSR, 52 * size increase with the gcc version I'm using. */
42 S_IRGRP, S_IWGRP, S_IXGRP,
43 S_IROTH, S_IWOTH, S_IXOTH
44};
45 53
46static const char MODE1[] = "rwxrwxrwx"; 54/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
47static const char MODE0[] = "---------"; 55 * and 'B' types don't appear to be available on linux. So I removed them. */
48static const char SMODE1[] = "..s..s..t"; 56static const char type_chars[16] = "?pc?d?b?-?l?s???";
49static const char SMODE0[] = "..S..S..T"; 57/* 0123456789abcdef */
58static const char mode_chars[7] = "rwxSTst";
50 59
51/* 60const char *bb_mode_string(int mode)
52 * Return the standard ls-like mode string from a file mode.
53 * This is static and so is overwritten on each call.
54 */
55const char *mode_string(int mode)
56{ 61{
57 static char buf[12]; 62 static char buf[12];
63 char *p = buf;
64
65 int i, j, k;
58 66
59 int i; 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);
60 88
61 buf[0] = TYPECHAR(mode);
62 for (i = 0; i < 9; i++) {
63 if (mode & SBIT[i])
64 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
65 else
66 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
67 }
68 return buf; 89 return buf;
69} 90}
70 91
71/* END CODE */ 92#else
72/* 93
73Local Variables: 94/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
74c-file-style: "linux" 95 * and 'B' types don't appear to be available on linux. So I removed them. */
75c-basic-offset: 4 96static const char type_chars[16] = "?pc?d?b?-?l?s???";
76tab-width: 4 97/* 0123456789abcdef */
77End: 98static const char mode_chars[7] = "rwxSTst";
78*/ 99
100const 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