t_env *ft_init(char **env) { char *shlvl; t_env *e; if (!(e = (t_env *)malloc(sizeof(t_env)))) return (ERROR); if (!*env) { creat_env(&e->env); e->list = ft_create_list(e->env); } else { e->list = ft_create_list(env); shlvl = ft_itoa(ft_atoi(ft_check_var(e->list, "SHLVL")) + 1); ft_set(e->list, "SHLVL", shlvl); free(shlvl); e->env = conv_lst(e->list); } ft_init_line(&e); e->prompt = ft_strjoin(ft_check_var(e->list, "USER"), "$>"); e->shell = ft_strdup("MISHELL"); ft_putstr(e->prompt); return (e); }
// #include <stdio.h> void check_cmd(t_env *e) { char **str; int i; //printf("\t\tReceived command\n"); i = 0; //str = ft_split(line); // ft_memcpy(e->line, line, _POSIX2_LINE_MAX); // e->len = sizeof(line) - 1; // launch_interprete(e); str = e->argv; put_env(e); if (str[0] && ft_strcmp(str[0], "setenv") == 0) { while(str[i]) i++; if (i == 3) ft_set(e->list, str[1], str[2]); else if(i == 2) ft_set(e->list, str[1], "\0"); else if(i > 3) ft_putstr_fd("setenv: Too many arguments.\n", 2); else print_env(e->env); } else if (str[0] && ft_strcmp(str[0], "unsetenv") == 0) { if (str[1]) ft_unset(e->list, str); else ft_putstr_fd("unsetenv: Too few arguments.\n", 2); } else check_cmd2(str, e); // if (str) // ft_free_tab(&str); }
int ft_setenv(t_datas *datas, char **cmd) { if (ft_strcmp(cmd[0], "setenv") != 0) return (-1); if (ft_setenv_usage(cmd) != 0) return (1); if (ft_setvar(&(datas->env), cmd[1], cmd[2]) != 0) return (ft_error(FTSH_NAME, "setenv", "memory allocation failed.")); if (ft_strcmp(cmd[1], "HOME") == 0 || (ft_strcmp(cmd[1], "CWD") != 0 && ft_strcmp(cmd[1], "OWD") != 0 && ft_strcmp(cmd[1], "$") != 0 && ft_strcmp(cmd[1], "?") != 0 && ft_getenv(datas->local, cmd[1]) != NULL && ft_strlen(cmd[1]) > 0)) ft_set(datas, cmd); return (0); }
void update_env(char **args, t_list **aim, int *i) { int ret; *i = 0; while (args[*i] && ((!ft_strcmp("-u", args[*i])) || !ft_strcmp("--unset", args[*i]) || ft_get_char(args[*i], '=') != -1 || !ft_strcmp("-i", args[*i]))) { if (args[*i + 1] && (!ft_strcmp("-u", args[*i]) || !ft_strcmp("--unset", args[*i]))) ft_unset(args[++(*i)], *aim); if ((ret = ft_get_char(args[*i], '=')) != -1) { args[*i][ret] = '\0'; if (!(*aim)) new_in_list(ft_ptrf("%s=%s", args[*i], args[*i] + ret + 1), aim, &env_node); else ft_set(args[*i], args[*i] + ret + 1, 1, *aim); } (*i)++; } }
/* * ft_attachstds() * Attach the standard in, out, err to the file table, this can't be located the * ft_create function because that the device "con:" etc are not attached to the * list of device when the os is the boot sequence. */ int ft_attachstds(struct filetable *ft) { char *console = NULL; int mode; int result = 0; //STDIN struct vnode *vn_stdin; mode = O_RDONLY; struct filedescriptor *fd_stdin = NULL; fd_stdin = (struct filedescriptor *) kmalloc(sizeof ( struct filedescriptor)); if (fd_stdin == NULL) { ft_destroy(ft); return 0; } console = kstrdup("con:"); result = vfs_open(console, mode, &vn_stdin); if (result) { vfs_close(vn_stdin); ft_destroy(ft); return 0; } kfree(console); fd_stdin->mode = mode; fd_stdin->offset = 0; fd_stdin->fdvnode = vn_stdin; fd_stdin->numOwners = 1; ft_set(ft, fd_stdin, STDIN_FILENO); //STDOUT struct vnode *vn_stdout; mode = O_WRONLY; struct filedescriptor *fd_stdout = NULL; fd_stdout = (struct filedescriptor *) kmalloc(sizeof (struct filedescriptor)); if (fd_stdout == NULL) { ft_destroy(ft); return 0; } console = kstrdup("con:"); result = vfs_open(console, mode, &vn_stdout); if (result) { vfs_close(vn_stdout); ft_destroy(ft); return 0; } kfree(console); fd_stdout->mode = mode; fd_stdout->offset = 0; fd_stdout->fdvnode = vn_stdout; fd_stdout->numOwners = 1; ft_set(ft, fd_stdout, STDOUT_FILENO); //STDERR struct vnode *vn_stderr; mode = O_WRONLY; struct filedescriptor *fd_stderr = NULL; fd_stderr = (struct filedescriptor *) kmalloc(sizeof (struct filedescriptor)); if (fd_stderr == NULL) { ft_destroy(ft); return 0; } console = kstrdup("con:"); result = vfs_open(console, mode, &vn_stderr); if (result) { vfs_close(vn_stderr); ft_destroy(ft); return 0; } kfree(console); fd_stderr->mode = mode; fd_stderr->offset = 0; fd_stderr->fdvnode = vn_stderr; fd_stderr->numOwners = 1; ft_set(ft, fd_stderr, STDERR_FILENO); return 1; }