Skip to content

Commit

Permalink
add MKSH_ENVDIR code for Jehanne (OS by Shamar (giacomotesio) from IRC):
Browse files Browse the repository at this point in the history
instead of parsing extern char **environ; read it from a filesystem
(typical for Plan 9, though this one is __jehanne__ per ifdef)

tested on MirBSD and found to be working
  • Loading branch information
mirabilos committed Oct 14, 2017
1 parent 63cf261 commit d76a7e5
Showing 1 changed file with 84 additions and 11 deletions.
95 changes: 84 additions & 11 deletions main.c
Expand Up @@ -34,9 +34,7 @@
#include <locale.h>
#endif

__RCSID("$MirOS: src/bin/mksh/main.c,v 1.344 2017/10/13 23:02:11 tg Exp $");

extern char **environ;
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.345 2017/10/14 21:05:22 tg Exp $");

#ifndef MKSHRC_PATH
#define MKSHRC_PATH "~/.mkshrc"
Expand All @@ -52,6 +50,7 @@ void chvt_reinit(void);
static void reclaim(void);
static void remove_temps(struct temp *);
static mksh_uari_t rndsetup(void);
static void init_environ(void);
#ifdef SIGWINCH
static void x_sigwinch(int);
#endif
Expand Down Expand Up @@ -397,14 +396,7 @@ main_init(int argc, const char *argv[], Source **sp, struct block **lp)
#endif

/* import environment */
if (environ != NULL) {
wp = (const char **)environ;
while (*wp != NULL) {
rndpush(*wp);
typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
++wp;
}
}
init_environ();

/* for security */
typeset(TinitIFS, 0, 0, 0, 0);
Expand Down Expand Up @@ -1976,3 +1968,84 @@ x_mkraw(int fd, mksh_ttyst *ocb, bool forread)

mksh_tcset(fd, &cb);
}

#ifdef MKSH_ENVDIR
static void
init_environ(void)
{
char *xp;
ssize_t n;
XString xs;
struct shf *shf;
DIR *dirp;
struct dirent *dent;

if ((dirp = opendir(MKSH_ENVDIR)) == NULL) {
warningf(false, "cannot read environment from %s: %s",
MKSH_ENVDIR, cstrerror(errno));
return;
}
XinitN(xs, 256, ATEMP);
read_envfile:
errno = 0;
if ((dent = readdir(dirp)) != NULL) {
if (skip_varname(dent->d_name, true)[0] == '\0') {
xp = shf_smprintf(Tf_sSs, MKSH_ENVDIR, dent->d_name);
if (!(shf = shf_open(xp, O_RDONLY, 0, 0))) {
warningf(false,
"cannot read environment %s from %s: %s",
dent->d_name, MKSH_ENVDIR,
cstrerror(errno));
goto read_envfile;
}
afree(xp, ATEMP);
n = strlen(dent->d_name);
xp = Xstring(xs, xp);
XcheckN(xs, xp, n + 32);
memcpy(xp, dent->d_name, n);
xp += n;
*xp++ = '=';
while ((n = shf_read(xp, Xnleft(xs, xp), shf)) > 0) {
xp += n;
if (Xnleft(xs, xp) <= 0)
XcheckN(xs, xp, Xlength(xs, xp));
}
if (n < 0) {
warningf(false,
"cannot read environment %s from %s: %s",
dent->d_name, MKSH_ENVDIR,
cstrerror(shf_errno(shf)));
} else {
*xp = '\0';
xp = Xstring(xs, xp);
rndpush(xp);
typeset(xp, IMPORT | EXPORT, 0, 0, 0);
}
shf_close(shf);
}
goto read_envfile;
} else if (errno)
warningf(false, "cannot read environment from %s: %s",
MKSH_ENVDIR, cstrerror(errno));
closedir(dirp);
Xfree(xs, xp);
}
#else
extern char **environ;

static void
init_environ(void)
{
const char **wp;

if (environ == NULL)
return;

wp = (const char **)environ;
while (*wp != NULL) {
rndpush(*wp);
typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
++wp;
}
}
#endif

0 comments on commit d76a7e5

Please sign in to comment.