home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xyzext.zip / xyz / server / samples / utils.c < prev   
C/C++ Source or Header  |  1992-07-18  |  21KB  |  873 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: utils.c,v 1.105 91/07/19 23:22:14 keith Exp $ */
  25. #include "Xos.h"
  26. #include <stdio.h>
  27. #include "misc.h"
  28. #include "X.h"
  29. #include "input.h"
  30. #include "opaque.h"
  31. #include <signal.h>
  32. #ifndef SYSV
  33. #include <sys/resource.h>
  34. #endif
  35.  
  36. #ifdef SIGNALRETURNSINT
  37. #define SIGVAL int
  38. #else
  39. #define SIGVAL void
  40. #endif
  41.  
  42. extern char *display;
  43.  
  44. extern long defaultScreenSaverTime;    /* for parsing command line */
  45. extern long defaultScreenSaverInterval;
  46. extern int defaultScreenSaverBlanking;
  47. extern int defaultBackingStore;
  48. extern Bool disableBackingStore;
  49. extern Bool disableSaveUnders;
  50. extern Bool PartialNetwork;
  51. #ifndef NOLOGOHACK
  52. extern int logoScreenSaver;
  53. #endif
  54. #ifdef RLIMIT_DATA
  55. extern int limitDataSpace;
  56. #endif
  57. #ifdef RLIMIT_STACK
  58. extern int limitStackSpace;
  59. #endif
  60. #ifdef RLIMIT_NOFILE
  61. extern int limitNoFile;
  62. #endif
  63. extern int defaultColorVisualClass;
  64. extern long ScreenSaverTime;        /* for forcing reset */
  65. extern Bool permitOldBugs;
  66. extern int monitorResolution;
  67.  
  68. Bool CoreDump;
  69.  
  70. void ddxUseMsg();
  71.  
  72. #ifndef SVR4
  73. extern char *sbrk();
  74. #endif
  75.  
  76. #ifdef AIXV3
  77. #define AIXFILE "/tmp/aixfile"
  78. FILE *aixfd;
  79. int FlushOn = 0;
  80. int SyncOn  = 0;
  81. extern int SelectWaitTime;
  82. #endif
  83.  
  84. #ifdef DEBUG
  85. #ifndef SPECIAL_MALLOC
  86. #define MEMBUG
  87. #endif
  88. #endif
  89.  
  90. #ifdef MEMBUG
  91. #define MEM_FAIL_SCALE 100000
  92. long Memory_fail = 0;
  93. static pointer minfree = NULL;
  94. static void CheckNode();
  95. #endif
  96.  
  97. Bool Must_have_memory = FALSE;
  98.  
  99. char *dev_tty_from_init = NULL;        /* since we need to parse it anyway */
  100.  
  101. /* Force connections to close on SIGHUP from init */
  102.  
  103. SIGVAL
  104. AutoResetServer ()
  105. {
  106.     dispatchException |= DE_RESET;
  107.     isItTimeToYield = TRUE;
  108. #ifdef GPROF
  109.     chdir ("/tmp");
  110.     exit (0);
  111. #endif
  112. #ifdef SYSV
  113.     signal (SIGHUP, AutoResetServer);
  114. #endif
  115. }
  116.  
  117. /* Force connections to close and then exit on SIGTERM, SIGINT */
  118.  
  119. SIGVAL
  120. GiveUp()
  121. {
  122.     dispatchException |= DE_TERMINATE;
  123.     isItTimeToYield = TRUE;
  124. }
  125.  
  126.  
  127. static void
  128. AbortServer()
  129. {
  130.     extern void AbortDDX();
  131.  
  132.     AbortDDX();
  133.     fflush(stderr);
  134.     if (CoreDump)
  135.     abort();
  136.     exit (1);
  137. }
  138.  
  139. void
  140. Error(str)
  141.     char *str;
  142. {
  143.     perror(str);
  144. }
  145.  
  146. #if defined (UTEK) || defined (UTEKV) || defined(sgi)
  147. /*
  148.  * Tektronix has a shared-memory time value which doesn't
  149.  * match gettimeofday at all, but it is only accessible
  150.  * inside the driver.  SGI has their own GetTimeInMillis.
  151.  */
  152. #else
  153. long
  154. GetTimeInMillis()
  155. {
  156.     struct timeval  tp;
  157.  
  158.     gettimeofday(&tp, 0);
  159.     return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
  160. }
  161. #endif
  162.  
  163. void UseMsg()
  164. {
  165. #if !defined(AIXrt) && !defined(AIX386)
  166.     ErrorF("use: X [:<display>] [option]\n");
  167.     ErrorF("-a #                   mouse acceleration (pixels)\n");
  168. #ifdef MEMBUG
  169.     ErrorF("-alloc int             chance alloc should fail\n");
  170. #endif
  171.     ErrorF("-auth string           select authorization file\n");    
  172.     ErrorF("bc                     enable bug compatibility\n");
  173.     ErrorF("-bs                    disable any backing store support\n");
  174.     ErrorF("-c                     turns off key-click\n");
  175.     ErrorF("c #                    key-click volume (0-100)\n");
  176.     ErrorF("-cc int                default color visual class\n");
  177.     ErrorF("-co string             color database file\n");
  178.     ErrorF("-dpi int               screen resolution in dots per inch\n");
  179.     ErrorF("-f #                   bell base (0-100)\n");
  180.     ErrorF("-fc string             cursor font\n");
  181.     ErrorF("-fn string             default font name\n");
  182.     ErrorF("-fp string             default font path\n");
  183.     ErrorF("-help                  prints message with these options\n");
  184.     ErrorF("-I                     ignore all remaining arguments\n");
  185. #ifdef RLIMIT_DATA
  186.     ErrorF("-ld int                limit data space to N Kb\n");
  187. #endif
  188. #ifdef RLIMIT_NOFILE
  189.     ErrorF("-lf int                limit number of open files to N\n");
  190. #endif
  191. #ifdef RLIMIT_STACK
  192.     ErrorF("-ls int                limit stack space to N Kb\n");
  193. #endif
  194. #ifndef NOLOGOHACK
  195.     ErrorF("-logo                  enable logo in screen saver\n");
  196.     ErrorF("nologo                 disable logo in screen saver\n");
  197. #endif
  198.     ErrorF("-p #                   screen-saver pattern duration (minutes)\n");
  199.     ErrorF("-r                     turns off auto-repeat\n");
  200.     ErrorF("r                      turns on auto-repeat \n");
  201.     ErrorF("-s #                   screen-saver timeout (minutes)\n");
  202.     ErrorF("-su                    disable any save under support\n");
  203.     ErrorF("-t #                   mouse threshold (pixels)\n");
  204.     ErrorF("-to #                  connection time out\n");
  205.     ErrorF("ttyxx                  server started from init on /dev/ttyxx\n");
  206.     ErrorF("v                      video blanking for screen-saver\n");
  207.     ErrorF("-v                     screen-saver without video blanking\n");
  208.     ErrorF("-wm                    WhenMapped default backing-store\n");
  209.     ErrorF("-x string              loads named extension at init time \n");
  210. #ifdef XDMCP
  211.     XdmcpUseMsg();
  212. #endif
  213. #endif /* !AIXrt && ! AIX386 */
  214.     ddxUseMsg();
  215. }
  216.  
  217. /*
  218.  * This function parses the command line. Handles device-independent fields
  219.  * and allows ddx to handle additional fields.  It is not allowed to modify
  220.  * argc or any of the strings pointed to by argv.
  221.  */
  222. void
  223. ProcessCommandLine ( argc, argv )
  224. int    argc;
  225. char    *argv[];
  226.  
  227. {
  228.     int i, skip;
  229.  
  230. #ifdef MEMBUG
  231.     if (!minfree)
  232.     minfree = (pointer)sbrk(0);
  233. #endif
  234.     defaultKeyboardControl.autoRepeat = TRUE;
  235.  
  236. #ifdef AIXV3
  237.     OpenDebug();
  238. #endif
  239.     for ( i = 1; i < argc; i++ )
  240.     {
  241.     /* call ddx first, so it can peek/override if it wants */
  242.         if(skip = ddxProcessArgument(argc, argv, i))
  243.     {
  244.         i += (skip - 1);
  245.     }
  246.     else if(argv[i][0] ==  ':')  
  247.     {
  248.         /* initialize display */
  249.         display = argv[i];
  250.         display++;
  251.     }
  252.     else if ( strcmp( argv[i], "-a") == 0)
  253.     {
  254.         if(++i < argc)
  255.             defaultPointerControl.num = atoi(argv[i]);
  256.         else
  257.         UseMsg();
  258.     }
  259. #ifdef MEMBUG
  260.     else if ( strcmp( argv[i], "-alloc") == 0)
  261.     {
  262.         if(++i < argc)
  263.             Memory_fail = atoi(argv[i]);
  264.         else
  265.         UseMsg();
  266.     }
  267. #endif
  268.     else if ( strcmp( argv[i], "-auth") == 0)
  269.     {
  270.         if(++i < argc)
  271.             InitAuthorization (argv[i]);
  272.         else
  273.         UseMsg();
  274.     }
  275.     else if ( strcmp( argv[i], "bc") == 0)
  276.         permitOldBugs = TRUE;
  277.     else if ( strcmp( argv[i], "-bs") == 0)
  278.         disableBackingStore = TRUE;
  279.     else if ( strcmp( argv[i], "c") == 0)
  280.     {
  281.         if(++i < argc)
  282.             defaultKeyboardControl.click = atoi(argv[i]);
  283.         else
  284.         UseMsg();
  285.     }
  286.     else if ( strcmp( argv[i], "-c") == 0)
  287.     {
  288.         defaultKeyboardControl.click = 0;
  289.     }
  290.     else if ( strcmp( argv[i], "-cc") == 0)
  291.     {
  292.         if(++i < argc)
  293.             defaultColorVisualClass = atoi(argv[i]);
  294.         else
  295.         UseMsg();
  296.     }
  297.     else if ( strcmp( argv[i], "-co") == 0)
  298.     {
  299.         if(++i < argc)
  300.             rgbPath = argv[i];
  301.         else
  302.         UseMsg();
  303.     }
  304.     else if ( strcmp( argv[i], "-core") == 0)
  305.         CoreDump = TRUE;
  306.     else if ( strcmp( argv[i], "-dpi") == 0)
  307.     {
  308.         if(++i < argc)
  309.             monitorResolution = atoi(argv[i]);
  310.         else
  311.         UseMsg();
  312.     }
  313.     else if ( strcmp( argv[i], "-f") == 0)
  314.     {
  315.         if(++i < argc)
  316.             defaultKeyboardControl.bell = atoi(argv[i]);
  317.         else
  318.         UseMsg();
  319.     }
  320.     else if ( strcmp( argv[i], "-fc") == 0)
  321.     {
  322.         if(++i < argc)
  323.             defaultCursorFont = argv[i];
  324.         else
  325.         UseMsg();
  326.     }
  327.     else if ( strcmp( argv[i], "-fn") == 0)
  328.     {
  329.         if(++i < argc)
  330.             defaultTextFont = argv[i];
  331.         else
  332.         UseMsg();
  333.     }
  334.     else if ( strcmp( argv[i], "-fp") == 0)
  335.     {
  336.         if(++i < argc)
  337.             defaultFontPath = argv[i];
  338.         else
  339.         UseMsg();
  340.     }
  341.     else if ( strcmp( argv[i], "-help") == 0)
  342.     {
  343.         UseMsg();
  344.         exit(0);
  345.     }
  346. #ifdef RLIMIT_DATA
  347.     else if ( strcmp( argv[i], "-ld") == 0)
  348.     {
  349.         if(++i < argc)
  350.         {
  351.             limitDataSpace = atoi(argv[i]);
  352.         if (limitDataSpace > 0)
  353.             limitDataSpace *= 1024;
  354.         }
  355.         else
  356.         UseMsg();
  357.     }
  358. #endif
  359. #ifdef RLIMIT_NOFILE
  360.     else if ( strcmp( argv[i], "-lf") == 0)
  361.     {
  362.         if(++i < argc)
  363.             limitNoFile = atoi(argv[i]);
  364.         else
  365.         UseMsg();
  366.     }
  367. #endif
  368. #ifdef RLIMIT_STACK
  369.     else if ( strcmp( argv[i], "-ls") == 0)
  370.     {
  371.         if(++i < argc)
  372.         {
  373.             limitStackSpace = atoi(argv[i]);
  374.         if (limitStackSpace > 0)
  375.             limitStackSpace *= 1024;
  376.         }
  377.         else
  378.         UseMsg();
  379.     }
  380. #endif
  381. #ifndef NOLOGOHACK
  382.     else if ( strcmp( argv[i], "-logo") == 0)
  383.     {
  384.         logoScreenSaver = 1;
  385.     }
  386.     else if ( strcmp( argv[i], "nologo") == 0)
  387.     {
  388.         logoScreenSaver = 0;
  389.     }
  390. #endif
  391.     else if ( strcmp( argv[i], "-p") == 0)
  392.     {
  393.         if(++i < argc)
  394.             defaultScreenSaverInterval = ((long)atoi(argv[i])) *
  395.                          MILLI_PER_MIN;
  396.         else
  397.         UseMsg();
  398.     }
  399.     else if ( strcmp( argv[i], "-pn") == 0)
  400.         PartialNetwork = TRUE;
  401.     else if ( strcmp( argv[i], "r") == 0)
  402.         defaultKeyboardControl.autoRepeat = TRUE;
  403.     else if ( strcmp( argv[i], "-r") == 0)
  404.         defaultKeyboardControl.autoRepeat = FALSE;
  405.     else if ( strcmp( argv[i], "-s") == 0)
  406.     {
  407.         if(++i < argc)
  408.             defaultScreenSaverTime = ((long)atoi(argv[i])) * MILLI_PER_MIN;
  409.         else
  410.         UseMsg();
  411.     }
  412.     else if ( strcmp( argv[i], "-su") == 0)
  413.         disableSaveUnders = TRUE;
  414.     else if ( strcmp( argv[i], "-t") == 0)
  415.     {
  416.         if(++i < argc)
  417.             defaultPointerControl.threshold = atoi(argv[i]);
  418.         else
  419.         UseMsg();
  420.     }
  421.     else if ( strcmp( argv[i], "-to") == 0)
  422.     {
  423.         if(++i < argc)
  424.         TimeOutValue = ((long)atoi(argv[i])) * MILLI_PER_SECOND;
  425.         else
  426.         UseMsg();
  427.     }
  428.     else if ( strcmp( argv[i], "-terminate") == 0)
  429.     {
  430.         extern Bool terminateAtReset;
  431.         
  432.         terminateAtReset = TRUE;
  433.     }
  434.     else if ( strcmp( argv[i], "v") == 0)
  435.         defaultScreenSaverBlanking = PreferBlanking;
  436.     else if ( strcmp( argv[i], "-v") == 0)
  437.         defaultScreenSaverBlanking = DontPreferBlanking;
  438. #ifdef MEMBUG
  439.     else if ( strcmp ( argv[i], "validateMemory") == 0)
  440.     {
  441.         extern unsigned long MemoryValidate;
  442.         MemoryValidate = 1;
  443.     }
  444.     else if ( strcmp ( argv[i], "neverFreeMemory") == 0)
  445.     {
  446.         extern unsigned long MemoryNeverFree;
  447.         MemoryNeverFree = 1;
  448.     }
  449. #endif
  450.     else if ( strcmp( argv[i], "-wm") == 0)
  451.         defaultBackingStore = WhenMapped;
  452.     else if ( strcmp( argv[i], "-x") == 0)
  453.     {
  454.         if(++i >= argc)
  455.         UseMsg();
  456.         /* For U**x, which doesn't support dynamic loading, there's nothing
  457.          * to do when we see a -x.  Either the extension is linked in or
  458.          * it isn't */
  459.     }
  460.     else if ( strcmp( argv[i], "-I") == 0)
  461.     {
  462.         /* ignore all remaining arguments */
  463.         break;
  464.     }
  465.     else if (strncmp (argv[i], "tty", 3) == 0)
  466.     {
  467.         /* just in case any body is interested */
  468.         dev_tty_from_init = argv[i];
  469.     }
  470. #ifdef XDMCP
  471.     else if ((skip = XdmcpOptions(argc, argv, i)) != i)
  472.     {
  473.         i = skip - 1;
  474.     }
  475. #endif
  476. #ifdef AIXV3
  477.         else if ( strcmp( argv[i], "-timeout") == 0)
  478.         {
  479.             if(++i < argc)
  480.                 SelectWaitTime = atoi(argv[i]);
  481.             else
  482.                 UseMsg();
  483.         }
  484.         else if ( strcmp( argv[i], "-flush") == 0)
  485.         {
  486.             FlushOn++;
  487.         }
  488.         else if ( strcmp( argv[i], "-sync") == 0)
  489.         {
  490.             SyncOn++;
  491.         }
  492. #endif
  493. #ifdef XYZEXT
  494.         else if (strncmp(argv[i], "-xyz", 4) == 0) {
  495.           /*
  496.            * ignore options beginning with -xyz because XamineYourZerverInit
  497.            * will handle them.
  498.            */
  499.     }
  500. #endif
  501.      else
  502.      {
  503.         UseMsg();
  504.         exit (1);
  505.         }
  506.     }
  507. }
  508.  
  509. #ifndef SPECIAL_MALLOC
  510.  
  511. #ifdef MEMBUG
  512. #define FIRSTMAGIC 0x11aaaa11
  513. #define SECONDMAGIC 0x22aaaa22
  514. #define FREEDMAGIC  0x33aaaa33
  515. #define BLANKMAGIC  0x44aaaa44
  516. #define ALLOCMAGIC  0x55aaaa55
  517.  
  518. typedef struct _MallocHeader    *MallocHeaderPtr;
  519.  
  520. typedef struct _MallocHeader {
  521.     unsigned long    amount;
  522.     unsigned long    time;
  523.     MallocHeaderPtr    prev;
  524.     MallocHeaderPtr    next;
  525.     unsigned long    magic;
  526. } MallocHeaderRec;
  527.  
  528. typedef struct _MallocTrailer {
  529.     unsigned long    magic;
  530. } MallocTrailerRec, *MallocTrailerPtr;
  531.  
  532. unsigned long    MemoryAllocTime;
  533. unsigned long    MemoryAllocBreakpoint = ~0;
  534. unsigned long    MemoryFreeBreakpoint = ~0;
  535. unsigned long    MemoryActive = 0;
  536. unsigned long    MemoryValidate;
  537. unsigned long    MemoryNeverFree;
  538.  
  539. MallocHeaderPtr    MemoryInUse;
  540. MallocHeaderPtr    MemoryFreed;
  541.  
  542. #define request(amount)    ((amount) + sizeof (MallocHeaderRec) + sizeof (MallocTrailerRec))
  543. #define Header(ptr)    ((MallocHeaderPtr) (((char *) ptr) - sizeof (MallocHeaderRec)))
  544. #define Trailer(ptr)    ((MallocTrailerPtr) (((char *) ptr) + Header(ptr)->amount))
  545.  
  546. static unsigned long *
  547. SetupBlock(ptr, amount)
  548.     unsigned long   *ptr;
  549. {
  550.     MallocHeaderPtr    head = (MallocHeaderPtr) ptr;
  551.     MallocTrailerPtr    tail = (MallocTrailerPtr) (((char *) ptr) + amount + sizeof (MallocHeaderRec));
  552.  
  553.     MemoryActive += amount;
  554.     head->magic = FIRSTMAGIC;
  555.     head->amount = amount;
  556.     if (MemoryAllocTime == MemoryAllocBreakpoint)
  557.     head->amount = amount;
  558.     head->time = MemoryAllocTime++;
  559.     head->next = MemoryInUse;
  560.     head->prev = 0;
  561.     if (MemoryInUse)
  562.     MemoryInUse->prev = head;
  563.     MemoryInUse = head;
  564.  
  565.     tail->magic = SECONDMAGIC;
  566.     
  567.     return (unsigned long *)(((char *) ptr) + sizeof (MallocHeaderRec));
  568. }
  569.  
  570. ValidateAllActiveMemory ()
  571. {
  572.     MallocHeaderPtr    head;
  573.     MallocTrailerPtr    tail;
  574.  
  575.     for (head = MemoryInUse; head; head = head->next)
  576.     {
  577.     tail = (MallocTrailerPtr) (((char *) (head + 1)) + head->amount);
  578.         if (head->magic == FREEDMAGIC)
  579.         FatalError("Free data on active list");
  580.         if(head->magic != FIRSTMAGIC || tail->magic != SECONDMAGIC)
  581.         FatalError("Garbage object on active list");
  582.     }
  583.     for (head = MemoryFreed; head; head = head->next)
  584.     {
  585.     tail = (MallocTrailerPtr) (((char *) (head + 1)) + head->amount);
  586.     if (head->magic != FREEDMAGIC || tail->magic != FREEDMAGIC)
  587.         FatalError("Non free data on free list");
  588.     if (!CheckMemoryContents (head, BLANKMAGIC))
  589.         FatalError("Freed data reused");
  590.     }
  591. }
  592.  
  593. FillMemoryContents (head, value)
  594.     MallocHeaderPtr head;
  595.     long        value;
  596. {
  597.     int            count;
  598.     long        *store;
  599.  
  600.     count = head->amount / sizeof (long);
  601.     store = (long *) (head + 1);
  602.     while (count--)
  603.     *store++ = value;
  604. }
  605.  
  606. CheckMemoryContents (head, value)
  607.     MallocHeaderPtr head;
  608.     long        value;
  609. {
  610.     int            count;
  611.     long        *check;
  612.  
  613.     count = head->amount / sizeof (long);
  614.     check = (long *) (head + 1);
  615.     while (count--)
  616.     if (*check++ != value)
  617.         return FALSE;
  618.     return TRUE;
  619. }
  620.  
  621. #endif
  622.  
  623. /* XALLOC -- X's internal memory allocator.  Why does it return unsigned
  624.  * int * instead of the more common char *?  Well, if you read K&R you'll
  625.  * see they say that alloc must return a pointer "suitable for conversion"
  626.  * to whatever type you really want.  In a full-blown generic allocator
  627.  * there's no way to solve the alignment problems without potentially
  628.  * wasting lots of space.  But we have a more limited problem. We know
  629.  * we're only ever returning pointers to structures which will have to
  630.  * be long word aligned.  So we are making a stronger guarantee.  It might
  631.  * have made sense to make Xalloc return char * to conform with people's
  632.  * expectations of malloc, but this makes lint happier.
  633.  */
  634.  
  635. unsigned long * 
  636. Xalloc (amount)
  637.     unsigned long amount;
  638. {
  639.     char        *malloc();
  640.     register pointer  ptr;
  641.     
  642.     if ((long)amount <= 0)
  643.     return (unsigned long *)NULL;
  644.     /* aligned extra on long word boundary */
  645.     amount = (amount + 3) & ~3;
  646. #ifdef MEMBUG
  647.     if (MemoryValidate)
  648.     ValidateAllActiveMemory ();
  649.     if (!Must_have_memory && Memory_fail &&
  650.     ((random() % MEM_FAIL_SCALE) < Memory_fail))
  651.     return (unsigned long *)NULL;
  652.     if (ptr = (pointer)malloc(request(amount)))
  653.     {
  654.     unsigned long    *ret;
  655.     ret = SetupBlock (ptr, amount);
  656.     FillMemoryContents ((MallocHeaderPtr) ptr, ALLOCMAGIC);
  657.     return ret;
  658.     }
  659. #else
  660.     if (ptr = (pointer)malloc(amount))
  661.     return (unsigned long *)ptr;
  662. #endif
  663.     if (Must_have_memory)
  664.     FatalError("Out of memory");
  665.     return (unsigned long *)NULL;
  666. }
  667.  
  668. /*****************
  669.  * Xcalloc
  670.  *****************/
  671.  
  672. unsigned long *
  673. Xcalloc (amount)
  674.     unsigned long   amount;
  675. {
  676.     unsigned long   *ret;
  677.  
  678.     ret = Xalloc (amount);
  679.     if (ret)
  680.     bzero ((char *) ret, (int) amount);
  681.     return ret;
  682. }
  683.  
  684. /*****************
  685.  * Xrealloc
  686.  *****************/
  687.  
  688. unsigned long *
  689. Xrealloc (ptr, amount)
  690.     register pointer ptr;
  691.     unsigned long amount;
  692. {
  693.     char *malloc();
  694.     char *realloc();
  695.  
  696. #ifdef MEMBUG
  697.     if (ptr)
  698.     {
  699.         if (MemoryValidate)
  700.         ValidateAllActiveMemory ();
  701.         if ((long)amount <= 0)
  702.         {
  703.         if (!amount)
  704.             Xfree(ptr);
  705.         return (unsigned long *)NULL;
  706.         }
  707.         if (!Must_have_memory && Memory_fail &&
  708.         ((random() % MEM_FAIL_SCALE) < Memory_fail))
  709.         return (unsigned long *)NULL;
  710.         amount = (amount + 3) & ~3;
  711.     CheckNode(ptr);
  712.     ptr = (pointer)realloc((char *) Header (ptr), request(amount));
  713.     if (ptr)
  714.         return SetupBlock (ptr, amount);
  715.     }
  716.     else
  717.     {
  718.     return Xalloc (amount);
  719.     }
  720. #else
  721.     if ((long)amount <= 0)
  722.     {
  723.     if (ptr && !amount)
  724.         free(ptr);
  725.     return (unsigned long *)NULL;
  726.     }
  727.     amount = (amount + 3) & ~3;
  728.     if (ptr)
  729.         ptr = (pointer)realloc((char *)ptr, amount);
  730.     else
  731.     ptr = (pointer)malloc(amount);
  732.     if (ptr)
  733.         return (unsigned long *)ptr;
  734. #endif
  735.     if (Must_have_memory)
  736.     FatalError("Out of memory");
  737.     return (unsigned long *)NULL;
  738. }
  739.                     
  740. /*****************
  741.  *  Xfree
  742.  *    calls free 
  743.  *****************/    
  744.  
  745. void
  746. Xfree(ptr)
  747.     register pointer ptr;
  748. {
  749. #ifdef MEMBUG
  750.     if (MemoryValidate)
  751.     ValidateAllActiveMemory ();
  752.     if (ptr)
  753.     {
  754.     MallocHeaderPtr        head;
  755.     MallocTrailerPtr    trail;
  756.  
  757.     CheckNode(ptr);
  758.     head = Header(ptr);
  759.     trail = Trailer(ptr);
  760.     if (head->time == MemoryFreeBreakpoint)
  761.         head->magic = FIRSTMAGIC;
  762.     head->magic = FREEDMAGIC;
  763.     trail->magic = FREEDMAGIC;
  764.     FillMemoryContents (head, BLANKMAGIC);
  765.     if (MemoryNeverFree)
  766.     {
  767.         head->prev = 0;
  768.         head->next = MemoryFreed;
  769.         MemoryFreed = head;
  770.     }
  771.     else
  772.         free ((char *) head);
  773.     }
  774. #else
  775.     if (ptr)
  776.     free((char *)ptr); 
  777. #endif
  778. }
  779.  
  780. #ifdef MEMBUG
  781. static void
  782. CheckNode(ptr)
  783.     pointer ptr;
  784. {
  785.     MallocHeaderPtr    head;
  786.     MallocHeaderPtr    f, prev;
  787.  
  788.     if (ptr < minfree)
  789.     FatalError("Trying to free static storage");
  790.     head = Header(ptr);
  791.     if (((pointer) head) < minfree)
  792.     FatalError("Trying to free static storage");
  793.     if (head->magic == FREEDMAGIC)
  794.     FatalError("Freeing something already freed");
  795.     if(head->magic != FIRSTMAGIC || Trailer(ptr)->magic != SECONDMAGIC)
  796.     FatalError("Freeing a garbage object");
  797.     if(head->prev)
  798.     head->prev->next = head->next;
  799.     else
  800.     MemoryInUse = head->next;
  801.     if (head->next)
  802.     head->next->prev = head->prev;
  803.     MemoryActive -= head->amount;
  804. }
  805.  
  806. DumpMemoryInUse (time)
  807.     unsigned long   time;
  808. {
  809.     MallocHeaderPtr    head;
  810.  
  811.     for (head = MemoryInUse; head; head = head->next)
  812.     if (head->time >= time)
  813.         fprintf (stderr, "0x%08x %5d %6d\n", head,
  814.                     head->amount,
  815.                     head->time);
  816. }
  817.  
  818. static unsigned long    MarkedTime;
  819.  
  820. MarkMemoryTime ()
  821. {
  822.     MarkedTime = MemoryAllocTime;
  823. }
  824.  
  825. DumpMemorySince ()
  826. {
  827.     DumpMemoryInUse (MarkedTime);
  828. }
  829. #endif
  830. #endif /* SPECIAL_MALLOC */
  831.  
  832. /*VARARGS1*/
  833. void
  834. FatalError(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
  835.     char *f;
  836.     char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  837. {
  838.     ErrorF("\nFatal server error:\n");
  839.     ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  840.     ErrorF("\n");
  841.     AbortServer();
  842.     /*NOTREACHED*/
  843. }
  844.  
  845. /*VARARGS1*/
  846. void
  847. ErrorF( f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
  848.     char *f;
  849.     char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  850. {
  851. #ifdef AIXV3
  852.     fprintf(aixfd, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  853.     if (FlushOn || SyncOn)
  854.         fflush (aixfd);
  855.     if (SyncOn)
  856.         sync();
  857. #else
  858.     fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  859. #endif
  860. }
  861.  
  862. #ifdef AIXV3
  863. OpenDebug()
  864. {
  865.         if((aixfd = fopen(AIXFILE,"w")) == NULL )
  866.         {
  867.                 fprintf(stderr,"open aixfile failed\n");
  868.                 exit(-1);
  869.         }
  870.         chmod(AIXFILE,00777);
  871. }
  872. #endif
  873.