home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xdm / util.c.orig < prev    next >
Encoding:
Text File  |  1991-04-16  |  4.3 KB  |  241 lines

  1. /*
  2.  * xdm - display manager daemon
  3.  *
  4.  * $XConsortium: util.c,v 1.13 91/04/17 10:06:32 rws Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. /*
  22.  * util.c
  23.  *
  24.  * various utility routines
  25.  */
  26.  
  27. # include   "dm.h"
  28. #if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
  29. # include   <signal.h>
  30. #else
  31. #define _POSIX_SOURCE
  32. # include   <signal.h>
  33. #undef _POSIX_SOURCE
  34. #endif
  35.  
  36. printEnv (e)
  37. char    **e;
  38. {
  39.     while (*e)
  40.         Debug ("%s\n", *e++);
  41. }
  42.  
  43. static char *
  44. makeEnv (name, value)
  45. char    *name;
  46. char    *value;
  47. {
  48.     char    *result;
  49.  
  50.     result = malloc ((unsigned) (strlen (name) + strlen (value) + 2));
  51.     if (!result) {
  52.         LogOutOfMem ("makeEnv");
  53.         return 0;
  54.     }
  55.     sprintf (result, "%s=%s", name, value);
  56.     return result;
  57. }
  58.  
  59. char *
  60. getEnv (e, name)
  61.     char    **e;
  62.     char    *name;
  63. {
  64.     int    l = strlen (name);
  65.  
  66.     while (*e) {
  67.         if ((int)strlen (*e) > l && !strncmp (*e, name, l) &&
  68.             (*e)[l] == '=')
  69.             return (*e) + l + 1;
  70.         ++e;
  71.     }
  72.     return 0;
  73. }
  74.  
  75. char **
  76. setEnv (e, name, value)
  77.     char    **e;
  78.     char    *name;
  79.     char    *value;
  80. {
  81.     char    **new, **old;
  82.     char    *newe;
  83.     int    envsize;
  84.     int    l;
  85.  
  86.     l = strlen (name);
  87.     newe = makeEnv (name, value);
  88.     if (!newe) {
  89.         LogOutOfMem ("setEnv");
  90.         return e;
  91.     }
  92.     if (e) {
  93.         for (old = e; *old; old++)
  94.             if ((int)strlen (*old) > l && !strncmp (*old, name, l) && (*old)[l] == '=')
  95.                 break;
  96.         if (*old) {
  97.             free (*old);
  98.             *old = newe;
  99.             return e;
  100.         }
  101.         envsize = old - e;
  102.         new = (char **) realloc ((char *) e,
  103.                 (unsigned) ((envsize + 2) * sizeof (char *)));
  104.     } else {
  105.         envsize = 0;
  106.         new = (char **) malloc (2 * sizeof (char *));
  107.     }
  108.     if (!new) {
  109.         LogOutOfMem ("setEnv");
  110.         free (newe);
  111.         return e;
  112.     }
  113.     new[envsize] = newe;
  114.     new[envsize+1] = 0;
  115.     return new;
  116. }
  117.  
  118. freeEnv (env)
  119.     char    **env;
  120. {
  121.     char    **e;
  122.  
  123.     if (env)
  124.     {
  125.         for (e = env; *e; e++)
  126.         free (*e);
  127.         free (env);
  128.     }
  129. }
  130.  
  131. # define isblank(c)    ((c) == ' ' || c == '\t')
  132.  
  133. char **
  134. parseArgs (argv, string)
  135. char    **argv;
  136. char    *string;
  137. {
  138.     char    *word;
  139.     char    *save;
  140.     int    i;
  141.  
  142.     i = 0;
  143.     while (argv && argv[i])
  144.         ++i;
  145.     if (!argv) {
  146.         argv = (char **) malloc (sizeof (char *));
  147.         if (!argv) {
  148.             LogOutOfMem ("parseArgs");
  149.             return 0;
  150.         }
  151.     }
  152.     word = string;
  153.     for (;;) {
  154.         if (!*string || isblank (*string)) {
  155.             if (word != string) {
  156.                 argv = (char **) realloc ((char *) argv,
  157.                     (unsigned) ((i + 2) * sizeof (char *)));
  158.                 save = malloc ((unsigned) (string - word + 1));
  159.                 if (!argv || !save) {
  160.                     LogOutOfMem ("parseArgs");
  161.                     if (argv)
  162.                         free ((char *) argv);
  163.                     if (save)
  164.                         free (save);
  165.                     return 0;
  166.                 }
  167.                 argv[i] = strncpy (save, word, string-word);
  168.                 argv[i][string-word] = '\0';
  169.                 i++;
  170.             }
  171.             if (!*string)
  172.                 break;
  173.             word = string + 1;
  174.         }
  175.         ++string;
  176.     }
  177.     argv[i] = 0;
  178.     return argv;
  179. }
  180.  
  181. freeArgs (argv)
  182.     char    **argv;
  183. {
  184.     char    **a;
  185.  
  186.     if (!argv)
  187.     return;
  188.  
  189.     for (a = argv; *a; a++)
  190.     free (*a);
  191.     free ((char *) argv);
  192. }
  193.  
  194. CleanUpChild ()
  195. {
  196. #if defined(SYSV) || defined(SVR4)
  197.     setpgrp ();
  198. #else
  199.     setpgrp (0, getpid ());
  200.     sigsetmask (0);
  201. #endif
  202. #ifdef SIGCHLD
  203.     (void) Signal (SIGCHLD, SIG_DFL);
  204. #endif
  205.     (void) Signal (SIGTERM, SIG_DFL);
  206.     (void) Signal (SIGPIPE, SIG_DFL);
  207.     (void) Signal (SIGALRM, SIG_DFL);
  208.     (void) Signal (SIGHUP, SIG_DFL);
  209.     CloseOnFork ();
  210. }
  211.  
  212. static char localHostbuf[256];
  213. static int  gotLocalHostname;
  214.  
  215. char *
  216. localHostname ()
  217. {
  218.     if (!gotLocalHostname)
  219.     {
  220.     XmuGetHostname (localHostbuf, sizeof (localHostbuf) - 1);
  221.     gotLocalHostname = 1;
  222.     }
  223.     return localHostbuf;
  224. }
  225.  
  226. SIGVAL (*Signal (sig, handler))()
  227.     int sig;
  228.     SIGVAL (*handler)();
  229. {
  230. #ifndef X_NOT_POSIX
  231.     struct sigaction sigact, osigact;
  232.     sigact.sa_handler = handler;
  233.     sigemptyset(&sigact.sa_mask);
  234.     sigact.sa_flags = 0;
  235.     sigaction(sig, &sigact, &osigact);
  236.     return osigact.sa_handler;
  237. #else
  238.     return signal(sig, handler);
  239. #endif
  240. }
  241.