environ #15

Merged
cromer merged 27 commits from environ into master 2021-07-17 22:15:24 -04:00
2 changed files with 60 additions and 8 deletions
Showing only changes of commit a80bedf135 - Show all commits

View File

@ -78,16 +78,49 @@ void change_directory(StringArray *args) {
return; return;
} }
else if (args->size == 1) { else if (args->size == 1) {
char *cwd = get_working_directory(); print_current_directory();
fprintf(stdout, "%s\n", cwd); }
if (cwd != NULL) { else {
free(cwd); char *value = NULL;
cwd = NULL; if (args->array[1][0] == '$') {
char *variable = remove_variable_symbol(args->array[1]);
char *array_value = get_array_list(variables, variable);
if (array_value == NULL) {
if (variable != NULL) {
free(variable);
variable = NULL;
}
print_current_directory();
return;
}
value = malloc((strlen(array_value) + 1) * sizeof(char *));
if (value == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(value, 0, strlen(array_value) + 1);
strcpy(value, array_value);
if (variable != NULL) {
free(variable);
variable = NULL;
} }
} }
else { else {
if (chdir(args->array[1]) != 0) { value = malloc((strlen(args->array[1]) + 1) * sizeof(char *));
fprintf(stderr, "cd: %s \"%s\"\n", strerror(errno), args->array[1]); if (value == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(value, 0, strlen(args->array[1]) + 1);
strcpy(value, args->array[1]);
}
if (chdir(value) != 0) {
fprintf(stderr, "cd: %s \"%s\"\n", strerror(errno), value);
} }
else { else {
char *cwd = get_working_directory(); char *cwd = get_working_directory();
@ -97,6 +130,20 @@ void change_directory(StringArray *args) {
cwd = NULL; cwd = NULL;
} }
} }
if (value != NULL) {
free(value);
value = NULL;
}
}
}
void print_current_directory() {
char *cwd = get_working_directory();
fprintf(stdout, "%s\n", cwd);
if (cwd != NULL) {
free(cwd);
cwd = NULL;
} }
} }

View File

@ -44,6 +44,11 @@ void exit_shell(StringArray *args);
*/ */
void change_directory(StringArray *args); void change_directory(StringArray *args);
/**
* Print the current working directory.
*/
void print_current_directory();
/** /**
* Print all of the environment variables. * Print all of the environment variables.
* @param args The arguments the user input. * @param args The arguments the user input.