Add putenv, setenv

This commit is contained in:
K. Lange 2018-05-02 19:25:03 +09:00
parent b4c1d997b4
commit 2a4914057c
9 changed files with 98 additions and 75 deletions

View File

@ -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);

View File

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

View File

@ -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;
}

View File

@ -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);

View File

@ -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();

View File

@ -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

View File

@ -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
libc/stdlib/putenv.c Normal file
View File

@ -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
libc/stdlib/setenv.c Normal file
View File

@ -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);
}