misc.c revision 99c4c48a
1/* $Xorg: misc.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
2/******************************************************************************
3
4Copyright 1993, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25******************************************************************************/
26/* $XFree86: xc/programs/xsm/misc.c,v 1.5 2001/07/25 15:05:29 dawes Exp $ */
27
28#include "xsm.h"
29
30
31#ifndef HAVE_PUTENV
32/*
33 * define our own putenv() if the system doesn't have one.
34 * putenv(s): place s (a string of the form "NAME=value") in
35 * the environment; replacing any existing NAME.  s is placed in
36 * environment, so if you change s, the environment changes (like
37 * putenv on a sun).  Binding removed if you putenv something else
38 * called NAME.
39 */
40int
41putenv(char *s)
42{
43    char *v;
44    int varlen, idx;
45    char **newenv;
46    static int virgin = 1; /* true while "environ" is a virgin */
47
48    v = strchr(s, '=');
49    if(v == 0)
50	return 0; /* punt if it's not of the right form */
51    varlen = (v + 1) - s;
52
53    for (idx = 0; environ[idx] != 0; idx++) {
54	if (strncmp(environ[idx], s, varlen) == 0) {
55	    if(v[1] != 0) { /* true if there's a value */
56		environ[idx] = s;
57		return 0;
58	    } else {
59		do {
60		    environ[idx] = environ[idx+1];
61		} while(environ[++idx] != 0);
62		return 0;
63	    }
64	}
65    }
66
67    /* add to environment (unless no value; then just return) */
68    if(v[1] == 0)
69	return 0;
70    if(virgin) {
71	register i;
72
73	newenv = (char **) XtMalloc((unsigned) ((idx + 2) * sizeof(char*)));
74	if(newenv == 0)
75	    return -1;
76	for(i = idx-1; i >= 0; --i)
77	    newenv[i] = environ[i];
78	virgin = 0;     /* you're not a virgin anymore, sweety */
79    } else {
80	newenv = (char **) realloc((char *) environ,
81				   (unsigned) ((idx + 2) * sizeof(char*)));
82	if (newenv == 0)
83	    return -1;
84    }
85
86    environ = newenv;
87    environ[idx] = s;
88    environ[idx+1] = 0;
89
90    return 0;
91}
92
93#endif /* NOPUTENV */
94
95
96
97int
98strbw(const char *a, const char *b)
99{
100    return !strncmp (a, b, strlen (b));
101}
102
103
104
105void
106nomem(void)
107{
108    fprintf (stderr, "Insufficient memory.\n");
109    exit (255);
110}
111
112