home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xdm / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  4.4 KB  |  243 lines

  1. /*
  2.  * xdm - display manager daemon
  3.  *
  4.  * $XConsortium: util.c,v 1.14 92/01/21 15:38:28 gildea 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.     if (!e) return 0;
  67.  
  68.     while (*e) {
  69.         if ((int)strlen (*e) > l && !strncmp (*e, name, l) &&
  70.             (*e)[l] == '=')
  71.             return (*e) + l + 1;
  72.         ++e;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. char **
  78. setEnv (e, name, value)
  79.     char    **e;
  80.     char    *name;
  81.     char    *value;
  82. {
  83.     char    **new, **old;
  84.     char    *newe;
  85.     int    envsize;
  86.     int    l;
  87.  
  88.     l = strlen (name);
  89.     newe = makeEnv (name, value);
  90.     if (!newe) {
  91.         LogOutOfMem ("setEnv");
  92.         return e;
  93.     }
  94.     if (e) {
  95.         for (old = e; *old; old++)
  96.             if ((int)strlen (*old) > l && !strncmp (*old, name, l) && (*old)[l] == '=')
  97.                 break;
  98.         if (*old) {
  99.             free (*old);
  100.             *old = newe;
  101.             return e;
  102.         }
  103.         envsize = old - e;
  104.         new = (char **) realloc ((char *) e,
  105.                 (unsigned) ((envsize + 2) * sizeof (char *)));
  106.     } else {
  107.         envsize = 0;
  108.         new = (char **) malloc (2 * sizeof (char *));
  109.     }
  110.     if (!new) {
  111.         LogOutOfMem ("setEnv");
  112.         free (newe);
  113.         return e;
  114.     }
  115.     new[envsize] = newe;
  116.     new[envsize+1] = 0;
  117.     return new;
  118. }
  119.  
  120. freeEnv (env)
  121.     char    **env;
  122. {
  123.     char    **e;
  124.  
  125.     if (env)
  126.     {
  127.         for (e = env; *e; e++)
  128.         free (*e);
  129.         free (env);
  130.     }
  131. }
  132.  
  133. # define isblank(c)    ((c) == ' ' || c == '\t')
  134.  
  135. char **
  136. parseArgs (argv, string)
  137. char    **argv;
  138. char    *string;
  139. {
  140.     char    *word;
  141.     char    *save;
  142.     int    i;
  143.  
  144.     i = 0;
  145.     while (argv && argv[i])
  146.         ++i;
  147.     if (!argv) {
  148.         argv = (char **) malloc (sizeof (char *));
  149.         if (!argv) {
  150.             LogOutOfMem ("parseArgs");
  151.             return 0;
  152.         }
  153.     }
  154.     word = string;
  155.     for (;;) {
  156.         if (!*string || isblank (*string)) {
  157.             if (word != string) {
  158.                 argv = (char **) realloc ((char *) argv,
  159.                     (unsigned) ((i + 2) * sizeof (char *)));
  160.                 save = malloc ((unsigned) (string - word + 1));
  161.                 if (!argv || !save) {
  162.                     LogOutOfMem ("parseArgs");
  163.                     if (argv)
  164.                         free ((char *) argv);
  165.                     if (save)
  166.                         free (save);
  167.                     return 0;
  168.                 }
  169.                 argv[i] = strncpy (save, word, string-word);
  170.                 argv[i][string-word] = '\0';
  171.                 i++;
  172.             }
  173.             if (!*string)
  174.                 break;
  175.             word = string + 1;
  176.         }
  177.         ++string;
  178.     }
  179.     argv[i] = 0;
  180.     return argv;
  181. }
  182.  
  183. freeArgs (argv)
  184.     char    **argv;
  185. {
  186.     char    **a;
  187.  
  188.     if (!argv)
  189.     return;
  190.  
  191.     for (a = argv; *a; a++)
  192.     free (*a);
  193.     free ((char *) argv);
  194. }
  195.  
  196. CleanUpChild ()
  197. {
  198. #if defined(SYSV) || defined(SVR4)
  199.     setpgrp ();
  200. #else
  201.     setpgrp (0, getpid ());
  202.     sigsetmask (0);
  203. #endif
  204. #ifdef SIGCHLD
  205.     (void) Signal (SIGCHLD, SIG_DFL);
  206. #endif
  207.     (void) Signal (SIGTERM, SIG_DFL);
  208.     (void) Signal (SIGPIPE, SIG_DFL);
  209.     (void) Signal (SIGALRM, SIG_DFL);
  210.     (void) Signal (SIGHUP, SIG_DFL);
  211.     CloseOnFork ();
  212. }
  213.  
  214. static char localHostbuf[256];
  215. static int  gotLocalHostname;
  216.  
  217. char *
  218. localHostname ()
  219. {
  220.     if (!gotLocalHostname)
  221.     {
  222.     XmuGetHostname (localHostbuf, sizeof (localHostbuf) - 1);
  223.     gotLocalHostname = 1;
  224.     }
  225.     return localHostbuf;
  226. }
  227.  
  228. SIGVAL (*Signal (sig, handler))()
  229.     int sig;
  230.     SIGVAL (*handler)();
  231. {
  232. #ifndef X_NOT_POSIX
  233.     struct sigaction sigact, osigact;
  234.     sigact.sa_handler = handler;
  235.     sigemptyset(&sigact.sa_mask);
  236.     sigact.sa_flags = 0;
  237.     sigaction(sig, &sigact, &osigact);
  238.     return osigact.sa_handler;
  239. #else
  240.     return signal(sig, handler);
  241. #endif
  242. }
  243.