blob: 52abe66f760b3ddee9211531fd528970e658a351 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009#include <assert.h>
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000010#include "libbb.h"
11
Manuel Novoa III cad53642003-03-19 09:13:01 +000012#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 Andersenaad1a882001-03-16 22:47:14 +000018
Denys Vlasenko6279aec2021-09-17 17:10:38 +020019/* Generate ls-style "mode string" like "-rwsr-xr-x" or "drwxrwxrwt" */
20
Manuel Novoa III cad53642003-03-19 09:13:01 +000021#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 Vlasenko6279aec2021-09-17 17:10:38 +020025# warning mode type bitflag value assumption(s) violated! falling back to larger version
Eric Andersenaad1a882001-03-16 22:47:14 +000026
Denys Vlasenko6279aec2021-09-17 17:10:38 +020027# 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 Andersenaad1a882001-03-16 22:47:14 +000031
Denys Vlasenko965b7952020-11-30 13:03:03 +010032static const mode_t mode_flags[] ALIGN4 = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 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 Andersenaad1a882001-03-16 22:47:14 +000036};
37
Manuel Novoa III cad53642003-03-19 09:13:01 +000038/* 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 Vlasenko6ca409e2007-08-12 20:58:27 +000040static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
Denis Vlasenko0e525412008-07-11 13:57:08 +000041/***************************************** 0123456789abcdef */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000042static const char mode_chars[7] ALIGN1 = "rwxSTst";
Eric Andersenaad1a882001-03-16 22:47:14 +000043
Denys Vlasenko6279aec2021-09-17 17:10:38 +020044char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000045{
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 char *p = buf;
Eric Andersenaad1a882001-03-16 22:47:14 +000047
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 int i, j, k;
Eric Andersenaad1a882001-03-16 22:47:14 +000049
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 *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 Yorston3a654352021-09-17 10:16:06 +010067 buf[10] = '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +000068
Eric Andersenaad1a882001-03-16 22:47:14 +000069 return buf;
70}
71
Manuel Novoa III cad53642003-03-19 09:13:01 +000072#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 Vlasenko3e134eb2016-04-22 18:09:21 +020076static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
Ron Yorston3a654352021-09-17 10:16:06 +010077/***************************************** 0123456789abcdef */
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020078static const char mode_chars[7] ALIGN1 = "rwxSTst";
Manuel Novoa III cad53642003-03-19 09:13:01 +000079
Denys Vlasenko6279aec2021-09-17 17:10:38 +020080char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode)
Manuel Novoa III cad53642003-03-19 09:13:01 +000081{
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 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 062913f2003-08-14 02:28:49 +0000101 *p = mode_chars[3 + (k & 2) + (i == 3)];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 }
103 } while (i < 3);
104
Ron Yorston3a654352021-09-17 10:16:06 +0100105 buf[10] = '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106
107 return buf;
108}
109
110#endif