From 2772d8d0b39d954853559ddd122c59b314125461 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 3 Apr 2017 18:43:58 +0200 Subject: [PATCH] some client side improvements, started to use readline(3) --- ftp/Makefile | 8 +++----- ftp/includes/ft_p.h | 18 ++++++++++++------ ftp/srcs/client.c | 45 ++++++++++++++++++++++++++++++++++++++------- ftp/srcs/ftp.c | 4 ++-- ftp/srcs/ftp_cmd.c | 2 +- 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/ftp/Makefile b/ftp/Makefile index 8f76905f..695a33b0 100644 --- a/ftp/Makefile +++ b/ftp/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/04/03 16:12:06 by jhalford ### ########.fr # +# Updated: 2017/04/03 18:19:23 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -24,8 +24,6 @@ LIBFT_DIR = libft/ LIBFT_LIB = $(LIBFT_DIR)libft.a LIBFT_INC = $(LIBFT_DIR)includes/ -LIBS = - SRC_DIR = srcs/ INC_DIR = includes/ OBJ_DIR = objs/ @@ -54,14 +52,14 @@ server: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(SERVER_OBJ) @$(CC) $(OBJS) -o $@ \ -I $(INC_DIR) \ -I $(LIBFT_INC) \ - $(LIBS) $(LIBFT_LIB) $(SERVER_OBJ) $(FLAGS) + $(LIBFT_LIB) $(SERVER_OBJ) $(FLAGS) @printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n" client: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ) @$(CC) $(OBJS) -o $@ \ -I $(INC_DIR) \ -I $(LIBFT_INC) \ - $(LIBS) $(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) + -lreadline $(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) @printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n" $(LIBFT_LIB): diff --git a/ftp/includes/ft_p.h b/ftp/includes/ft_p.h index 88c15329..910ebd38 100644 --- a/ftp/includes/ft_p.h +++ b/ftp/includes/ft_p.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */ -/* Updated: 2017/04/03 17:12:59 by jhalford ### ########.fr */ +/* Updated: 2017/04/03 18:42:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,19 @@ # define FTP_SERVER_USAGE "%s " # define FTP_CLIENT_USAGE "%s " # define FTP_BUF 1024 +# define FTP_READ_BUF 1024 # define FTP_REPLY_BUF 1024 -#include "libft.h" -#include "sys/socket.h" -#include "netdb.h" -#include "netinet/in.h" -#include "arpa/inet.h" +# include "libft.h" +# include +# include +# include +# include + +# include + +# include +# include typedef struct s_ftp_reply t_ftp_reply; diff --git a/ftp/srcs/client.c b/ftp/srcs/client.c index cde29185..27988d76 100644 --- a/ftp/srcs/client.c +++ b/ftp/srcs/client.c @@ -6,14 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */ -/* Updated: 2017/04/03 17:20:42 by jhalford ### ########.fr */ +/* Updated: 2017/04/03 18:43:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_p.h" -#define FTP_CLIENT_USAGE "%s " - int create_client(char *addr, int port) { int sock; @@ -31,11 +29,25 @@ int create_client(char *addr, int port) return (sock); } +void sigint_nl(int signo) +{ + (void)signo; + ft_putchar('\n'); + signal(SIGINT, SIG_DFL); + kill(SIGINT, getpid()); +} + int main(int ac, char **av) { - int port; - int sock; + int port; + int sock; + char buf[FTP_READ_BUF]; + char *input; + pid_t pid; + pid_t client_pid; + int status; + client_pid = getpid(); if (ac != 3) ft_usage(FTP_CLIENT_USAGE, av[0]); port = ft_atoi(av[2]); @@ -44,10 +56,29 @@ int main(int ac, char **av) perror(av[0]); return (1); } + signal(SIGINT, SIG_IGN); while (1) { - ft_readline(); - write(sock, "bonjour\n", 8); + if ((pid = fork()) < 0) + exit(1); + if (pid == 0) + { + signal(SIGINT, sigint_nl); + if (!(input = readline("ft_p> "))) + exit(1); + if (*input) + { + write(sock, input, ft_strlen(input)); + read(sock, buf, FTP_READ_BUF); + write(1, buf, ft_strlen(buf)); + } + ft_strdel(&input); + tcsetpgrp(STDIN, client_pid); + exit(0); + } + waitpid(pid, &status, 0); + if (WEXITSTATUS(status) == 1) + return (1); } close(sock); return (0); diff --git a/ftp/srcs/ftp.c b/ftp/srcs/ftp.c index 76ec6820..0098217b 100644 --- a/ftp/srcs/ftp.c +++ b/ftp/srcs/ftp.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/03 16:08:44 by jhalford #+# #+# */ -/* Updated: 2017/04/03 17:20:01 by jhalford ### ########.fr */ +/* Updated: 2017/04/03 18:24:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int ftp_spawn(int cs) ft_printf("{yel}{inv}%i {gre} new connection {eoc}\n", getpid()); while ((r = read(cs, buf, FTP_BUF)) > 0) { - buf[r - 1] = 0; + buf[r] = 0; ft_printf("{yel}{inv}%i {eoc} received %i bytes: [%s]\n", getpid(), r, buf); ft_bzero(&reply, sizeof(reply)); ftp_cmd(buf, &reply); diff --git a/ftp/srcs/ftp_cmd.c b/ftp/srcs/ftp_cmd.c index 80dbe100..08af8e47 100644 --- a/ftp/srcs/ftp_cmd.c +++ b/ftp/srcs/ftp_cmd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/03 16:42:58 by jhalford #+# #+# */ -/* Updated: 2017/04/03 17:20:02 by jhalford ### ########.fr */ +/* Updated: 2017/04/03 18:42:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */