Fix for Bug #9190, -U did not work on case insensitive file systems because of call...
[cmake.git] / Utilities / cmtar / wrapper.c
blob4f1271a4595f0452e006675371a89d32fb9f649d
1 /*
2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
5 **
6 ** wrapper.c - libtar high-level wrapper code
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 #include <libtar/compat.h>
19 #if defined(HAVE_SYS_PARAM_H)
20 # include <sys/param.h>
21 #endif
22 #if defined(HAVE_DIRENT_H)
23 #include <dirent.h>
24 #else
25 #include <libtarint/filesystem.h>
26 #endif
27 #include <errno.h>
29 #ifdef HAVE_FNMATCH_H
30 #include <fnmatch.h>
31 #endif
33 #ifdef STDC_HEADERS
34 # include <string.h>
35 #endif
38 int
39 tar_extract_glob(TAR *t, char *globname, char *prefix)
41 char *filename;
42 char buf[TAR_MAXPATHLEN];
43 int i;
44 char *pathname;
46 while ((i = th_read(t)) == 0)
48 pathname = th_get_pathname(t);
49 filename = pathname;
51 if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
53 if (pathname)
55 free(pathname);
58 if (TH_ISREG(t) && tar_skip_regfile(t))
59 return -1;
60 continue;
63 if (t->options & TAR_VERBOSE)
64 th_print_long_ls(t);
66 if (prefix != NULL)
67 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
68 else
69 strlcpy(buf, filename, sizeof(buf));
71 if (tar_extract_file(t, filename) != 0)
73 if (pathname)
75 free(pathname);
77 return -1;
80 if (pathname)
82 free(pathname);
86 return (i == 1 ? 0 : -1);
90 int
91 tar_extract_all(TAR *t, char *prefix)
93 char *filename;
94 char buf[TAR_MAXPATHLEN];
95 int i;
96 char *pathname;
98 #ifdef DEBUG
99 printf("==> tar_extract_all(TAR *t, \"%s\")\n",
100 (prefix ? prefix : "(null)"));
101 #endif
103 while ((i = th_read(t)) == 0)
105 #ifdef DEBUG
106 puts(" tar_extract_all(): calling th_get_pathname()");
107 #endif
109 pathname = th_get_pathname(t);
110 filename = pathname;
112 if (t->options & TAR_VERBOSE)
113 th_print_long_ls(t);
114 if (prefix != NULL)
115 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
116 else
117 strlcpy(buf, filename, sizeof(buf));
119 if (pathname)
121 free(pathname);
124 #ifdef DEBUG
125 printf(" tar_extract_all(): calling tar_extract_file(t, "
126 "\"%s\")\n", buf);
127 #endif
129 if (tar_extract_file(t, buf) != 0)
130 return -1;
133 return (i == 1 ? 0 : -1);
138 tar_append_tree(TAR *t, char *realdir, char *savedir)
140 char realpath[TAR_MAXPATHLEN];
141 char savepath[TAR_MAXPATHLEN];
142 size_t plen;
143 #if defined(HAVE_DIRENT_H)
144 struct dirent *dent;
145 DIR *dp;
146 #else
147 kwDirEntry * dent;
148 kwDirectory *dp;
149 #endif
150 struct stat s;
151 strncpy(realpath, realdir, sizeof(realpath));
152 realpath[sizeof(realpath)-1] = 0;
153 plen = strlen(realpath);
154 if ( realpath[plen-1] == '/' )
156 realpath[plen-1] = 0;
160 #ifdef DEBUG
161 printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
162 t, realdir, (savedir ? savedir : "[NULL]"));
163 #endif
165 if (tar_append_file(t, realdir, savedir) != 0)
166 return -1;
168 #ifdef DEBUG
169 puts(" tar_append_tree(): done with tar_append_file()...");
170 #endif
172 if ( stat(realpath, &s) != 0 )
174 return -1;
176 if (
177 #if defined(_WIN32) && !defined(__CYGWIN__)
178 (s.st_mode & _S_IFDIR) == 0
179 #else
180 !S_ISDIR(s.st_mode)
181 #endif
183 return 0;
184 #if defined(HAVE_DIRENT_H)
185 dp = opendir(realdir);
186 #else
187 dp = kwOpenDir(realdir);
188 #endif
190 if (dp == NULL)
192 if (errno == ENOTDIR)
193 return 0;
194 return -1;
196 #if defined(HAVE_DIRENT_H)
197 while ((dent = readdir(dp)) != NULL)
198 #else
199 while ((dent = kwReadDir(dp)) != NULL)
200 #endif
202 if (strcmp(dent->d_name, ".") == 0 ||
203 strcmp(dent->d_name, "..") == 0)
204 continue;
206 snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
207 dent->d_name);
208 if (savedir)
209 snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
210 dent->d_name);
212 #ifndef WIN32
213 if (lstat(realpath, &s) != 0)
214 return -1;
215 #else
216 if (stat(realpath, &s) != 0)
217 return -1;
218 #endif
219 if (S_ISDIR(s.st_mode))
221 if (tar_append_tree(t, realpath,
222 (savedir ? savepath : NULL)) != 0)
223 return -1;
224 continue;
227 if (tar_append_file(t, realpath,
228 (savedir ? savepath : NULL)) != 0)
229 return -1;
232 #if defined(HAVE_DIRENT_H)
233 closedir(dp);
234 #else
235 kwCloseDir(dp);
236 #endif
238 return 0;