create makefile and parser for philo

This commit is contained in:
Denis 2021-05-09 17:37:40 +03:00
parent ad6600d071
commit 98e51402ec
4 changed files with 162 additions and 9 deletions

View File

@ -0,0 +1,45 @@
.PHONY: all clean fclean re
SRCS = main.c utilc.c
SRC_DIR = ./srcs/
OBJ_DIR = ./obj/
OBJS = $(SRCS:%.c=%.o)
OBJS_FILES = $(addprefix $(OBJ_DIR), $(OBJS))
HEADER = philo_header.h
vpath %.h includes
vpath %.o $(OBJ_DIR)
vpath %.c $(SRC_DIR)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Wextra -Werror -I ./includes/
NAME = philo_one
all: $(OBJ_DIR) $(NAME)
norme :
norminette $(addprefix $(SRC_DIR), $(SRCS))
$(OBJ_DIR):
mkdir -p obj
$(NAME): $(OBJS_FILES)
$(CC) $(CFLAGS) -o $(NAME) $(OBJS_FILES)
clean:
$(RM) $(OBJS_FILES)
$(OBJ_DIR)%.o: %.c $(HEADER)
$(CC) $(CFLAGS) -c $< -o $@
fclean: clean
$(RM) $(NAME)
re: fclean $(NAME)

View File

@ -6,6 +6,7 @@
# include <pthread.h>
# include <sys/time.h>
# include <stdio.h>
# include <string.h>
typedef struct s_all
@ -20,5 +21,7 @@ typedef struct s_all
void *ft_calloc(size_t num, size_t size);
t_all *structalloc_and_bzero(void);
int parse_and_check_values(char **argv, int quantity);
int ft_atoi(const char *str);
int check_f(char ch);
#endif

View File

@ -1,15 +1,81 @@
#include "../includes/philo_header.h"
int check_param(t_all *all, int optional)
{
if (optional)
{
if (all->number_of_philosophers <= 0)
return(1);
if (all->time_to_die <= 0)
return(1);
if (all->time_to_eat <= 0)
return(1);
if (all->time_to_sleep <= 0)
return(1);
if (all->number_of_times_each_philosopher_must_eat <= 0)
return(1);
}
else
{
if (all->number_of_philosophers <= 0)
return(1);
if (all->time_to_die <= 0)
return(1);
if (all->time_to_eat <= 0)
return(1);
if (all->time_to_sleep <= 0)
return(1);
}
return (0);
}
int parse_param(t_all *all, int optional, char **argv)
{
if (optional)
{
all->number_of_philosophers = ft_atoi(argv[1]);
all->time_to_die = ft_atoi(argv[2]);
all->time_to_eat = ft_atoi(argv[3]);
all->time_to_sleep = ft_atoi(argv[4]);
all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
return (check_param(all, 1));
}
else
{
all->number_of_philosophers = ft_atoi(argv[1]);
all->time_to_die = ft_atoi(argv[2]);
all->time_to_eat = ft_atoi(argv[3]);
all->time_to_sleep = ft_atoi(argv[4]);
return (check_param(all, 0));
}
}
int parse_and_check_values(char **argv, int quantity)
{
t_all *all;
all = structalloc_and_bzero();
if (quantity == 5)
{
printf("ok standart run\n");
parse_param(all, 0, argv);
return(1);
}
else if (quantity == 6)
{
printf("ok run with optional\n");
parse_param(all, 1, argv);
return(1);
}
else
{
printf("error please use argument 5 (optional) or 4 (standart) \n");
free(all);
return(0);
}
}
int main(int argc, char **argv)
{
parse_and_check_values(argv, argc);
}

View File

@ -1,5 +1,43 @@
#include "../includes/philo_header.h"
int check_f(char ch)
{
if (ch == '\t' || ch == '\n' || ch == ' ' || \
ch == '\v' || ch == '\f' || ch == '\r')
return (1);
else
return (0);
}
int ft_atoi(const char *str)
{
int i;
unsigned long int result;
int operator;
i = 0;
operator = 1;
result = 0;
while (check_f(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
operator *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
if (result > 922337203685477580 && operator == -1)
return (0);
else if (result > 922337203685477580 && operator == 1)
return (-1);
result = result * 10 + str[i] - '0';
i++;
}
return (result * operator);
}
void *ft_calloc(size_t num, size_t size)
{
void *addr;
@ -23,9 +61,10 @@ t_all *structalloc_and_bzero(void)
t_all *all;
all = ft_calloc(sizeof(t_all), 1);
all->number_of_philosophers = 0;
all->time_to_die = 0;
all->time_to_eat = 0;
all->time_to_sleep = 0;
all->number_of_times_each_philosopher_must_eat = 0;
all->number_of_philosophers = -1;
all->time_to_die = -1;
all->time_to_eat = -1;
all->time_to_sleep = -1;
all->number_of_times_each_philosopher_must_eat = -1;
return(all);
}