This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
putenv.c
106 lines (85 loc) · 2.32 KB
/
putenv.c
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
/*
** $Id: putenv.c,v 2.0 90/09/19 20:16:07 paul Rel $
**
** Implements putenv(3c).
*/
/*
** Copyright 1989,1990 by Paul Sutcliffe Jr.
**
** Permission is hereby granted to copy, reproduce, redistribute,
** or otherwise use this software as long as: there is no monetary
** profit gained specifically from the use or reproduction or this
** software, it is not sold, rented, traded or otherwise marketed,
** and this copyright notice is included prominently in any copy
** made.
**
** The author make no claims as to the fitness or correctness of
** this software for any use whatsoever, and it is provided as is.
** Any use of this software is at the user's own risk.
*/
/*
** $Log: putenv.c,v $
** Revision 2.0 90/09/19 20:16:07 paul
** Initial 2.0 release
**
*/
#include "getty.h"
#if defined(RCSID) && !defined(lint)
static char *RcsId =
"@(#)$Id: putenv.c,v 2.0 90/09/19 20:16:07 paul Rel $";
#endif
#ifndef MAXENV
#define MAXENV 64 /* max # lines in envorinment */
#endif /* MAXENV */
/*
** putenv() - change or add value to environment
**
** Returns non-zero if an error occurrs, zero otherwise.
*/
extern char **environ;
int
putenv(s)
char *s;
{
register int i;
register char *p;
char *q, **envp, *env[MAXENV];
boolean match = FALSE;
if (s == (char *) NULL)
return(FAIL); /* can't add NULL to the environment */
if ((p = malloc((unsigned) strlen(s)+1)) == (char *) NULL)
return(FAIL); /* malloc failed */
(void) strcpy(p, s);
if ((q = index(p, '=')) == (char *) NULL) {
free(p);
return(FAIL); /* not in the form ``name=value'' */
}
*q = '\0'; /* split into two fields, name & value */
/* copy the environ list, replacing `s' if a match is found
*/
for (i=0, envp=environ; *envp != (char *) NULL; i++, envp++) {
if (strnequal(*envp, p, strlen(p))) {
match = TRUE;
env[i] = s;
} else
env[i] = *envp;
}
if (!match) {
*q = '='; /* put back the equal sign */
env[i++] = p; /* add p to env list */
} else
free(p); /* not needed, s replaced old value */
env[i++] = (char *) NULL;
/* now dup env to make new environment
*/
if ((envp = (char **) malloc((unsigned) (i*sizeof(char *)))) ==
(char **) NULL) {
return(FAIL);
}
environ = envp; /* point to new area */
for (i=0; env[i] != (char *) NULL; i++)
*envp++ = env[i];
*envp = (char *) NULL;
return(SUCCESS);
}
/* end of putenv.c */