decode.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** decode.c - libtar code to decode tar header blocks
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #if defined(_WIN32) && !defined(__CYGWIN__)
  15. # include <libtar/compat.h>
  16. #else
  17. # include <sys/param.h>
  18. #endif
  19. #ifndef WIN32
  20. #include <pwd.h>
  21. #include <grp.h>
  22. #endif
  23. #ifdef STDC_HEADERS
  24. # include <string.h>
  25. #endif
  26. /* determine full path name */
  27. /* caller must "free" returned pointer when done with it */
  28. /* th_get_pathname return values come directly from strdup */
  29. char *
  30. th_get_pathname(TAR *t)
  31. {
  32. char filename[TAR_MAXPATHLEN];
  33. if (t->th_buf.gnu_longname)
  34. return strdup(t->th_buf.gnu_longname);
  35. if (t->th_buf.prefix[0] != '\0')
  36. {
  37. snprintf(filename, sizeof(filename), "%.155s/%.100s",
  38. t->th_buf.prefix, t->th_buf.name);
  39. return strdup(filename);
  40. }
  41. snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
  42. return strdup(filename);
  43. }
  44. uid_t
  45. th_get_uid(TAR *t)
  46. {
  47. int uid;
  48. #ifndef WIN32
  49. struct passwd *pw;
  50. pw = getpwnam(t->th_buf.uname);
  51. if (pw != NULL)
  52. return pw->pw_uid;
  53. /* if the password entry doesn't exist */
  54. #endif
  55. sscanf(t->th_buf.uid, "%o", &uid);
  56. return uid;
  57. }
  58. gid_t
  59. th_get_gid(TAR *t)
  60. {
  61. int gid;
  62. #ifndef WIN32
  63. struct group *gr;
  64. gr = getgrnam(t->th_buf.gname);
  65. if (gr != NULL)
  66. return gr->gr_gid;
  67. /* if the group entry doesn't exist */
  68. #endif
  69. sscanf(t->th_buf.gid, "%o", &gid);
  70. return gid;
  71. }
  72. mode_t
  73. th_get_mode(TAR *t)
  74. {
  75. mode_t mode;
  76. mode = (mode_t)oct_to_int(t->th_buf.mode);
  77. if (! (mode & S_IFMT))
  78. {
  79. switch (t->th_buf.typeflag)
  80. {
  81. #ifndef WIN32
  82. case SYMTYPE:
  83. mode |= S_IFLNK;
  84. break;
  85. #endif
  86. case CHRTYPE:
  87. mode |= S_IFCHR;
  88. break;
  89. case BLKTYPE:
  90. mode |= S_IFBLK;
  91. break;
  92. case DIRTYPE:
  93. mode |= S_IFDIR;
  94. break;
  95. #ifndef WIN32
  96. case FIFOTYPE:
  97. mode |= S_IFIFO;
  98. break;
  99. #endif
  100. case AREGTYPE:
  101. if (t->th_buf.name[strlen(t->th_buf.name) - 1] == '/')
  102. {
  103. mode |= S_IFDIR;
  104. break;
  105. }
  106. /* FALLTHROUGH */
  107. case LNKTYPE:
  108. case REGTYPE:
  109. default:
  110. mode |= S_IFREG;
  111. }
  112. }
  113. return mode;
  114. }