Fix missing prototype compiler warnings
[libtar.git] / lib / wrapper.c
blob44cc43587e8b78c933ffb7fcbf69a72ad60c1e7b
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 <stdlib.h>
17 #include <sys/param.h>
18 #include <dirent.h>
19 #include <errno.h>
21 #ifdef STDC_HEADERS
22 # include <string.h>
23 #endif
26 int
27 tar_extract_glob(TAR *t, char *globname, char *prefix)
29 char *filename;
30 char buf[MAXPATHLEN];
31 int i;
33 while ((i = th_read(t)) == 0)
35 filename = th_get_pathname(t);
36 if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
38 if (TH_ISREG(t) && tar_skip_regfile(t))
39 return -1;
40 continue;
42 if (t->options & TAR_VERBOSE)
43 th_print_long_ls(t);
44 if (prefix != NULL)
45 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
46 else
47 strlcpy(buf, filename, sizeof(buf));
48 if (tar_extract_file(t, buf) != 0)
49 return -1;
52 return (i == 1 ? 0 : -1);
56 int
57 tar_extract_all(TAR *t, char *prefix)
59 char *filename;
60 char buf[MAXPATHLEN];
61 int i;
63 #ifdef DEBUG
64 printf("==> tar_extract_all(TAR *t, \"%s\")\n",
65 (prefix ? prefix : "(null)"));
66 #endif
68 while ((i = th_read(t)) == 0)
70 #ifdef DEBUG
71 puts(" tar_extract_all(): calling th_get_pathname()");
72 #endif
73 filename = th_get_pathname(t);
74 if (t->options & TAR_VERBOSE)
75 th_print_long_ls(t);
76 if (prefix != NULL)
77 snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
78 else
79 strlcpy(buf, filename, sizeof(buf));
80 #ifdef DEBUG
81 printf(" tar_extract_all(): calling tar_extract_file(t, "
82 "\"%s\")\n", buf);
83 #endif
84 if (tar_extract_file(t, buf) != 0)
85 return -1;
88 return (i == 1 ? 0 : -1);
92 int
93 tar_append_tree(TAR *t, char *realdir, char *savedir)
95 char realpath[MAXPATHLEN];
96 char savepath[MAXPATHLEN];
97 struct dirent *dent;
98 DIR *dp;
99 struct stat s;
101 #ifdef DEBUG
102 printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
103 t, realdir, (savedir ? savedir : "[NULL]"));
104 #endif
106 if (tar_append_file(t, realdir, savedir) != 0)
107 return -1;
109 #ifdef DEBUG
110 puts(" tar_append_tree(): done with tar_append_file()...");
111 #endif
113 dp = opendir(realdir);
114 if (dp == NULL)
116 if (errno == ENOTDIR)
117 return 0;
118 return -1;
120 while ((dent = readdir(dp)) != NULL)
122 if (strcmp(dent->d_name, ".") == 0 ||
123 strcmp(dent->d_name, "..") == 0)
124 continue;
126 snprintf(realpath, MAXPATHLEN, "%s/%s", realdir,
127 dent->d_name);
128 if (savedir)
129 snprintf(savepath, MAXPATHLEN, "%s/%s", savedir,
130 dent->d_name);
132 if (lstat(realpath, &s) != 0)
133 return -1;
135 if (S_ISDIR(s.st_mode))
137 if (tar_append_tree(t, realpath,
138 (savedir ? savepath : NULL)) != 0)
139 return -1;
140 continue;
143 if (tar_append_file(t, realpath,
144 (savedir ? savepath : NULL)) != 0)
145 return -1;
148 closedir(dp);
150 return 0;