mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Add export builtin to sh(1).
This commit is contained in:
parent
0c7d1fe2c1
commit
0c5eff4475
1 changed files with 56 additions and 0 deletions
56
sh/sh.cpp
56
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;
|
||||
|
|
Loading…
Reference in a new issue