output.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** output.c - libtar code to print out 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 WIN32
  15. #include <pwd.h>
  16. #include <grp.h>
  17. #endif
  18. #include <time.h>
  19. #include <limits.h>
  20. //#include <sys/param.h>
  21. #ifdef STDC_HEADERS
  22. # include <string.h>
  23. #endif
  24. #ifndef _POSIX_LOGIN_NAME_MAX
  25. # define _POSIX_LOGIN_NAME_MAX 9
  26. #endif
  27. void
  28. th_print(TAR *t)
  29. {
  30. puts("\nPrinting tar header:");
  31. printf(" name = \"%.100s\"\n", t->th_buf.name);
  32. printf(" mode = \"%.8s\"\n", t->th_buf.mode);
  33. printf(" uid = \"%.8s\"\n", t->th_buf.uid);
  34. printf(" gid = \"%.8s\"\n", t->th_buf.gid);
  35. printf(" size = \"%.12s\"\n", t->th_buf.size);
  36. printf(" mtime = \"%.12s\"\n", t->th_buf.mtime);
  37. printf(" chksum = \"%.8s\"\n", t->th_buf.chksum);
  38. printf(" typeflag = \'%c\'\n", t->th_buf.typeflag);
  39. printf(" linkname = \"%.100s\"\n", t->th_buf.linkname);
  40. printf(" magic = \"%.6s\"\n", t->th_buf.magic);
  41. /*printf(" version = \"%.2s\"\n", t->th_buf.version); */
  42. printf(" version[0] = \'%c\',version[1] = \'%c\'\n",
  43. t->th_buf.version[0], t->th_buf.version[1]);
  44. printf(" uname = \"%.32s\"\n", t->th_buf.uname);
  45. printf(" gname = \"%.32s\"\n", t->th_buf.gname);
  46. printf(" devmajor = \"%.8s\"\n", t->th_buf.devmajor);
  47. printf(" devminor = \"%.8s\"\n", t->th_buf.devminor);
  48. printf(" prefix = \"%.155s\"\n", t->th_buf.prefix);
  49. printf(" padding = \"%.12s\"\n", t->th_buf.padding);
  50. printf(" gnu_longname = \"%s\"\n",
  51. (t->th_buf.gnu_longname ? t->th_buf.gnu_longname : "[NULL]"));
  52. printf(" gnu_longlink = \"%s\"\n",
  53. (t->th_buf.gnu_longlink ? t->th_buf.gnu_longlink : "[NULL]"));
  54. }
  55. void
  56. th_print_long_ls(TAR *t)
  57. {
  58. char modestring[12];
  59. #ifndef WIN32
  60. struct passwd *pw;
  61. struct group *gr;
  62. #endif
  63. uid_t uid;
  64. gid_t gid;
  65. char username[_POSIX_LOGIN_NAME_MAX];
  66. char groupname[_POSIX_LOGIN_NAME_MAX];
  67. time_t mtime;
  68. struct tm *mtm;
  69. #ifdef HAVE_STRFTIME
  70. char timebuf[18];
  71. #else
  72. const char *months[] = {
  73. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  74. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  75. };
  76. #endif
  77. uid = th_get_uid(t);
  78. #ifndef WIN32
  79. pw = getpwuid(uid);
  80. if (pw != NULL)
  81. strlcpy(username, pw->pw_name, sizeof(username));
  82. else
  83. #endif
  84. snprintf(username, sizeof(username), "%d", uid);
  85. gid = th_get_gid(t);
  86. #ifndef WIN32
  87. gr = getgrgid(gid);
  88. if (gr != NULL)
  89. strlcpy(groupname, gr->gr_name, sizeof(groupname));
  90. else
  91. #endif
  92. snprintf(groupname, sizeof(groupname), "%d", gid);
  93. strmode(th_get_mode(t), modestring);
  94. printf("%.10s %-8.8s %-8.8s ", modestring, username, groupname);
  95. #ifndef WIN32
  96. if (TH_ISCHR(t) || TH_ISBLK(t))
  97. printf(" %3d, %3d ", th_get_devmajor(t), th_get_devminor(t));
  98. else
  99. printf("%9ld ", (long)th_get_size(t));
  100. #endif
  101. mtime = th_get_mtime(t);
  102. mtm = localtime(&mtime);
  103. #ifdef HAVE_STRFTIME
  104. strftime(timebuf, sizeof(timebuf), "%h %e %H:%M %Y", mtm);
  105. printf("%s", timebuf);
  106. #else
  107. printf("%.3s %2d %2d:%02d %4d",
  108. months[mtm->tm_mon],
  109. mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_year + 1900);
  110. #endif
  111. printf(" %s", th_get_pathname(t));
  112. #ifndef _WIN32
  113. if (TH_ISSYM(t) || TH_ISLNK(t))
  114. {
  115. if (TH_ISSYM(t))
  116. printf(" -> ");
  117. else
  118. printf(" link to ");
  119. if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
  120. printf("%s", t->th_buf.gnu_longlink);
  121. else
  122. printf("%.100s", t->th_buf.linkname);
  123. }
  124. #endif
  125. putchar('\n');
  126. }