Skip to content

Commit

Permalink
removed depedencies on MAXLINE to read [server]
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 27, 2017
1 parent 27d2936 commit f59ede6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LINKER = gcc

# Flags

FLAGS = -Wall -Wextra -Werror
FLAGS = -Wall -Wextra -Werror -g3

# Project Directory

Expand Down
12 changes: 10 additions & 2 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# include <libftp.h>

/*
** On MacOS environ must not be free'd
** On MacOS extern char **environ must not be free'd
*/

#ifdef __APPLE__
Expand All @@ -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
3 changes: 2 additions & 1 deletion libftp/inc/libftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions libftp/src/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

#include <libftp.h>

char get_char(void)
char get_char(int fd)
{
char c;

read(0, &c, 1);
read(fd, &c, 1);
return (c);
}

Expand All @@ -33,21 +33,21 @@ void resize_str(char **str, size_t new_size)
}
}

char *ft_readline(void)
char *ft_readline(int fd)
{
char *line;
char c;
int loop;
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';
Expand All @@ -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);
}
17 changes: 17 additions & 0 deletions libftp/src/respond.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# include <libftp.h>

/*
** 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");
}
}
4 changes: 2 additions & 2 deletions src/client/lhandle_request.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# include <ft_p.h>

void ft_send_response(char *buff, int connfd, int print)
void ft_send_lresponse(char *buff, int connfd, int print)
{
if (buff)
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
12 changes: 0 additions & 12 deletions src/server/handle_request.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
# include <ft_p.h>

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;
Expand Down
15 changes: 9 additions & 6 deletions src/server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PORT>");
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f59ede6

Please sign in to comment.