mode_string.c 3.3 KB

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