Forum

SetEnv

Vladimir Ionescu
18 December 2006, 10:29
Hi!

I have run into some problems installing Hiawatha on a HP-UX computer. The problem is that the functions setenv and unsetenv are not defined on some UNIX systems (older HP-UX and Solaris for example). I solved the problem by creating my own setenv and unsetenv, using the existing putenv and getenv. Here is the code, maybe someone else needs it, or maybe it will be included in the next version:

int alt_setenv(const char *name, const char *value, int overwrite) {
char *buf;
int lenn, lenv;
if(getenv(name) == NULL || overwrite) {
lenn = strlen(name);
lenv = strlen(value);
buf = (char *) malloc((lenn + lenv + 1) * sizeof(char));
if(buf == NULL) {
return -1;
}
buf[0] = \'\\0\';
strncat(buf, name, lenn);
buf[lenn] = \'=\';
buf[lenn + 1] = \'\\0\';
strncat(buf, value, lenv);
return putenv(buf);
}
return 0;
}

int alt_setenv(const char *name, const char *value, int overwrite) {
char *buf;
int lenn, lenv;
if(getenv(name) == NULL || overwrite) {
lenn = strlen(name);
lenv = strlen(value);
buf = (char *) malloc((lenn + lenv + 1) * sizeof(char));
if(buf == NULL) {
return -1;
}
buf[0] = \'\\0\';
strncat(buf, name, lenn);
buf[lenn] = \'=\';
buf[lenn + 1] = \'\\0\';
strncat(buf, value, lenv);
return putenv(buf);
}
return 0;
}
Hugo Leisink
18 December 2006, 10:33
Thanks for your feedback. I will try to fix the problem in the next release (5.4).

Can you please send me the alt_unsetenv() function, cause you've posted alt_setenv() twice.
This topic has been closed.