File: environ.c

package info (click to toggle)
xview 3.2p1.4-28.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 26,680 kB
  • ctags: 34,403
  • sloc: ansic: 241,397; yacc: 1,435; sh: 1,086; makefile: 148; lex: 76; perl: 54; asm: 50; cpp: 15
file content (206 lines) | stat: -rw-r--r-- 4,606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ident	"@(#)environ.c	1.9	93/06/28 SMI"

/*
 *      (c) Copyright 1989 Sun Microsystems, Inc.
 */

/*
 *      Sun design patents pending in the U.S. and foreign countries. See
 *      LEGAL_NOTICE file for terms of the license.
 */

#include <stdio.h>
#ifdef SYSV
#include <string.h>
#else
#include <strings.h>
extern char *strrchr();
extern char *strchr();
#endif
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "mem.h"

extern	char **environ;

/* -----------------------------------------------------------------------
 *	Local Data Structures
 * -----------------------------------------------------------------------*/

/*
 * 	Env - environment object
 */
typedef struct _env {
	char	**environ;	/* array of environment strings */
	int	length;		/* length of environ array */
	int	used;		/* number of entries actually used */
} Env;

/* -----------------------------------------------------------------------
 *	Local Functions
 * -----------------------------------------------------------------------*/

/*
 *	createEnv - Creates a new environment array that is the length of
 *		    of the current environment plus the number of additions.
 */
static void
createEnv(env,nadditions)
	Env	*env;
	int	nadditions;
{
	int	i = 0;

	/* find the number of items in the current environ */
	while (environ[i] != (char *)NULL) {
		i++;
	}

	/* create space for the environ strings */
	env->used = i;
	env->length = env->used + nadditions + 1;
	env->environ = MemAlloc(env->length*sizeof(char *));

	/* copy the current environ into the new one */
	for (i=0; i<env->used; i++) {
		env->environ[i] = MemNewString(environ[i]);
	}
	env->environ[i] = (char *)NULL;
}

/*
 *	putEnv - Puts the name,value pair into the specified environment
 *	         replacing any existing values.
 *		 Assumes there is space for the new setting.
 */
static void
putEnv(env,name,value)
	Env	*env;
	char	*name;
	char	*value;
{
	int	nameLen = strlen(name);
	char	*envVar;
	int	count;

	/* create new env string with space for '=' and null */
	envVar = (char *)MemAlloc(nameLen + strlen(value) +2);

	(void)sprintf(envVar,"%s=%s",name,value);

	/* search through, checking for variable in question */
	for (count=0 ; count<env->used; count++) {
		if (!strncmp(env->environ[count],name,nameLen))
			break;
	}
	

	if (count == env->used)		/* finished loop without match */
		env->used++;		/* added 1 more var to the env */
	else
		MemFree(env->environ[count]);	/* don't need */

	env->environ[count] = envVar;

	/* make sure the last entry in the vector is NULL */
	env->environ[env->used] = (char *)NULL;

}

/*
 *	putDisplayEnv - sets the DISPLAY env to the appropriate screen
 */
static void
putDisplayEnv(env,dpy,screen)
	Env	*env;
	Display	*dpy;
	int	screen;
{
	char	*display = DisplayString(dpy);
	char	*colon,*dot;
	char	value[128];
	int	len;

	if ((colon = strrchr(display,':')) == (char *)NULL) {
		return;
	}
	if ((dot = strchr(colon,'.')) != (char *)NULL) {
		len = dot - display;
	} else {
		len = colon - display;
	}

	(void)sprintf(value,"%*s.%d",len,display,screen);

	putEnv(env,"DISPLAY",value);
}

#ifndef NOSVENV
/*
 *	putSunViewEnv - sets the various SV environment variables
 */
static void
putSunViewEnv(env,dpy,screen)
	Env	*env;
	Display *dpy;
	int	screen;
{
	static char	*svEnv[] = { "WINDOW_PARENT", 
				     "WMGR_ENV_PLACEHOLDER", 
				     "WINDOW_TTYPARMS" };
	int		i, svEnvLen = sizeof(svEnv)/sizeof(char *);
	char		*result,*curpos;
	unsigned long	nitems,remainder;
	extern void	*GetWindowProperty();
	extern Atom	AtomSunViewEnv;

	result = (char *)GetWindowProperty(dpy,RootWindow(dpy,screen),
			AtomSunViewEnv,0L,100000L,
			XA_STRING,8,&nitems,&remainder);

	if (result == NULL)
		return;

	curpos = result;
	for (i=0; i<svEnvLen; i++) {
		putEnv(env,svEnv[i],curpos);
		curpos += strlen(curpos) + 1;
	}
	XFree((char *)result);
}
#endif /* NOSVENV */

/* -----------------------------------------------------------------------
 *	Global Functions
 * -----------------------------------------------------------------------*/

/*
 *	MakeEnviron - returns a new environment array that contains the
 *		      current environ plus a modified DISPLAY and
 *		      SunView environment variables.
 */
char **
MakeEnviron(dpy,screen)
	Display	*dpy;
	int	screen;
{
	Env	newEnv;
	int	nadditions;

	nadditions = 1;		/* for DISPLAY */

#ifndef NOSVENV
	nadditions += 3;	/* for SV environment */
#endif /* NOSVENV */

	createEnv(&newEnv,nadditions);

	putDisplayEnv(&newEnv,dpy,screen);

#ifndef NOSVENV
	putSunViewEnv(&newEnv,dpy,screen);
#endif /* NOSVENV */

	return newEnv.environ;
}