OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [misc/] [putenv.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2
 * This file is part of the Linux-8086 C library and is distributed
3
 * under the GNU Library General Public License.
4
 */
5
#include <string.h>
6
#include <stdlib.h>
7
#include <malloc.h>
8
 
9
extern char ** environ;
10
#define ADD_NUM 4
11
 
12
int
13
putenv(var)
14
const char * var;
15
{
16
static char ** mall_env = 0;
17
static int extras = 0;
18
   char **p, **d;
19
   char * r;
20
   int len;
21
 
22
   r = strchr(var, '=');
23
   if( r == 0 )  len = strlen(var);
24
   else          len = r-var;
25
 
26
   if (!environ) {
27
        environ = (char**)malloc(ADD_NUM * sizeof(char*));
28
        memset(environ, 0, sizeof(char*)*ADD_NUM);
29
        extras = ADD_NUM;
30
   }
31
 
32
   for(p=environ; *p; p++)
33
   {
34
      if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' )
35
      {
36
         while( p[0] = p[1] ) p++;
37
         extras++;
38
         break;
39
      }
40
   }
41
   if( r == 0 ) return 0;
42
   if( extras <= 0 )     /* Need more space */
43
   {
44
      d = malloc((p-environ+1+ADD_NUM)*sizeof(char*));
45
      if( d == 0 ) return -1;
46
 
47
      memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*));
48
      p = d + (p-environ);
49
      extras=ADD_NUM;
50
 
51
      if( mall_env ) free(mall_env);
52
      environ = d;
53
      mall_env = d;
54
   }
55
   *p++ = strdup((char*)var);
56
   *p = '\0';
57
   extras--;
58
 
59
   return 0;
60
}
61
 
62
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.