eliminate duplicates

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15155 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
2005-05-17 09:02:32 +00:00
parent dc22282272
commit 3b147c4bdd

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000 Kungliga Tekniska H<>gskolan
* Copyright (c) 2000, 2005 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -39,65 +39,106 @@ RCSID("$Id$");
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "roken.h"
/* find assignment in env list; len is length of variable including
* equal
*/
static int
find_var(char **env, char *assignment, size_t len)
{
int i;
for(i = 0; env != NULL && env[i] != NULL; i++)
if(strncmp(env[i], assignment, len) == 0)
return i;
return -1;
}
/*
* return count of environment assignments from `file' and
* return count of environment assignments from open file F in
* assigned and list of malloced strings in env, return 0 or errno
* number
*/
static int
rk_read_env_file(FILE *F, char ***env, int *assigned)
{
int index = 0;
*assigned = 0;
int i;
char **l;
char buf[BUFSIZ], *p, *r;
char **tmp;
int ret = 0;
for(index = 0; *env != NULL && (*env)[index] != NULL; index++);
l = *env;
/* This is somewhat more relaxed on what it accepts then
* Wietses sysv_environ from K4 was...
*/
while (fgets(buf, BUFSIZ, F) != NULL) {
buf[strcspn(buf, "#\n")] = '\0';
for(p = buf; isspace((unsigned char)*p); p++);
if (*p == '\0')
continue;
/* Here one should check that it's a 'valid' env string... */
r = strchr(p, '=');
if (r == NULL)
continue;
if((i = find_var(l, p, r - p + 1)) >= 0) {
char *val = strdup(p);
if(val == NULL) {
ret = ENOMEM;
break;
}
free(l[i]);
l[i] = val;
(*assigned)++;
continue;
}
tmp = realloc(l, (index+2) * sizeof (char *));
if(tmp == NULL) {
ret = ENOMEM;
break;
}
l = tmp;
l[index] = strdup(p);
if(l[index] == NULL) {
ret = ENOMEM;
break;
}
l[++index] = NULL;
(*assigned)++;
}
if(ferror(F))
ret = errno;
*env = l;
return ret;
}
/*
* return count of environment assignments from file and
* list of malloced strings in `env'
*/
int ROKEN_LIB_FUNCTION
read_environment(const char *file, char ***env)
{
int i, k;
int assigned;
FILE *F;
char **l;
char buf[BUFSIZ], *p, *r;
if ((F = fopen(file, "r")) == NULL) {
if ((F = fopen(file, "r")) == NULL)
return 0;
}
i = 0;
if (*env) {
l = *env;
while (*l != NULL) {
i++;
l++;
}
}
l = *env;
/* This is somewhat more relaxed on what it accepts then
* Wietses sysv_environ from K4 was...
*/
while (fgets(buf, BUFSIZ, F) != NULL) {
if (buf[0] == '#')
continue;
p = strchr(buf, '#');
if (p != NULL)
*p = '\0';
p = buf;
while (*p == ' ' || *p == '\t' || *p == '\n') p++;
if (*p == '\0')
continue;
k = strlen(p);
if (p[k-1] == '\n')
p[k-1] = '\0';
/* Here one should check that is is a 'valid' env string... */
r = strchr(p, '=');
if (r == NULL)
continue;
l = realloc(l, (i+1) * sizeof (char *));
l[i++] = strdup(p);
}
rk_read_env_file(F, env, &assigned);
fclose(F);
l = realloc(l, (i+1) * sizeof (char *));
l[i] = NULL;
*env = l;
return i;
return assigned;
}