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