| 1 |
/* |
| 2 |
Copyright (c) 1990-2005 Info-ZIP. All rights reserved. |
| 3 |
|
| 4 |
See the accompanying file LICENSE, version 2005-Feb-10 or later |
| 5 |
(the contents of which are also included in zip.h) for terms of use. |
| 6 |
If, for some reason, all these files are missing, the Info-ZIP license |
| 7 |
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html |
| 8 |
*/ |
| 9 |
#include "zip.h" |
| 10 |
|
| 11 |
#ifndef UTIL /* the companion #endif is a bit of ways down ... */ |
| 12 |
|
| 13 |
#include <time.h> |
| 14 |
#if defined(__IBMC__) || defined(MSC) |
| 15 |
#include <direct.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
/* Extra malloc() space in names for cutpath() */ |
| 19 |
#define PAD 0 |
| 20 |
#define PATH_END '/' |
| 21 |
|
| 22 |
|
| 23 |
#include "os2zip.h" |
| 24 |
|
| 25 |
/* Library functions not in (most) header files */ |
| 26 |
|
| 27 |
extern char *label; |
| 28 |
local ulg label_time = 0; |
| 29 |
local ulg label_mode = 0; |
| 30 |
local time_t label_utim = 0; |
| 31 |
|
| 32 |
/* Local functions */ |
| 33 |
local char *readd OF((DIR *)); |
| 34 |
|
| 35 |
|
| 36 |
local char *readd(d) |
| 37 |
DIR *d; /* directory stream to read from */ |
| 38 |
/* Return a pointer to the next name in the directory stream d, or NULL if |
| 39 |
no more entries or an error occurs. */ |
| 40 |
{ |
| 41 |
struct dirent *e; |
| 42 |
|
| 43 |
e = readdir(d); |
| 44 |
return e == NULL ? (char *) NULL : e->d_name; |
| 45 |
} |
| 46 |
|
| 47 |
int wild(w) |
| 48 |
char *w; /* path/pattern to match */ |
| 49 |
/* If not in exclude mode, expand the pattern based on the contents of the |
| 50 |
file system. Return an error code in the ZE_ class. */ |
| 51 |
{ |
| 52 |
DIR *d; /* stream for reading directory */ |
| 53 |
char *e; /* name found in directory */ |
| 54 |
int r; /* temporary variable */ |
| 55 |
char *n; /* constructed name from directory */ |
| 56 |
int f; /* true if there was a match */ |
| 57 |
char *a; /* alloc'ed space for name */ |
| 58 |
char *p; /* path */ |
| 59 |
char *q; /* name */ |
| 60 |
char v[5]; /* space for device current directory */ |
| 61 |
|
| 62 |
if (volume_label == 1) { |
| 63 |
volume_label = 2; |
| 64 |
label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0', |
| 65 |
&label_time, &label_mode, &label_utim); |
| 66 |
if (label != NULL) { |
| 67 |
newname(label, 0, 0); |
| 68 |
} |
| 69 |
if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK; |
| 70 |
/* "zip -$ foo a:" can be used to force drive name */ |
| 71 |
} |
| 72 |
|
| 73 |
if (w == NULL) |
| 74 |
return ZE_OK; |
| 75 |
|
| 76 |
/* special handling of stdin request */ |
| 77 |
if (strcmp(w, "-") == 0) /* if compressing stdin */ |
| 78 |
return newname(w, 0, 0); |
| 79 |
|
| 80 |
/* Allocate and copy pattern */ |
| 81 |
if ((p = a = malloc(strlen(w) + 1)) == NULL) |
| 82 |
return ZE_MEM; |
| 83 |
strcpy(p, w); |
| 84 |
|
| 85 |
/* catch special case: treat "*.*" as "*" for DOS-impaired people */ |
| 86 |
r = strlen(p); |
| 87 |
if (strcmp(p + r - 3, "*.*") == 0) |
| 88 |
p[r - 2] = '\0'; |
| 89 |
|
| 90 |
/* Normalize path delimiter as '/'. */ |
| 91 |
for (q = p; *q; q++) /* use / consistently */ |
| 92 |
if (*q == '\\') |
| 93 |
*q = '/'; |
| 94 |
|
| 95 |
/* Only name can have special matching characters */ |
| 96 |
if ((q = isshexp(p)) != NULL && |
| 97 |
(strrchr(q, '/') != NULL || strrchr(q, ':') != NULL)) |
| 98 |
{ |
| 99 |
free((zvoid *)a); |
| 100 |
return ZE_PARMS; |
| 101 |
} |
| 102 |
|
| 103 |
/* Separate path and name into p and q */ |
| 104 |
if ((q = strrchr(p, '/')) != NULL && (q == p || q[-1] != ':')) |
| 105 |
{ |
| 106 |
*q++ = '\0'; /* path/name -> path, name */ |
| 107 |
if (*p == '\0') /* path is just / */ |
| 108 |
p = strcpy(v, "/."); |
| 109 |
} |
| 110 |
else if ((q = strrchr(p, ':')) != NULL) |
| 111 |
{ /* has device and no or root path */ |
| 112 |
*q++ = '\0'; |
| 113 |
p = strcat(strcpy(v, p), ":"); /* copy device as path */ |
| 114 |
if (*q == '/') /* -> device:/., name */ |
| 115 |
{ |
| 116 |
strcat(p, "/"); |
| 117 |
q++; |
| 118 |
} |
| 119 |
strcat(p, "."); |
| 120 |
} |
| 121 |
else if (recurse && (strcmp(p, ".") == 0 || strcmp(p, "..") == 0)) |
| 122 |
{ /* current or parent directory */ |
| 123 |
/* I can't understand Mark's code so I am adding a hack here to get |
| 124 |
* "zip -r foo ." to work. Allow the dubious "zip -r foo .." but |
| 125 |
* reject "zip -rm foo ..". |
| 126 |
*/ |
| 127 |
if (dispose && strcmp(p, "..") == 0) |
| 128 |
ziperr(ZE_PARMS, "cannot remove parent directory"); |
| 129 |
q = "*"; |
| 130 |
} |
| 131 |
else /* no path or device */ |
| 132 |
{ |
| 133 |
q = p; |
| 134 |
p = strcpy(v, "."); |
| 135 |
} |
| 136 |
if (recurse && *q == '\0') { |
| 137 |
q = "*"; |
| 138 |
} |
| 139 |
/* Search that level for matching names */ |
| 140 |
if ((d = opendir(p)) == NULL) |
| 141 |
{ |
| 142 |
free((zvoid *)a); |
| 143 |
return ZE_MISS; |
| 144 |
} |
| 145 |
if ((r = strlen(p)) > 1 && |
| 146 |
(strcmp(p + r - 2, ":.") == 0 || strcmp(p + r - 2, "/.") == 0)) |
| 147 |
*(p + r - 1) = '\0'; |
| 148 |
f = 0; |
| 149 |
while ((e = readd(d)) != NULL) { |
| 150 |
if (strcmp(e, ".") && strcmp(e, "..") && MATCH(q, e, 0)) |
| 151 |
{ |
| 152 |
f = 1; |
| 153 |
if (strcmp(p, ".") == 0) { /* path is . */ |
| 154 |
r = procname(e, 0); /* name is name */ |
| 155 |
if (r) { |
| 156 |
f = 0; |
| 157 |
break; |
| 158 |
} |
| 159 |
} else |
| 160 |
{ |
| 161 |
if ((n = malloc(strlen(p) + strlen(e) + 2)) == NULL) |
| 162 |
{ |
| 163 |
free((zvoid *)a); |
| 164 |
closedir(d); |
| 165 |
return ZE_MEM; |
| 166 |
} |
| 167 |
n = strcpy(n, p); |
| 168 |
if (n[r = strlen(n) - 1] != '/' && n[r] != ':') |
| 169 |
strcat(n, "/"); |
| 170 |
r = procname(strcat(n, e), 0); /* name is path/name */ |
| 171 |
free((zvoid *)n); |
| 172 |
if (r) { |
| 173 |
f = 0; |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
closedir(d); |
| 180 |
|
| 181 |
/* Done */ |
| 182 |
free((zvoid *)a); |
| 183 |
return f ? ZE_OK : ZE_MISS; |
| 184 |
} |
| 185 |
|
| 186 |
int procname(n, caseflag) |
| 187 |
char *n; /* name to process */ |
| 188 |
int caseflag; /* true to force case-sensitive match */ |
| 189 |
/* Process a name or sh expression to operate on (or exclude). Return |
| 190 |
an error code in the ZE_ class. */ |
| 191 |
{ |
| 192 |
char *a; /* path and name for recursion */ |
| 193 |
DIR *d; /* directory stream from opendir() */ |
| 194 |
char *e; /* pointer to name from readd() */ |
| 195 |
int m; /* matched flag */ |
| 196 |
char *p; /* path for recursion */ |
| 197 |
struct stat s; /* result of stat() */ |
| 198 |
struct zlist far *z; /* steps through zfiles list */ |
| 199 |
|
| 200 |
if (n == NULL) /* volume_label request in freshen|delete mode ?? */ |
| 201 |
return ZE_OK; |
| 202 |
|
| 203 |
if (strcmp(n, "-") == 0) /* if compressing stdin */ |
| 204 |
return newname(n, 0, caseflag); |
| 205 |
else if (LSSTAT(n, &s) |
| 206 |
#if defined(__TURBOC__) || defined(__WATCOMC__) |
| 207 |
/* For these 2 compilers, stat() succeeds on wild card names! */ |
| 208 |
|| isshexp(n) |
| 209 |
#endif |
| 210 |
) |
| 211 |
{ |
| 212 |
/* Not a file or directory--search for shell expression in zip file */ |
| 213 |
p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */ |
| 214 |
m = 1; |
| 215 |
for (z = zfiles; z != NULL; z = z->nxt) { |
| 216 |
if (MATCH(p, z->iname, caseflag)) |
| 217 |
{ |
| 218 |
z->mark = pcount ? filter(z->zname, caseflag) : 1; |
| 219 |
if (verbose) |
| 220 |
fprintf(mesg, "zip diagnostic: %scluding %s\n", |
| 221 |
z->mark ? "in" : "ex", z->name); |
| 222 |
m = 0; |
| 223 |
} |
| 224 |
} |
| 225 |
free((zvoid *)p); |
| 226 |
return m ? ZE_MISS : ZE_OK; |
| 227 |
} |
| 228 |
|
| 229 |
/* Live name--use if file, recurse if directory */ |
| 230 |
for (p = n; *p; p++) /* use / consistently */ |
| 231 |
if (*p == '\\') |
| 232 |
*p = '/'; |
| 233 |
if ((s.st_mode & S_IFDIR) == 0) |
| 234 |
{ |
| 235 |
/* add or remove name of file */ |
| 236 |
if ((m = newname(n, 0, caseflag)) != ZE_OK) |
| 237 |
return m; |
| 238 |
} else { |
| 239 |
/* Add trailing / to the directory name */ |
| 240 |
if ((p = malloc(strlen(n)+2)) == NULL) |
| 241 |
return ZE_MEM; |
| 242 |
if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) { |
| 243 |
*p = '\0'; /* avoid "./" prefix and do not create zip entry */ |
| 244 |
} else { |
| 245 |
strcpy(p, n); |
| 246 |
a = p + strlen(p); |
| 247 |
if (a[-1] != '/') |
| 248 |
strcpy(a, "/"); |
| 249 |
if (dirnames && (m = newname(p, 1, caseflag)) != ZE_OK) { |
| 250 |
free((zvoid *)p); |
| 251 |
return m; |
| 252 |
} |
| 253 |
} |
| 254 |
/* recurse into directory */ |
| 255 |
if (recurse && (d = opendir(n)) != NULL) |
| 256 |
{ |
| 257 |
while ((e = readd(d)) != NULL) { |
| 258 |
if (strcmp(e, ".") && strcmp(e, "..")) |
| 259 |
{ |
| 260 |
if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL) |
| 261 |
{ |
| 262 |
closedir(d); |
| 263 |
free((zvoid *)p); |
| 264 |
return ZE_MEM; |
| 265 |
} |
| 266 |
strcat(strcpy(a, p), e); |
| 267 |
if ((m = procname(a, caseflag)) != ZE_OK) /* recurse on name */ |
| 268 |
{ |
| 269 |
if (m == ZE_MISS) |
| 270 |
zipwarn("name not matched: ", a); |
| 271 |
else |
| 272 |
ziperr(m, a); |
| 273 |
} |
| 274 |
free((zvoid *)a); |
| 275 |
} |
| 276 |
} |
| 277 |
closedir(d); |
| 278 |
} |
| 279 |
free((zvoid *)p); |
| 280 |
} /* (s.st_mode & S_IFDIR) == 0) */ |
| 281 |
return ZE_OK; |
| 282 |
} |
| 283 |
|
| 284 |
char *ex2in(x, isdir, pdosflag) |
| 285 |
char *x; /* external file name */ |
| 286 |
int isdir; /* input: x is a directory */ |
| 287 |
int *pdosflag; /* output: force MSDOS file attributes? */ |
| 288 |
/* Convert the external file name to a zip file name, returning the malloc'ed |
| 289 |
string or NULL if not enough memory. */ |
| 290 |
{ |
| 291 |
char *n; /* internal file name (malloc'ed) */ |
| 292 |
char *t; /* shortened name */ |
| 293 |
int dosflag; |
| 294 |
|
| 295 |
dosflag = dosify || IsFileSystemFAT(x) || (x == label); |
| 296 |
if (!dosify && use_longname_ea && (t = GetLongPathEA(x)) != NULL) |
| 297 |
{ |
| 298 |
x = t; |
| 299 |
dosflag = 0; |
| 300 |
} |
| 301 |
|
| 302 |
/* Find starting point in name before doing malloc */ |
| 303 |
/* Strip drive specification */ |
| 304 |
t = *x && *(x + 1) == ':' ? x + 2 : x; |
| 305 |
/* Strip "//host/share/" part of a UNC name */ |
| 306 |
if ((!strncmp(x,"//",2) || !strncmp(x,"\\\\",2)) && |
| 307 |
(x[2] != '\0' && x[2] != '/' && x[2] != '\\')) { |
| 308 |
n = x + 2; |
| 309 |
while (*n != '\0' && *n != '/' && *n != '\\') |
| 310 |
n++; /* strip host name */ |
| 311 |
if (*n != '\0') { |
| 312 |
n++; |
| 313 |
while (*n != '\0' && *n != '/' && *n != '\\') |
| 314 |
n++; /* strip `share' name */ |
| 315 |
} |
| 316 |
if (*n != '\0') |
| 317 |
t = n + 1; |
| 318 |
} |
| 319 |
/* Strip leading "/" to convert an absolute path into a relative path */ |
| 320 |
while (*t == '/' || *t == '\\') |
| 321 |
t++; |
| 322 |
/* Strip leading "./" as well as drive letter */ |
| 323 |
while (*t == '.' && (t[1] == '/' || t[1] == '\\')) |
| 324 |
t += 2; |
| 325 |
|
| 326 |
/* Make changes, if any, to the copied name (leave original intact) */ |
| 327 |
for (n = t; *n; n++) |
| 328 |
if (*n == '\\') |
| 329 |
*n = '/'; |
| 330 |
|
| 331 |
if (!pathput) |
| 332 |
t = last(t, PATH_END); |
| 333 |
|
| 334 |
/* Malloc space for internal name and copy it */ |
| 335 |
if ((n = malloc(strlen(t) + 1)) == NULL) |
| 336 |
return NULL; |
| 337 |
strcpy(n, t); |
| 338 |
|
| 339 |
if (dosify) |
| 340 |
msname(n); |
| 341 |
|
| 342 |
/* Returned malloc'ed name */ |
| 343 |
if (pdosflag) |
| 344 |
*pdosflag = dosflag; |
| 345 |
return n; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
char *in2ex(n) |
| 350 |
char *n; /* internal file name */ |
| 351 |
/* Convert the zip file name to an external file name, returning the malloc'ed |
| 352 |
string or NULL if not enough memory. */ |
| 353 |
{ |
| 354 |
char *x; /* external file name */ |
| 355 |
|
| 356 |
if ((x = malloc(strlen(n) + 1 + PAD)) == NULL) |
| 357 |
return NULL; |
| 358 |
strcpy(x, n); |
| 359 |
|
| 360 |
return x; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
void stamp(f, d) |
| 365 |
char *f; /* name of file to change */ |
| 366 |
ulg d; /* dos-style time to change it to */ |
| 367 |
/* Set last updated and accessed time of file f to the DOS time d. */ |
| 368 |
{ |
| 369 |
SetFileTime(f, d); |
| 370 |
} |
| 371 |
|
| 372 |
ulg filetime(f, a, n, t) |
| 373 |
char *f; /* name of file to get info on */ |
| 374 |
ulg *a; /* return value: file attributes */ |
| 375 |
long *n; /* return value: file size */ |
| 376 |
iztimes *t; /* return value: access, modific. and creation times */ |
| 377 |
/* If file *f does not exist, return 0. Else, return the file's last |
| 378 |
modified date and time as an MSDOS date and time. The date and |
| 379 |
time is returned in a long with the date most significant to allow |
| 380 |
unsigned integer comparison of absolute times. Also, if a is not |
| 381 |
a NULL pointer, store the file attributes there, with the high two |
| 382 |
bytes being the Unix attributes, and the low byte being a mapping |
| 383 |
of that to DOS attributes. If n is not NULL, store the file size |
| 384 |
there. If t is not NULL, the file's access, modification and creation |
| 385 |
times are stored there as UNIX time_t values. |
| 386 |
If f is "-", use standard input as the file. If f is a device, return |
| 387 |
a file size of -1 */ |
| 388 |
{ |
| 389 |
struct stat s; /* results of stat() */ |
| 390 |
char *name; |
| 391 |
ulg r; |
| 392 |
unsigned int len = strlen(f); |
| 393 |
int isstdin = !strcmp(f, "-"); |
| 394 |
|
| 395 |
if (f == label) { |
| 396 |
if (a != NULL) |
| 397 |
*a = label_mode; |
| 398 |
if (n != NULL) |
| 399 |
*n = -2L; /* convention for a label name */ |
| 400 |
if (t != NULL) |
| 401 |
t->atime = t->mtime = t->ctime = label_utim; |
| 402 |
return label_time; |
| 403 |
} |
| 404 |
|
| 405 |
if ((name = malloc(len + 1)) == NULL) { |
| 406 |
ZIPERR(ZE_MEM, "filetime"); |
| 407 |
} |
| 408 |
strcpy(name, f); |
| 409 |
if (name[len - 1] == '/') |
| 410 |
name[len - 1] = '\0'; |
| 411 |
/* not all systems allow stat'ing a file with / appended */ |
| 412 |
|
| 413 |
if (isstdin) { |
| 414 |
/* it is common for some PC based compilers to |
| 415 |
fail with fstat() on devices or pipes */ |
| 416 |
if (fstat(fileno(stdin), &s) != 0) { |
| 417 |
s.st_mode = S_IFREG; s.st_size = -1L; |
| 418 |
} |
| 419 |
time(&s.st_ctime); |
| 420 |
s.st_atime = s.st_mtime = s.st_ctime; |
| 421 |
} else if (LSSTAT(name, &s) != 0) { |
| 422 |
/* Accept about any file kind including directories |
| 423 |
* (stored with trailing / with -r option) |
| 424 |
*/ |
| 425 |
free(name); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
if (a != NULL) { |
| 430 |
*a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name)); |
| 431 |
} |
| 432 |
if (n != NULL) |
| 433 |
*n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L; |
| 434 |
#ifdef __WATCOMC__ |
| 435 |
/* of course, Watcom always has to make an exception */ |
| 436 |
if (s.st_atime == 312764400) |
| 437 |
s.st_atime = s.st_mtime; |
| 438 |
if (s.st_ctime == 312764400) |
| 439 |
s.st_ctime = s.st_mtime; |
| 440 |
#endif |
| 441 |
if (t != NULL) { |
| 442 |
t->atime = s.st_atime; |
| 443 |
t->mtime = s.st_mtime; |
| 444 |
t->ctime = s.st_ctime; |
| 445 |
} |
| 446 |
|
| 447 |
r = GetFileTime(name); |
| 448 |
free(name); |
| 449 |
|
| 450 |
return r; |
| 451 |
} |
| 452 |
|
| 453 |
int deletedir(d) |
| 454 |
char *d; /* directory to delete */ |
| 455 |
/* Delete the directory *d if it is empty, do nothing otherwise. |
| 456 |
Return the result of rmdir(), delete(), or system(). |
| 457 |
*/ |
| 458 |
{ |
| 459 |
return rmdir(d); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
#if defined MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */ |
| 464 |
|
| 465 |
#ifdef MSC /* Microsoft C */ |
| 466 |
|
| 467 |
zvoid far *zcalloc (unsigned items, unsigned size) |
| 468 |
{ |
| 469 |
return (zvoid far *)halloc((long)items, size); |
| 470 |
} |
| 471 |
|
| 472 |
zvoid zcfree (zvoid far *ptr) |
| 473 |
{ |
| 474 |
hfree((void huge *)ptr); |
| 475 |
} |
| 476 |
|
| 477 |
#endif /* MSC */ |
| 478 |
|
| 479 |
#endif /* MY_ZCALLOC */ |
| 480 |
|
| 481 |
#endif /* !UTIL */ |