diff --git a/libft/Makefile b/libft/Makefile index b631ed02..80bba6c0 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -152,6 +152,7 @@ sstr/ft_sstrprint.c\ sstr/ft_sstrprint_fd.c\ sstr/ft_sstrsort.c\ sstr/ft_sstrstr.c\ +str/ft_strsepjoin.c\ str/ft_atoi.c\ str/ft_convert_base.c\ str/ft_putaddr_fd.c\ @@ -216,7 +217,7 @@ SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) NB = $(words $(SRC_BASE)) INDEX = 0 -SHELL := bash +SHELL := /bin/bash all : @$(MAKE) -j $(NAME) @@ -236,7 +237,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) @$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}")) @printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')" @$(CC) $(FLAGS) -MMD -c $< -o $@\ - -I $(INC_DIR) + -I $(INC_DIR) -lm @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) clean : diff --git a/libft/includes/error.h b/libft/includes/error.h index 64f16559..3780ad8f 100644 --- a/libft/includes/error.h +++ b/libft/includes/error.h @@ -17,7 +17,7 @@ # include /* -** DEBUG with no malloc +** DEBUG with malloc */ # define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}" # define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ diff --git a/libft/includes/net.h b/libft/includes/net.h index c339753b..91698c7f 100644 --- a/libft/includes/net.h +++ b/libft/includes/net.h @@ -1,8 +1,9 @@ #ifndef NET_H # define NET_H -# define ACK 2 -# define NACK 3 +# define ACK 2 +# define NACK 3 +# define NET_MAXSIZE 512 # include # include @@ -11,9 +12,12 @@ int create_server(int port, int backlog, char *protoname); int create_client(char *addr, int port, char *protoname); +void listener(int domain, int sock, int proto, void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); int net_send(int sock, char *msg, int size); +int net_send_large(int sock, char *msg, int size); int net_get(int sock, char *msg, int size); int net_get_fd(int sock, int fd, int size); +int net_get_large(int sock, int fd); #endif diff --git a/libft/includes/str.h b/libft/includes/str.h index 3db450ed..0d08a4b6 100644 --- a/libft/includes/str.h +++ b/libft/includes/str.h @@ -51,6 +51,7 @@ int ft_strequ(char const *s1, char const *s2); int ft_strnequ(char const *s1, char const *s2, size_t n); char *ft_strsub(char const *s, unsigned int start, size_t len); char *ft_strjoin(char const *s1, char const *s2); +char *ft_strsepjoin(char **tab, char sep); char *ft_strtrim(char const *s); char **ft_strsplit(char const *s, char c); diff --git a/libft/srcs/net/create_client.c b/libft/srcs/net/create_client.c index 77411380..dfc2628a 100644 --- a/libft/srcs/net/create_client.c +++ b/libft/srcs/net/create_client.c @@ -16,3 +16,28 @@ int create_client(char *addr, int port, char *protoname) return (-1); return (sock); } + +void listener(int domain, int sock, int proto, void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)) +{ int sd; + struct sockaddr_in addr; + unsigned char buf[1024]; + + sd = socket(domain, sock, proto); + if (sd < 0) + { + perror("listener socket"); + exit(0); + } + for (;;) + { int bytes; + socklen_t len=sizeof(addr); + + bzero(buf, sizeof(buf)); + bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); + if (bytes > 0 && handler) + handler(buf, bytes, &addr); + else + perror("recvfrom"); + } + exit(0); +} diff --git a/libft/srcs/net/net_get.c b/libft/srcs/net/net_get.c index e09418c1..9541282c 100644 --- a/libft/srcs/net/net_get.c +++ b/libft/srcs/net/net_get.c @@ -26,3 +26,21 @@ int net_get_fd(int sock, int fd, int size) return (-1); return (0); } + +int net_get_large(int sock, int fd) +{ + int i; + int num_blks; + int num_last_blk; + + net_get(sock, (char*)&num_blks, sizeof(num_blks)); + net_get(sock, (char*)&num_last_blk, sizeof(num_last_blk)); + num_blks = ntohs(num_blks); + num_last_blk = ntohs(num_last_blk); + i = -1; + while (++i < num_blks - 1) + net_get_fd(sock, fd, NET_MAXSIZE); + if (num_last_blk) + net_get_fd(sock, fd, num_last_blk); + return (0); +} diff --git a/libft/srcs/net/net_send.c b/libft/srcs/net/net_send.c index e4a76e0e..de981f56 100644 --- a/libft/srcs/net/net_send.c +++ b/libft/srcs/net/net_send.c @@ -12,3 +12,26 @@ int net_send(int sock, char *msg, int size) return (1); return (0); } + +int net_send_large(int sock, char *msg, int size) +{ + int i; + int num_blks; + int num_last_blk; + + num_blks = htons(size / NET_MAXSIZE + 1); + num_last_blk = htons(size % NET_MAXSIZE); + if (net_send(sock, (char*)&num_blks, sizeof(num_blks))) + return (1); + if (net_send(sock, (char*)&num_last_blk, sizeof(num_blks))) + return (1); + + num_blks = ntohs(num_blks); + num_last_blk = ntohs(num_last_blk); + i = -1; + while (++i < num_blks - 1) + net_send(sock, msg + i * NET_MAXSIZE, NET_MAXSIZE); + if (num_last_blk) + net_send(sock, msg + i * NET_MAXSIZE, num_last_blk); + return (0); +} diff --git a/libft/srcs/rs/rs.c b/libft/srcs/rs/rs.c index 3f738785..3cddd3ef 100644 --- a/libft/srcs/rs/rs.c +++ b/libft/srcs/rs/rs.c @@ -1,6 +1,7 @@ #include "libft.h" #include +double sqrt(double x); struct s_stats g_rs = {0, 0, 0, 0, 0, 0, 0}; void rs_clear() @@ -32,8 +33,8 @@ void rs_push(double n) void rs_calcmore() { - void *libm; - double (*sqrt)(double); + /* void *libm; */ + /* double (*sqrt)(double); */ if (g_rs.count == 0) { @@ -47,10 +48,10 @@ void rs_calcmore() return ; } g_rs.var = g_rs.m / (g_rs.count - 1); - if ((libm = dlopen("libm.dylib", 0)) == NULL) - printf("%s\n", dlerror()); - else if ((sqrt = dlsym(libm, "sqrt")) == NULL) - printf("%s\n", dlerror()); - else - g_rs.stdev = sqrt(g_rs.var); + /* if ((libm = dlopen("libm.dylib", 0)) == NULL) */ + /* printf("%s\n", dlerror()); */ + /* else if ((sqrt = dlsym(libm, "sqrt")) == NULL) */ + /* printf("%s\n", dlerror()); */ + /* else */ + g_rs.stdev = sqrt(g_rs.var); } diff --git a/libft/srcs/str/ft_strsepjoin.c b/libft/srcs/str/ft_strsepjoin.c new file mode 100644 index 00000000..8fbb0ee3 --- /dev/null +++ b/libft/srcs/str/ft_strsepjoin.c @@ -0,0 +1,25 @@ +#include "libft.h" + +char *ft_strsepjoin(char **tab, char sep) +{ + char *join; + char **p; + int len; + + len = 0; + if (!(p = tab)) + return (NULL); + while (*p) + len += ft_strlen(*p++) + 1; + if (!(join = ft_strnew(len))) + return (NULL); + *join = 0; + p = tab; + while (*p) + { + ft_strcat(join, *p++); + ft_strcat(join, &sep); + } + join[len - 1] = 0; + return (join); +}