mode_string.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mode_string implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include <assert.h>
  10. #include "libbb.h"
  11. #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
  12. || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
  13. || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
  14. || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
  15. #error permission bitflag value assumption(s) violated!
  16. #endif
  17. #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
  18. || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
  19. || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
  20. || ( S_IFIFO != 0010000 )
  21. #warning mode type bitflag value assumption(s) violated! falling back to larger version
  22. #if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
  23. #undef mode_t
  24. #define mode_t unsigned short
  25. #endif
  26. static const mode_t mode_flags[] ALIGN4 = {
  27. S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
  28. S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
  29. S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
  30. };
  31. /* The static const char arrays below are duplicated for the two cases
  32. * because moving them ahead of the mode_flags declaration cause a text
  33. * size increase with the gcc version I'm using. */
  34. /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  35. * and 'B' types don't appear to be available on linux. So I removed them. */
  36. static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
  37. /***************************************** 0123456789abcdef */
  38. static const char mode_chars[7] ALIGN1 = "rwxSTst";
  39. const char* FAST_FUNC bb_mode_string(mode_t mode)
  40. {
  41. static char buf[12];
  42. char *p = buf;
  43. int i, j, k;
  44. *p = type_chars[ (mode >> 12) & 0xf ];
  45. i = 0;
  46. do {
  47. j = k = 0;
  48. do {
  49. *++p = '-';
  50. if (mode & mode_flags[i+j]) {
  51. *p = mode_chars[j];
  52. k = j;
  53. }
  54. } while (++j < 3);
  55. if (mode & mode_flags[i+j]) {
  56. *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
  57. }
  58. i += 4;
  59. } while (i < 12);
  60. /* Note: We don't bother with nul termination because bss initialization
  61. * should have taken care of that for us. If the user scribbled in buf
  62. * memory, they deserve whatever happens. But we'll at least assert. */
  63. assert(buf[10] == 0);
  64. return buf;
  65. }
  66. #else
  67. /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  68. * and 'B' types don't appear to be available on linux. So I removed them. */
  69. static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
  70. /********************************** 0123456789abcdef */
  71. static const char mode_chars[7] ALIGN1 = "rwxSTst";
  72. const char* FAST_FUNC bb_mode_string(mode_t mode)
  73. {
  74. static char buf[12];
  75. char *p = buf;
  76. int i, j, k, m;
  77. *p = type_chars[ (mode >> 12) & 0xf ];
  78. i = 0;
  79. m = 0400;
  80. do {
  81. j = k = 0;
  82. do {
  83. *++p = '-';
  84. if (mode & m) {
  85. *p = mode_chars[j];
  86. k = j;
  87. }
  88. m >>= 1;
  89. } while (++j < 3);
  90. ++i;
  91. if (mode & (010000 >> i)) {
  92. *p = mode_chars[3 + (k & 2) + (i == 3)];
  93. }
  94. } while (i < 3);
  95. /* Note: We don't bother with nul termination because bss initialization
  96. * should have taken care of that for us. If the user scribbled in buf
  97. * memory, they deserve whatever happens. But we'll at least assert. */
  98. assert(buf[10] == 0);
  99. return buf;
  100. }
  101. #endif