archive_entry_strmode.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry_strmode.c,v 1.4 2008/06/15 05:14:01 kientzle Exp $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_STRING_H
  31. #include <string.h>
  32. #endif
  33. #include "archive_entry.h"
  34. #include "archive_entry_private.h"
  35. const char *
  36. archive_entry_strmode(struct archive_entry *entry)
  37. {
  38. static const mode_t permbits[] =
  39. { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 };
  40. char *bp = entry->strmode;
  41. mode_t mode;
  42. int i;
  43. /* Fill in a default string, then selectively override. */
  44. strcpy(bp, "?rwxrwxrwx ");
  45. mode = archive_entry_mode(entry);
  46. switch (archive_entry_filetype(entry)) {
  47. case AE_IFREG: bp[0] = '-'; break;
  48. case AE_IFBLK: bp[0] = 'b'; break;
  49. case AE_IFCHR: bp[0] = 'c'; break;
  50. case AE_IFDIR: bp[0] = 'd'; break;
  51. case AE_IFLNK: bp[0] = 'l'; break;
  52. case AE_IFSOCK: bp[0] = 's'; break;
  53. case AE_IFIFO: bp[0] = 'p'; break;
  54. default:
  55. if (archive_entry_hardlink(entry) != NULL) {
  56. bp[0] = 'h';
  57. break;
  58. }
  59. }
  60. for (i = 0; i < 9; i++)
  61. if (!(mode & permbits[i]))
  62. bp[i+1] = '-';
  63. if (mode & S_ISUID) {
  64. if (mode & 0100) bp[3] = 's';
  65. else bp[3] = 'S';
  66. }
  67. if (mode & S_ISGID) {
  68. if (mode & 0010) bp[6] = 's';
  69. else bp[6] = 'S';
  70. }
  71. if (mode & S_ISVTX) {
  72. if (mode & 0001) bp[9] = 't';
  73. else bp[9] = 'T';
  74. }
  75. if (archive_entry_acl_count(entry, ARCHIVE_ENTRY_ACL_TYPE_ACCESS))
  76. bp[10] = '+';
  77. return (bp);
  78. }