cd builtin ok
This commit is contained in:
Binary file not shown.
BIN
microshell
BIN
microshell
Binary file not shown.
61
microshell.c
61
microshell.c
@@ -1,4 +1,4 @@
|
|||||||
#include <unistd.h> // write, fork, execve, pipe
|
#include <unistd.h> // write, fork, execve, pipe, chdir
|
||||||
#include <stdio.h> // printf
|
#include <stdio.h> // printf
|
||||||
#include <string.h> // strcmp
|
#include <string.h> // strcmp
|
||||||
#include <sys/types.h> // pid_t
|
#include <sys/types.h> // pid_t
|
||||||
@@ -8,36 +8,37 @@
|
|||||||
#define STDOUT 1
|
#define STDOUT 1
|
||||||
#define STDERR 2
|
#define STDERR 2
|
||||||
|
|
||||||
void print_cmd(char **av)
|
typedef enum {FALSE, TRUE} BOOL;
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
i = 0;
|
void builtin_cd(char *av)
|
||||||
while (av[i])
|
{
|
||||||
{
|
printf("here\n");
|
||||||
size = 0;
|
if (av)
|
||||||
while (av[i][size] != '\0')
|
chdir(av);
|
||||||
size++;
|
|
||||||
write(1, av[i], size);
|
|
||||||
write(1, " ", 1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
write(1, "\n", 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//void exec_cmd(char **av, int end, char **en)
|
void exec_cmd(char **av, char **en, int fdin, int fdout)
|
||||||
void exec_cmd(char **av, char **en)
|
|
||||||
{
|
{
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
// av[end] = NULL;
|
|
||||||
if (pid == 0) // child process
|
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);
|
execve(av[0], av, en);
|
||||||
}
|
}
|
||||||
else // parent process
|
else // parent process
|
||||||
{
|
{
|
||||||
|
if (fdout != STDOUT_FILENO)
|
||||||
|
close(fdout);
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,22 +47,38 @@ int main(int ac, char **av, char **en)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int start;
|
int start;
|
||||||
|
int fds[2];
|
||||||
|
int old_fdin;
|
||||||
|
|
||||||
if (ac <= 1)
|
if (ac <= 1)
|
||||||
return (0);
|
return (0);
|
||||||
i = 1;
|
i = 1;
|
||||||
|
old_fdin = STDIN_FILENO;
|
||||||
|
fds[STDIN] = STDIN_FILENO;
|
||||||
|
fds[STDOUT] = STDOUT_FILENO;
|
||||||
while (i < ac)
|
while (i < ac)
|
||||||
{
|
{
|
||||||
start = i;
|
start = i;
|
||||||
while (i < ac && strcmp(av[i], "|") && strcmp(av[i], ";"))
|
while (i < ac && strcmp(av[i], "|") && strcmp(av[i], ";"))
|
||||||
i++;
|
i++;
|
||||||
|
if (av[i] && !strcmp(av[i], "|"))
|
||||||
|
pipe(fds);
|
||||||
av[i] = NULL;
|
av[i] = NULL;
|
||||||
// print_cmd(&av[start]);
|
if (strcmp(av[start], "cd") == 0)
|
||||||
exec_cmd(&av[start], en);
|
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)
|
if (i < ac)
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.rozmichelle.com/pipes-forks-dups/
|
// 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/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user