1 | /******************************************************************************* |
---|
2 | * The Elm Mail System - $Revision: 1.1.1.2 $ $State: Exp $ |
---|
3 | * |
---|
4 | * Copyright (c) 1992 USENET Community Trust |
---|
5 | ******************************************************************************* |
---|
6 | * Bug reports, patches, comments, suggestions should be sent to: |
---|
7 | * |
---|
8 | * Syd Weinstein, Elm Coordinator |
---|
9 | * elm@DSI.COM dsinc!elm |
---|
10 | * |
---|
11 | ******************************************************************************* |
---|
12 | * $Log: not supported by cvs2svn $ |
---|
13 | * Revision 1.1.1.1 1996/02/18 21:38:11 ylo |
---|
14 | * Imported ssh-1.2.13. |
---|
15 | * |
---|
16 | * Revision 1.1 1995/07/13 22:17:30 ylo |
---|
17 | * Added putenv.c (this file was missing from the original distribution). |
---|
18 | * |
---|
19 | * Revision 5.1 1992/10/03 22:41:36 syd |
---|
20 | * Initial checkin as of 2.4 Release at PL0 |
---|
21 | * |
---|
22 | * |
---|
23 | ******************************************************************************/ |
---|
24 | |
---|
25 | /* |
---|
26 | * This code was stolen from cnews. Modified to make "newenv" static so |
---|
27 | * that realloc() can be used on subsequent calls to avoid memory leaks. |
---|
28 | * |
---|
29 | * We only need this if Configure said there isn't a putenv() in libc. |
---|
30 | */ |
---|
31 | |
---|
32 | #include "includes.h" |
---|
33 | |
---|
34 | /* peculiar return values */ |
---|
35 | #define WORKED 0 |
---|
36 | #define FAILED 1 |
---|
37 | #define YES 1 |
---|
38 | #define NO 0 |
---|
39 | |
---|
40 | int |
---|
41 | putenv(var) /* put var in the environment */ |
---|
42 | char *var; |
---|
43 | { |
---|
44 | register char **envp; |
---|
45 | register int oldenvcnt; |
---|
46 | extern char **environ; |
---|
47 | static char **newenv = NULL; |
---|
48 | |
---|
49 | /* count variables, look for var */ |
---|
50 | for (envp = environ; *envp != 0; envp++) { |
---|
51 | register char *varp = var, *ep = *envp; |
---|
52 | register int namesame; |
---|
53 | |
---|
54 | namesame = NO; |
---|
55 | for (; *varp == *ep && *varp != '\0'; ++ep, ++varp) |
---|
56 | if (*varp == '=') |
---|
57 | namesame = YES; |
---|
58 | if (*varp == *ep && *ep == '\0') |
---|
59 | return WORKED; /* old & new var's are the same */ |
---|
60 | if (namesame) { |
---|
61 | *envp = var; /* replace var with new value */ |
---|
62 | return WORKED; |
---|
63 | } |
---|
64 | } |
---|
65 | oldenvcnt = envp - environ; |
---|
66 | |
---|
67 | /* allocate new environment with room for one more variable */ |
---|
68 | if (newenv == NULL) |
---|
69 | newenv = (char **)malloc((unsigned)((oldenvcnt+1+1)*sizeof(*envp))); |
---|
70 | else |
---|
71 | newenv = (char **)realloc((char *)newenv, (unsigned)((oldenvcnt+1+1)*sizeof(*envp))); |
---|
72 | if (newenv == NULL) |
---|
73 | return FAILED; |
---|
74 | |
---|
75 | /* copy old environment pointers, add var, switch environments */ |
---|
76 | (void) bcopy((char *)environ, (char *)newenv, oldenvcnt*sizeof(*envp)); |
---|
77 | newenv[oldenvcnt] = var; |
---|
78 | newenv[oldenvcnt+1] = NULL; |
---|
79 | environ = newenv; |
---|
80 | return WORKED; |
---|
81 | } |
---|