Browse Source

Add putenv, setenv

K. Lange 4 years ago
parent
commit
2a4914057c
9 changed files with 98 additions and 75 deletions
  1. 1 25
      apps/compositor.c
  2. 0 1
      apps/init.c
  3. 1 1
      apps/sh.c
  4. 1 24
      apps/terminal-vga.c
  5. 1 23
      apps/terminal.c
  6. 3 0
      base/usr/include/stdlib.h
  7. 1 1
      libc/stdlib/getenv.c
  8. 74 0
      libc/stdlib/putenv.c
  9. 16 0
      libc/stdlib/setenv.c

+ 1 - 25
apps/compositor.c

@@ -1871,31 +1871,7 @@ int main(int argc, char * argv[]) {
 		yg->server_ident = "compositor";
 	}
 	// XXX need setenv to get display variable
-	//setenv("DISPLAY", yg->server_ident, 1);
-	char * _env = malloc(sizeof(char) * 64);
-	sprintf(_env, "DISPLAY=%s", yg->server_ident);
-
-	/* count environ */
-	size_t env_c = 0;
-	for (char ** env = environ; *env; env++, env_c++);
-	char ** env_new = malloc(sizeof(char *) * (env_c + 2));
-	int set_env = 0;
-	for (size_t i = 0; i < env_c; ++i) {
-		if (strstr(environ[i], "DISPLAY=") == environ[i]) {
-			TRACE("Display already set, replacing.\n");
-			env_new[i] = _env;
-			set_env = 1;
-		} else {
-			env_new[i] = environ[i];
-		}
-	}
-	if (!set_env) {
-		env_new[env_c] = _env;
-		env_c++;
-	}
-	env_new[env_c] = NULL;
-	environ = env_new;
-	TRACE("environment has %d items", env_c);
+	setenv("DISPLAY", yg->server_ident, 1);
 
 	FILE * server = pex_bind(yg->server_ident);
 	TRACE("pex bound? %d", server);

+ 0 - 1
apps/init.c

@@ -25,7 +25,6 @@ int start_options(char * args[]) {
 			"HOME=/",
 			"PATH=/bin",
 			"USER=root",
-			"PRETEND_STDOUT_IS_TTY=1",
 			"WM_THEME=fancy",
 			NULL,
 		};

+ 1 - 1
apps/sh.c

@@ -898,7 +898,7 @@ uint32_t shell_cmd_history(int argc, char * argv[]) {
 
 uint32_t shell_cmd_export(int argc, char * argv[]) {
 	if (argc > 1) {
-		//putenv(argv[1]);
+		putenv(argv[1]);
 	}
 	return 0;
 }

+ 1 - 24
apps/terminal-vga.c

@@ -808,30 +808,7 @@ int main(int argc, char ** argv) {
 		}
 	}
 
-	//putenv("TERM=toaru");
-
-#if 1
-	char * _env = malloc(strlen("TERM=toaru"));
-	sprintf(_env, "TERM=toaru");
-	size_t env_c = 0;
-	for (char ** env = environ; *env; env++, env_c++);
-	char ** env_new = malloc(sizeof(char *) * (env_c + 2));
-	int set_env = 0;
-	for (size_t i = 0; i < env_c; ++i) {
-		if (strstr(environ[i], "TERM=") == environ[i]) {
-			env_new[i] = _env;
-			set_env = 1;
-		} else {
-			env_new[i] = environ[i];
-		}
-	}
-	if (!set_env) {
-		env_new[env_c] = _env;
-		env_c++;
-	}
-	env_new[env_c] = NULL;
-	environ = env_new;
-#endif
+	putenv("TERM=toaru");
 
 	syscall_openpty(&fd_master, &fd_slave, NULL, NULL, NULL);
 

+ 1 - 23
apps/terminal.c

@@ -1685,29 +1685,7 @@ int main(int argc, char ** argv) {
 	}
 
 	// XXX
-	//putenv("TERM=toaru");
-#if 1
-	char * _env = malloc(strlen("TERM=toaru"));
-	sprintf(_env, "TERM=toaru");
-	size_t env_c = 0;
-	for (char ** env = environ; *env; env++, env_c++);
-	char ** env_new = malloc(sizeof(char *) * (env_c + 2));
-	int set_env = 0;
-	for (size_t i = 0; i < env_c; ++i) {
-		if (strstr(environ[i], "TERM=") == environ[i]) {
-			env_new[i] = _env;
-			set_env = 1;
-		} else {
-			env_new[i] = environ[i];
-		}
-	}
-	if (!set_env) {
-		env_new[env_c] = _env;
-		env_c++;
-	}
-	env_new[env_c] = NULL;
-	environ = env_new;
-#endif
+	putenv("TERM=toaru");
 
 	/* Initialize the windowing library */
 	yctx = yutani_init();

+ 3 - 0
base/usr/include/stdlib.h

@@ -16,4 +16,7 @@ extern int system(const char * command);
 
 extern int abs(int j);
 
+extern int putenv(char * name);
+extern int setenv(const char *name, const char *value, int overwrite);
+
 #define NULL 0

+ 1 - 1
libc/stdlib/getenv.c

@@ -1,7 +1,7 @@
 #include <string.h>
+#include <stdlib.h>
 
 extern char ** environ;
-extern int _environ_size;
 
 char * getenv(const char *name) {
 	char ** e = environ;

+ 74 - 0
libc/stdlib/putenv.c

@@ -0,0 +1,74 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+extern char ** environ;
+extern int _environ_size;
+
+static int why_no_strnstr(char * a, char * b, int n) {
+	for (int i = 0; (i < n) && (a[i]) && (b[i]); ++i) {
+		if (a[i] != b[i]) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
+int putenv(char * string) {
+	if (_environ_size == 0) {
+		/* Find actual size */
+		int size = 0;
+
+		char ** tmp = environ;
+		while (*tmp) {
+			size++;
+			tmp++;
+		}
+
+		/* Multiply by two */
+		_environ_size = size * 2;
+
+		char ** new_environ = malloc(sizeof(char*) * _environ_size);
+		int i = 0;
+		while (environ[i]) {
+			new_environ[i] = environ[i];
+			i++;
+		}
+
+		while (i < _environ_size) {
+			new_environ[i] = NULL;
+			i++;
+		}
+
+		environ = new_environ;
+	}
+
+	char name[strlen(string)];
+	strcpy(name, string);
+	char * c = strchr(name, '=');
+	if (!c) {
+		return 1;
+	}
+	*c = NULL;
+
+	int s = strlen(name);
+
+	int i;
+	for (i = 0; environ[i]; ++i) {
+		if (!why_no_strnstr(name, environ[i], s)) {
+			environ[i] = string;
+			return 0;
+		}
+	}
+	/* Not found */
+	if (i >= _environ_size) {
+		environ = realloc(environ, _environ_size * 2);
+		for (int i = _environ_size; i < _environ_size * 2; ++i) {
+			environ[i] = NULL;
+		}
+		_environ_size *= 2;
+	}
+
+	environ[i] = string;
+	return 0;
+}

+ 16 - 0
libc/stdlib/setenv.c

@@ -0,0 +1,16 @@
+#include <stdlib.h>
+#include <string.h>
+
+int setenv(const char *name, const char *value, int overwrite) {
+	if (!overwrite) {
+		char * tmp = getenv(name);
+		if (tmp)
+			return 0;
+	}
+	char * tmp = malloc(strlen(name) + strlen(value) + 2);
+	*tmp = '\0';
+	strcat(tmp, name);
+	strcat(tmp, "=");
+	strcat(tmp, value);
+	return putenv(tmp);
+}