home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / globals.c < prev    next >
C/C++ Source or Header  |  1997-03-28  |  5KB  |  196 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(PK_MEM);   /* essentially memory error before we've started */
  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(PK_WARN);   /* programming error, but after we're all done */
  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(PK_ERR);   /* programming error while still working */
  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.  
  145.     if (!pG)
  146.         return (struct Globals *)NULL;
  147. #endif /* REENTRANT */
  148.  
  149.     /* for REENTRANT version, G is defined as (*pG) */
  150.  
  151.     memzero(&G, sizeof(struct Globals));
  152.  
  153. #ifdef CMS_MVS
  154.     G.aflag=1;
  155.     G.C_flag=1;
  156. #endif
  157.  
  158.     G.lflag=(-1);
  159.     G.wildzipfn = "";
  160.     G.pfnames = fnames;
  161.     G.pxnames = &fnames[1];
  162.     G.pInfo = G.info;
  163.     G.sol = TRUE;          /* at start of line */
  164.  
  165. #ifndef FUNZIP
  166.     G.message = UzpMessagePrnt;
  167.     G.input = UzpInput;           /* not used by anyone at the moment... */
  168. #if defined(WINDLL) || defined(MACOS)
  169.     G.mpause = NULL;              /* has scrollbars:  no need for pausing */
  170. #else
  171.     G.mpause = UzpMorePause;
  172. #endif
  173.     G.decr_passwd = UzpPassword;
  174. #endif /* !FUNZIP */
  175.  
  176. #if (!defined(DOS_H68_OS2_W32) && !defined(AMIGA) && !defined(RISCOS))
  177. #if (!defined(MACOS) && !defined(ATARI) && !defined(VMS))
  178.     G.echofd = -1;
  179. #endif /* !(MACOS || ATARI || VMS) */
  180. #endif /* !(DOS_H68_OS2_W32 || AMIGA || RISCOS) */
  181.  
  182. #ifdef SYSTEM_SPECIFIC_CTOR
  183.     SYSTEM_SPECIFIC_CTOR(__G);
  184. #endif
  185.  
  186. #ifdef REENTRANT
  187. #ifdef USETHREADID
  188.     registerGlobalPointer(__G);
  189. #else
  190.     GG = &G;
  191. #endif /* ?USETHREADID */
  192. #endif /* REENTRANT */
  193.  
  194.     return &G;
  195. }
  196.