mode_string.c 3.4 KB

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