blob: 8d4d736ef83c037a1ea857cd7658a12594c6eb96 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * mode_string implementation for busybox
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
Manuel Novoa III cad53642003-03-19 09:13:01 +000012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Eric Andersenaad1a882001-03-16 22:47:14 +000014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
Eric Andersenaad1a882001-03-16 22:47:14 +000021 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023#include <assert.h>
24#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000025
Manuel Novoa III cad53642003-03-19 09:13:01 +000026#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
Eric Andersenaad1a882001-03-16 22:47:14 +000032
Manuel Novoa III cad53642003-03-19 09:13:01 +000033#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
Eric Andersenaad1a882001-03-16 22:47:14 +000038
Manuel Novoa III cad53642003-03-19 09:13:01 +000039#if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
40#undef mode_t
41#define mode_t unsigned short
42#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000043
Manuel Novoa III cad53642003-03-19 09:13:01 +000044static const mode_t mode_flags[] = {
45 S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
46 S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
47 S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
Eric Andersenaad1a882001-03-16 22:47:14 +000048};
49
Manuel Novoa III cad53642003-03-19 09:13:01 +000050/* The static const char arrays below are duplicated for the two cases
51 * because moving them ahead of the mode_flags declaration cause a text
52 * size increase with the gcc version I'm using. */
Eric Andersenaad1a882001-03-16 22:47:14 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
55 * and 'B' types don't appear to be available on linux. So I removed them. */
56static const char type_chars[16] = "?pc?d?b?-?l?s???";
57/* 0123456789abcdef */
58static const char mode_chars[7] = "rwxSTst";
Eric Andersenaad1a882001-03-16 22:47:14 +000059
Manuel Novoa III cad53642003-03-19 09:13:01 +000060const char *bb_mode_string(int mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000061{
62 static char buf[12];
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 char *p = buf;
Eric Andersenaad1a882001-03-16 22:47:14 +000064
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 int i, j, k;
Eric Andersenaad1a882001-03-16 22:47:14 +000066
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 *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);
88
Eric Andersenaad1a882001-03-16 22:47:14 +000089 return buf;
90}
91
Manuel Novoa III cad53642003-03-19 09:13:01 +000092#else
93
94/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
95 * and 'B' types don't appear to be available on linux. So I removed them. */
96static const char type_chars[16] = "?pc?d?b?-?l?s???";
97/* 0123456789abcdef */
98static const char mode_chars[7] = "rwxSTst";
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