summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/putenv.c
blob: 34cbd3add896f0de2d88900afbfa7d71d386bab9 (plain)
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
/****************************************************************/
/*                                                              */
/*      putenv(3)                                               */
/*                                                              */
/*              Change or add an environment entry              */
/*                                                              */
/****************************************************************/
/*   origination        1987-Oct-7               T. Holm        */
/****************************************************************/

/*
Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
From: tholm@uvicctr.UUCP (Terrence W. Holm)
Newsgroups: comp.os.minix
Subject: putenv(3)
Message-ID: <395@uvicctr.UUCP>
Date: 5 May 88 06:40:52 GMT
Organization: University of Victoria, Victoria B.C. Canada

EFTH Minix report #2  - May 1988 -  putenv(3)

This is an implementation of putenv(3) that we
wrote for Minix. Please consider this a public
domain program.
*/

#define NULL 0
#define  PSIZE  sizeof(char *)

extern  char  **environ;

char  *strchr();
char  *malloc();

/****************************************************************/
/*                                                              */
/*      int                                                     */
/*      putenv( entry )                                         */
/*                                                              */
/*              The "entry" should follow the form              */
/*              "NAME=VALUE". This routine will search the      */
/*              user environment for "NAME" and replace its     */
/*              value with "VALUE".                             */
/*                                                              */
/*              Note that "entry" is not copied, it is used     */
/*              as the environment entry. This means that it    */
/*              must not be unallocated or otherwise modifed    */
/*              by the caller, unless it is replaced by a       */
/*              subsequent putenv().                            */
/*                                                              */
/*              If the name is not found in the environment,    */
/*              then a new vector of pointers is allocated,     */
/*              "entry" is put at the end and the global        */
/*              variable "environ" is updated.                  */
/*                                                              */
/*              This function normally returns 0, but -1        */
/*              is returned if it can not allocate enough       */
/*              space using malloc(3), or "entry" does not      */
/*              contain a '='.                                  */
/*                                                              */
/****************************************************************/


int
putenv( entry )
  char *entry;
{
  unsigned length;
  unsigned size;
  char     *temp;
  char     **p;
  char     **new_environ;

  /*  Find the length of the "NAME="  */

  temp = strchr(entry,'=');
  if ( temp == 0 )
    return( -1 );

  length = (unsigned) (temp - entry + 1);


  /*  Scan through the environment looking for "NAME="  */

  for ( p=environ; *p != 0 ; p++ )
    if ( strncmp( entry, *p, length ) == 0 )
      {
      *p = entry;
      return( 0 );
      }


  /*  The name was not found, build a bigger environment  */

  size = p - environ;

  new_environ = (char **) malloc( (size+2)*PSIZE );

  if ( new_environ == (char **) NULL )
    return( -1 );

  memcpy ((char *) new_environ, (char *) environ, size*PSIZE );

  new_environ[size]   = entry;
  new_environ[size+1] = NULL;

  environ = new_environ;

  return(0);
}