mode_string.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* Aug 13, 2003
  23. * Fix a bug reported by junkio@cox.net involving the mode_chars index.
  24. */
  25. #include <assert.h>
  26. #include <sys/stat.h>
  27. #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
  28. || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
  29. || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
  30. || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
  31. #error permission bitflag value assumption(s) violated!
  32. #endif
  33. #if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
  34. || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
  35. || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
  36. || ( S_IFIFO != 0010000 )
  37. #warning mode type bitflag value assumption(s) violated! falling back to larger version
  38. #if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
  39. #undef mode_t
  40. #define mode_t unsigned short
  41. #endif
  42. static const mode_t mode_flags[] = {
  43. S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
  44. S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
  45. S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
  46. };
  47. /* The static const char arrays below are duplicated for the two cases
  48. * because moving them ahead of the mode_flags declaration cause a text
  49. * size increase with the gcc version I'm using. */
  50. /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  51. * and 'B' types don't appear to be available on linux. So I removed them. */
  52. static const char type_chars[16] = "?pc?d?b?-?l?s???";
  53. /* 0123456789abcdef */
  54. static const char mode_chars[7] = "rwxSTst";
  55. const char *bb_mode_string(int mode)
  56. {
  57. static char buf[12];
  58. char *p = buf;
  59. int i, j, k;
  60. *p = type_chars[ (mode >> 12) & 0xf ];
  61. i = 0;
  62. do {
  63. j = k = 0;
  64. do {
  65. *++p = '-';
  66. if (mode & mode_flags[i+j]) {
  67. *p = mode_chars[j];
  68. k = j;
  69. }
  70. } while (++j < 3);
  71. if (mode & mode_flags[i+j]) {
  72. *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
  73. }
  74. i += 4;
  75. } while (i < 12);
  76. /* Note: We don't bother with nul termination because bss initialization
  77. * should have taken care of that for us. If the user scribbled in buf
  78. * memory, they deserve whatever happens. But we'll at least assert. */
  79. assert(buf[10] == 0);
  80. return buf;
  81. }
  82. #else
  83. /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  84. * and 'B' types don't appear to be available on linux. So I removed them. */
  85. static const char type_chars[16] = "?pc?d?b?-?l?s???";
  86. /* 0123456789abcdef */
  87. static const char mode_chars[7] = "rwxSTst";
  88. const char *bb_mode_string(int mode)
  89. {
  90. static char buf[12];
  91. char *p = buf;
  92. int i, j, k, m;
  93. *p = type_chars[ (mode >> 12) & 0xf ];
  94. i = 0;
  95. m = 0400;
  96. do {
  97. j = k = 0;
  98. do {
  99. *++p = '-';
  100. if (mode & m) {
  101. *p = mode_chars[j];
  102. k = j;
  103. }
  104. m >>= 1;
  105. } while (++j < 3);
  106. ++i;
  107. if (mode & (010000 >> i)) {
  108. *p = mode_chars[3 + (k & 2) + (i == 3)];
  109. }
  110. } while (i < 3);
  111. /* Note: We don't bother with nul termination because bss initialization
  112. * should have taken care of that for us. If the user scribbled in buf
  113. * memory, they deserve whatever happens. But we'll at least assert. */
  114. assert(buf[10] == 0);
  115. return buf;
  116. }
  117. #endif