Table of Contents
The repository: t-h2o/microshell
Microshell
-
You have to use the absolute path
-
You can only use
;
(command separtor) or|
(pipe)
./micro /bin/ls ";" /bin/ls -la
static void find_command(char ***arguments, t_command *cmd)
{
int index;
index = 0;
while ((*arguments)[index]
&& strcmp((*arguments)[index], ";")
&& strcmp((*arguments)[index], "|"))
++index;
if ((*arguments)[index] == 0)
{
*arguments = &((*arguments)[index]);
return ;
}
if (!strcmp((*arguments)[index], "|"))
cmd->isPipe = 1;
(*arguments)[index] = 0;
*arguments = &((*arguments)[index + 1]);
}
static void execute_command(t_command *cmd, char **envp)
{
int pid;
pid = fork();
if (pid == 0)
{
if (cmd->isPipe)
{
dup2(cmd->fd[1], 1);
close(cmd->fd[1]);
close(cmd->fd[0]);
}
if (execve(cmd->bin, cmd->args, envp) == -1)
{
print_error("microshell: Error execve\n");
exit(1);
}
}
else
{
if (cmd->isPipe)
{
dup2(cmd->fd[0], 0);
close(cmd->fd[0]);
close(cmd->fd[1]);
}
waitpid(pid, 0, 0);
}
}