minix/lib/other/putenv.c
Ben Gras 9df1183b94 . removed const from putenv() for g++
. added safecopies.c:
  these are library functions to maintain grant tables in own address space
. sys_safecopy.c:
  interfaces to kernel calls to perform safe copy functions in from or to
  foreign process
. changes in i/o fields (type merged with request) reflected in
  library functions (sys_out.c, sys_vinb.c, sys_vinl.c, sys_vinw.c,
  sys_voutb.c, sys_voutl.c, sys_voutw.c)
. type merged with request in sys_sdevio, also now accepts offset which
  is used when a grant is specified (the _DIO_SAFE subtype)
. system printf() function changed to send DIAGNOSTICS_S messages, which
  specify a grant id instead of a direct address for the buffer to be
  printed; tty and log can then safecopy the buffer
2006-06-20 08:45:04 +00:00

80 lines
1.5 KiB
C
Executable file

/*
* (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
#include <stdlib.h>
#include <string.h>
#define ENTRY_INC 10
#define rounded(x) (((x / ENTRY_INC) + 1) * ENTRY_INC)
extern _CONST char ***_penviron;
int
putenv(name)
char *name;
{
register _CONST char **v = *_penviron;
register char *r;
static int size = 0;
/* When size != 0, it contains the number of entries in the
* table (including the final NULL pointer). This means that the
* last non-null entry is environ[size - 2].
*/
if (!name) return 0;
if (*_penviron == NULL) return 1;
if (r = strchr(name, '=')) {
register _CONST char *p, *q;
*r = '\0';
if (v != NULL) {
while ((p = *v) != NULL) {
q = name;
while (*q && (*q++ == *p++))
/* EMPTY */ ;
if (*q || (*p != '=')) {
v++;
} else {
/* The name was already in the
* environment.
*/
*r = '=';
*v = name;
return 0;
}
}
}
*r = '=';
v = *_penviron;
}
if (!size) {
register _CONST char **p;
register int i = 0;
if (v)
do {
i++;
} while (*v++);
if (!(v = malloc(rounded(i) * sizeof(char **))))
return 1;
size = i;
p = *_penviron;
*_penviron = v;
while (*v++ = *p++); /* copy the environment */
v = *_penviron;
} else if (!(size % ENTRY_INC)) {
if (!(v = realloc(*_penviron, rounded(size) * sizeof(char **))))
return 1;
*_penviron = v;
}
v[size - 1] = name;
v[size] = NULL;
size++;
return 0;
}