permstring.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * A single utility routine.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2001 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2003, 2006 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. #include "rsync.h"
  23. /* Produce a string representation of Unix mode bits like that used by ls(1).
  24. * The "buf" buffer must be at least 11 characters. */
  25. void permstring(char *perms, mode_t mode)
  26. {
  27. static const char *perm_map = "rwxrwxrwx";
  28. int i;
  29. strlcpy(perms, "----------", 11);
  30. for (i = 0; i < 9; i++) {
  31. if (mode & (1 << i))
  32. perms[9-i] = perm_map[8-i];
  33. }
  34. /* Handle setuid/sticky bits. You might think the indices are
  35. * off by one, but remember there's a type char at the
  36. * start. */
  37. if (mode & S_ISUID)
  38. perms[3] = (mode & S_IXUSR) ? 's' : 'S';
  39. if (mode & S_ISGID)
  40. perms[6] = (mode & S_IXGRP) ? 's' : 'S';
  41. #ifdef S_ISVTX
  42. if (mode & S_ISVTX)
  43. perms[9] = (mode & S_IXOTH) ? 't' : 'T';
  44. #endif
  45. if (S_ISDIR(mode))
  46. perms[0] = 'd';
  47. else if (S_ISLNK(mode))
  48. perms[0] = 'l';
  49. else if (S_ISBLK(mode))
  50. perms[0] = 'b';
  51. else if (S_ISCHR(mode))
  52. perms[0] = 'c';
  53. else if (S_ISSOCK(mode))
  54. perms[0] = 's';
  55. else if (S_ISFIFO(mode))
  56. perms[0] = 'p';
  57. }