3
0

mode_string.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include <assert.h>
  10. #include "libbb.h"
  11. #if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
  12. || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
  13. || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
  14. || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
  15. #error permission bitflag value assumption(s) violated!
  16. #endif
  17. /* Generate ls-style "mode string" like "-rwsr-xr-x" or "drwxrwxrwt" */
  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[] ALIGN4 = {
  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 previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  33. * and 'B' types don't appear to be available on linux. So I removed them. */
  34. static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
  35. /***************************************** 0123456789abcdef */
  36. static const char mode_chars[7] ALIGN1 = "rwxSTst";
  37. char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode)
  38. {
  39. char *p = buf;
  40. int i, j, k;
  41. *p = type_chars[ (mode >> 12) & 0xf ];
  42. i = 0;
  43. do {
  44. j = k = 0;
  45. do {
  46. *++p = '-';
  47. if (mode & mode_flags[i+j]) {
  48. *p = mode_chars[j];
  49. k = j;
  50. }
  51. } while (++j < 3);
  52. if (mode & mode_flags[i+j]) {
  53. *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
  54. }
  55. i += 4;
  56. } while (i < 12);
  57. buf[10] = '\0';
  58. return buf;
  59. }
  60. #else
  61. /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
  62. * and 'B' types don't appear to be available on linux. So I removed them. */
  63. static const char type_chars[16] ALIGN1 = "?pc?d?b?-?l?s???";
  64. /***************************************** 0123456789abcdef */
  65. static const char mode_chars[7] ALIGN1 = "rwxSTst";
  66. char* FAST_FUNC bb_mode_string(char buf[11], mode_t mode)
  67. {
  68. char *p = buf;
  69. int i, j, k, m;
  70. *p = type_chars[ (mode >> 12) & 0xf ];
  71. i = 0;
  72. m = 0400;
  73. do {
  74. j = k = 0;
  75. do {
  76. *++p = '-';
  77. if (mode & m) {
  78. *p = mode_chars[j];
  79. k = j;
  80. }
  81. m >>= 1;
  82. } while (++j < 3);
  83. ++i;
  84. if (mode & (010000 >> i)) {
  85. *p = mode_chars[3 + (k & 2) + (i == 3)];
  86. }
  87. } while (i < 3);
  88. buf[10] = '\0';
  89. return buf;
  90. }
  91. #endif