add environ builtin

This commit is contained in:
Chris Cromer 2021-07-17 14:42:27 -04:00
parent cf4161d545
commit f580c03ebc
2 changed files with 23 additions and 0 deletions

View File

@ -30,6 +30,10 @@ bool is_builtin(char *command) {
return true;
}
if (strcmp(command, "environ") == 0) {
return true;
}
return false;
}
@ -40,6 +44,9 @@ void run_builtin(StringArray *args) {
else if (strcmp(args->array[0], "cd") == 0) {
change_directory(args);
}
else if (strcmp(args->array[0], "environ") == 0) {
print_environ(args);
}
else {
fprintf(stderr, "Builtin %s does not exist!\n", args->array[0]);
}
@ -72,3 +79,13 @@ void change_directory(StringArray *args) {
}
}
}
void print_environ(StringArray *args) {
if (args->size > 1) {
fprintf(stderr, "Too many arguments!\n");
return;
}
for (size_t i = 0; i < variables->size; i++) {
fprintf(stdout, "%s=%s\n", variables->keys->array[i], variables->values->array[i]);
}
}

View File

@ -44,4 +44,10 @@ void exit_shell(StringArray *args);
*/
void change_directory(StringArray *args);
/**
* Print all of the environment variables.
* @param args The arguments the user input.
*/
void print_environ(StringArray * args);
#endif