decode.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. # ifdef HAVE_SYS_PARAM_H
  18. # include <sys/param.h>
  19. # endif
  20. #endif
  21. #ifndef WIN32
  22. #include <pwd.h>
  23. #include <grp.h>
  24. #endif
  25. #ifdef STDC_HEADERS
  26. # include <string.h>
  27. #endif
  28. /* determine full path name */
  29. /* caller must "free" returned pointer when done with it */
  30. /* th_get_pathname return values come directly from strdup */
  31. char *
  32. th_get_pathname(TAR *t)
  33. {
  34. char filename[TAR_MAXPATHLEN];
  35. if (t->th_buf.gnu_longname)
  36. return strdup(t->th_buf.gnu_longname);
  37. if (t->th_buf.prefix[0] != '\0')
  38. {
  39. snprintf(filename, sizeof(filename), "%.155s/%.100s",
  40. t->th_buf.prefix, t->th_buf.name);
  41. return strdup(filename);
  42. }
  43. snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
  44. return strdup(filename);
  45. }
  46. uid_t
  47. th_get_uid(TAR *t)
  48. {
  49. int uid;
  50. #ifndef WIN32
  51. struct passwd *pw;
  52. pw = getpwnam(t->th_buf.uname);
  53. if (pw != NULL)
  54. return pw->pw_uid;
  55. /* if the password entry doesn't exist */
  56. #endif
  57. sscanf(t->th_buf.uid, "%o", &uid);
  58. return uid;
  59. }
  60. gid_t
  61. th_get_gid(TAR *t)
  62. {
  63. int gid;
  64. #ifndef WIN32
  65. struct group *gr;
  66. gr = getgrnam(t->th_buf.gname);
  67. if (gr != NULL)
  68. return gr->gr_gid;
  69. /* if the group entry doesn't exist */
  70. #endif
  71. sscanf(t->th_buf.gid, "%o", &gid);
  72. return gid;
  73. }
  74. mode_t
  75. th_get_mode(TAR *t)
  76. {
  77. mode_t mode;
  78. mode = (mode_t)oct_to_int(t->th_buf.mode);
  79. if (! (mode & S_IFMT))
  80. {
  81. switch (t->th_buf.typeflag)
  82. {
  83. #ifndef WIN32
  84. case SYMTYPE:
  85. mode |= S_IFLNK;
  86. break;
  87. #endif
  88. case CHRTYPE:
  89. mode |= S_IFCHR;
  90. break;
  91. case BLKTYPE:
  92. mode |= S_IFBLK;
  93. break;
  94. case DIRTYPE:
  95. mode |= S_IFDIR;
  96. break;
  97. #ifndef WIN32
  98. case FIFOTYPE:
  99. mode |= S_IFIFO;
  100. break;
  101. #endif
  102. case AREGTYPE:
  103. if (t->th_buf.name[strlen(t->th_buf.name) - 1] == '/')
  104. {
  105. mode |= S_IFDIR;
  106. break;
  107. }
  108. /* FALLTHROUGH */
  109. case LNKTYPE:
  110. case REGTYPE:
  111. default:
  112. mode |= S_IFREG;
  113. }
  114. }
  115. return mode;
  116. }