2001-03-17 06:47:14 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2003-03-19 17:13:01 +08:00
|
|
|
* mode_string implementation for busybox
|
2001-03-17 06:47:14 +08:00
|
|
|
*
|
2003-03-19 17:13:01 +08:00
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
2001-03-17 06:47:14 +08:00
|
|
|
*
|
2006-04-04 00:39:31 +08:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 06:47:14 +08:00
|
|
|
*/
|
|
|
|
|
2003-08-14 10:28:49 +08:00
|
|
|
/* Aug 13, 2003
|
|
|
|
* Fix a bug reported by junkio@cox.net involving the mode_chars index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/stat.h>
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2006-04-04 00:39:31 +08:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
#if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
|
|
|
|
|| ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
|
|
|
|
|| ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
|
|
|
|
|| ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
|
|
|
|
#error permission bitflag value assumption(s) violated!
|
|
|
|
#endif
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
#if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
|
|
|
|
|| ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
|
|
|
|
|| ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
|
|
|
|
|| ( S_IFIFO != 0010000 )
|
|
|
|
#warning mode type bitflag value assumption(s) violated! falling back to larger version
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
#if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
|
|
|
|
#undef mode_t
|
|
|
|
#define mode_t unsigned short
|
|
|
|
#endif
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
static const mode_t mode_flags[] = {
|
|
|
|
S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
|
|
|
|
S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
|
|
|
|
S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
|
2001-03-17 06:47:14 +08:00
|
|
|
};
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
/* The static const char arrays below are duplicated for the two cases
|
|
|
|
* because moving them ahead of the mode_flags declaration cause a text
|
|
|
|
* size increase with the gcc version I'm using. */
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
|
|
|
|
* and 'B' types don't appear to be available on linux. So I removed them. */
|
2007-08-13 04:58:27 +08:00
|
|
|
static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
|
2008-07-11 21:57:08 +08:00
|
|
|
/***************************************** 0123456789abcdef */
|
2007-08-13 04:58:27 +08:00
|
|
|
static const char mode_chars[7] ALIGN1 = "rwxSTst";
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2008-06-27 10:52:20 +08:00
|
|
|
const char* FAST_FUNC bb_mode_string(mode_t mode)
|
2001-03-17 06:47:14 +08:00
|
|
|
{
|
|
|
|
static char buf[12];
|
2003-03-19 17:13:01 +08:00
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
int i, j, k;
|
2001-03-17 06:47:14 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
*p = type_chars[ (mode >> 12) & 0xf ];
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
j = k = 0;
|
|
|
|
do {
|
|
|
|
*++p = '-';
|
|
|
|
if (mode & mode_flags[i+j]) {
|
|
|
|
*p = mode_chars[j];
|
|
|
|
k = j;
|
|
|
|
}
|
|
|
|
} while (++j < 3);
|
|
|
|
if (mode & mode_flags[i+j]) {
|
|
|
|
*p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
|
|
|
|
}
|
|
|
|
i += 4;
|
|
|
|
} while (i < 12);
|
|
|
|
|
|
|
|
/* Note: We don't bother with nul termination because bss initialization
|
|
|
|
* should have taken care of that for us. If the user scribbled in buf
|
|
|
|
* memory, they deserve whatever happens. But we'll at least assert. */
|
|
|
|
assert(buf[10] == 0);
|
2001-03-17 06:47:14 +08:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
|
|
|
|
* and 'B' types don't appear to be available on linux. So I removed them. */
|
|
|
|
static const char type_chars[16] = "?pc?d?b?-?l?s???";
|
2008-07-11 21:57:08 +08:00
|
|
|
/********************************** 0123456789abcdef */
|
2003-03-19 17:13:01 +08:00
|
|
|
static const char mode_chars[7] = "rwxSTst";
|
|
|
|
|
2008-06-27 10:52:20 +08:00
|
|
|
const char* FAST_FUNC bb_mode_string(mode_t mode)
|
2003-03-19 17:13:01 +08:00
|
|
|
{
|
|
|
|
static char buf[12];
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
int i, j, k, m;
|
|
|
|
|
|
|
|
*p = type_chars[ (mode >> 12) & 0xf ];
|
|
|
|
i = 0;
|
|
|
|
m = 0400;
|
|
|
|
do {
|
|
|
|
j = k = 0;
|
|
|
|
do {
|
|
|
|
*++p = '-';
|
|
|
|
if (mode & m) {
|
|
|
|
*p = mode_chars[j];
|
|
|
|
k = j;
|
|
|
|
}
|
|
|
|
m >>= 1;
|
|
|
|
} while (++j < 3);
|
|
|
|
++i;
|
|
|
|
if (mode & (010000 >> i)) {
|
2003-08-14 10:28:49 +08:00
|
|
|
*p = mode_chars[3 + (k & 2) + (i == 3)];
|
2003-03-19 17:13:01 +08:00
|
|
|
}
|
|
|
|
} while (i < 3);
|
|
|
|
|
|
|
|
/* Note: We don't bother with nul termination because bss initialization
|
|
|
|
* should have taken care of that for us. If the user scribbled in buf
|
|
|
|
* memory, they deserve whatever happens. But we'll at least assert. */
|
|
|
|
assert(buf[10] == 0);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|