pathname.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. Copyright (c) 1990-2003 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*---------------------------------------------------------------------------
  9. pathname.c
  10. Function dealing with the pathname. Mostly C-string work.
  11. ---------------------------------------------------------------------------*/
  12. /*****************************************************************************/
  13. /* Includes */
  14. /*****************************************************************************/
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <sound.h>
  19. #include "pathname.h"
  20. #include "helpers.h"
  21. #include "macstuff.h"
  22. /*****************************************************************************/
  23. /* Global Vars */
  24. /*****************************************************************************/
  25. const char ResourceMark[] = "XtraStuf.mac:"; /* see also macos.c */
  26. #include "zip.h"
  27. /*****************************************************************************/
  28. /* Functions */
  29. /*****************************************************************************/
  30. /*
  31. *----------------------------------------------------------------------
  32. *
  33. * FSpFindFolder --
  34. *
  35. * This function is a version of the FindFolder function that
  36. * returns the result as a FSSpec rather than a vRefNum and dirID.
  37. *
  38. * Results:
  39. * Results will be simaler to that of the FindFolder function.
  40. *
  41. * Side effects:
  42. * None.
  43. *
  44. *----------------------------------------------------------------------
  45. */
  46. OSErr
  47. FSpFindFolder(
  48. short vRefNum, /* Volume reference number. */
  49. OSType folderType, /* Folder type taken by FindFolder. */
  50. Boolean createFolder, /* Should we create it if non-existant. */
  51. FSSpec *spec) /* Pointer to resulting directory. */
  52. {
  53. short foundVRefNum;
  54. long foundDirID;
  55. OSErr err;
  56. err = FindFolder(vRefNum, folderType, createFolder,
  57. &foundVRefNum, &foundDirID);
  58. if (err != noErr) {
  59. return err;
  60. }
  61. err = FSMakeFSSpecCompat(foundVRefNum, foundDirID, "\p", spec);
  62. return err;
  63. }
  64. /*
  65. ** return volumename from pathname
  66. **
  67. */
  68. unsigned short GetVolumeFromPath(const char *FullPath, char *VolumeName)
  69. {
  70. const char *VolEnd, *tmpPtr1;
  71. char *tmpPtr2 = VolumeName;
  72. AssertStr(FullPath,"GetVolumeFromPath")
  73. for (VolEnd = FullPath; *VolEnd != '\0' && *VolEnd != ':'; VolEnd++)
  74. ;
  75. if (*VolEnd == '\0') return 0;
  76. for (tmpPtr1 = FullPath; tmpPtr1 != VolEnd;)
  77. {
  78. *tmpPtr2++ = *tmpPtr1++;
  79. }
  80. *tmpPtr2 = '\0';
  81. return (unsigned short) strlen(VolumeName);
  82. }
  83. /***********************************/
  84. /* Function FindNewExtractFolder() */
  85. /***********************************/
  86. char *FindNewExtractFolder(char *ExtractPath, Boolean uniqueFolder)
  87. {
  88. char buffer[NAME_MAX], *tmpPtr, *namePtr;
  89. char *last_dotpos = ExtractPath;
  90. short count = 0, folderCount = 0;
  91. OSErr err;
  92. FSSpec Spec;
  93. long theDirID;
  94. Boolean isDirectory;
  95. unsigned short namelen, pathlen = strlen(ExtractPath);
  96. unsigned long ext_length = 0;
  97. unsigned long num_to_cut = 0;
  98. long firstpart_length = pathlen;
  99. AssertStr(ExtractPath,"FindNewExtractFolder ExtractPath == NULL")
  100. for (tmpPtr = ExtractPath; *tmpPtr; tmpPtr++)
  101. if (*tmpPtr == ':')
  102. {
  103. folderCount++;
  104. namePtr = tmpPtr;
  105. }
  106. if (folderCount > 1) {
  107. namelen = strlen(namePtr);
  108. } else {
  109. namelen = strlen(ExtractPath);
  110. }
  111. if (uniqueFolder) {
  112. for (count = 0; count < 99; count++)
  113. {
  114. memset(buffer,0,sizeof(buffer));
  115. if (namelen >= 28)
  116. ExtractPath[pathlen-2] = 0x0;
  117. else
  118. ExtractPath[pathlen-1] = 0x0;
  119. sprintf(buffer,"%s%d",ExtractPath,count);
  120. GetCompletePath(ExtractPath, buffer, &Spec,&err);
  121. err = FSpGetDirectoryID(&Spec, &theDirID, &isDirectory);
  122. if (err == -43) break;
  123. }
  124. } else {
  125. /* Look for the last extension pos */
  126. for (tmpPtr = ExtractPath; *tmpPtr; tmpPtr++)
  127. if (*tmpPtr == '.') last_dotpos = tmpPtr;
  128. ext_length = strlen(last_dotpos);
  129. if (ext_length < 6) { /* up to 5 chars are treated as a */
  130. /* normal extension like ".html" or ".class" */
  131. int nameLength = last_dotpos - ExtractPath;
  132. if (nameLength > 1) {
  133. ExtractPath[nameLength] = 0x0;
  134. } else {
  135. ExtractPath[pathlen-1] = 0x0;
  136. }
  137. } else {
  138. ExtractPath[pathlen-1] = 0x0;
  139. }
  140. GetCompletePath(ExtractPath, ExtractPath, &Spec,&err);
  141. }
  142. /* Foldernames must always end with a colon */
  143. sstrcat(ExtractPath,":");
  144. return ExtractPath;
  145. }
  146. /*
  147. ** creates an archive file name
  148. **
  149. */
  150. void createArchiveName(char *thePath)
  151. {
  152. char *tmpPtr, *namePtr;
  153. short folderCount = 0;
  154. unsigned short namelen, pathlen = strlen(thePath);
  155. if (thePath[pathlen-1] == ':') thePath[pathlen-1] = 0x0;
  156. for (tmpPtr = thePath; *tmpPtr; tmpPtr++)
  157. if (*tmpPtr == ':')
  158. {
  159. folderCount++;
  160. namePtr = tmpPtr;
  161. }
  162. namelen = strlen(namePtr);
  163. /* we have to eliminate illegal chars:
  164. * The name space for Mac filenames and Zip filenames (unix style names)
  165. * do both include all printable extended-ASCII characters. The only
  166. * difference we have to take care of is the single special character
  167. * used as path delimiter:
  168. * ':' on MacOS and '/' on Unix and '\\' on Dos.
  169. * So, to convert between Mac filenames and Unix filenames without any
  170. * loss of information, we simply interchange ':' and '/'. Additionally,
  171. * we try to convert the coding of the extended-ASCII characters into
  172. * InfoZip's standard ISO 8859-1 codepage table.
  173. */
  174. MakeCompatibleString(namePtr, '/', '_', '.', '-', -1);
  175. /* Avoid filenames like: "Archive..zip" */
  176. if (thePath[pathlen-1] == '.')
  177. {
  178. thePath[pathlen-1] = 0;
  179. }
  180. if (folderCount >= 1)
  181. { /* path contains at least one folder */
  182. if (namelen >= 28)
  183. {
  184. pathlen = pathlen-4;
  185. }
  186. thePath[pathlen] = '.';
  187. thePath[pathlen+1] = 'z';
  188. thePath[pathlen+2] = 'i';
  189. thePath[pathlen+3] = 'p';
  190. thePath[pathlen+4] = 0x0;
  191. return;
  192. }
  193. else
  194. { /* path contains no folder */
  195. FindDesktopFolder(thePath);
  196. createArchiveName(thePath);
  197. }
  198. }
  199. /*
  200. ** finds the desktop-folder on a volume with
  201. ** largest amount of free-space.
  202. */
  203. void FindDesktopFolder(char *Path)
  204. {
  205. char buffer[255];
  206. FSSpec volumes[50]; /* 50 Volumes should be enough */
  207. short actVolCount, volIndex = 1, VolCount = 0;
  208. OSErr err;
  209. short i, foundVRefNum;
  210. FSSpec spec;
  211. UInt64 freeBytes;
  212. UInt64 totalBytes;
  213. UInt64 MaxFreeBytes;
  214. err = OnLine(volumes, 50, &actVolCount, &volIndex);
  215. printerr("OnLine:", (err != -35) && (err != 0), err, __LINE__, __FILE__, "");
  216. MaxFreeBytes = 0;
  217. for (i=0; i < actVolCount; i++)
  218. {
  219. XGetVInfo(volumes[i].vRefNum,
  220. volumes[i].name,
  221. &volumes[i].vRefNum,
  222. &freeBytes,
  223. &totalBytes);
  224. if (MaxFreeBytes < freeBytes) {
  225. MaxFreeBytes = freeBytes;
  226. foundVRefNum = volumes[i].vRefNum;
  227. }
  228. if ((freeBytes == 0) && (MaxFreeBytes < freeBytes)) {
  229. MaxFreeBytes = freeBytes;
  230. foundVRefNum = volumes[i].vRefNum;
  231. }
  232. }
  233. FSpFindFolder(foundVRefNum, kDesktopFolderType,
  234. kDontCreateFolder,&spec);
  235. GetFullPathFromSpec(buffer, &spec , &err);
  236. sstrcat(buffer,Path);
  237. sstrcpy(Path,buffer);
  238. }
  239. /*
  240. ** return the path without the filename
  241. **
  242. */
  243. char *TruncFilename(char *DirPath, const char *FilePath)
  244. {
  245. char *tmpPtr;
  246. char *dirPtr = NULL;
  247. AssertStr(DirPath,"TruncFilename")
  248. Assert_it(Spec,"TruncFilename","")
  249. sstrcpy(DirPath, FilePath);
  250. for (tmpPtr = DirPath; *tmpPtr; tmpPtr++)
  251. if (*tmpPtr == ':')
  252. dirPtr = tmpPtr;
  253. if (dirPtr)
  254. *++dirPtr = '\0';
  255. else
  256. printerr("TruncFilename: FilePath has no Folders", -1,
  257. -1, __LINE__, __FILE__, FilePath);
  258. return DirPath;
  259. }
  260. /*
  261. ** return only filename
  262. **
  263. */
  264. char *GetFilename(char *FileName, const char *FilePath)
  265. {
  266. const char *tmpPtr;
  267. const char *dirPtr = NULL;
  268. Assert_it(FileName,"GetFilename","")
  269. Assert_it(FilePath,"GetFilename","")
  270. for (tmpPtr = FilePath; *tmpPtr; tmpPtr++)
  271. {
  272. if (*tmpPtr == ':')
  273. {
  274. dirPtr = tmpPtr;
  275. }
  276. }
  277. if (dirPtr)
  278. {
  279. ++dirPtr; /* jump over the ':' */
  280. }
  281. else
  282. {
  283. return strcpy(FileName, FilePath); /* FilePath has no Folders */
  284. }
  285. return strcpy(FileName, dirPtr);
  286. }
  287. /*
  288. ** return fullpathname from folder/dir-id
  289. **
  290. */
  291. char *GetFullPathFromID(char *CompletePath, short vRefNum, long dirID,
  292. ConstStr255Param name, OSErr *err)
  293. {
  294. FSSpec spec;
  295. *err = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);
  296. printerr("FSMakeFSSpecCompat:", (*err != -43) && (*err != 0), *err,
  297. __LINE__, __FILE__, "");
  298. if ( (*err == noErr) || (*err == fnfErr) )
  299. {
  300. return GetFullPathFromSpec(CompletePath, &spec, err);
  301. }
  302. return NULL;
  303. }
  304. /*
  305. ** convert real-filename to archive-filename
  306. **
  307. */
  308. char *Real2RfDfFilen(char *RfDfFilen, const char *RealPath,
  309. short CurrentFork, short MacZipMode, Boolean DataForkOnly)
  310. {
  311. AssertStr(RealPath,"Real2RfDfFilen")
  312. AssertStr(RfDfFilen,"Real2RfDfFilen")
  313. if (DataForkOnly) /* make no changes */
  314. {
  315. return sstrcpy(RfDfFilen, RealPath);
  316. }
  317. switch (MacZipMode)
  318. {
  319. case JohnnyLee_EF:
  320. {
  321. sstrcpy(RfDfFilen, RealPath);
  322. if (CurrentFork == DataFork) /* data-fork */
  323. return sstrcat(RfDfFilen, "d");
  324. if (CurrentFork == ResourceFork) /* resource-fork */
  325. return sstrcat(RfDfFilen, "r");
  326. break;
  327. }
  328. case NewZipMode_EF:
  329. {
  330. switch (CurrentFork)
  331. {
  332. case DataFork:
  333. {
  334. sstrcpy(RfDfFilen, RealPath);
  335. return RfDfFilen; /* data-fork */
  336. break;
  337. }
  338. case ResourceFork:
  339. {
  340. sstrcpy(RfDfFilen, ResourceMark);
  341. sstrcat(RfDfFilen, RealPath); /* resource-fork */
  342. return RfDfFilen;
  343. break;
  344. }
  345. default:
  346. {
  347. printerr("Real2RfDfFilen:", -1, -1,
  348. __LINE__, __FILE__, RealPath);
  349. return NULL; /* function should never reach this point */
  350. }
  351. }
  352. break;
  353. }
  354. default:
  355. {
  356. printerr("Real2RfDfFilen:", -1, -1, __LINE__, __FILE__, RealPath);
  357. return NULL; /* function should never reach this point */
  358. }
  359. }
  360. printerr("Real2RfDfFilen:", -1, -1, __LINE__, __FILE__, RealPath);
  361. return NULL; /* function should never come reach this point */
  362. }
  363. /*
  364. ** convert archive-filename into a real filename
  365. **
  366. */
  367. char *RfDfFilen2Real(char *RealFn, const char *RfDfFilen, short MacZipMode,
  368. Boolean DataForkOnly, short *CurrentFork)
  369. {
  370. short length;
  371. int result;
  372. AssertStr(RfDfFilen,"RfDfFilen2Real")
  373. if (DataForkOnly ||
  374. (MacZipMode == UnKnown_EF) ||
  375. (MacZipMode < JohnnyLee_EF))
  376. {
  377. *CurrentFork = DataFork;
  378. return sstrcpy(RealFn,RfDfFilen);
  379. }
  380. result = strncmp(RfDfFilen, ResourceMark, sizeof(ResourceMark)-2);
  381. if (result == 0)
  382. {
  383. MacZipMode = NewZipMode_EF;
  384. }
  385. switch (MacZipMode)
  386. {
  387. case JohnnyLee_EF:
  388. {
  389. sstrcpy(RealFn, RfDfFilen);
  390. length = strlen(RealFn); /* determine Fork type */
  391. if (RealFn[length-1] == 'd') *CurrentFork = DataFork;
  392. else *CurrentFork = ResourceFork;
  393. RealFn[length-1] = '\0'; /* simply cut one char */
  394. return RealFn;
  395. break;
  396. }
  397. case NewZipMode_EF:
  398. { /* determine Fork type */
  399. result = strncmp(RfDfFilen, ResourceMark, sizeof(ResourceMark)-2);
  400. if (result != 0)
  401. {
  402. *CurrentFork = DataFork;
  403. sstrcpy(RealFn, RfDfFilen);
  404. return RealFn; /* data-fork */
  405. }
  406. else
  407. {
  408. *CurrentFork = ResourceFork;
  409. if (strlen(RfDfFilen) > (sizeof(ResourceMark) - 1))
  410. {
  411. sstrcpy(RealFn, &RfDfFilen[sizeof(ResourceMark)-1]);
  412. }
  413. else RealFn[0] = '\0';
  414. return RealFn; /* resource-fork */
  415. }
  416. break;
  417. }
  418. default:
  419. {
  420. *CurrentFork = NoFork;
  421. printerr("RfDfFilen2Real():", -1, MacZipMode,
  422. __LINE__, __FILE__, RfDfFilen);
  423. return NULL; /* function should never reach this point */
  424. }
  425. }
  426. printerr("RfDfFilen2Real():", -1, MacZipMode, __LINE__, __FILE__, RfDfFilen);
  427. return NULL; /* function should never reach this point */
  428. }
  429. /*
  430. ** return the applications name (argv[0])
  431. **
  432. */
  433. char *GetAppName(void)
  434. {
  435. ProcessSerialNumber psn;
  436. static Str255 AppName;
  437. ProcessInfoRec pinfo;
  438. OSErr err;
  439. GetCurrentProcess(&psn);
  440. pinfo.processName = AppName;
  441. pinfo.processInfoLength = sizeof(pinfo);
  442. pinfo.processAppSpec = NULL;
  443. err = GetProcessInformation(&psn,&pinfo);
  444. AppName[AppName[0]+1] = 0x00;
  445. return (char *)&AppName[1];
  446. }
  447. /*
  448. ** return fullpathname from FSSpec
  449. **
  450. */
  451. char *GetFullPathFromSpec(char *FullPath, FSSpec *Spec, OSErr *err)
  452. {
  453. Handle hFullPath;
  454. short len;
  455. Assert_it(Spec,"GetFullPathFromSpec","")
  456. *err = FSpGetFullPath(Spec, &len, &hFullPath);
  457. printerr("FSpGetFullPath:", (*err != -43) && (*err != 0), *err,
  458. __LINE__, __FILE__, "");
  459. memmove(FullPath, (Handle) *hFullPath, len);
  460. FullPath[len] = '\0'; /* make c-string */
  461. DisposeHandle((Handle)hFullPath); /* we don't need it any more */
  462. printerr("Warning path length exceeds limit: ", len >= NAME_MAX, len,
  463. __LINE__, __FILE__, " chars ");
  464. return FullPath;
  465. }
  466. /*
  467. * This function expands a given partial path to a complete path.
  468. * Path expansions are relative to the running app.
  469. * This function follows the notation:
  470. * 1. relative path:
  471. * a: ":subfolder:filename" -> ":current folder:subfolder:filename"
  472. * b: "::folder2:filename" -> folder2 is beside the current
  473. * folder on the same level
  474. * c: "filename" -> in current folder
  475. *
  476. * An absolute path will be returned.
  477. The following characteristics of Macintosh pathnames should be noted:
  478. A full pathname never begins with a colon, but must contain at
  479. least one colon.
  480. A partial pathname always begins with a colon separator except in
  481. the case where the file partial pathname is a simple file or
  482. directory name.
  483. Single trailing separator colons in full or partial pathnames are
  484. ignored except in the case of full pathnames to volumes.
  485. In full pathnames to volumes, the trailing separator colon is required.
  486. Consecutive separator colons can be used to ascend a level from a
  487. directory to its parent directory. Two consecutive separator colons
  488. will ascend one level, three consecutive separator colons will ascend
  489. two levels, and so on. Ascending can only occur from a directory;
  490. not a file.
  491. */
  492. char *GetCompletePath(char *CompletePath, const char *name, FSSpec *Spec,
  493. OSErr *err)
  494. {
  495. Boolean hasDirName = false;
  496. char currentdir[NAME_MAX];
  497. char *tmpPtr;
  498. unsigned short pathlen;
  499. AssertStr(name,"GetCompletePath")
  500. Assert_it(Spec,"GetCompletePath","")
  501. Assert_it((CompletePath != name),"GetCompletePath","")
  502. for (tmpPtr = name; *tmpPtr; tmpPtr++)
  503. if (*tmpPtr == ':') hasDirName = true;
  504. if (name[0] != ':') /* case c: path including volume name or only filename */
  505. {
  506. if (hasDirName)
  507. { /* okey, starts with volume name, so it must be a complete path */
  508. sstrcpy(CompletePath, name);
  509. }
  510. else
  511. { /* only filename: add cwd and return */
  512. getcwd(currentdir, NAME_MAX);
  513. sstrcat(currentdir, name);
  514. sstrcpy(CompletePath, currentdir);
  515. }
  516. }
  517. else if (name[1] == ':') /* it's case b: "::folder2:filename" */
  518. {
  519. printerr("GetCompletePath ", -1, *err, __LINE__, __FILE__, "not implemented");
  520. /* it's not yet implemented; do we really need this case ?*/
  521. return NULL;
  522. }
  523. else /* it's case a: ":subfolder:filename" */
  524. {
  525. getcwd(CompletePath, NAME_MAX); /* we don't need a second colon */
  526. CompletePath[strlen(CompletePath)-1] = '\0';
  527. sstrcat(CompletePath, name);
  528. }
  529. pathlen = strlen(CompletePath);
  530. *err = FSpLocationFromFullPath(pathlen, CompletePath, Spec);
  531. return CompletePath;
  532. }
  533. char *MakeFilenameShorter(const char *LongFilename)
  534. {
  535. static char filename[35]; /* contents should be never longer than 32 chars */
  536. static unsigned char Num = 0; /* change the number for every call */
  537. /* this var will rollover without a problem */
  538. char tempLongFilename[1024], charnum[5];
  539. char *last_dotpos = tempLongFilename;
  540. unsigned long full_length = strlen(LongFilename);
  541. unsigned long ext_length = 0;
  542. unsigned long num_to_cut = 0;
  543. long firstpart_length;
  544. char *tmpPtr;
  545. short MaxLength = 31;
  546. if (full_length <= MaxLength) /* filename is not long */
  547. {
  548. return strcpy(filename,LongFilename);
  549. }
  550. Num++;
  551. strcpy(tempLongFilename,LongFilename);
  552. /* Look for the last extension pos */
  553. for (tmpPtr = tempLongFilename; *tmpPtr; tmpPtr++)
  554. if (*tmpPtr == '.') last_dotpos = tmpPtr;
  555. ext_length = strlen(last_dotpos);
  556. firstpart_length = last_dotpos - tempLongFilename;
  557. if (ext_length > 6) /* up to 5 chars are treated as a */
  558. { /* normal extension like ".html" or ".class" */
  559. firstpart_length = 0;
  560. }
  561. num_to_cut = full_length - MaxLength;
  562. /* number the files to make the names unique */
  563. sprintf(charnum,"~%x", Num);
  564. num_to_cut += strlen(charnum);
  565. if (firstpart_length == 0)
  566. {
  567. firstpart_length = full_length;
  568. tempLongFilename[firstpart_length - num_to_cut] = 0;
  569. sprintf(filename,"%s%s", tempLongFilename, charnum);
  570. }
  571. else
  572. {
  573. tempLongFilename[firstpart_length - num_to_cut] = 0;
  574. sprintf(filename,"%s%s%s", tempLongFilename, charnum, last_dotpos);
  575. }
  576. return filename;
  577. }