| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "makeint.h" |
| 19 |
|
| 20 |
#ifndef NO_ARCHIVES |
| 21 |
|
| 22 |
#include "filedef.h" |
| 23 |
#include "dep.h" |
| 24 |
#include <fnmatch.h> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
int |
| 33 |
ar_name (const char *name) |
| 34 |
{ |
| 35 |
const char *p = strchr (name, '('); |
| 36 |
const char *end; |
| 37 |
|
| 38 |
if (p == 0 || p == name) |
| 39 |
return 0; |
| 40 |
|
| 41 |
end = p + strlen (p) - 1; |
| 42 |
if (*end != ')' || end == p + 1) |
| 43 |
return 0; |
| 44 |
|
| 45 |
if (p[1] == '(' && end[-1] == ')') |
| 46 |
OS (fatal, NILF, _("attempt to use unsupported feature: '%s'"), name); |
| 47 |
|
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
void |
| 57 |
ar_parse_name (const char *name, char **arname_p, char **memname_p) |
| 58 |
{ |
| 59 |
char *p; |
| 60 |
|
| 61 |
*arname_p = xstrdup (name); |
| 62 |
p = strchr (*arname_p, '('); |
| 63 |
*(p++) = '\0'; |
| 64 |
p[strlen (p) - 1] = '\0'; |
| 65 |
*memname_p = p; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
struct member_date_lookup |
| 72 |
{ |
| 73 |
const char *name; |
| 74 |
time_t *member_date; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
static long int |
| 79 |
ar_member_date_1 (int desc UNUSED, const char *mem, int truncated, |
| 80 |
long int hdrpos UNUSED, long int datapos UNUSED, |
| 81 |
long int size UNUSED, long int date, |
| 82 |
int uid UNUSED, int gid UNUSED, unsigned int mode UNUSED, |
| 83 |
const void *data) |
| 84 |
{ |
| 85 |
const struct member_date_lookup *lookup_data = data; |
| 86 |
if (ar_name_equal (lookup_data->name, mem, truncated)) |
| 87 |
{ |
| 88 |
*lookup_data->member_date = date; |
| 89 |
return 1; |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
int |
| 97 |
ar_member_date (const char *name, time_t *member_date) |
| 98 |
{ |
| 99 |
char *arname; |
| 100 |
char *memname; |
| 101 |
int found; |
| 102 |
struct member_date_lookup lookup_data; |
| 103 |
|
| 104 |
ar_parse_name (name, &arname, &memname); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
{ |
| 114 |
struct file *arfile; |
| 115 |
arfile = lookup_file (arname); |
| 116 |
if (arfile == 0 && file_exists_p (arname)) |
| 117 |
arfile = enter_file (strcache_add (arname)); |
| 118 |
|
| 119 |
if (arfile != 0) |
| 120 |
(void) f_mtime (arfile, 0); |
| 121 |
} |
| 122 |
|
| 123 |
lookup_data.name = memname; |
| 124 |
lookup_data.member_date = member_date; |
| 125 |
found = ar_scan (arname, ar_member_date_1, &lookup_data); |
| 126 |
|
| 127 |
free (arname); |
| 128 |
|
| 129 |
|
| 130 |
return (found == 1) ? 1 : 0; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
int |
| 136 |
ar_touch (const char *name) |
| 137 |
{ |
| 138 |
char *arname, *memname; |
| 139 |
int val; |
| 140 |
|
| 141 |
ar_parse_name (name, &arname, &memname); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
{ |
| 146 |
struct file *arfile; |
| 147 |
arfile = enter_file (strcache_add (arname)); |
| 148 |
f_mtime (arfile, 0); |
| 149 |
} |
| 150 |
|
| 151 |
val = 1; |
| 152 |
switch (ar_member_touch (arname, memname)) |
| 153 |
{ |
| 154 |
case -1: |
| 155 |
OS (error, NILF, _("touch: Archive '%s' does not exist"), arname); |
| 156 |
break; |
| 157 |
case -2: |
| 158 |
OS (error, NILF, _("touch: '%s' is not a valid archive"), arname); |
| 159 |
break; |
| 160 |
case -3: |
| 161 |
perror_with_name ("touch: ", arname); |
| 162 |
break; |
| 163 |
case 1: |
| 164 |
OSS (error, NILF, |
| 165 |
_("touch: Member '%s' does not exist in '%s'"), memname, arname); |
| 166 |
break; |
| 167 |
case 0: |
| 168 |
val = 0; |
| 169 |
break; |
| 170 |
default: |
| 171 |
OS (error, NILF, |
| 172 |
_("touch: Bad return code from ar_member_touch on '%s'"), name); |
| 173 |
} |
| 174 |
|
| 175 |
free (arname); |
| 176 |
|
| 177 |
return val; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
struct ar_glob_state |
| 183 |
{ |
| 184 |
const char *arname; |
| 185 |
const char *pattern; |
| 186 |
size_t size; |
| 187 |
struct nameseq *chain; |
| 188 |
unsigned int n; |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static long int |
| 195 |
ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED, |
| 196 |
long int hdrpos UNUSED, long int datapos UNUSED, |
| 197 |
long int size UNUSED, long int date UNUSED, int uid UNUSED, |
| 198 |
int gid UNUSED, unsigned int mode UNUSED, const void *arg) |
| 199 |
{ |
| 200 |
struct ar_glob_state *state = (struct ar_glob_state *)arg; |
| 201 |
|
| 202 |
if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0) |
| 203 |
{ |
| 204 |
|
| 205 |
struct nameseq *new = xcalloc (state->size); |
| 206 |
#ifdef VMS |
| 207 |
if (state->suffix) |
| 208 |
new->name = strcache_add( |
| 209 |
concat(5, state->arname, "(", mem, state->suffix, ")")); |
| 210 |
else |
| 211 |
#endif |
| 212 |
new->name = strcache_add(concat(4, state->arname, "(", mem, ")")); |
| 213 |
new->next = state->chain; |
| 214 |
state->chain = new; |
| 215 |
++state->n; |
| 216 |
} |
| 217 |
|
| 218 |
return 0L; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static int |
| 224 |
ar_glob_pattern_p (const char *pattern, int quote) |
| 225 |
{ |
| 226 |
const char *p; |
| 227 |
int opened = 0; |
| 228 |
|
| 229 |
for (p = pattern; *p != '\0'; ++p) |
| 230 |
switch (*p) |
| 231 |
{ |
| 232 |
case '?': |
| 233 |
case '*': |
| 234 |
return 1; |
| 235 |
|
| 236 |
case '\\': |
| 237 |
if (quote) |
| 238 |
++p; |
| 239 |
break; |
| 240 |
|
| 241 |
case '[': |
| 242 |
opened = 1; |
| 243 |
break; |
| 244 |
|
| 245 |
case ']': |
| 246 |
if (opened) |
| 247 |
return 1; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
struct nameseq * |
| 258 |
ar_glob (const char *arname, const char *member_pattern, size_t size) |
| 259 |
{ |
| 260 |
struct ar_glob_state state; |
| 261 |
struct nameseq *n; |
| 262 |
const char **names; |
| 263 |
unsigned int i; |
| 264 |
if (! ar_glob_pattern_p (member_pattern, 1)) |
| 265 |
return 0; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
state.arname = arname; |
| 270 |
state.pattern = member_pattern; |
| 271 |
state.size = size; |
| 272 |
state.chain = 0; |
| 273 |
state.n = 0; |
| 274 |
ar_scan (arname, ar_glob_match, &state); |
| 275 |
|
| 276 |
if (state.chain == 0) |
| 277 |
return 0; |
| 278 |
|
| 279 |
|
| 280 |
names = alloca (state.n * sizeof (const char *)); |
| 281 |
i = 0; |
| 282 |
for (n = state.chain; n != 0; n = n->next) |
| 283 |
names[i++] = n->name; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
qsort ((void *)names, i, sizeof (*names), alpha_compare); |
| 288 |
|
| 289 |
|
| 290 |
i = 0; |
| 291 |
for (n = state.chain; n != 0; n = n->next) |
| 292 |
n->name = names[i++]; |
| 293 |
|
| 294 |
return state.chain; |
| 295 |
} |
| 296 |
|
| 297 |
#endif |