blob: 383306d6463ba05f229a77f78405129f7ed6a07e [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** decode.c - libtar code to decode 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 <sys/param.h>
17#include <pwd.h>
18#include <grp.h>
19
20#ifdef STDC_HEADERS
21# include <string.h>
22#endif
23
24
25/* determine full path name */
26char *
27th_get_pathname(TAR *t)
28{
29 char filename[MAXPATHLEN];
30
31 if (t->th_buf.gnu_longname) {
32 printf("returning gnu longname\n");
33 return t->th_buf.gnu_longname;
34 }
35
36 if (t->th_buf.prefix[0] != '\0')
37 {
38 snprintf(filename, sizeof(filename), "%.155s/%.100s",
39 t->th_buf.prefix, t->th_buf.name);
40 return strdup(filename);
41 }
42
43 snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
44 return strdup(filename);
45}
46
47
48uid_t
49th_get_uid(TAR *t)
50{
51 int uid;
52 struct passwd *pw;
53
54 pw = getpwnam(t->th_buf.uname);
55 if (pw != NULL)
56 return pw->pw_uid;
57
58 /* if the password entry doesn't exist */
59 sscanf(t->th_buf.uid, "%o", &uid);
60 return uid;
61}
62
63
64gid_t
65th_get_gid(TAR *t)
66{
67 int gid;
68 struct group *gr;
69
70 gr = getgrnam(t->th_buf.gname);
71 if (gr != NULL)
72 return gr->gr_gid;
73
74 /* if the group entry doesn't exist */
75 sscanf(t->th_buf.gid, "%o", &gid);
76 return gid;
77}
78
79
80mode_t
81th_get_mode(TAR *t)
82{
83 mode_t mode;
84
85 mode = (mode_t)oct_to_int(t->th_buf.mode);
86 if (! (mode & S_IFMT))
87 {
88 switch (t->th_buf.typeflag)
89 {
90 case SYMTYPE:
91 mode |= S_IFLNK;
92 break;
93 case CHRTYPE:
94 mode |= S_IFCHR;
95 break;
96 case BLKTYPE:
97 mode |= S_IFBLK;
98 break;
99 case DIRTYPE:
100 mode |= S_IFDIR;
101 break;
102 case FIFOTYPE:
103 mode |= S_IFIFO;
104 break;
105 case AREGTYPE:
106 if (t->th_buf.name[strlen(t->th_buf.name) - 1] == '/')
107 {
108 mode |= S_IFDIR;
109 break;
110 }
111 /* FALLTHROUGH */
112 case LNKTYPE:
113 case REGTYPE:
114 default:
115 mode |= S_IFREG;
116 }
117 }
118
119 return mode;
120}
121
122