Updated formatting of documentation plus a little reorganization.
[cmake.git] / Utilities / cmtar / decode.c
blobb183010174bc7d0edba9384ea0861413fa996ed5
1 /*
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
13 #include <libtarint/internal.h>
15 #include <stdio.h>
17 #if defined(_WIN32) && !defined(__CYGWIN__)
18 # include <libtar/compat.h>
19 #else
20 # ifdef HAVE_SYS_PARAM_H
21 # include <sys/param.h>
22 # endif
23 #endif
25 #ifndef WIN32
26 #include <pwd.h>
27 #include <grp.h>
28 #endif
30 #ifdef STDC_HEADERS
31 # include <string.h>
32 #endif
35 /* determine full path name */
36 /* caller must "free" returned pointer when done with it */
37 /* th_get_pathname return values come directly from strdup */
38 char *
39 th_get_pathname(TAR *t)
41 char filename[TAR_MAXPATHLEN];
43 if (t->th_buf.gnu_longname)
44 return strdup(t->th_buf.gnu_longname);
46 if (t->th_buf.prefix[0] != '\0')
48 snprintf(filename, sizeof(filename), "%.155s/%.100s",
49 t->th_buf.prefix, t->th_buf.name);
50 return strdup(filename);
53 snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
54 return strdup(filename);
58 uid_t
59 th_get_uid(TAR *t)
61 int uid;
62 #ifndef WIN32
63 struct passwd *pw;
65 pw = getpwnam(t->th_buf.uname);
66 if (pw != NULL)
67 return pw->pw_uid;
69 /* if the password entry doesn't exist */
70 #endif
71 sscanf(t->th_buf.uid, "%o", &uid);
72 return uid;
76 gid_t
77 th_get_gid(TAR *t)
79 int gid;
80 #ifndef WIN32
81 struct group *gr;
83 gr = getgrnam(t->th_buf.gname);
84 if (gr != NULL)
85 return gr->gr_gid;
87 /* if the group entry doesn't exist */
88 #endif
89 sscanf(t->th_buf.gid, "%o", &gid);
90 return gid;
94 mode_t
95 th_get_mode(TAR *t)
97 mode_t mode;
99 mode = (mode_t)oct_to_int(t->th_buf.mode);
100 if (! (mode & S_IFMT))
102 switch (t->th_buf.typeflag)
104 #ifndef WIN32
105 case SYMTYPE:
106 mode |= S_IFLNK;
107 break;
108 #endif
109 case CHRTYPE:
110 mode |= S_IFCHR;
111 break;
112 case BLKTYPE:
113 mode |= S_IFBLK;
114 break;
115 case DIRTYPE:
116 mode |= S_IFDIR;
117 break;
118 #ifndef WIN32
119 case FIFOTYPE:
120 mode |= S_IFIFO;
121 break;
122 #endif
123 case AREGTYPE:
124 if (t->th_buf.name[strlen(t->th_buf.name) - 1] == '/')
126 mode |= S_IFDIR;
127 break;
129 /* FALLTHROUGH */
130 case LNKTYPE:
131 case REGTYPE:
132 default:
133 mode |= S_IFREG;
137 return mode;