home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip52.zip / globals.c < prev    next >
C/C++ Source or Header  |  1996-03-12  |  5KB  |  194 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   globals.c
  4.  
  5.   Routines to allocate and initialize globals, with or without threads.
  6.  
  7.   Contents:  registerGlobalPointer()
  8.              deregisterGlobalPointer()
  9.              getGlobalPointer()
  10.              globalsCtor()
  11.  
  12.   ---------------------------------------------------------------------------*/
  13.  
  14.  
  15. #define UNZIP_INTERNAL
  16. #include "unzip.h"
  17.  
  18. char *fnames[2] = {"*", NULL};   /* default filenames vector */
  19.  
  20.  
  21. #ifndef REENTRANT
  22.    struct Globals G;
  23. #else /* REENTRANT */
  24.  
  25. #  ifndef USETHREADID
  26.      struct Globals *GG;
  27. #  else /* USETHREADID */
  28. #    define THREADID_ENTRIES  0x40
  29.  
  30.      int lastScan;
  31.      struct Globals *threadPtrTable[THREADID_ENTRIES];
  32.      ulg             threadIdTable [THREADID_ENTRIES] = {
  33.          0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  34.          0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,    /* Make sure there are */
  35.          0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,    /* THREADID_ENTRIES 0s */
  36.          0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
  37.      };
  38.  
  39.      static char Far TooManyThreads[] =
  40.        "error:  more than %d simultaneous threads.\n\
  41.         Some threads are probably not calling DESTROYTHREAD()\n";
  42.      static char Far EntryNotFound[] =
  43.        "error:  couldn't find global pointer in table.\n\
  44.         Maybe somebody accidentally called DESTROYTHREAD() twice.\n";
  45.      static char Far GlobalPointerMismatch[] =
  46.        "error:  global pointer in table does not match pointer passed as\
  47.  parameter\n";
  48.  
  49. static void registerGlobalPointer OF((__GPRO));
  50.  
  51.  
  52.  
  53. static void registerGlobalPointer(__G)
  54.     __GDEF
  55. {
  56.     int scan=0;
  57.     ulg tid = GetThreadId();
  58.  
  59.     while (threadIdTable[scan] && scan < THREADID_ENTRIES)
  60.         scan++;
  61.  
  62.     if (scan == THREADID_ENTRIES) {
  63.         char *tooMany = LoadFarString(TooManyThreads);
  64.         Info(slide, 0x421, ((char *)slide, tooMany, THREADID_ENTRIES));
  65.         free(pG);
  66.         EXIT(1);
  67.     }
  68.  
  69.     threadIdTable [scan] = tid;
  70.     threadPtrTable[scan] = pG;
  71.     lastScan = scan;
  72. }
  73.  
  74.  
  75.  
  76. void deregisterGlobalPointer(__G)
  77.     __GDEF
  78. {
  79.     int scan=0;
  80.     ulg tid = GetThreadId();
  81.  
  82.  
  83.     while (threadIdTable[scan] != tid && scan < THREADID_ENTRIES)
  84.         scan++;
  85.  
  86. /*---------------------------------------------------------------------------
  87.     There are two things we can do if we can't find the entry:  ignore it or
  88.     scream.  The most likely reason for it not to be here is the user calling
  89.     this routine twice.  Since this could cause BIG problems if any globals
  90.     are accessed after the first call, we'd better scream.
  91.   ---------------------------------------------------------------------------*/
  92.  
  93.     if (scan == THREADID_ENTRIES || threadPtrTable[scan] != pG) {
  94.         char *noEntry;
  95.         if (scan == THREADID_ENTRIES)
  96.             noEntry = LoadFarString(EntryNotFound);
  97.         else
  98.             noEntry = LoadFarString(GlobalPointerMismatch);
  99.         Info(slide, 0x421, ((char *)slide, noEntry));
  100.         EXIT(1);
  101.     }
  102.  
  103.     threadIdTable [scan] = 0;
  104.     lastScan = scan;
  105.     free(threadPtrTable[scan]);
  106. }
  107.  
  108.  
  109.  
  110. struct Globals *getGlobalPointer()
  111. {
  112.     int scan=0;
  113.     ulg tid = GetThreadId();
  114.  
  115.     while (threadIdTable[scan] != tid && scan < THREADID_ENTRIES)
  116.         scan++;
  117.  
  118. /*---------------------------------------------------------------------------
  119.     There are two things we can do if we can't find the entry:  ignore it or
  120.     scream.  The most likely reason for it not to be here is the user calling
  121.     this routine twice.  Since this could cause BIG problems if any globals
  122.     are accessed after the first call, we'd better scream.
  123.   ---------------------------------------------------------------------------*/
  124.  
  125.     if (scan == THREADID_ENTRIES) {
  126.         char *noEntry;
  127.         noEntry = LoadFarString(EntryNotFound);
  128.         fprintf(stderr, noEntry);  /* can't use Info w/o a global pointer */
  129.         EXIT(1);
  130.     }
  131.  
  132.     return threadPtrTable[scan];
  133. }
  134.  
  135. #  endif /* ?USETHREADID */
  136. #endif /* ?REENTRANT */
  137.  
  138.  
  139.  
  140. struct Globals *globalsCtor()
  141. {
  142. #ifdef REENTRANT
  143.     struct Globals *pG = (struct Globals *)malloc(sizeof(struct Globals));
  144. #endif
  145.  
  146.     /* for REENTRANT version, G is defined as (*pG) */
  147.  
  148.     memzero(&G, sizeof(struct Globals));
  149.  
  150. #ifdef CMS_MVS
  151.     G.aflag=1;
  152.     G.C_flag=1;
  153. #endif
  154.  
  155.     G.lflag=(-1);
  156.     G.wildzipfn = "";
  157.     G.pfnames = fnames;
  158.     G.pxnames = &fnames[1];
  159.     G.pInfo = G.info;
  160.     G.sol = TRUE;          /* at start of line */
  161.  
  162.     G.local_hdr_sig[1] = G.central_hdr_sig[1] = G.end_central_sig[1] = '\0';
  163.  
  164. #ifndef FUNZIP
  165.     G.message = UzpMessagePrnt;
  166.     G.input = UzpInput;           /* not used by anyone at the moment... */
  167. #if defined(MSWIN) || defined(MACOS)
  168.     G.mpause = NULL;              /* has scrollbars:  no need for pausing */
  169. #else
  170.     G.mpause = UzpMorePause;
  171. #endif
  172. #endif /* !FUNZIP */
  173.  
  174. #if (!defined(DOS_H68_OS2_W32) && !defined(AMIGA) && !defined(RISCOS))
  175. #if (!defined(MACOS) && !defined(ATARI) && !defined(VMS))
  176.     G.echofd = -1;
  177. #endif /* !(MACOS || ATARI || VMS) */
  178. #endif /* !(DOS_H68_OS2_W32 || AMIGA || RISCOS) */
  179.  
  180. #ifdef SYSTEM_SPECIFIC_CTOR
  181.     SYSTEM_SPECIFIC_CTOR(__G);
  182. #endif
  183.  
  184. #ifdef REENTRANT
  185. #ifdef USETHREADID
  186.     registerGlobalPointer(__G);
  187. #else
  188.     GG = &G;
  189. #endif /* ?USETHREADID */
  190. #endif /* REENTRANT */
  191.  
  192.     return &G;
  193. }
  194.