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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uC-libc/] [misc/] [setenv.c] - Blame information for rev 1782

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
setenv(var, value, overwrite)
14
const char * var;
15
const char * value;
16
int overwrite;
17
{
18
static char ** mall_env = 0;
19
static int extras = 0;
20
   char **p, **d;
21
   char * t;
22
   int len;
23
 
24
   len = strlen(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
         if (!overwrite)
37
                return -1;
38
         while( p[0] = p[1] ) p++;
39
         extras++;
40
         break;
41
      }
42
   }
43
 
44
   if( extras <= 0 )     /* Need more space */
45
   {
46
      d = malloc((p-environ+1+ADD_NUM)*sizeof(char*));
47
      if( d == 0 ) return -1;
48
 
49
      memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*));
50
      p = d + (p-environ);
51
      extras=ADD_NUM;
52
 
53
      if( mall_env ) free(mall_env);
54
      environ = d;
55
      mall_env = d;
56
   }
57
 
58
   t = malloc(len + 1 + strlen(value) + 1);
59
   if (!t)
60
        return -1;
61
 
62
   strcpy(t, var);
63
   strcat(t, "=");
64
   strcat(t, value);
65
 
66
   *p++ = (char*)t;
67
   *p = '\0';
68
   extras--;
69
 
70
   return 0;
71
}
72
 
73
 

powered by: WebSVN 2.1.0

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