decode.c 2.2 KB

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