mode_string.c 3.3 KB

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