"new stuff irc"
This commit is contained in:
ariard 2017-08-16 19:53:26 +02:00
commit 47b19c606b
9 changed files with 111 additions and 13 deletions

View file

@ -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 :

View file

@ -17,7 +17,7 @@
# include <stdarg.h>
/*
** 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__

View file

@ -3,6 +3,7 @@
# define ACK 2
# define NACK 3
# define NET_MAXSIZE 512
# include <sys/socket.h>
# include <netdb.h>
@ -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

View file

@ -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);

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -1,6 +1,7 @@
#include "libft.h"
#include <math.h>
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
/* 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);
}

View file

@ -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);
}