Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmtar / output.c
blob283c5945ad45d14707cff8840c93c18e41e08d53
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
13 #include <libtarint/internal.h>
15 #include <stdio.h>
16 #include <stdlib.h>
18 #if !defined(_WIN32) || defined(__CYGWIN__)
19 # include <pwd.h>
20 # include <grp.h>
21 #endif
23 #include <time.h>
24 #include <limits.h>
26 #ifdef STDC_HEADERS
27 # include <string.h>
28 #endif
31 #ifndef _POSIX_LOGIN_NAME_MAX
32 # define _POSIX_LOGIN_NAME_MAX 9
33 #endif
36 void
37 th_print(TAR *t)
39 puts("\nPrinting tar header:");
40 printf(" name = \"%.100s\"\n", t->th_buf.name);
41 printf(" mode = \"%.8s\"\n", t->th_buf.mode);
42 printf(" uid = \"%.8s\"\n", t->th_buf.uid);
43 printf(" gid = \"%.8s\"\n", t->th_buf.gid);
44 printf(" size = \"%.12s\"\n", t->th_buf.size);
45 printf(" mtime = \"%.12s\"\n", t->th_buf.mtime);
46 printf(" chksum = \"%.8s\"\n", t->th_buf.chksum);
47 printf(" typeflag = \'%c\'\n", t->th_buf.typeflag);
48 printf(" linkname = \"%.100s\"\n", t->th_buf.linkname);
49 printf(" magic = \"%.6s\"\n", t->th_buf.magic);
50 /*printf(" version = \"%.2s\"\n", t->th_buf.version); */
51 printf(" version[0] = \'%c\',version[1] = \'%c\'\n",
52 t->th_buf.version[0], t->th_buf.version[1]);
53 printf(" uname = \"%.32s\"\n", t->th_buf.uname);
54 printf(" gname = \"%.32s\"\n", t->th_buf.gname);
55 printf(" devmajor = \"%.8s\"\n", t->th_buf.devmajor);
56 printf(" devminor = \"%.8s\"\n", t->th_buf.devminor);
57 printf(" prefix = \"%.155s\"\n", t->th_buf.prefix);
58 printf(" padding = \"%.12s\"\n", t->th_buf.padding);
59 printf(" gnu_longname = \"%s\"\n",
60 (t->th_buf.gnu_longname ? t->th_buf.gnu_longname : "[NULL]"));
61 printf(" gnu_longlink = \"%s\"\n",
62 (t->th_buf.gnu_longlink ? t->th_buf.gnu_longlink : "[NULL]"));
66 void
67 th_print_long_ls(TAR *t)
69 char modestring[12];
70 #if !defined(_WIN32) || defined(__CYGWIN__)
71 struct passwd *pw;
72 struct group *gr;
73 #endif
74 uid_t uid;
75 gid_t gid;
76 char username[_POSIX_LOGIN_NAME_MAX];
77 char groupname[_POSIX_LOGIN_NAME_MAX];
78 time_t mtime;
79 struct tm *mtm;
80 char *pathname;
82 #ifdef HAVE_STRFTIME
83 char timebuf[18];
84 #else
85 const char *months[] = {
86 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
87 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
89 #endif
91 uid = th_get_uid(t);
92 #if !defined(_WIN32) || defined(__CYGWIN__)
93 pw = getpwuid(uid);
94 if (pw != NULL)
95 strlcpy(username, pw->pw_name, sizeof(username));
96 else
97 #endif
98 snprintf(username, sizeof(username), "%d", (int)uid);
99 gid = th_get_gid(t);
100 #if !defined(_WIN32) || defined(__CYGWIN__)
101 gr = getgrgid(gid);
102 if (gr != NULL)
103 strlcpy(groupname, gr->gr_name, sizeof(groupname));
104 else
105 #endif
106 snprintf(groupname, sizeof(groupname), "%d", (int)gid);
108 strmode(th_get_mode(t), modestring);
109 printf("%.10s %-8.8s %-8.8s ", modestring, username, groupname);
111 #if !defined(_WIN32) || defined(__CYGWIN__)
112 if (TH_ISCHR(t) || TH_ISBLK(t))
113 printf(" %3d, %3d ", th_get_devmajor(t), th_get_devminor(t));
114 else
115 #endif
116 printf("%9ld ", (long)th_get_size(t));
118 mtime = th_get_mtime(t);
119 mtm = localtime(&mtime);
120 #ifdef HAVE_STRFTIME
121 strftime(timebuf, sizeof(timebuf), "%b %d %H:%M %Y", mtm);
122 printf("%s", timebuf);
123 #else
124 printf("%.3s %2d %2d:%02d %4d",
125 months[mtm->tm_mon],
126 mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_year + 1900);
127 #endif
129 pathname = th_get_pathname(t);
130 if (pathname)
132 printf(" %s", pathname);
133 free(pathname);
136 #if !defined(_WIN32) || defined(__CYGWIN__)
137 if (TH_ISSYM(t) || TH_ISLNK(t))
139 if (TH_ISSYM(t))
140 printf(" -> ");
141 else
142 printf(" link to ");
143 if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
144 printf("%s", t->th_buf.gnu_longlink);
145 else
146 printf("%.100s", t->th_buf.linkname);
148 #endif
150 putchar('\n');