Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** EPITECH PROJECT, 2019
- ** PSU_minishell2_2018
- ** File description:
- ** cd
- */
- #include "../../include/my.h"
- #include <fcntl.h>
- void go_back_directory(env_t *env)
- {
- int i = my_strlen(find_node(env, "PWD")->content) - 1;
- for (; find_node(env, "PWD")->content[i] != '/' &&
- find_node(env, "PWD")->content[i] != '\0'; i--);
- find_node(env, "PWD")->content[i] = '\0';
- }
- void cd_opener(env_t *env, command_t *cmd)
- {
- int pass = 0;
- (my_strcmp(cmd->args[1], "..") == 0) ? go_back_directory(env) : 0;
- if (chdir(cmd->args[1]) == -1 && my_strcmp(cmd->args[1], "-") == 0) {
- my_putstr(cmd->args[1]);
- if (open(cmd->args[1], O_RDONLY) != -1) {
- my_putstr(": Not a directory.\n");
- pass = 1;
- } else {
- my_putstr(": No such file or directory.\n");
- pass = 1;
- }
- if (pass == 0) {
- my_strcat(find_node(env, "PWD")->content, cmd->args[1]);
- printf("%s\n", find_node(env, "PWD")->content);
- }
- }
- }
- void cd_chdir(env_t *env)
- {
- if (chdir(find_node(env, "HOME")->content) == -1) {
- my_putstr(find_node(env, "HOME")->content);
- if (open(find_node(env, "HOME")->content, O_RDONLY) != -1)
- my_putstr(": Not a directory.\n");
- else
- my_putstr(": No such file or directory.\n");
- }
- }
- void cd_cmd(env_t *env, command_t *cmd)
- {
- if (cmd->args[1] != NULL)
- cd_opener(env, cmd);
- else
- cd_chdir(env);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement