aboutsummaryrefslogtreecommitdiff
path: root/sys/etc/envreset.x
blob: 9ca8c5d7f27f60d258cbe8518fdc443a87f894d3 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<knet.h>
include	"environ.h"

# ENVRESET -- Update the value of the named environment variable in place.
# This is used to permanently change the value of an environment variable,
# unlike ENVPUTS which will create a temporary redefinition which can later
# be discarded via ENVFREE.  A fixed amount of string storage is allocated
# for the value string when the environment variable is first defined; if
# the new value won't fit we simply call ENVPUTS to redefine the variable
# at the top of the environment stack.  A more sophisticated storage
# mechanism could be devised which could dynamically allocate more storage,
# but the simpler scheme seems adequate at present.

procedure envreset (key, value)

char	key[ARB]		# environment variable name
char	value[ARB]		# new string value

long	sum
pointer	el, ep
int	head, ip, junk, maxch
int	envputs(), strlen()
include	"environ.com"

begin
	# Get index into envbuf of the first element of the thread.
	if (key[1] == EOS)
	    head = NULL
	else {
	    sum = 0
	    do ip = 1, MAX_HASHCHARS {
		if (key[ip] == EOS)
		    break
		sum = sum + (sum + key[ip])
	    }
	    head = threads[mod(sum,NTHREADS)+1]
	}

	# If thread is not empty search down it for the named key; if the key
	# is redefined the most recent entry is updated.

	el = NULL
	if (head != NULL)
	    for (el = envbuf + head;  el > envbuf;  el = envbuf + E_NEXT(el)) {
		ep = E_SETP(el)
		for (ip=1;  key[ip] == Memc[ep];  ip=ip+1)
		    ep = ep + 1
		if (key[ip] == EOS && Memc[ep] == '=')
		    break
	    }

	# If the named key is not found or the new value won't fit add or
	# redefine the variable, otherwise set the new value.

	if (el <= envbuf)
	    junk = envputs (key, value)
	else if (strlen(value) > E_LEN(el))
	    junk = envputs (key, value)
	else {
	    maxch = E_LEN(el)
	    call strcpy (value, Memc[ep+1], maxch)
	    call ki_envreset (key, value)
	}
end