| 1 |
/* |
| 2 |
Copyright (c) 1990-1999 Info-ZIP. All rights reserved. |
| 3 |
|
| 4 |
See the accompanying file LICENSE, version 1999-Oct-05 or later |
| 5 |
(the contents of which are also included in zip.h) for terms of use. |
| 6 |
If, for some reason, both of these files are missing, the Info-ZIP license |
| 7 |
also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html |
| 8 |
*/ |
| 9 |
/*--------------------------------------------------------------------------- |
| 10 |
|
| 11 |
macos.c |
| 12 |
|
| 13 |
Macintosh-specific routines for use with Info-ZIP's Zip 2.3 and later. |
| 14 |
|
| 15 |
---------------------------------------------------------------------------*/ |
| 16 |
|
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
/* Includes */ |
| 20 |
/*****************************************************************************/ |
| 21 |
|
| 22 |
#include "zip.h" |
| 23 |
|
| 24 |
#include "revision.h" |
| 25 |
#include "crypt.h" |
| 26 |
|
| 27 |
#include <signal.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <sound.h> |
| 31 |
|
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include <Strings.h> |
| 35 |
#include <setjmp.h> |
| 36 |
|
| 37 |
/* #include "charmap.h" */ |
| 38 |
#include "helpers.h" |
| 39 |
#include "macstuff.h" |
| 40 |
#include "pathname.h" |
| 41 |
#include "recurse.h" |
| 42 |
|
| 43 |
|
| 44 |
/*****************************************************************************/ |
| 45 |
/* Macros, typedefs */ |
| 46 |
/*****************************************************************************/ |
| 47 |
|
| 48 |
#define PATH_END MacPathEnd |
| 49 |
|
| 50 |
/*****************************************************************************/ |
| 51 |
/* Global Vars */ |
| 52 |
/*****************************************************************************/ |
| 53 |
|
| 54 |
int error_level; /* used only in ziperr() */ |
| 55 |
|
| 56 |
|
| 57 |
/* Note: sizeof() returns the size of this allusion |
| 58 |
13 is current length of "XtraStuf.mac:" */ |
| 59 |
extern const char ResourceMark[13]; /* var is initialized in file pathname.c */ |
| 60 |
|
| 61 |
|
| 62 |
extern jmp_buf EnvForExit; |
| 63 |
MacZipGlobals MacZip; |
| 64 |
|
| 65 |
unsigned long count_of_Zippedfiles = 0; |
| 66 |
|
| 67 |
|
| 68 |
/*****************************************************************************/ |
| 69 |
/* Module level Vars */ |
| 70 |
/*****************************************************************************/ |
| 71 |
|
| 72 |
static const char MacPathEnd = ':'; /* the Macintosh dir separator */ |
| 73 |
|
| 74 |
/* Inform Progress vars */ |
| 75 |
long estTicksToFinish; |
| 76 |
long createTime; |
| 77 |
long updateTicks; |
| 78 |
|
| 79 |
static char *Time_Est_strings[] = { |
| 80 |
"Zipping Files; Items done:", |
| 81 |
"More than 24 hours", |
| 82 |
"More than %s hours", |
| 83 |
"About %s hours, %s minutes", |
| 84 |
"About an hour", |
| 85 |
"Less than an hour", |
| 86 |
"About %s minutes, %s seconds", |
| 87 |
"About a minute", |
| 88 |
"Less than a minute", |
| 89 |
"About %s seconds", |
| 90 |
"About a second", |
| 91 |
"About 1 minute, %s seconds"}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
/*****************************************************************************/ |
| 96 |
/* Prototypes */ |
| 97 |
/*****************************************************************************/ |
| 98 |
|
| 99 |
int DoCurrentDir(void); |
| 100 |
|
| 101 |
void DoAboutBox(void); |
| 102 |
void DoQuit(void); |
| 103 |
void DoEventLoop(void); |
| 104 |
|
| 105 |
void ZipInitAllVars(void); |
| 106 |
void UserStop(void); |
| 107 |
Boolean IsZipFile(char *name); |
| 108 |
|
| 109 |
static long EstimateCompletionTime(const long progressMax, |
| 110 |
const long progressSoFar, unsigned char percent); |
| 111 |
static void UpdateTimeToComplete(void); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#ifdef USE_SIOUX |
| 117 |
#include <sioux.h> |
| 118 |
void DoWarnUserDupVol( char *FullPath ); |
| 119 |
|
| 120 |
/*****************************************************************************/ |
| 121 |
/* Functions */ |
| 122 |
/*****************************************************************************/ |
| 123 |
|
| 124 |
/* |
| 125 |
** Standalone Unzip with Metrowerks SIOUX starts here |
| 126 |
** |
| 127 |
*/ |
| 128 |
int main(int argc, char **argv) |
| 129 |
{ |
| 130 |
int return_code; |
| 131 |
|
| 132 |
SIOUXSettings.asktosaveonclose = FALSE; |
| 133 |
SIOUXSettings.showstatusline = TRUE; |
| 134 |
|
| 135 |
SIOUXSettings.columns = 100; |
| 136 |
SIOUXSettings.rows = 40; |
| 137 |
|
| 138 |
/* 30 = MacZip Johnny Lee's; 40 = new (my) MacZip */ |
| 139 |
MacZip.MacZipMode = NewZipMode_EF; |
| 140 |
|
| 141 |
argc = ccommand(&argv); |
| 142 |
if (verbose) PrintArguments(argc, argv); |
| 143 |
|
| 144 |
ZipInitAllVars(); |
| 145 |
|
| 146 |
return_code = zipmain(argc, argv); |
| 147 |
|
| 148 |
if (verbose) printf("\n\n Finish"); |
| 149 |
return return_code; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
/* |
| 155 |
** SIOUX needs no extra event handling |
| 156 |
** |
| 157 |
*/ |
| 158 |
|
| 159 |
void UserStop(void) |
| 160 |
{ |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
/* |
| 167 |
** Password enter function '*' printed for each char |
| 168 |
** |
| 169 |
*/ |
| 170 |
|
| 171 |
int macgetch(void) |
| 172 |
{ |
| 173 |
WindowPtr whichWindow; |
| 174 |
EventRecord theEvent; |
| 175 |
char c; /* one-byte buffer for read() to use */ |
| 176 |
|
| 177 |
do { |
| 178 |
SystemTask(); |
| 179 |
if (!GetNextEvent(everyEvent, &theEvent)) |
| 180 |
theEvent.what = nullEvent; |
| 181 |
else { |
| 182 |
switch (theEvent.what) { |
| 183 |
case keyDown: |
| 184 |
c = theEvent.message & charCodeMask; |
| 185 |
break; |
| 186 |
case mouseDown: |
| 187 |
if (FindWindow(theEvent.where, &whichWindow) == |
| 188 |
inSysWindow) |
| 189 |
SystemClick(&theEvent, whichWindow); |
| 190 |
break; |
| 191 |
case updateEvt: |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
} while (theEvent.what != keyDown); |
| 196 |
|
| 197 |
printf("*"); |
| 198 |
fflush(stdout); |
| 199 |
|
| 200 |
return (int)c; |
| 201 |
} |
| 202 |
|
| 203 |
#endif |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
/******************************/ |
| 209 |
/* Function version_local() */ |
| 210 |
/******************************/ |
| 211 |
|
| 212 |
/* |
| 213 |
** Print Compilers version and compile time/date |
| 214 |
** |
| 215 |
*/ |
| 216 |
|
| 217 |
void version_local() |
| 218 |
{ |
| 219 |
/* prints e.g: |
| 220 |
Compiled with Metrowerks CodeWarrior version 2000 for PowerPC Processor |
| 221 |
compile time: Feb 4 1998 17:49:49. |
| 222 |
*/ |
| 223 |
|
| 224 |
static ZCONST char CompiledWith[] = |
| 225 |
"\n\nCompiled with %s %x for %s \n %s %s %s.\n\n"; |
| 226 |
|
| 227 |
printf(CompiledWith, |
| 228 |
|
| 229 |
|
| 230 |
#ifdef __MWERKS__ |
| 231 |
" Metrowerks CodeWarrior version", __MWERKS__, |
| 232 |
#endif |
| 233 |
|
| 234 |
|
| 235 |
#ifdef __MC68K__ |
| 236 |
" MC68K Processor", |
| 237 |
#else |
| 238 |
" PowerPC Processor", |
| 239 |
#endif |
| 240 |
|
| 241 |
#ifdef __DATE__ |
| 242 |
"compile time: ", __DATE__, __TIME__ |
| 243 |
#else |
| 244 |
"", "", "" |
| 245 |
#endif |
| 246 |
); |
| 247 |
} /* end function version_local() */ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
/* |
| 254 |
** Deletes a dir if the switch '-m' is used |
| 255 |
** |
| 256 |
*/ |
| 257 |
|
| 258 |
int deletedir(char *path) |
| 259 |
{ |
| 260 |
static char Num = 0; |
| 261 |
static FSSpec trashfolder; |
| 262 |
static Boolean FirstCall = true; |
| 263 |
static Boolean Immediate_File_Deletion = false; |
| 264 |
OSErr err; |
| 265 |
FSSpec dirToDelete; |
| 266 |
char currpath[NAME_MAX], *envptr; |
| 267 |
CInfoPBRec fpb; |
| 268 |
|
| 269 |
/* init this function */ |
| 270 |
if ((path == NULL) || |
| 271 |
(strlen(path) == 0)) |
| 272 |
{ |
| 273 |
Num = 0; |
| 274 |
FirstCall = true; |
| 275 |
return -1; |
| 276 |
} |
| 277 |
|
| 278 |
UserStop(); |
| 279 |
|
| 280 |
GetCompletePath(currpath,path,&dirToDelete, &err); |
| 281 |
|
| 282 |
if (FirstCall == true) |
| 283 |
{ |
| 284 |
FirstCall = false; |
| 285 |
envptr = getenv("Immediate_File_Deletion"); |
| 286 |
if (!(envptr == (char *)NULL || *envptr == '\0')) |
| 287 |
{ |
| 288 |
if (stricmp(envptr,"yes") == 0) |
| 289 |
Immediate_File_Deletion = true; |
| 290 |
else |
| 291 |
Immediate_File_Deletion = false; |
| 292 |
} |
| 293 |
err = FSpFindFolder(dirToDelete.vRefNum, kTrashFolderType, |
| 294 |
kDontCreateFolder,&trashfolder); |
| 295 |
printerr("FSpFindFolder:",err,err,__LINE__,__FILE__,path); |
| 296 |
} |
| 297 |
|
| 298 |
fpb.dirInfo.ioNamePtr = dirToDelete.name; |
| 299 |
fpb.dirInfo.ioVRefNum = dirToDelete.vRefNum; |
| 300 |
fpb.dirInfo.ioDrDirID = dirToDelete.parID; |
| 301 |
fpb.dirInfo.ioFDirIndex = 0; |
| 302 |
|
| 303 |
err = PBGetCatInfoSync(&fpb); |
| 304 |
printerr("PBGetCatInfo deletedir ", err, err, |
| 305 |
__LINE__, __FILE__, ""); |
| 306 |
|
| 307 |
if (fpb.dirInfo.ioDrNmFls > 0) |
| 308 |
{ |
| 309 |
return 0; /* do not move / delete folders which are not empty */ |
| 310 |
} |
| 311 |
|
| 312 |
if (Immediate_File_Deletion) |
| 313 |
{ |
| 314 |
err = FSpDelete(&dirToDelete); |
| 315 |
return err; |
| 316 |
} |
| 317 |
|
| 318 |
err = CatMove (dirToDelete.vRefNum, dirToDelete.parID, |
| 319 |
dirToDelete.name, trashfolder.parID, trashfolder.name); |
| 320 |
|
| 321 |
/* -48 = file is already existing so we have to rename it before |
| 322 |
moving the file */ |
| 323 |
if (err == -48) |
| 324 |
{ |
| 325 |
Num++; |
| 326 |
if (dirToDelete.name[0] >= 28) /* cut foldername if to long */ |
| 327 |
dirToDelete.name[0] = 28; |
| 328 |
P2CStr(dirToDelete.name); |
| 329 |
sprintf(currpath,"%s~%d",(char *)dirToDelete.name,Num); |
| 330 |
C2PStr(currpath); |
| 331 |
C2PStr((char *)dirToDelete.name); |
| 332 |
err = HRename (dirToDelete.vRefNum, dirToDelete.parID, |
| 333 |
dirToDelete.name, (unsigned char *) currpath); |
| 334 |
|
| 335 |
err = CatMove (dirToDelete.vRefNum, dirToDelete.parID, |
| 336 |
(unsigned char *) currpath, trashfolder.parID, |
| 337 |
trashfolder.name); |
| 338 |
} |
| 339 |
|
| 340 |
return err; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
/* |
| 347 |
** Set the file-type so the archive will get the correct icon, type |
| 348 |
** and creator code. |
| 349 |
*/ |
| 350 |
|
| 351 |
void setfiletype(char *new_f, unsigned long Creator, unsigned long Type) |
| 352 |
{ |
| 353 |
OSErr err; |
| 354 |
|
| 355 |
if (strcmp(zipfile, new_f) == 0) |
| 356 |
err = FSpChangeCreatorType(&MacZip.ZipFileSpec, Creator, Type); |
| 357 |
printerr("FSpChangeCreatorType:", err, err, __LINE__, __FILE__, new_f); |
| 358 |
|
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
/* |
| 367 |
** Convert the external (native) filename into Zip's internal Unix compatible |
| 368 |
** name space. |
| 369 |
*/ |
| 370 |
|
| 371 |
char *ex2in(char *externalFilen, int isdir, int *pdosflag) |
| 372 |
/* char *externalFilen external file name */ |
| 373 |
/* int isdir input: externalFilen is a directory */ |
| 374 |
/* int *pdosflag output: force MSDOS file attributes? */ |
| 375 |
/* Convert the external file name to a zip file name, returning the malloc'ed |
| 376 |
string or NULL if not enough memory. */ |
| 377 |
{ |
| 378 |
char *internalFilen; /* internal file name (malloc'ed) */ |
| 379 |
char *t; /* shortened name */ |
| 380 |
char *Pathname; |
| 381 |
char buffer[NAME_MAX]; |
| 382 |
int dosflag; |
| 383 |
|
| 384 |
AssertStr(externalFilen, externalFilen) |
| 385 |
AssertBool(isdir,"") |
| 386 |
|
| 387 |
dosflag = dosify; /* default for non-DOS and non-OS/2 */ |
| 388 |
|
| 389 |
/* Find starting point in name before doing malloc */ |
| 390 |
for (t = externalFilen; *t == PATH_END; t++) |
| 391 |
; |
| 392 |
|
| 393 |
if (!MacZip.StoreFullPath) |
| 394 |
{ |
| 395 |
Pathname = StripPartialDir(buffer, MacZip.SearchDir,t); |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
Pathname = t; |
| 400 |
} |
| 401 |
|
| 402 |
/* Make changes, if any, to the copied name (leave original intact) */ |
| 403 |
if (!pathput) |
| 404 |
{ |
| 405 |
t = last(Pathname, PATH_END); |
| 406 |
} |
| 407 |
else t = Pathname; |
| 408 |
|
| 409 |
/* Malloc space for internal name and copy it */ |
| 410 |
if ((internalFilen = malloc(strlen(t) + 10 + strlen(ResourceMark) )) == NULL) |
| 411 |
return NULL; |
| 412 |
|
| 413 |
sstrcpy(internalFilen, t); |
| 414 |
|
| 415 |
/* we have to eliminate illegal chars: |
| 416 |
* The name space for Mac filenames and Zip filenames (unix style names) |
| 417 |
* do both include all printable extended-ASCII characters. The only |
| 418 |
* difference we have to take care of is the single special character |
| 419 |
* used as path delimiter: |
| 420 |
* ':' on MacOS and '/' on Unix and '\' on Dos. |
| 421 |
* So, to convert between Mac filenames and Unix filenames without any |
| 422 |
* loss of information, we simply interchange ':' and '/'. Additionally, |
| 423 |
* we try to convert the coding of the extended-ASCII characters into |
| 424 |
* InfoZip's standard ISO 8859-1 codepage table. |
| 425 |
*/ |
| 426 |
MakeCompatibleString(internalFilen, ':', '/', '/', ':', |
| 427 |
MacZip.CurrTextEncodingBase); |
| 428 |
|
| 429 |
/* Returned malloc'ed name */ |
| 430 |
if (pdosflag) |
| 431 |
*pdosflag = dosflag; |
| 432 |
|
| 433 |
if (isdir) |
| 434 |
{ |
| 435 |
return internalFilen; /* avoid warning on unused variable */ |
| 436 |
} |
| 437 |
|
| 438 |
if (dosify) |
| 439 |
{ |
| 440 |
msname(internalFilen); |
| 441 |
printf("\n ex2in: %s",internalFilen); |
| 442 |
} |
| 443 |
|
| 444 |
return internalFilen; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
/* |
| 450 |
** Collect all filenames. Go through all directories |
| 451 |
** |
| 452 |
*/ |
| 453 |
|
| 454 |
int wild(char *Pathpat) |
| 455 |
/* path/pattern to match */ |
| 456 |
/* If not in exclude mode, expand the pattern based on the contents of the |
| 457 |
file system. Return an error code in the ZE_ class. */ |
| 458 |
{ |
| 459 |
FSSpec Spec; |
| 460 |
char fullpath[NAME_MAX]; |
| 461 |
OSErr err; |
| 462 |
|
| 463 |
AssertStr(Pathpat, Pathpat); |
| 464 |
|
| 465 |
if (noisy) printf("%s \n\n",GetZipVersionsInfo()); |
| 466 |
|
| 467 |
if (extra_fields == 0) |
| 468 |
{ |
| 469 |
MacZip.DataForkOnly = true; |
| 470 |
} |
| 471 |
|
| 472 |
/* for switch '-R' -> '.' means current dir */ |
| 473 |
if (strcmp(Pathpat,".") == 0) sstrcpy(Pathpat,"*"); |
| 474 |
|
| 475 |
sstrcpy(MacZip.Pattern,Pathpat); |
| 476 |
|
| 477 |
if (recurse) |
| 478 |
{ |
| 479 |
MacZip.StoreFoldersAlso = true; |
| 480 |
MacZip.SearchLevels = 0; /* if 0 we aren't checking levels */ |
| 481 |
} |
| 482 |
else |
| 483 |
{ |
| 484 |
MacZip.StoreFoldersAlso = false; |
| 485 |
MacZip.SearchLevels = 1; |
| 486 |
} |
| 487 |
|
| 488 |
/* make complete path */ |
| 489 |
GetCompletePath(fullpath, MacZip.Pattern, &Spec,&err); |
| 490 |
err = PrintUserHFSerr((err != -43) && (err != 0), err, MacZip.Pattern); |
| 491 |
printerr("GetCompletePath:", err, err, __LINE__, __FILE__, fullpath); |
| 492 |
|
| 493 |
/* extract the filepattern */ |
| 494 |
GetFilename(MacZip.Pattern, fullpath); |
| 495 |
|
| 496 |
/* extract Path and get FSSpec of search-path */ |
| 497 |
/* get FSSpec of search-path ; we need a dir to start |
| 498 |
searching for filenames */ |
| 499 |
TruncFilename(MacZip.SearchDir, fullpath); |
| 500 |
GetCompletePath(MacZip.SearchDir, MacZip.SearchDir, &Spec,&err); |
| 501 |
|
| 502 |
if (noisy) { |
| 503 |
if (MacZip.SearchLevels == 0) |
| 504 |
{ |
| 505 |
printf("\nSearch Pattern: [%s] Levels: all", MacZip.Pattern); |
| 506 |
} |
| 507 |
else |
| 508 |
{ |
| 509 |
printf("\nSearch Pattern: [%s] Levels: %d", MacZip.Pattern, |
| 510 |
MacZip.SearchLevels); |
| 511 |
} |
| 512 |
printf("\nSearch Path: [%s]", MacZip.SearchDir); |
| 513 |
printf("\nZip-File: [%s] \n",MacZip.ZipFullPath); |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
/* we are working only with pathnames; |
| 518 |
* this can cause big problems on a mac ... |
| 519 |
*/ |
| 520 |
if (CheckMountedVolumes(MacZip.SearchDir) > 1) |
| 521 |
DoWarnUserDupVol(MacZip.SearchDir); |
| 522 |
|
| 523 |
/* start getting all filenames */ |
| 524 |
err = FSpRecurseDirectory(&Spec, MacZip.SearchLevels); |
| 525 |
printerr("FSpRecurseDirectory:", err, err, __LINE__, __FILE__, ""); |
| 526 |
|
| 527 |
return ZE_OK; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
/* |
| 533 |
** Convert the internal filename into a external (native). |
| 534 |
** The user will see this modified filename. |
| 535 |
** For more performance: |
| 536 |
** I do not completly switch back to the native macos filename. |
| 537 |
** The user will still see directory separator '/' and the converted |
| 538 |
** charset. |
| 539 |
*/ |
| 540 |
|
| 541 |
char *in2ex(char *n) /* internal file name */ |
| 542 |
/* Convert the zip file name to an external file name, returning the malloc'ed |
| 543 |
string or NULL if not enough memory. */ |
| 544 |
{ |
| 545 |
char *x; /* external file name */ |
| 546 |
|
| 547 |
AssertStr(n,n) |
| 548 |
|
| 549 |
if ((x = malloc(strlen(n) + 1)) == NULL) |
| 550 |
return NULL; |
| 551 |
|
| 552 |
RfDfFilen2Real(x, n, MacZip.MacZipMode, MacZip.DataForkOnly, |
| 553 |
&MacZip.CurrentFork); |
| 554 |
|
| 555 |
return x; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
/* |
| 562 |
** Process on filenames. This function will be called to collect |
| 563 |
** the filenames. |
| 564 |
*/ |
| 565 |
|
| 566 |
int procname(char *filename, /* name to process */ |
| 567 |
int caseflag) /* true to force case-sensitive match |
| 568 |
(always false on a Mac) */ |
| 569 |
/* Process a name . Return |
| 570 |
an error code in the ZE_ class. */ |
| 571 |
{ |
| 572 |
int rc; /* matched flag */ |
| 573 |
|
| 574 |
AssertBool(caseflag,"caseflag") |
| 575 |
AssertStr(filename,filename) |
| 576 |
|
| 577 |
/* add or remove name of file */ |
| 578 |
rc = newname(filename, MacZip.isDirectory, caseflag); |
| 579 |
|
| 580 |
return rc; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
ulg filetime( |
| 587 |
char *f, /* name of file to get info on */ |
| 588 |
ulg *a, /* return value: file attributes */ |
| 589 |
long *n, /* return value: file size */ |
| 590 |
iztimes *t) /* return value: access, modific. and creation times */ |
| 591 |
/* If file *f does not exist, return 0. Else, return the file's last |
| 592 |
modified date and time as an MSDOS date and time. The date and |
| 593 |
time is returned in a long with the date most significant to allow |
| 594 |
unsigned integer comparison of absolute times. Also, if a is not |
| 595 |
a NULL pointer, store the file attributes there, with the high two |
| 596 |
bytes being the Unix attributes, and the low byte being a mapping |
| 597 |
of that to DOS attributes. If n is not NULL, store the file size |
| 598 |
there. If t is not NULL, the file's access, modification and creation |
| 599 |
times are stored there as UNIX time_t values. |
| 600 |
If f is "-", use standard input as the file. If f is a device, return |
| 601 |
a file size of -1 */ |
| 602 |
{ |
| 603 |
struct stat s; /* results of stat() */ |
| 604 |
|
| 605 |
AssertStr(f,f) |
| 606 |
|
| 607 |
if (strlen(f) == 0) return 0; |
| 608 |
|
| 609 |
if (SSTAT(f, &s) != 0) |
| 610 |
/* Accept about any file kind including directories |
| 611 |
* (stored with trailing : with -r option) |
| 612 |
*/ |
| 613 |
return 0; |
| 614 |
|
| 615 |
if (a != NULL) { |
| 616 |
*a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE); |
| 617 |
if (MacZip.isDirectory) { |
| 618 |
*a |= MSDOS_DIR_ATTR; |
| 619 |
} |
| 620 |
} |
| 621 |
if (n != NULL) |
| 622 |
*n = (s.st_mode & UNX_IFMT) == UNX_IFREG ? s.st_size : -1L; |
| 623 |
if (t != NULL) { |
| 624 |
t->atime = s.st_atime; |
| 625 |
t->mtime = s.st_mtime; |
| 626 |
t->ctime = s.st_ctime; /* on Mac, st_ctime contains creation time! */ |
| 627 |
} |
| 628 |
|
| 629 |
return unix2dostime(&s.st_mtime); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
void stamp(char *f, ulg d) |
| 635 |
/* char *f; name of file to change */ |
| 636 |
/* ulg d; dos-style time to change it to */ |
| 637 |
/* Set last updated and accessed time of file f to the DOS time d. */ |
| 638 |
{ |
| 639 |
time_t u[2]; /* argument for utime() */ |
| 640 |
|
| 641 |
f = f; |
| 642 |
|
| 643 |
/* Convert DOS time to time_t format in u */ |
| 644 |
|
| 645 |
u[0] = u[1] = dos2unixtime(d); |
| 646 |
/* utime(f, u); */ |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
/* |
| 653 |
** return only the longest part of the path: |
| 654 |
** second parameter: Volume:test folder:second folder: |
| 655 |
** third parameter: Volume:test folder:second folder:third folder:file |
| 656 |
** result will be: third folder:file |
| 657 |
** first parameter: contains string buffer that will be used to prepend |
| 658 |
** the "resource mark" part in front of the result when |
| 659 |
** a resource fork is processed in "M3" mode. |
| 660 |
** |
| 661 |
*/ |
| 662 |
|
| 663 |
char *StripPartialDir(char *CompletePath, |
| 664 |
const char *PartialPath, const char *FullPath) |
| 665 |
{ |
| 666 |
const char *tmpPtr1 = PartialPath; |
| 667 |
const char *tmpPtr2 = FullPath; |
| 668 |
int result; |
| 669 |
|
| 670 |
Assert_it(CompletePath,"StripPartialDir","") |
| 671 |
AssertStrNoOverlap(FullPath,PartialPath,PartialPath) |
| 672 |
|
| 673 |
if (MacZip.DataForkOnly) |
| 674 |
{ |
| 675 |
tmpPtr2 += strlen(tmpPtr1); |
| 676 |
return (char *)tmpPtr2; |
| 677 |
} |
| 678 |
|
| 679 |
switch (MacZip.MacZipMode) |
| 680 |
{ |
| 681 |
case JohnnyLee_EF: |
| 682 |
{ |
| 683 |
tmpPtr2 += strlen(tmpPtr1); |
| 684 |
return (char *)tmpPtr2; |
| 685 |
break; |
| 686 |
} |
| 687 |
|
| 688 |
case NewZipMode_EF: |
| 689 |
{ /* determine Fork type */ |
| 690 |
result = strncmp(FullPath, ResourceMark, sizeof(ResourceMark)-2); |
| 691 |
if (result != 0) |
| 692 |
{ /* data fork */ |
| 693 |
MacZip.CurrentFork = DataFork; |
| 694 |
tmpPtr2 += strlen(tmpPtr1); |
| 695 |
return (char *)tmpPtr2; |
| 696 |
} |
| 697 |
else |
| 698 |
{ /* resource fork */ |
| 699 |
MacZip.CurrentFork = ResourceFork; |
| 700 |
sstrcpy(CompletePath, ResourceMark); |
| 701 |
tmpPtr2 += strlen(tmpPtr1); |
| 702 |
tmpPtr2 += sizeof(ResourceMark); |
| 703 |
sstrcat(CompletePath, tmpPtr2); |
| 704 |
return (char *)CompletePath; |
| 705 |
} |
| 706 |
break; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
return NULL; /* function should never reach this point */ |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
/* |
| 717 |
** Init all global variables |
| 718 |
** Must be called for each zip-run |
| 719 |
*/ |
| 720 |
|
| 721 |
void ZipInitAllVars(void) |
| 722 |
{ |
| 723 |
getcwd(MacZip.CurrentPath, sizeof(MacZip.CurrentPath)); |
| 724 |
/* MacZip.MacZipMode = JohnnyLee_EF; */ |
| 725 |
MacZip.MacZipMode = NewZipMode_EF; |
| 726 |
|
| 727 |
MacZip.DataForkOnly = false; |
| 728 |
MacZip.CurrentFork = NoFork; |
| 729 |
|
| 730 |
MacZip.StoreFoldersAlso = false; |
| 731 |
|
| 732 |
MacZip.FoundFiles = 0; |
| 733 |
MacZip.FoundDirectories = 0; |
| 734 |
MacZip.RawCountOfItems = 0; |
| 735 |
MacZip.BytesOfData = 0; |
| 736 |
|
| 737 |
MacZip.StoreFullPath = false; |
| 738 |
MacZip.StatingProgress = false; |
| 739 |
MacZip.IncludeInvisible = false; |
| 740 |
|
| 741 |
MacZip.isMacStatValid = false; |
| 742 |
|
| 743 |
MacZip.CurrTextEncodingBase = FontScript(); |
| 744 |
|
| 745 |
MacZip.HaveGMToffset = false; |
| 746 |
|
| 747 |
createTime = TickCount(); |
| 748 |
estTicksToFinish = -1; |
| 749 |
updateTicks = 0; |
| 750 |
|
| 751 |
/* init some functions */ |
| 752 |
IsZipFile(NULL); |
| 753 |
|
| 754 |
destroy(NULL); |
| 755 |
deletedir(NULL); |
| 756 |
ShowCounter(true); |
| 757 |
|
| 758 |
extra_fields = 1; |
| 759 |
error_level = 0; |
| 760 |
count_of_Zippedfiles = 0; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
/* |
| 767 |
** Get the findercomment and store it as file-comment in the Zip-file |
| 768 |
** |
| 769 |
*/ |
| 770 |
char *GetComment(char *filename) |
| 771 |
{ |
| 772 |
OSErr err; |
| 773 |
static char buffer[NAME_MAX]; |
| 774 |
char buffer2[NAME_MAX]; |
| 775 |
char *tmpPtr; |
| 776 |
|
| 777 |
if (filename == NULL) return NULL; |
| 778 |
|
| 779 |
/* now we can convert Unix-Path in HFS-Path */ |
| 780 |
for (tmpPtr = filename; *tmpPtr; tmpPtr++) |
| 781 |
if (*tmpPtr == '/') |
| 782 |
*tmpPtr = ':'; |
| 783 |
|
| 784 |
if (MacZip.StoreFullPath) |
| 785 |
{ /* filename is already a fullpath */ |
| 786 |
sstrcpy(buffer,filename); |
| 787 |
} |
| 788 |
else |
| 789 |
{ /* make a fullpath */ |
| 790 |
sstrcpy(buffer,MacZip.SearchDir); |
| 791 |
sstrcat(buffer,filename); |
| 792 |
} |
| 793 |
|
| 794 |
/* make fullpath and get FSSpec */ |
| 795 |
/* Unfortunately: I get only the converted filename here */ |
| 796 |
/* so filenames with extended characters can not be found */ |
| 797 |
GetCompletePath(buffer2,buffer, &MacZip.fileSpec, &err); |
| 798 |
printerr("GetCompletePath:",(err != -43) && (err != -120) && (err != 0) , |
| 799 |
err,__LINE__,__FILE__,buffer); |
| 800 |
|
| 801 |
err = FSpDTGetComment(&MacZip.fileSpec, (unsigned char *) buffer); |
| 802 |
printerr("FSpDTGetComment:", (err != -5012) && (err != 0), err, |
| 803 |
__LINE__, __FILE__, filename); |
| 804 |
P2CStr((unsigned char *) buffer); |
| 805 |
if (err == -5012) return NULL; /* no finder-comments found */ |
| 806 |
|
| 807 |
if (noisy) printf("\n%32s -> %s",filename, buffer); |
| 808 |
/* |
| 809 |
Beside the script change we need only to change 0x0d in 0x0a |
| 810 |
so the last two arguments are not needed and does nothing. |
| 811 |
*/ |
| 812 |
MakeCompatibleString(buffer, 0x0d, 0x0a, ' ', ' ', |
| 813 |
MacZip.CurrTextEncodingBase); |
| 814 |
|
| 815 |
return buffer; |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
/* |
| 822 |
** Print a progress indicator for stating the files |
| 823 |
** |
| 824 |
*/ |
| 825 |
|
| 826 |
void PrintStatProgress(char *msg) |
| 827 |
{ |
| 828 |
|
| 829 |
if (!noisy) return; /* do no output if noisy is false */ |
| 830 |
|
| 831 |
MacZip.StatingProgress = true; |
| 832 |
|
| 833 |
if (strcmp(msg,"done") == 0) |
| 834 |
{ |
| 835 |
MacZip.StatingProgress = false; |
| 836 |
printf("\n ... done \n\n"); |
| 837 |
} |
| 838 |
else printf("\n %s",msg); |
| 839 |
|
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
void InformProgress(const long progressMax, const long progressSoFar ) |
| 846 |
{ |
| 847 |
int curr_percent; |
| 848 |
char no_time[5] = "..."; |
| 849 |
|
| 850 |
curr_percent = percent(progressMax, progressSoFar); |
| 851 |
|
| 852 |
if (curr_percent < 95) |
| 853 |
{ |
| 854 |
estTicksToFinish = EstimateCompletionTime(progressMax, |
| 855 |
progressSoFar, curr_percent); |
| 856 |
} |
| 857 |
else |
| 858 |
{ |
| 859 |
rightStatusString(no_time); |
| 860 |
leftStatusString(no_time); |
| 861 |
} |
| 862 |
|
| 863 |
updateTicks = TickCount() + 60; |
| 864 |
return; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
void ShowCounter(Boolean reset) |
| 869 |
{ |
| 870 |
static char statusline[100]; |
| 871 |
static unsigned long filecount = 0; |
| 872 |
|
| 873 |
if (reset) |
| 874 |
{ |
| 875 |
filecount = 0; |
| 876 |
return; |
| 877 |
} |
| 878 |
|
| 879 |
if (noisy) |
| 880 |
{ |
| 881 |
sprintf(statusline, "%6d", filecount++); |
| 882 |
rightStatusString(statusline); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
static long EstimateCompletionTime(const long progressMax, |
| 888 |
const long progressSoFar, |
| 889 |
unsigned char curr_percent) |
| 890 |
{ |
| 891 |
long max = progressMax, value = progressSoFar; |
| 892 |
static char buf[100]; |
| 893 |
unsigned long ticksTakenSoFar = TickCount() - createTime; |
| 894 |
float currentRate = (float) ticksTakenSoFar / (float) value; |
| 895 |
long newEst = (long)( currentRate * (float)( max - value )); |
| 896 |
|
| 897 |
sprintf(buf, "%d [%d%%]",progressSoFar, curr_percent); |
| 898 |
rightStatusString(buf); |
| 899 |
|
| 900 |
estTicksToFinish = newEst; |
| 901 |
|
| 902 |
UpdateTimeToComplete(); |
| 903 |
|
| 904 |
return estTicksToFinish; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
static void UpdateTimeToComplete(void) |
| 912 |
{ |
| 913 |
short days, hours, minutes, seconds; |
| 914 |
char estStr[255]; |
| 915 |
Str15 xx, yy; |
| 916 |
short idx = 0; |
| 917 |
|
| 918 |
if ( estTicksToFinish == -1 ) |
| 919 |
return; |
| 920 |
|
| 921 |
days = estTicksToFinish / 5184000L; |
| 922 |
hours = ( estTicksToFinish - ( days * 5184000L )) / 216000L; |
| 923 |
minutes = ( estTicksToFinish - ( days * 5184000L ) - |
| 924 |
( hours * 216000L )) / 3600L; |
| 925 |
seconds = ( estTicksToFinish - ( days * 5184000L ) - |
| 926 |
( hours * 216000L ) - ( minutes * 3600L )) / 60L; |
| 927 |
|
| 928 |
xx[0] = 0; |
| 929 |
yy[0] = 0; |
| 930 |
|
| 931 |
if ( days ) |
| 932 |
{ |
| 933 |
/* "more than 24 hours" */ |
| 934 |
|
| 935 |
idx = 1; |
| 936 |
goto setEstTimeStr; |
| 937 |
} |
| 938 |
|
| 939 |
if ( hours >= 8 ) |
| 940 |
{ |
| 941 |
/* "more than x hours" */ |
| 942 |
|
| 943 |
NumToString( hours, xx ); |
| 944 |
idx = 2; |
| 945 |
goto setEstTimeStr; |
| 946 |
} |
| 947 |
|
| 948 |
if ( estTicksToFinish > 252000L ) /* > 1hr, 10 minutes */ |
| 949 |
{ |
| 950 |
/* "about x hours, y minutes" */ |
| 951 |
|
| 952 |
NumToString( hours, xx ); |
| 953 |
NumToString( minutes, yy ); |
| 954 |
idx = 3; |
| 955 |
goto setEstTimeStr; |
| 956 |
} |
| 957 |
|
| 958 |
if ( estTicksToFinish > 198000L ) /* > 55 minutes */ |
| 959 |
{ |
| 960 |
/* "about an hour" */ |
| 961 |
idx = 4; |
| 962 |
goto setEstTimeStr; |
| 963 |
} |
| 964 |
|
| 965 |
if ( estTicksToFinish > 144000L ) /* > 40 minutes */ |
| 966 |
{ |
| 967 |
/* "less than an hour" */ |
| 968 |
|
| 969 |
idx = 5; |
| 970 |
goto setEstTimeStr; |
| 971 |
} |
| 972 |
|
| 973 |
if ( estTicksToFinish > 4200L ) /* > 1 minute, 10 sec */ |
| 974 |
{ |
| 975 |
/* "about x minutes, y seconds */ |
| 976 |
|
| 977 |
NumToString( minutes, xx ); |
| 978 |
NumToString( seconds, yy ); |
| 979 |
|
| 980 |
if ( minutes == 1 ) |
| 981 |
idx = 11; |
| 982 |
else |
| 983 |
idx = 6; |
| 984 |
goto setEstTimeStr; |
| 985 |
} |
| 986 |
|
| 987 |
if ( estTicksToFinish > 3000L ) /* > 50 seconds */ |
| 988 |
{ |
| 989 |
/* "about a minute" */ |
| 990 |
|
| 991 |
idx = 7; |
| 992 |
goto setEstTimeStr; |
| 993 |
} |
| 994 |
|
| 995 |
if ( estTicksToFinish > 1500L ) /* > 25 seconds */ |
| 996 |
{ |
| 997 |
/* "less than a minute" */ |
| 998 |
|
| 999 |
idx = 8; |
| 1000 |
goto setEstTimeStr; |
| 1001 |
} |
| 1002 |
|
| 1003 |
if ( estTicksToFinish > 120L ) /* > 2 seconds */ |
| 1004 |
{ |
| 1005 |
NumToString( seconds, xx ); |
| 1006 |
idx = 9; |
| 1007 |
goto setEstTimeStr; |
| 1008 |
} |
| 1009 |
|
| 1010 |
idx = 10; |
| 1011 |
|
| 1012 |
setEstTimeStr: |
| 1013 |
sprintf(estStr,Time_Est_strings[idx],P2CStr(xx),P2CStr(yy)); |
| 1014 |
leftStatusString((char *)estStr); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
/* |
| 1022 |
** Just return the zip version |
| 1023 |
** |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
char *GetZipVersionsInfo(void) |
| 1027 |
{ |
| 1028 |
static char ZipVersion[100]; |
| 1029 |
|
| 1030 |
sprintf(ZipVersion, "Zip Module\n%d.%d%d%s of %s", Z_MAJORVER, Z_MINORVER, |
| 1031 |
Z_PATCHLEVEL, Z_BETALEVEL, REVDATE); |
| 1032 |
|
| 1033 |
return ZipVersion; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
#ifndef USE_SIOUX |
| 1040 |
|
| 1041 |
/* |
| 1042 |
** Just return the copyright message |
| 1043 |
** |
| 1044 |
*/ |
| 1045 |
|
| 1046 |
char *GetZipCopyright(void) |
| 1047 |
{ |
| 1048 |
static char CopyR[300]; |
| 1049 |
|
| 1050 |
sstrcpy(CopyR, copyright[0]); |
| 1051 |
sstrcat(CopyR, copyright[1]); |
| 1052 |
sstrcat(CopyR, "\r\rPlease send bug reports to the authors at\r"\ |
| 1053 |
"[email protected]"); |
| 1054 |
|
| 1055 |
return CopyR; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
/* |
| 1062 |
** Just return the compilers date/time |
| 1063 |
** |
| 1064 |
*/ |
| 1065 |
|
| 1066 |
char *GetZipVersionLocal(void) |
| 1067 |
{ |
| 1068 |
static char ZipVersionLocal[50]; |
| 1069 |
|
| 1070 |
sprintf(ZipVersionLocal, "[%s %s]", __DATE__, __TIME__); |
| 1071 |
|
| 1072 |
return ZipVersionLocal; |
| 1073 |
} |
| 1074 |
|
| 1075 |
#endif /* #ifndef USE_SIOUX */ |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|