Add:support: Simple setenv/getenv for the support libc
authorzaxl <zaxl@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sat, 8 Nov 2008 01:01:45 +0000 (01:01 +0000)
committerzaxl <zaxl@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sat, 8 Nov 2008 01:01:45 +0000 (01:01 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk/navit@1690 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/support/libc/libc.c

index 42d77f6..982c79d 100644 (file)
 #include "locale.h"
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 int errno;
 
+static int at_exit_registered;
+#define REGISTER_AT_EXIT()                                     \
+       do {                                                    \
+       if (!at_exit_registered)                                \
+               at_exit_registered = !atexit(cleanup_libc);     \
+       } while(0)
+
+#define MAXENV 32
+static char *envnames[MAXENV];
+static char *envvars[MAXENV];
+
+static void cleanup_libc(void)
+{
+       int i;
+       for (i=0; i <MAXENV; i++) {
+               if (envnames[i])
+                       free(envnames[i]);
+               if (envvars[i])
+                       free(envvars[i]);
+       }
+}
+
 char *
 getenv(char *name)
 {
-       return 0;
+       int i;
+       REGISTER_AT_EXIT();
+       for (i=0; i < MAXENV; i++) {
+               if (envnames[i] && !strcmp(envnames[i], name))
+                       return envvars[i];
+       }
+       return NULL;
 }
 
-void
-setenv(void)
+int
+setenv(const char *name, const char *value, int overwrite)
+{
+       int i;
+       char *val;
+       REGISTER_AT_EXIT();
+       for (i=0; i < MAXENV; i++) {
+               if (envnames[i] && !strcmp(envnames[i], name)) {
+                       if (overwrite) {
+                               val = strdup(value);
+                               if (!val)
+                                       return -1;
+                               if (envvars[i])
+                                       free(envvars[i]);
+                               envvars[i] = val;
+                       }
+                       return 0;
+               }
+       }
+       for (i=0; i < MAXENV; i++) {
+               if (!envnames[i]) {
+                       envnames[i] = strdup(name);
+                       envvars[i] = strdup(value);
+                       if (!envnames[i] || !envvars[i]) {
+                               if (envnames[i])
+                                       free(envnames[i]);
+                               if (envvars[i])
+                                       free(envvars[i]);
+                               envnames[i] = NULL;
+                               envvars[i] = NULL;
+                               return -1;
+                       }
+                       return 0;
+               }
+       }
+       return -1;
+}
+
+int unsetenv(const char *name)
 {
+       int i;
+       for (i=0; i < MAXENV; i++) {
+               if (envnames[i] && !strcmp(envnames[i], name)) {
+                       free(envnames[i]);
+                       envnames[i] = NULL;
+                       if (envvars[i])
+                               free(envvars[i]);
+                       envvars[i] = NULL;
+               }
+       }
+       return 0;
 }
 
 char *
-getcwd(void)
+getcwd(char *buf, int size)
 {
        return "dummy";
 }
 
 char *
-getwd(void)
+getwd(char *buf)
 {
        return "dummy";
 }
@@ -30,7 +109,7 @@ char *strtok_r(char *str, const char *delim, char **saveptr)
 }
 
 void
-perror(char *x)
+perror(const char *x)
 {
 }