85 lines
1.6 KiB
C
85 lines
1.6 KiB
C
#include <unistd.h> // write, fork, execve, pipe, chdir
|
|
#include <stdio.h> // printf
|
|
#include <string.h> // strcmp
|
|
#include <sys/types.h> // pid_t
|
|
#include <sys/wait.h> // waitpid
|
|
|
|
#define STDIN 0
|
|
#define STDOUT 1
|
|
#define STDERR 2
|
|
|
|
typedef enum {FALSE, TRUE} BOOL;
|
|
|
|
void builtin_cd(char *av)
|
|
{
|
|
printf("here\n");
|
|
if (av)
|
|
chdir(av);
|
|
}
|
|
|
|
void exec_cmd(char **av, char **en, int fdin, int fdout)
|
|
{
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) // child process
|
|
{
|
|
if (fdin != STDIN_FILENO)
|
|
{
|
|
dup2(fdin, STDIN_FILENO);
|
|
close(fdin);
|
|
}
|
|
if (fdout != STDOUT_FILENO)
|
|
{
|
|
dup2(fdout, STDOUT_FILENO);
|
|
close(fdout);
|
|
}
|
|
execve(av[0], av, en);
|
|
}
|
|
else // parent process
|
|
{
|
|
if (fdout != STDOUT_FILENO)
|
|
close(fdout);
|
|
waitpid(pid, NULL, 0);
|
|
}
|
|
}
|
|
|
|
int main(int ac, char **av, char **en)
|
|
{
|
|
int i;
|
|
int start;
|
|
int fds[2];
|
|
int old_fdin;
|
|
|
|
if (ac <= 1)
|
|
return (0);
|
|
i = 1;
|
|
old_fdin = STDIN_FILENO;
|
|
fds[STDIN] = STDIN_FILENO;
|
|
fds[STDOUT] = STDOUT_FILENO;
|
|
while (i < ac)
|
|
{
|
|
start = i;
|
|
while (i < ac && strcmp(av[i], "|") && strcmp(av[i], ";"))
|
|
i++;
|
|
if (av[i] && !strcmp(av[i], "|"))
|
|
pipe(fds);
|
|
av[i] = NULL;
|
|
if (strcmp(av[start], "cd") == 0)
|
|
builtin_cd(av[start + 1]);
|
|
else
|
|
exec_cmd(av + start, en, old_fdin, fds[STDOUT]);
|
|
if (old_fdin != STDIN_FILENO)
|
|
close(old_fdin);
|
|
old_fdin = fds[STDIN];
|
|
fds[STDOUT] = STDOUT_FILENO;
|
|
if (i < ac)
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
// pipes fork and dups : https://www.rozmichelle.com/pipes-forks-dups/
|
|
// subject and example : https://github.com/Glagan/42-exam-rank-04
|
|
// lsof -c microshell : https://www.thegeekstuff.com/2012/08/lsof-command-examples/
|
|
|