1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
** cmd.c for minishell in /home/boivin_r/rendu/PSU_2014_minishell1
**
** Made by Rémi boivin
** Login <boivin_r@epitech.net>
**
** Started on Tue Jan 20 13:42:36 2015 Rémi boivin
** Last update Sat Jan 31 17:28:33 2015 Rémi boivin
*/
#include "my.h"
int check_command(char **tab1, t_env *env2)
{
int erno;
erno = 0;
my_strcpy(env2->commande, tab1[0]);
del_str(env2->commande, "PATH");
del_char(env2->commande, '='); // supprime le charatère '='
strcat(env2->commande, "/");
strcat(env2->commande, env2->buffer);
env2->commande[my_strlen(env2->commande) - 1] = '\0';
erno = execute(env2); // exécute la commende contenu dans env2->commande
return (erno);
int check_command1(char **tab1, t_env *env2)
{
int j;
int erno;
erno = 65280;
j = 0;
while (erno != 0)
{
if (j >= 5)
{
my_putstr_error("Commande introuvable.\n");
return (-1);
}
else
{
my_strcpy(env2->commande, tab1[j]);
del_char(env2->commande, '=');
del_str(env2->commande, "PATH");
strcat(env2->commande, "/");
strcat(env2->commande, env2->buffer);
env2->commande[my_strlen(env2->commande) - 1] = '\0';
erno = execute(env2);
}
j++;
}
return (0);
}
char *launch_current(char *current, char **env)
{
char *pwd;
char *tmp;
char *tmp1;
int i;
i = 0;
if (current == NULL)
my_putstr_error("erreur current is NULL.\n");
else
{
tmp = malloc(my_strlen(current) * sizeof(*tmp));
tmp1 = malloc(my_strlen(current) * sizeof(*tmp1));
pwd = malloc(500 * sizeof(char));
if (tmp == NULL || tmp1 == NULL || pwd == NULL)
my_putstr_error("Error malloc tmp, tmp1 or pwd is NULL.\n");
else
{
pwd = *my_getenv("PWD", env);// stocke la valeur env de PWD
my_strcpy(tmp, pwd);
while (i < 4)
i++;
i = 0;
while (tmp[i] != '\0')
{
if (tmp[i] == 'P' && tmp[i + 1] =='W' && tmp[i + 2] == 'D')
{
tmp[i] = '\n';
tmp[i + 1] = '\n';
tmp[i + 2] = '\n';
tmp[i + 3] = '\n';
}
i++;
}
del_char(tmp, '\n');
strcat(tmp, "/");
strcat(tmp, current);
}
}
return (tmp);
}
}
int loop(t_env *env2)
{
char **tab1;
int i;
int erno;
erno = 0;
i = 0;
tab1 = init(env2);
if (env2->buffer == NULL)
my_putstr_error("error malloc\n");
while (tab1[i] != NULL)
{
env2->commande = malloc((my_strlen(tab1[i] + 1) + my_strlen(env2->buffer) + 1) * sizeof(char));
i++;
}
if (env2->commande == NULL)
my_putstr_error("errreur amlloc");
erno = check_command(tab1, env2); // on esssaye d'executer la commande et si erno == 65280
if (erno == 65280)
check_command1(tab1, env2); // on execute jusqu'a ce que la commande s'exécute ou que i est == 7
return (0);
}
int parse1(t_env *env2, t_list *env, char *error)
{
if (my_strcmp(env2->buffer, "exit\n") == 0)
return (0);
else
{
if (env2->buffer[0] == '.' && env2->buffer[1] =='/')
{
del_str(env2->buffer, "./");
if (env2->buffer == NULL)
my_putstr_error("[CMD] Error: env2->buffer is NULL.\n");
else
execute1(env2, env, error);
}
else if (my_strncmp(env2->buffer, "cd", 1) == 0)
my_cd(env2);
else if (my_strncmp(env2->buffer, "setenv\n", 5) == 0)
setenv(env, env2);
else
loop(env2);
}
return (2);
}
int prompt(t_env *env2, t_list *env)
{
char *error;
if (env2->buffer == NULL)
my_putstr_error("[CMD] Errreur malloc: env2->buffer is NULL.\n");
else
{
my_putstr("$-->");
memset(&env2->buffer[0], 0, 4096);
if (read(0, env2->buffer, 4095) == 0)
{
my_putchar('\n');
return (0);
}
else
{
error = malloc(my_strlen(env2->buffer) * sizeof(char));
if (error == NULL)
my_putstr_error("[CMD] Error malloc: char* error.\n");
else
parse1(env2, env, error);
}
}
return (42);
} |
Partager