helpers.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. Copyright (c) 1990-2001 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. helpers.c
  10. Some useful functions Used by unzip and zip.
  11. ---------------------------------------------------------------------------*/
  12. /*****************************************************************************/
  13. /* Includes */
  14. /*****************************************************************************/
  15. #include "zip.h"
  16. #include <ctype.h>
  17. #include <time.h>
  18. #include <sound.h>
  19. #include "macstuff.h"
  20. #include "helpers.h"
  21. #include "pathname.h"
  22. /*****************************************************************************/
  23. /* Global Vars */
  24. /*****************************************************************************/
  25. extern int noisy;
  26. extern char MacPathEnd;
  27. extern char *zipfile; /* filename of the Zipfile */
  28. extern char *tempzip; /* Temporary zip file name */
  29. extern ZCONST unsigned char MacRoman_to_WinCP1252[128];
  30. static char argStr[1024];
  31. static char *argv[MAX_ARGS + 1];
  32. /*****************************************************************************/
  33. /* Functions */
  34. /*****************************************************************************/
  35. /*
  36. ** Copy a C string to a Pascal string
  37. **
  38. */
  39. unsigned char *CToPCpy(unsigned char *pstr, char *cstr)
  40. {
  41. register char *dptr;
  42. register unsigned len;
  43. len=0;
  44. dptr=(char *)pstr+1;
  45. while (len<255 && (*dptr++ = *cstr++)!='\0') ++len;
  46. *pstr= (unsigned char)len;
  47. return pstr;
  48. }
  49. /*
  50. ** Copy a Pascal string to a C string
  51. **
  52. */
  53. char *PToCCpy(unsigned char *pstr, char *cstr)
  54. {
  55. strncpy(cstr, (char *) &pstr[1], *pstr);
  56. cstr[pstr[0]] = '\0'; /* set endmarker for c-string */
  57. return cstr;
  58. }
  59. /*
  60. ** strcpy() and strcat() work-alikes which allow overlapping buffers.
  61. */
  62. char *sstrcpy(char *to,const char *from)
  63. {
  64. memmove(to, from, 1+strlen(from));
  65. return to;
  66. }
  67. char *sstrcat(char *to,const char *from)
  68. {
  69. sstrcpy(to + strlen(to), from);
  70. return to;
  71. }
  72. /*
  73. ** Alloc memory and init it
  74. **
  75. */
  76. char *StrCalloc(unsigned short size)
  77. {
  78. char *strPtr = NULL;
  79. if ((strPtr = calloc(size, sizeof(char))) == NULL)
  80. printerr("StrCalloc failed:", -1, size, __LINE__, __FILE__, "");
  81. Assert_it(strPtr,"strPtr == NULL","")
  82. return strPtr;
  83. }
  84. /*
  85. ** Release only non NULL pointers
  86. **
  87. */
  88. char *StrFree(char *strPtr)
  89. {
  90. if (strPtr != NULL)
  91. {
  92. free(strPtr);
  93. }
  94. return NULL;
  95. }
  96. /*
  97. ** Return a value in a binary string
  98. **
  99. */
  100. char *sBit2Str(unsigned short value)
  101. {
  102. static char str[sizeof(value)*8];
  103. int biz = 16;
  104. int strwid = 16;
  105. int i, j;
  106. char *tempPtr = str;
  107. j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
  108. for (i = 0; i < j; i++) {
  109. *tempPtr++ = ' ';
  110. }
  111. while (--biz >= 0)
  112. {
  113. *tempPtr++ = ((value >> biz) & 1) + '0';
  114. if (!(biz % 4) && biz) {
  115. *tempPtr++ = ' ';
  116. }
  117. }
  118. *tempPtr = '\0';
  119. return str;
  120. }
  121. /*
  122. ** Parse commandline style arguments
  123. **
  124. */
  125. int ParseArguments(char *s, char ***arg)
  126. {
  127. int n = 1, Quote = 0;
  128. char *p = s, *p1, c;
  129. argv[0] = GetAppName();
  130. *arg = argv;
  131. p1 = (char *) argStr;
  132. while ((c = *p++) != 0) {
  133. if (c==' ') continue;
  134. argv[n++] = p1;
  135. if (n > MAX_ARGS)
  136. return (n-1);
  137. do {
  138. if (c=='\\' && *p++)
  139. c = *p++;
  140. else
  141. if ((c=='"') || (c == '\'')) {
  142. if (!Quote) {
  143. Quote = c;
  144. continue;
  145. }
  146. if (c == Quote) {
  147. Quote = 0;
  148. continue;
  149. }
  150. }
  151. *p1++ = c;
  152. } while (*p && ((c = *p++) != ' ' || Quote));
  153. *p1++ = '\0';
  154. }
  155. return n;
  156. }
  157. /*
  158. ** Print commandline style arguments
  159. **
  160. */
  161. void PrintArguments(int argc, char **argv)
  162. {
  163. printf("\n Arguments:");
  164. printf("\n --------------------------");
  165. while(--argc >= 0)
  166. printf("\n argc: %d argv: [%s]", argc, &*argv[argc]);
  167. printf("\n --------------------------\n\n");
  168. return;
  169. }
  170. /*
  171. ** return some error-msg on file-system
  172. **
  173. */
  174. int PrintUserHFSerr(int cond, int err, char *msg2)
  175. {
  176. char *msg;
  177. if (cond != 0)
  178. {
  179. switch (err)
  180. {
  181. case -35:
  182. msg = "No such Volume";
  183. break;
  184. case -56:
  185. msg = "No such Drive";
  186. break;
  187. case -37:
  188. msg = "Bad Volume Name";
  189. break;
  190. case -49:
  191. msg = "File is already open for writing";
  192. break;
  193. case -43:
  194. msg = "Directory/File not found";
  195. break;
  196. case -120:
  197. msg = "Directory/File not found or incomplete pathname";
  198. break;
  199. default: return err;
  200. }
  201. fprintf(stderr, "\n\n Error: %s ->%s", msg, msg2);
  202. exit(err);
  203. }
  204. return 0;
  205. }
  206. /*
  207. ** Check mounted volumes and return number of volumes
  208. ** with the same name.
  209. */
  210. short CheckMountedVolumes(char *FullPath)
  211. {
  212. FSSpec volumes[50]; /* 50 Volumes should be enough */
  213. char VolumeName[257], volume[257];
  214. short actVolCount, volIndex = 1, VolCount = 0;
  215. OSErr err;
  216. int i;
  217. GetVolumeFromPath(FullPath, VolumeName);
  218. err = OnLine(volumes, 50, &actVolCount, &volIndex);
  219. printerr("OnLine:", (err != -35) && (err != 0), err, __LINE__, __FILE__, "");
  220. for (i=0; i < actVolCount; i++)
  221. {
  222. PToCCpy(volumes[i].name,volume);
  223. if (stricmp(volume, VolumeName) == 0) VolCount++;
  224. }
  225. printerr("OnLine: ", (VolCount == 0), VolCount, __LINE__, __FILE__, FullPath);
  226. return VolCount;
  227. }
  228. /*
  229. ** compares strings, ignoring differences in case
  230. **
  231. */
  232. int stricmp(const char *p1, const char *p2)
  233. {
  234. int diff;
  235. while (*p1 && *p2)
  236. {
  237. if (*p1 != *p2)
  238. {
  239. if (isalpha(*p1) && isalpha(*p2))
  240. {
  241. diff = toupper(*p1) - toupper(*p2);
  242. if (diff) return diff;
  243. }
  244. else break;
  245. }
  246. p1++;
  247. p2++;
  248. }
  249. return *p1 - *p2;
  250. }
  251. /*
  252. ** Convert the MacOS-Strings (Filenames/Findercomments) to a most compatible.
  253. ** These strings will be stored in the public area of the zip-archive.
  254. ** Every foreign platform (outside macos) will access these strings
  255. ** for extraction.
  256. */
  257. void MakeCompatibleString(char *MacOS_Str,
  258. const char SpcChar1, const char SpcChar2,
  259. const char SpcChar3, const char SpcChar4,
  260. short CurrTextEncodingBase)
  261. {
  262. char *tmpPtr;
  263. register uch curch;
  264. Assert_it(MacOS_Str,"MakeCompatibleString MacOS_Str == NULL","")
  265. for (tmpPtr = MacOS_Str; (curch = *tmpPtr) != '\0'; tmpPtr++)
  266. {
  267. if (curch == SpcChar1)
  268. *tmpPtr = SpcChar2;
  269. else
  270. if (curch == SpcChar3)
  271. *tmpPtr = SpcChar4;
  272. else /* default */
  273. /* now convert from MacRoman to ISO-8859-1 */
  274. /* but convert only if MacRoman is activ */
  275. if ((CurrTextEncodingBase == kTextEncodingMacRoman) &&
  276. (curch > 127))
  277. {
  278. *tmpPtr = (char)MacRoman_to_WinCP1252[curch - 128];
  279. }
  280. } /* end for */
  281. }
  282. Boolean CheckForSwitch(char *Switch, int argc, char **argv)
  283. {
  284. char *p; /* steps through option arguments */
  285. int i; /* arg counter, root directory flag */
  286. for (i = 1; i < argc; i++)
  287. {
  288. if (argv[i][0] == '-')
  289. {
  290. if (argv[i][1])
  291. {
  292. for (p = argv[i]+1; *p; p++)
  293. {
  294. if (*p == Switch[0])
  295. {
  296. return true;
  297. }
  298. if ((Switch[1] != NULL) &&
  299. ((*p == Switch[0]) && (*p == Switch[1])))
  300. {
  301. return true;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. return false;
  308. }
  309. #if (defined(USE_SIOUX) || defined(MACUNZIP_STANDALONE))
  310. /*
  311. ** checks the condition and returns an error-msg
  312. ** this function is for internal use only
  313. */
  314. OSErr printerr(const char *msg, int cond, int err, int line, char *file,
  315. const char *msg2)
  316. {
  317. if (cond != 0)
  318. {
  319. fprintf(stderr, "\nint err: %d: %s %d [%d/%s] {%s}\n", clock(), msg, err,
  320. line, file, msg2);
  321. }
  322. return cond;
  323. }
  324. /*
  325. fake-functions:
  326. Not Implemented for metrowerks SIOUX
  327. */
  328. void leftStatusString(char *status)
  329. {
  330. status = status;
  331. }
  332. void rightStatusString(char *status)
  333. {
  334. status = status;
  335. }
  336. void DoWarnUserDupVol( char *FullPath )
  337. {
  338. char VolName[257];
  339. GetVolumeFromPath(FullPath, VolName);
  340. printf("\n There are more than one volume that has the same name !!\n");
  341. printf("\n Volume: %s\n",VolName);
  342. printf("\n This port has one weak point:");
  343. printf("\n It is based on pathnames. As you may be already know:");
  344. printf("\n Pathnames are not unique on a Mac !");
  345. printf("\n MacZip has problems to find the correct location of");
  346. printf("\n the archive or the files.\n");
  347. printf("\n My (Big) recommendation: Name all your volumes with an");
  348. printf("\n unique name and MacZip will run without any problem.");
  349. }
  350. #endif