Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.DS_Store*
._.DS_Store*
*.swp
*.swo

# Prerequisites
*.d
Expand Down
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
NAME = minishell
SRCFILES = ast.c \
SRCFILES = 42sh_header.c \
ast.c \
ast_free.c \
ast_get.c \
ast_helper.c \
ast_main.c \
ast_parse.c \
ast_print.c \
ast_validate.c \
autocomplete.c \
builtin_cd.c \
builtins.c \
builtin_bg.c \
builtin_jobs.c \
builtins_env.c \
char_array.c \
child_argv.c \
Expand Down Expand Up @@ -37,14 +39,13 @@ SRC = $(addprefix src/,$(SRCFILES))
OBJ = $(addprefix obj/,$(OBJFILES))
CC = gcc
FLAGS = -g -Wextra -Wall -Werror
INC = -I libft/ -I libft/ft_printf/ -I inc/
INC = -I libft/inc -I inc/
LIB = -L libft/ -lft -ltermcap

all:$(NAME)

$(NAME): $(OBJ)
make -C libft/
# make -C libft/ft_printf/
$(CC) $(FLAGS) -o $@ $^ $(LIB)

objdir:
Expand All @@ -58,15 +59,18 @@ rmobj:

rmbin:
rm -rf $(NAME)
rm -rf ASTDEMO

again: rmobj rmbin all

ast:
make -C libft/
gcc -o ASTDEMO src/ast*.c -L libft/ -l ft -I libft/inc

clean: rmobj
make clean -C libft/
# make clean -C libft/ft_printf/

fclean: clean rmbin
make fclean -C libft/
# make fclean -C libft/ft_printf/

re: fclean all
15 changes: 13 additions & 2 deletions inc/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: asarandi <asarandi@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/30 19:50:54 by asarandi #+# #+# */
/* Updated: 2018/04/11 18:15:27 by asarandi ### ########.fr */
/* Updated: 2018/04/16 00:32:30 by ztisnes ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,9 +36,16 @@ typedef struct s_av
char **argv;
} t_av;

typedef struct s_io
{
int stdin;
int stdout;
int stderr;
} t_io;

typedef struct s_process
{
char *fullpath;
char **argv;
char **envp;
pid_t pid;
Expand All @@ -47,6 +54,11 @@ typedef struct s_process
int fd[2];
} t_process;

typedef struct s_group
{
t_process **p;
} t_group;

typedef struct s_exec
{
char *cmd;
Expand Down Expand Up @@ -84,7 +96,6 @@ typedef struct s_shell
int state;
t_exec *exec;
t_exec *last_exec;

} t_shell;

t_shell *g_sh;
Expand Down
72 changes: 72 additions & 0 deletions job_control/builtin_bg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_bg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/16 01:27:04 by ztisnes #+# #+# */
/* Updated: 2018/04/16 03:00:51 by ztisnes ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell"
#include <stdio.h>

// int getnum(char *str)
// {
// int num;
// int sign;
//
// num = 0;
// if (str == NULL)
// return (num);
// str = getsign()
// }

int ft_atoi(char *str)
{
int i;
int flag;
long number;

i = 0;
flag = 1;
number = 0;
while ((str[i] == ' ') || (str[i] == '\t') || (str[i] == '\n') \
|| (str[i] == '\v') || (str[i] == '\r') || (str[i] == '\f'))
i++;
if (str[i] == '-')
flag = -1;
if ((str[i] == '-') || (str[i] == '+'))
i++;
while ((str[i] >= '0') && (str[i] <= '9'))
{
number *= 10;
number += ((int)str[i] - '0');
i++;
}
return (number * flag);
}

void builtin_bg(t_process *cmd, t_group *sh)
{
int num;
int i;
int pgid;

i = 0;
num = 0;
if (!sh->p)
return ;
if (cmd->argv[1])
num = ft_atoi((cmd->argv[1]) - 1);
while ((sh->p[i] != NULL) && (i < num))
i++;
if (sh->p[i] != NULL)
{
pgid = sh->p[i]->pid.pgid;
printf("%d\n", pgid);
kill(-pgid, SIGCONT);
}
}
101 changes: 101 additions & 0 deletions job_control/builtin_jobs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_jobs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/13 23:50:39 by ztisnes #+# #+# */
/* Updated: 2018/04/16 01:49:14 by ztisnes ### ########.fr */
/* */
/* ************************************************************************** */

// #include "minishell.h"

