Skip to content
Snippets Groups Projects
Commit 28ab9b34 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Add umask command

parent 09d279d8
Branches
No related merge requests found
......@@ -54,7 +54,8 @@ void execute_command(); // Executes provided command.
void recursive_execute(); // Continues executing command.
void change_directory(); // Change current directory.
char* get_directory_path(); // Gets full pathname of current directory.
void redirect_fd(); // Redirects passed file descriptors.
void get_umask(); // Gets current umask value.
void set_umask(); // Sets umask value.
void free_memory(); // Frees memory of vars.
void make_arg_example();
......@@ -110,10 +111,31 @@ int main(int argc, char* argv[]) {
}
}
// Check for pwd command.
} else if (strcmp(**command_argv->argv, "pwd") == 0) {
current_directory = get_directory_path();
printf("%s\n", current_directory);
free(current_directory);
if (line_argv->argc > 1) {
err_msg("Too many arguments.");
} else if (command_argv->argc > 1) {
err_msg("Too many arguments.");
} else {
current_directory = get_directory_path();
printf("%s\n", current_directory);
free(current_directory);
}
// Check for umask command.
} else if (strcmp(**command_argv->argv, "umask") == 0) {
if (line_argv->argc > 1) {
err_msg("Too many arguments.");
} else {
if (command_argv->argc > 2) {
err_msg("Too many arguments.");
} else if (command_argv->argc < 2) {
get_umask();
} else {
set_umask(*(*command_argv->argv + 1));
}
}
// Attempt to exec command.
} else {
......@@ -315,6 +337,58 @@ char* get_directory_path() {
}
/**
* Gets current umask value.
*/
void get_umask() {
mode_t old_val = umask(0);
umask(old_val);
printf("Umask Value: %d\n", old_val);
}
/**
* Sets current umask value.
*/
void set_umask(char* string_val) {
int valid_input = 1;
// Validate input.
if (strlen(string_val) > 0) {
if ((string_val[0] - '0') > 7) {
valid_input = 0;
}
}
if (strlen(string_val) > 1) {
if ((string_val[1] - '0') > 7) {
valid_input = 0;
}
}
if (strlen(string_val) > 2) {
if ((string_val[2] - '0') > 7) {
valid_input = 0;
}
}
if (strlen(string_val) > 3) {
if ((string_val[3] - '0') > 7) {
valid_input = 0;
}
}
if (strlen(string_val) > 4) {
valid_input = 0;
}
if (valid_input == 1) {
// Passed validation. Changing mask value.
int new_val = atoi(string_val);
mode_t old_val = umask(new_val);
printf("Umask Value: %d Prev Value: %d\n", new_val, old_val);
} else {
printf("Invalid umask value.\n");
}
}
/**
* Frees memory of main program variables.
*/
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment