libtar-1.2.11 tarball sources, taken from Debian's orig tar
[libtar.git] / lib / wrapper.c
blob37400e66c1a217cb6036f2463075dad2ffb82a1a
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 <internal.h>
15 #include <stdio.h>
16 #include <sys/param.h>
17 #include <dirent.h>
18 #include <errno.h>
20 #ifdef STDC_HEADERS
21 # include <string.h>
22 #endif
25 int
26 tar_extract_glob(TAR *t, char *globname, char *prefix)
28 char *filename;
29 char buf[MAXPATHLEN];
30 int i;
32 while ((i = th_read(t)) == 0)
34 filename = th_get_pathname(t);
35 if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
37 if (TH_ISREG(t) && tar_skip_regfile(t))
38 return -1;
39 continue;
41 if (t->options & TAR_VERBOSE)
42 th_print_long_ls(t);
43 if (prefix != NULL)
44 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
45 else
46 strlcpy(buf, filename, sizeof(buf));
47 if (tar_extract_file(t, filename) != 0)
48 return -1;
51 return (i == 1 ? 0 : -1);
55 int
56 tar_extract_all(TAR *t, char *prefix)
58 char *filename;
59 char buf[MAXPATHLEN];
60 int i;
62 #ifdef DEBUG
63 printf("==> tar_extract_all(TAR *t, \"%s\")\n",
64 (prefix ? prefix : "(null)"));
65 #endif
67 while ((i = th_read(t)) == 0)
69 #ifdef DEBUG
70 puts(" tar_extract_all(): calling th_get_pathname()");
71 #endif
72 filename = th_get_pathname(t);
73 if (t->options & TAR_VERBOSE)
74 th_print_long_ls(t);
75 if (prefix != NULL)
76 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
77 else
78 strlcpy(buf, filename, sizeof(buf));
79 #ifdef DEBUG
80 printf(" tar_extract_all(): calling tar_extract_file(t, "
81 "\"%s\")\n", buf);
82 #endif
83 if (tar_extract_file(t, buf) != 0)
84 return -1;
87 return (i == 1 ? 0 : -1);
91 int
92 tar_append_tree(TAR *t, char *realdir, char *savedir)
94 char realpath[MAXPATHLEN];
95 char savepath[MAXPATHLEN];
96 struct dirent *dent;
97 DIR *dp;
98 struct stat s;
100 #ifdef DEBUG
101 printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
102 t, realdir, (savedir ? savedir : "[NULL]"));
103 #endif
105 if (tar_append_file(t, realdir, savedir) != 0)
106 return -1;
108 #ifdef DEBUG
109 puts(" tar_append_tree(): done with tar_append_file()...");
110 #endif
112 dp = opendir(realdir);
113 if (dp == NULL)
115 if (errno == ENOTDIR)
116 return 0;
117 return -1;
119 while ((dent = readdir(dp)) != NULL)
121 if (strcmp(dent->d_name, ".") == 0 ||
122 strcmp(dent->d_name, "..") == 0)
123 continue;
125 snprintf(realpath, MAXPATHLEN, "%s/%s", realdir,
126 dent->d_name);
127 if (savedir)
128 snprintf(savepath, MAXPATHLEN, "%s/%s", savedir,
129 dent->d_name);
131 if (lstat(realpath, &s) != 0)
132 return -1;
134 if (S_ISDIR(s.st_mode))
136 if (tar_append_tree(t, realpath,
137 (savedir ? savepath : NULL)) != 0)
138 return -1;
139 continue;
142 if (tar_append_file(t, realpath,
143 (savedir ? savepath : NULL)) != 0)
144 return -1;
147 closedir(dp);
149 return 0;