diff --git a/Makefile b/Makefile index 27b4f25..662b5f1 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ LINKER = gcc # Flags -FLAGS = -Wall -Wextra -Werror +FLAGS = -Wall -Wextra -Werror -g3 # Project Directory diff --git a/inc/ft_p.h b/inc/ft_p.h index 06d2b1b..9fb4b42 100644 --- a/inc/ft_p.h +++ b/inc/ft_p.h @@ -16,7 +16,7 @@ # include /* -** On MacOS environ must not be free'd +** On MacOS extern char **environ must not be free'd */ #ifdef __APPLE__ @@ -25,11 +25,19 @@ # define FREE_ENVIRON TRUE #endif +/* +** server +*/ void ft_cd(char *ptr, char **pptr); -void ft_lcd(char *ptr, char **pptr); void ft_invalid(char *ptr); void ft_handle_request(char *ptr, int arg); + +/* +** client +*/ + +void ft_lcd(char *ptr, char **pptr); int ft_lhandle_request(char *ptr, int arg); #endif \ No newline at end of file diff --git a/libftp/inc/libftp.h b/libftp/inc/libftp.h index d6db173..ef72c52 100644 --- a/libftp/inc/libftp.h +++ b/libftp/inc/libftp.h @@ -73,12 +73,13 @@ int ft_dstrlen(char **); int ft_empty(char *); pid_t ft_wfork(void); char *ft_wgetcwd(void); -char *ft_wreadline(void); +char *ft_wreadline(int); char *ft_get_environ(char *); void ft_set_environ(char *, char *); char *ft_path(char *); char *ft_lpath(char *name); char **ft_get_argv(char *); +void ft_send_response(char *buff, int connfd); /* ** Host names diff --git a/libftp/src/readline.c b/libftp/src/readline.c index 7c18fc0..84b9319 100644 --- a/libftp/src/readline.c +++ b/libftp/src/readline.c @@ -12,11 +12,11 @@ #include -char get_char(void) +char get_char(int fd) { char c; - read(0, &c, 1); + read(fd, &c, 1); return (c); } @@ -33,7 +33,7 @@ void resize_str(char **str, size_t new_size) } } -char *ft_readline(void) +char *ft_readline(int fd) { char *line; char c; @@ -41,13 +41,13 @@ char *ft_readline(void) int size; loop = 0; - size = 10; + size = 50; line = ft_strnew(size); if (line == NULL) return (NULL); while (1) { - c = get_char(); + c = get_char(fd); if (c == '\n' || c == '\0') { line[loop] = '\0'; @@ -66,11 +66,11 @@ char *ft_readline(void) ** wrapper for ft_readline */ -char *ft_wreadline(void) +char *ft_wreadline(int fd) { char *n; - if (!(n = ft_readline())) + if (!(n = ft_readline(fd))) ft_err_sys("readline error"); return (n); } \ No newline at end of file diff --git a/libftp/src/respond.c b/libftp/src/respond.c new file mode 100644 index 0000000..dbc6d99 --- /dev/null +++ b/libftp/src/respond.c @@ -0,0 +1,17 @@ +# include + +/* +** write to fd +*/ + +void ft_send_response(char *buff, int connfd) +{ + if (buff) + { + ft_wwriten(connfd, buff, ft_strlen(buff)); + } + else + { + ft_err_quit("ft_send_response fail"); + } +} \ No newline at end of file diff --git a/src/client/lhandle_request.c b/src/client/lhandle_request.c index 4ddc0f8..8e2d3f6 100644 --- a/src/client/lhandle_request.c +++ b/src/client/lhandle_request.c @@ -1,6 +1,6 @@ # include -void ft_send_response(char *buff, int connfd, int print) +void ft_send_lresponse(char *buff, int connfd, int print) { if (buff) { @@ -74,7 +74,7 @@ int ft_lhandle_request(char *line, int connfd) ** using buff send response to client */ - ft_send_response(buff, connfd, n); + ft_send_lresponse(buff, connfd, n); /* ** free diff --git a/src/client/main.c b/src/client/main.c index c32eb43..7da19c7 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -58,7 +58,7 @@ int main(int argc, char **argv) /* ** read from stdin */ - while ((cmd = ft_wreadline())) + while ((cmd = ft_wreadline(0))) { if (ft_strlen(cmd) > 0 && !ft_empty(cmd)) { diff --git a/src/server/handle_request.c b/src/server/handle_request.c index 9f09c94..0e5feaa 100644 --- a/src/server/handle_request.c +++ b/src/server/handle_request.c @@ -1,17 +1,5 @@ # include -void ft_send_response(char *buff, int connfd) -{ - if (buff) - { - ft_wwriten(connfd, buff, MAXLINE); - } - else - { - ft_err_quit("ft_send_response fail"); - } -} - void ft_handle_request(char *line, int connfd) { char **argv; diff --git a/src/server/main.c b/src/server/main.c index 1c420e3..a10ae43 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -7,7 +7,7 @@ int main(int argc, char **argv) pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; - char buff[MAXLINE]; + char *buff; if (argc != 2) ft_err_msg("ft_p: usage server "); @@ -57,15 +57,18 @@ int main(int argc, char **argv) /* ** read from client as long as client is writing */ - while (ft_wreadn(connfd, buff, MAXLINE)) + while ((buff = ft_wreadline(connfd))) { - /* - ** use buff to handle client request - */ + if (ft_strequ(buff, "quit")) + { + ft_strdel(&buff); + break; + } ft_handle_request(buff, connfd); + ft_strdel(&buff); } /* - ** free the environment, wont work on mac + ** free the environment */ if (FREE_ENVIRON) ft_free_environ();