mode_string.c 4.0 KB

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