globals.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (c) 1990-2007 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2003-May-08 or later
  4. (the contents of which are also included in unzip.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. globals.c
  10. Routines to allocate and initialize globals, with or without threads.
  11. Contents: registerGlobalPointer()
  12. deregisterGlobalPointer()
  13. getGlobalPointer()
  14. globalsCtor()
  15. ---------------------------------------------------------------------------*/
  16. #define UNZIP_INTERNAL
  17. #include "unzip.h"
  18. #ifndef FUNZIP
  19. /* initialization of sigs is completed at runtime so unzip(sfx) executable
  20. * won't look like a zipfile
  21. */
  22. char central_hdr_sig[4] = {0, 0, 0x01, 0x02};
  23. char local_hdr_sig[4] = {0, 0, 0x03, 0x04};
  24. char end_central_sig[4] = {0, 0, 0x05, 0x06};
  25. char end_central64_sig[4] = {0, 0, 0x06, 0x06};
  26. char end_centloc64_sig[4] = {0, 0, 0x06, 0x07};
  27. /* extern char extd_local_sig[4] = {0, 0, 0x07, 0x08}; NOT USED YET */
  28. ZCONST char *fnames[2] = {"*", NULL}; /* default filenames vector */
  29. #endif
  30. #ifndef REENTRANT
  31. Uz_Globs G;
  32. #else /* REENTRANT */
  33. # ifndef USETHREADID
  34. Uz_Globs *GG;
  35. # else /* USETHREADID */
  36. # define THREADID_ENTRIES 0x40
  37. int lastScan;
  38. Uz_Globs *threadPtrTable[THREADID_ENTRIES];
  39. ulg threadIdTable [THREADID_ENTRIES] = {
  40. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  41. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* Make sure there are */
  42. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* THREADID_ENTRIES 0s */
  43. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
  44. };
  45. static ZCONST char Far TooManyThreads[] =
  46. "error: more than %d simultaneous threads.\n\
  47. Some threads are probably not calling DESTROYTHREAD()\n";
  48. static ZCONST char Far EntryNotFound[] =
  49. "error: couldn't find global pointer in table.\n\
  50. Maybe somebody accidentally called DESTROYTHREAD() twice.\n";
  51. static ZCONST char Far GlobalPointerMismatch[] =
  52. "error: global pointer in table does not match pointer passed as\
  53. parameter\n";
  54. static void registerGlobalPointer OF((__GPRO));
  55. static void registerGlobalPointer(__G)
  56. __GDEF
  57. {
  58. int scan=0;
  59. ulg tid = GetThreadId();
  60. while (threadIdTable[scan] && scan < THREADID_ENTRIES)
  61. scan++;
  62. if (scan == THREADID_ENTRIES) {
  63. ZCONST char *tooMany = LoadFarString(TooManyThreads);
  64. Info(slide, 0x421, ((char *)slide, tooMany, THREADID_ENTRIES));
  65. free(pG);
  66. EXIT(PK_MEM); /* essentially memory error before we've started */
  67. }
  68. threadIdTable [scan] = tid;
  69. threadPtrTable[scan] = pG;
  70. lastScan = scan;
  71. }
  72. void deregisterGlobalPointer(__G)
  73. __GDEF
  74. {
  75. int scan=0;
  76. ulg tid = GetThreadId();
  77. while (threadIdTable[scan] != tid && scan < THREADID_ENTRIES)
  78. scan++;
  79. /*---------------------------------------------------------------------------
  80. There are two things we can do if we can't find the entry: ignore it or
  81. scream. The most likely reason for it not to be here is the user calling
  82. this routine twice. Since this could cause BIG problems if any globals
  83. are accessed after the first call, we'd better scream.
  84. ---------------------------------------------------------------------------*/
  85. if (scan == THREADID_ENTRIES || threadPtrTable[scan] != pG) {
  86. ZCONST char *noEntry;
  87. if (scan == THREADID_ENTRIES)
  88. noEntry = LoadFarString(EntryNotFound);
  89. else
  90. noEntry = LoadFarString(GlobalPointerMismatch);
  91. Info(slide, 0x421, ((char *)slide, noEntry));
  92. EXIT(PK_WARN); /* programming error, but after we're all done */
  93. }
  94. threadIdTable [scan] = 0;
  95. lastScan = scan;
  96. free(threadPtrTable[scan]);
  97. }
  98. Uz_Globs *getGlobalPointer()
  99. {
  100. int scan=0;
  101. ulg tid = GetThreadId();
  102. while (threadIdTable[scan] != tid && scan < THREADID_ENTRIES)
  103. scan++;
  104. /*---------------------------------------------------------------------------
  105. There are two things we can do if we can't find the entry: ignore it or
  106. scream. The most likely reason for it not to be here is the user calling
  107. this routine twice. Since this could cause BIG problems if any globals
  108. are accessed after the first call, we'd better scream.
  109. ---------------------------------------------------------------------------*/
  110. if (scan == THREADID_ENTRIES) {
  111. ZCONST char *noEntry = LoadFarString(EntryNotFound);
  112. fprintf(stderr, noEntry); /* can't use Info w/o a global pointer */
  113. EXIT(PK_ERR); /* programming error while still working */
  114. }
  115. return threadPtrTable[scan];
  116. }
  117. # endif /* ?USETHREADID */
  118. #endif /* ?REENTRANT */
  119. Uz_Globs *globalsCtor()
  120. {
  121. #ifdef REENTRANT
  122. Uz_Globs *pG = (Uz_Globs *)malloc(sizeof(Uz_Globs));
  123. if (!pG)
  124. return (Uz_Globs *)NULL;
  125. #endif /* REENTRANT */
  126. /* for REENTRANT version, G is defined as (*pG) */
  127. memzero(&G, sizeof(Uz_Globs));
  128. #ifndef FUNZIP
  129. #ifdef CMS_MVS
  130. uO.aflag=1;
  131. uO.C_flag=1;
  132. #endif
  133. #ifdef TANDEM
  134. uO.aflag=1; /* default to '-a' auto create Text Files as type 101 */
  135. #endif
  136. #ifdef VMS
  137. # if (!defined(NO_TIMESTAMPS))
  138. uO.D_flag=1; /* default to '-D', no restoration of dir timestamps */
  139. # endif
  140. #endif
  141. uO.lflag=(-1);
  142. G.wildzipfn = "";
  143. G.pfnames = (char **)fnames;
  144. G.pxnames = (char **)&fnames[1];
  145. G.pInfo = G.info;
  146. G.sol = TRUE; /* at start of line */
  147. G.message = UzpMessagePrnt;
  148. G.input = UzpInput; /* not used by anyone at the moment... */
  149. #if defined(WINDLL) || defined(MACOS)
  150. G.mpause = NULL; /* has scrollbars: no need for pausing */
  151. #else
  152. G.mpause = UzpMorePause;
  153. #endif
  154. G.decr_passwd = UzpPassword;
  155. #endif /* !FUNZIP */
  156. #if (!defined(DOS_FLX_H68_NLM_OS2_W32) && !defined(AMIGA) && !defined(RISCOS))
  157. #if (!defined(MACOS) && !defined(ATARI) && !defined(VMS))
  158. G.echofd = -1;
  159. #endif /* !(MACOS || ATARI || VMS) */
  160. #endif /* !(DOS_FLX_H68_NLM_OS2_W32 || AMIGA || RISCOS) */
  161. #ifdef SYSTEM_SPECIFIC_CTOR
  162. SYSTEM_SPECIFIC_CTOR(__G);
  163. #endif
  164. #ifdef REENTRANT
  165. #ifdef USETHREADID
  166. registerGlobalPointer(__G);
  167. #else
  168. GG = &G;
  169. #endif /* ?USETHREADID */
  170. #endif /* REENTRANT */
  171. return &G;
  172. }