blob: 3f57c831ed8d3b3572d4573a6272dda57147277b [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
bigbiff7ba75002020-04-11 20:47:09 -040027#ifdef USE_FSCRYPT
28#include "fscrypt_policy.h"
29#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030
31#ifndef _POSIX_LOGIN_NAME_MAX
32# define _POSIX_LOGIN_NAME_MAX 9
33#endif
34
35
36void
37th_print(TAR *t)
38{
Mohd Faraz566ef6b2023-01-16 16:16:21 +010039 LOG("Printing tar header:");
40 LOG(" name = \"%.100s\"\n", t->th_buf.name);
41 LOG(" mode = \"%.8s\"\n", t->th_buf.mode);
42 LOG(" uid = \"%.8s\"\n", t->th_buf.uid);
43 LOG(" gid = \"%.8s\"\n", t->th_buf.gid);
44 LOG(" size = \"%.12s\"\n", t->th_buf.size);
45 LOG(" mtime = \"%.12s\"\n", t->th_buf.mtime);
46 LOG(" chksum = \"%.8s\"\n", t->th_buf.chksum);
47 LOG(" typeflag = \'%c\'\n", t->th_buf.typeflag);
48 LOG(" linkname = \"%.100s\"\n", t->th_buf.linkname);
49 LOG(" magic = \"%.6s\"\n", t->th_buf.magic);
50 /*LOG(" version = \"%.2s\"\n", t->th_buf.version); */
51 /*LOG(" version[0] = \'%c\',version[1] = \'%c\'\n",
Ethan Yonkerfefe5912017-09-30 22:22:13 -050052 t->th_buf.version[0], t->th_buf.version[1]);*/
Mohd Faraz566ef6b2023-01-16 16:16:21 +010053 LOG(" uname = \"%.32s\"\n", t->th_buf.uname);
54 LOG(" gname = \"%.32s\"\n", t->th_buf.gname);
55 LOG(" devmajor = \"%.8s\"\n", t->th_buf.devmajor);
56 LOG(" devminor = \"%.8s\"\n", t->th_buf.devminor);
57 LOG(" prefix = \"%.155s\"\n", t->th_buf.prefix);
58 LOG(" padding = \"%.12s\"\n", t->th_buf.padding);
59 LOG(" gnu_longname = \"%s\"\n",
bigbiff bigbiff9c754052013-01-09 09:09:08 -050060 (t->th_buf.gnu_longname ? t->th_buf.gnu_longname : "[NULL]"));
Mohd Faraz566ef6b2023-01-16 16:16:21 +010061 LOG(" gnu_longlink = \"%s\"\n",
bigbiff bigbiff9c754052013-01-09 09:09:08 -050062 (t->th_buf.gnu_longlink ? t->th_buf.gnu_longlink : "[NULL]"));
bigbiff7ba75002020-04-11 20:47:09 -040063#ifdef USE_FSCRYPT
Mohd Faraz566ef6b2023-01-16 16:16:21 +010064 LOG(" fep = \"%s\"\n",
Mohd Faraz97b84992023-01-07 21:26:56 +010065 (t->th_buf.fep ? get_policy_descriptor(t->th_buf.fep) : (uint8_t*) "[NULL]"));
bigbiff2e344ab2021-05-07 10:41:55 -040066#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050067}
68
69
70void
71th_print_long_ls(TAR *t)
72{
73 char modestring[12];
74 struct passwd *pw;
75 struct group *gr;
76 uid_t uid;
77 gid_t gid;
78 char username[_POSIX_LOGIN_NAME_MAX];
79 char groupname[_POSIX_LOGIN_NAME_MAX];
80 time_t mtime;
81 struct tm *mtm;
82
83#ifdef HAVE_STRFTIME
84 char timebuf[18];
85#else
86 const char *months[] = {
87 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
88 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
89 };
90#endif
91
92 uid = th_get_uid(t);
93 pw = getpwuid(uid);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050094 if ((t->options & TAR_USE_NUMERIC_ID) || pw == NULL)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050095 snprintf(username, sizeof(username), "%d", uid);
96 else
97 strlcpy(username, pw->pw_name, sizeof(username));
98
99 gid = th_get_gid(t);
100 gr = getgrgid(gid);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500101 if ((t->options & TAR_USE_NUMERIC_ID) || gr == NULL)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500102 snprintf(groupname, sizeof(groupname), "%d", gid);
103 else
104 strlcpy(groupname, gr->gr_name, sizeof(groupname));
105
106 strmode(th_get_mode(t), modestring);
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100107 LOG("%.10s %-8.8s %-8.8s ", modestring, username, groupname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500108
109 if (TH_ISCHR(t) || TH_ISBLK(t))
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100110 LOG(" %3d, %3d ", (int)th_get_devmajor(t), (int)th_get_devminor(t));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500111 else
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100112 LOG("%9ld ", (long)th_get_size(t));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500113
114 mtime = th_get_mtime(t);
115 mtm = localtime(&mtime);
116#ifdef HAVE_STRFTIME
117 strftime(timebuf, sizeof(timebuf), "%h %e %H:%M %Y", mtm);
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100118 LOG("%s", timebuf);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500119#else
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100120 LOG("%.3s %2d %2d:%02d %4d",
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500121 months[mtm->tm_mon],
122 mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_year + 1900);
123#endif
124
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100125 LOG(" %s", th_get_pathname(t));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500126
127 if (TH_ISSYM(t) || TH_ISLNK(t))
128 {
129 if (TH_ISSYM(t))
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100130 LOG(" -> ");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500131 else
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100132 LOG(" link to ");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500133 if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100134 LOG("%s", t->th_buf.gnu_longlink);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500135 else
Mohd Faraz566ef6b2023-01-16 16:16:21 +0100136 LOG("%.100s", t->th_buf.linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500137 }
138
139 putchar('\n');
140}
141
142