From 0c5eff44750f0e7fef99e51d2b931d7be83b6d39 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 28 Dec 2015 20:55:57 +0100 Subject: [PATCH] Add export builtin to sh(1). --- sh/sh.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/sh/sh.cpp b/sh/sh.cpp index 3add0bf9..8a604ec9 100644 --- a/sh/sh.cpp +++ b/sh/sh.cpp @@ -1133,6 +1133,62 @@ struct execute_result execute(const char* const* tokens, do_exit = true; internal_status = exitcode; } + else if ( strcmp(argv[0], "export") == 0 ) + { + internal = true; + internal_status = 0; + if ( argv[1] ) + { + size_t eqpos = strcspn(argv[1], "="); + if ( argv[1][eqpos] == '=' ) + { + char* name = strndup(argv[1], eqpos); + if ( name ) + { + if ( setenv(name, argv[1] + eqpos + 1, 1) < 0 ) + { + error(0, errno, "export: setenv"); + internal_status = 1; + } + } + else + { + error(0, errno, "export: malloc"); + internal_status = 1; + } + } + } + else + { + for ( size_t i = 0; environ[i]; i++ ) + { + const char* envvar = environ[i]; + printf("export "); + while ( *envvar && *envvar != '=' ) + putchar((unsigned char) *envvar++); + if ( *envvar == '=' ) + putchar((unsigned char) *envvar++); + putchar('\''); + while ( *envvar ) + { + if ( *envvar == '\'' ) + { + putchar('\''); + putchar('\\'); + putchar((unsigned char) *envvar++); + putchar('\''); + } + else + { + putchar((unsigned char) *envvar++); + } + } + putchar('\''); + putchar('\n'); + } + } + + } else if ( strcmp(argv[0], "unset") == 0 ) { internal = true;