mode_string.c 3.9 KB

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