Skip to content

Commit

Permalink
fixed all mem leaks on server side apart from environ
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 15, 2017
1 parent 2310e8a commit 99da248
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 15 deletions.
4 changes: 2 additions & 2 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
# include <libftp.h>

void ft_pwd(char*);
void ft_cd(char *, char *, char **);
void ft_cd(char *, char **);
void ft_ls(char *);
void ft_invalid(char *);
void ft_handle_request(char *, int, char *);
void ft_handle_request(char *, int);

#endif
1 change: 1 addition & 0 deletions libft/ft_strtrim.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ char *ft_strtrim(char const *s)
{
len = start_end[1] - start_end[0] + 1;
copy = ft_strsub(s, start_end[0], len);
free(start_end);
}
}
return (copy);
Expand Down
1 change: 1 addition & 0 deletions libftp/src/chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ int ft_wchdir(char *path)
{
ft_set_environ("OLDPWD", cwd);
}
ft_strdel(&cwd);
}
return (n);
}
10 changes: 8 additions & 2 deletions src/server/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
** Change the server directory
*/

void ft_cd(char *buff, char *root_dir, char **argv)
void ft_cd(char *buff, char **argv)
{
char *resp;
char *path;

resp = NULL;
path = NULL;
if (buff && root_dir && argv)
if (buff && argv)
{

/*
Expand Down Expand Up @@ -45,6 +45,12 @@ void ft_cd(char *buff, char *root_dir, char **argv)
*/

ft_fill_buffer(buff, resp);

/*
** free path
*/

ft_strdel(&path);
}
}
else
Expand Down
15 changes: 13 additions & 2 deletions src/server/handle_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void ft_send_response(char *buff, int connfd)
}
}

void ft_handle_request(char *line, int connfd, char *root)
void ft_handle_request(char *line, int connfd)
{
char **argv;
char buff[MAXLINE];
Expand All @@ -22,6 +22,11 @@ void ft_handle_request(char *line, int connfd, char *root)
*/

argv = ft_get_argv(line);

/*
** clear buff
*/

ft_bzero(buff, MAXLINE);

/*
Expand All @@ -31,7 +36,7 @@ void ft_handle_request(char *line, int connfd, char *root)
if (ft_strequ(argv[0], "pwd"))
ft_pwd(buff);
else if (ft_strequ(argv[0], "cd"))
ft_cd(buff, root, argv);
ft_cd(buff, argv);
else if (ft_strequ(argv[0], "ls"))
ft_ls(buff);
else
Expand All @@ -42,4 +47,10 @@ void ft_handle_request(char *line, int connfd, char *root)
*/

ft_send_response(buff, connfd);

/*
** free
*/

ft_dstrdel(argv);
}
67 changes: 61 additions & 6 deletions src/server/ls.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
#include <ft_p.h>

// this function has a fuck load of memory leaks
/*
** append src to dst after newline is appended to src
*/

char *read_dir(char *dir)
static char *append(char *dst, char *src)
{
char *final;
char *append;

final = NULL;
append = NULL;
if (dst && src)
{

/*
** append newline to src
*/

if ((append = ft_strjoin(src, "\n" )))
{

/*
** append newlined src to dst
*/

if ((final = ft_strjoin(dst, append)))
{

/*
** argument dst is a malloced str that append must free
*/

ft_strdel(&dst);

}

ft_strdel(&append);
}
}
return (final);
}

/*
** this function has a fuck load of memory leaks
*/

static char *read_dir(char *dir)
{
DIR *p_dir;
struct dirent *drnt;
char *dir_contents;

if ((dir_contents = ft_strnew(1)))
if ((dir_contents = ft_strdup("")))
{

/*
Expand All @@ -29,7 +73,7 @@ char *read_dir(char *dir)
** append the contents to the char * alone with a newline
*/

dir_contents = ft_strjoin( dir_contents, ft_strjoin( drnt->d_name, "\n" ) );
dir_contents = append(dir_contents, drnt->d_name);

}

Expand Down Expand Up @@ -72,14 +116,25 @@ void ft_ls(char *buff)
{

/*
** fill buff with direcotory contents
** fill buff with direcotory contents,
** using resp if newline was found
*/

ft_fill_buffer(buff, resp);
ft_strdel(&dir_contents);
}
else
{

/*
** fill buff with direcotory contents,
** using dir_con if newline was not found
*/

ft_fill_buffer(buff, dir_contents);

}
ft_strdel(&dir_contents);
ft_strdel(&resp);
}
else
ft_err_quit("ft_ls fail");
Expand Down
4 changes: 1 addition & 3 deletions src/server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ int main(void)
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
char buff[MAXLINE];
char *root;

root = ft_wgetcwd();
/*
** create a socket
*/
Expand Down Expand Up @@ -71,7 +69,7 @@ int main(void)
** use buff to handle client request
*/

ft_handle_request(buff, connfd, root);
ft_handle_request(buff, connfd);

}

Expand Down

0 comments on commit 99da248

Please sign in to comment.