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