#define _POSIX_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

/*
** //TODO:
** jobs: Display the current program that is executed from the list
** job control functions that returns true or/and false when program is successful
** Create a signal with the PID once it is suspended after command is running (bg, fg)
** Ctrl Z for suspend -> kill(pid, SIGSTOP)
** Ctrl C kills the process -> kill(pid, SIGINT)
** fg: foreground
** bg: background
** &: echos the process
*/

// void builtin_fg(t_exec *cmd, t_shell *sh)
// {
//
// }

// void builtin_ampersand()
// {
//
// }
void builtin_jobs(t_io *fd, t_process *sh)
{
int i;
char *tmp;
t_queue *node;
t_process *pipeline;

i = -1;
node = init_queue();
if (sh->p != NULL)
{
while (pipeline = sh->p[++i] != NULL)
{
//Maybe use queue or printf here?
tmp = ft_base64encode("0123456789", (i + 1));
ft_putstr_fd("[", fd->stdout);
ft_putstr_fd(tmp, fd->stdout);
ft_putstr_fd("]", fd->stdout);
ft_putstr_fd(pipeline->fullpath, fd->stdout);
ft_putstr_fd("\n", fd->stdout);
free(tmp);
}
}
free(fd);
}

int main(void)
{
pid_t pid;
int p1[2], p2[2];
char c = '?';

if (pipe(p1) != 0)
perror("pipe() #1 error");
else if (pipe(p2) != 0)
perror("pipe() #2 error");
else
{
if ((pid = fork()) == 0)
{
printf("child's process group id is %d\n", (int)getpgrp());
write(p2[1], &c, 1);
puts("child is waiting for parent to complete task");
read(p1[0], &c, 1);
printf("child's process group id is now %d\n", (int)getpgrp());
exit(0);
}
else
{
printf("parent's process group id is %d\n", (int)getpgrp());
read(p2[0], &c, 1);
printf("parent is performing setpgid() on pid %d\n",(int)pid);
if (setpgid(pid, 0) != 0)
perror("setpgid() error");
write(p1[1], &c, 1);
printf("parent's process group id is now %d\n", (int)getpgrp());
sleep(5);
}
}
}
39 changes: 39 additions & 0 deletions job_control/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/15 18:25:40 by ztisnes #+# #+# */
/* Updated: 2018/04/15 18:33:47 by ztisnes ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include <stdio.h>
int main() {

Queue* q = init_queue();

printf("Empty? %d\n", isEmpty(q));
printf("Enqueueing 'Hello'\n");
ft_enqueue(q, "Hello");
printf("Empty? %d\n", isEmpty(q));
printf("Peeking: %s\n", peek_queue(q));
printf("Enqueueing 'World'\n");
ft_enqueue(q, "World");
printf("Empty? %d\n", isEmpty(q));
printf("Peeking: %s\n", peek_queue(q));
printf("Enqueueing ':)'\n");
ft_enqueue(q, ":)");
printf("Empty? %d\n", isEmpty(q));
printf("Peeking: %s\n", peek_queue(q));
printf("Dequeue: %s\n", ft_dequeue(q));
printf("Dequeue: %s\n", ft_dequeue(q));
printf("Dequeue: %s\n", ft_dequeue(q));
printf("Dequeue: %s\n", ft_dequeue(q));
printf("Empty? %d\n", isEmpty(q));

return 0;
}
3 changes: 2 additions & 1 deletion libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: asarandi <asarandi@student.42.us.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2017/09/20 17:41:00 by asarandi #+# #+# #
# Updated: 2018/04/10 21:56:26 by asarandi ### ########.fr #
# Updated: 2018/04/14 18:02:29 by ztisnes ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -80,6 +80,7 @@ FILES = ft_str_append.c \
ft_tolower.c \
ft_toupper.c \
ft_uriencode.c \
queue.c \
get_next_line.c

OBJ = $(FILES:%.c=%.o)
Expand Down
8 changes: 7 additions & 1 deletion libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: asarandi <asarandi@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/09/20 17:51:15 by asarandi #+# #+# */
/* Updated: 2018/04/10 21:55:43 by asarandi ### ########.fr */
/* Updated: 2018/04/15 01:48:45 by ztisnes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,12 @@
# include <string.h>
# define BUFF_SIZE 1

typedef struct s_queue
{
t_node *first;
t_node *last;
} t_queue;

typedef struct s_list
{
void *content;
Expand Down
Loading