Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back "ExamRank04 Fixes and Improvements" #153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 67 additions & 59 deletions .subjects/STUD_PART/exam_04/0/microshell/microshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct s_base
int size;
int type;
int fd[2];
pid_t pid;
struct s_base *prev;
struct s_base *next;
} t_base;
Expand Down Expand Up @@ -158,39 +159,36 @@ int parser_argv(t_base **ptr, char **av)

void exec_cmd(t_base *temp, char **env)
{
pid_t pid;
int status;
int pipe_open;

pipe_open = 0;
if (temp->type == TYPE_PIPE || (temp->prev && temp->prev->type == TYPE_PIPE))
{
pipe_open = 1;
if (temp->type == TYPE_PIPE)
if (pipe(temp->fd))
exit_fatal();
}
pid = fork();
if (pid < 0)
temp->pid = fork();
if (temp->pid < 0)
exit_fatal();
else if (pid == 0) //child
else if (temp->pid == 0) //child
{
if (temp->type == TYPE_PIPE && dup2(temp->fd[STDOUT], STDOUT) < 0)
exit_fatal();
if (temp->prev && temp->prev->type == TYPE_PIPE && dup2(temp->prev->fd[STDIN], STDIN) < 0)
exit_fatal();
if (temp->type == TYPE_PIPE)
{
if (dup2(temp->fd[STDOUT], STDOUT) < 0)
exit_fatal();
if (close(temp->fd[STDIN]) || close(temp->fd[STDOUT]))
exit_fatal();
}
if (temp->prev && temp->prev->type == TYPE_PIPE)
{
if (dup2(temp->prev->fd[STDIN], STDIN) < 0)
exit_fatal();
if (close(temp->prev->fd[STDIN]))
exit_fatal();
}
if ((execve(temp->argv[0], temp->argv, env)) < 0)
exit_execve(temp->argv[0]);
exit(EXIT_SUCCESS);
}
else //parent
{
waitpid(pid, &status, 0);
if (pipe_open)
{
if (temp->type == TYPE_PIPE)
close(temp->fd[STDOUT]);
if (!temp->next || temp->type == TYPE_BREAK)
close(temp->fd[STDIN]);
}
if (temp->prev && temp->prev->type == TYPE_PIPE)
close(temp->prev->fd[STDIN]);
}
Expand All @@ -205,7 +203,7 @@ void exec_cmds(t_base *ptr, char **env)
{
if (strcmp("cd", temp->argv[0]) == 0)
{
if (temp->size < 2)
if (temp->size != 2)
exit_cd_1();
else if (chdir(temp->argv[1]))
exit_cd_2(temp->argv[1]);
Expand All @@ -216,67 +214,77 @@ void exec_cmds(t_base *ptr, char **env)
}
}

void wait_childs(t_base *temp)
{
while (temp)
{
waitpid(temp->pid, NULL, 0);
temp = temp->next;
}
}

/*
**====================================
**============Part main===============
**====================================
*/

void free_all(t_base *ptr)
void free_all(t_base **ptr)
{
t_base *temp;
t_base *curr;
t_base *next;
int i;

while (ptr)
curr = *ptr;
while (curr)
{
temp = ptr->next;
next = curr->next;
i = 0;
while (i < ptr->size)
free(ptr->argv[i++]);
free(ptr->argv);
free(ptr);
ptr = temp;
while (i < curr->size)
free(curr->argv[i++]);
free(curr->argv);
free(curr);
curr = next;
}
ptr = NULL;
*ptr = NULL;
}

int main(int ac, char **av, char **env)
{
t_base *ptr = NULL;
int i;

if (ac == 1)
return (0);
i = 1;
if (ac > 1)
while (av[i]) //pipeline loop
{
while (av[i])
if (strcmp(av[i], ";") == 0)
{
i++;
continue ;
}
while (av[i] && strcmp(av[i], ";") != 0) //cmd loop
{
if (strcmp(av[i], ";") == 0)
{
i++;
continue ;
}
i += parser_argv(&ptr, &av[i]);
if (!av[i])
break;
else
if (av[i] && !strcmp(av[i], "|"))
i++;
}
/*while (ptr)
{

printf("=================\n");
for (i = 0; i < ptr->size; i++)
printf("%s\n", ptr->argv[i]);
printf("TYPE: %d\n", ptr->type);
printf("SIZE: %d\n", ptr->size);
printf("=================\n");
ptr = ptr->next;
}
(void)**env;
printf("END\n");*/
if (ptr)
exec_cmds(ptr, env);
free_all(ptr);
/*while (ptr)
{
printf("=================\n");
for (i = 0; i < ptr->size; i++)
printf("%s\n", ptr->argv[i]);
printf("TYPE: %d\n", ptr->type);
printf("SIZE: %d\n", ptr->size);
printf("=================\n");
ptr = ptr->next;
}
(void)**env;
printf("END\n");*/
exec_cmds(ptr, env);
wait_childs(ptr);
free_all(&ptr);
}
return (0);
}
22 changes: 21 additions & 1 deletion .subjects/STUD_PART/exam_04/0/microshell/test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ulimit -n 30
./a.out /bin/ls microshell.c
./a.out /bin/ls salut
./a.out ";"
./a.out ";abc"
./a.out "|xyz"
./a.out ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";"
./a.out ";" ";" /bin/echo OK
./a.out ";" ";" /bin/echo OK ";"
Expand All @@ -27,4 +29,22 @@ ulimit -n 30
./a.out blah "|" /bin/echo OK ";"
./a.out ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" blah "|" /bin/echo OK ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";"
./a.out cd ";" /bin/ls
./a.out /bin/ls "|" /usr/bin/grep microshell "|" /usr/bin/grep micro "|" /usr/bin/grep shell "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|"
./a.out cd a b c ";" /bin/ls
./a.out cd ./non_existing_directory ";" /bin/ls
./a.out cd_but_fake ";" /bin/ls
./a.out /bin/pwd ";" cd ";" /bin/pwd ";" cd /root extra_arg2 extra_arg3 ";"
./a.out ";" cd ./microshell ";" /bin/pwd ";" /bin/ls "|" /bin/grep microshell.c ";" cd .. ";" ";" /bin/pwd ";" ";"
./a.out /bin/ls "|" /usr/bin/grep microshell "|" /usr/bin/grep micro "|" /usr/bin/grep shell "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro "|" /usr/bin/grep micro

KILLABLE_PNAME=/tmp/examrank04_killable.out
pkill --signal SIGKILL -f $KILLABLE_PNAME
cp a.out $KILLABLE_PNAME

# no waitpid at all
(./a.out /bin/sleep 2 ";" /bin/echo "#test_2 This line should be printed 2nd" &)
sleep 1
./a.out /bin/echo "#test_2 This line should be printed 1st"
sleep 1
pkill --signal SIGKILL -f $KILLABLE_PNAME

rm -f $KILLABLE_PNAME