Skip to content

Commit

Permalink
can handle pwd, and exit lol
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 14, 2017
1 parent 08ae4cc commit 7cbd052
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 12 deletions.
7 changes: 4 additions & 3 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

# include <libftp.h>

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

#endif
5 changes: 5 additions & 0 deletions libftp/inc/libftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# include <errno.h>
# include <fcntl.h>
# include <stdarg.h>
# include <dirent.h>

/*
** application constants
Expand Down Expand Up @@ -59,11 +60,15 @@ void ft_wclose(int);
void ft_wconnect(int, const struct sockaddr *, socklen_t);
void ft_set_sockaddr(struct sockaddr *, int, int, in_addr_t);
void ft_fill_buffer(char *, char *);
void ft_check_exit(char *);
int ft_wsocket(int, int, int);
int ft_waccept(int, struct sockaddr *, socklen_t *);
int ft_dstrlen(char **);
pid_t ft_wfork(void);
char *ft_wgetcwd(void);
char *ft_wreadline(void);
char *ft_get_environ(char *);
char **ft_get_argv(char *);

/*
** Error handling
Expand Down
20 changes: 20 additions & 0 deletions libftp/src/argv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <libftp.h>

char **ft_get_argv(char *line)
{
char **argv;

argv = NULL;
if (line)
{
if ((argv = ft_strsplit(line, ' ')))
{
if (ft_dstrlen(argv) > 0)
{
return (argv);
}
}
}
ft_err_quit("ft_get_argv fail");
return (argv);
}
14 changes: 14 additions & 0 deletions libftp/src/dstrlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <libftp.h>

int ft_dstrlen(char **str)
{
int len;

len = 0;
while (*str)
{
str++;
len++;
}
return (len);
}
93 changes: 93 additions & 0 deletions libftp/src/environ.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# include <libftp.h>

char *search_environ(char *curr, char *target)
{
char *end;
char *name;
char *value;

end = NULL;
value = NULL;
name = NULL;

/*
** find the pointer to the '=' delimeter
*/

if ((end = ft_strchr(curr, '=')))
{

/*
** using the end pointer, calculate the size and create a new string
*/

if ((name = ft_strnew(end - curr)))
{

/*
** copy n bytes into name
*/

ft_strncpy(name, curr, end - curr);

/*
** check if name is infact the target
*/

if (ft_strequ(name, target))
{

/*
** create new string with value prefixed by the '=' delimete
*/

if ((value = ft_strdup(end)))
{
return (value);
}

}
}
}
return (value);
}

char *ft_get_environ(char *target)
{
extern char **environ;
char *res;
char *value;
int count;

count = 0;
res = NULL;
value = NULL;

/*
** loop through the environment
*/

while (environ[count])
{

/*
** check for the = delimeter
*/

if ((res = search_environ(environ[count], target)))
{

/*
** res includes '=' in the beginning. create new string
** without it and free
*/

value = ft_strdup(res + 1);
ft_strdel(&res);
return (value);
}
count++;
}
ft_err_quit("environ variable not found");
return (value);
}
17 changes: 17 additions & 0 deletions libftp/src/invalid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# include <libftp.h>

void ft_invalid(char *buff)
{
char *defaults;

defaults = NULL;
if (buff && (defaults = ft_strdup("ft_p: argument not supported")))
{
ft_strcpy(buff, defaults);
ft_strdel(&defaults);
}
else
{
ft_err_quit("ft_pwd fail");
}
}
16 changes: 16 additions & 0 deletions src/client/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <ft_p.h>

void ft_check_exit(char *line)
{
char **argv;

argv = NULL;
if (line)
{
argv = ft_get_argv(line);
if (ft_strequ(argv[0], "exit"))
{
exit(EXIT_SUCCESS);
}
}
}
6 changes: 6 additions & 0 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ int main(int argc, char **argv)

ft_fill_buffer(buff, cmd);

/*
** check exit
*/

ft_check_exit(buff);

/*
** write buff to server socket
*/
Expand Down
1 change: 1 addition & 0 deletions src/server/cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# include <ft_p.h>
51 changes: 51 additions & 0 deletions src/server/handle_request.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# include <ft_p.h>

char **ft_get_argv(char *line)
{
char **argv;

argv = NULL;
if (line)
{
if ((argv = ft_strsplit(line, ' ')))
{
if (ft_dstrlen(argv) > 0)
{
return (argv);
}
}
}
ft_err_quit("ft_get_argv fail");
return (argv);
}

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 *root)
{
char **argv;
char buff[MAXLINE];

/*
** get command line by splitting string
*/

argv = ft_get_argv(line);
ft_bzero(buff, MAXLINE);
ft_bzero(root, ft_strlen(root)); //to avoid unused var variable, to be used for cd
if (ft_strequ(argv[0], "pwd"))
ft_pwd(buff);
else
ft_invalid(buff);
ft_send_response(buff, connfd);
}
39 changes: 30 additions & 9 deletions src/server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ int main(void)
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
char buff[MAXLINE];
char **argv;
char *root;

root = ft_wgetcwd();
/*
** create a socket
*/
Expand All @@ -36,7 +37,7 @@ int main(void)
*/

ft_wlisten(listenfd, BACKLOG);
while ( 42 )
while (42)
{
clilen = sizeof(cliaddr);

Expand All @@ -53,18 +54,38 @@ int main(void)

if ((childpid = ft_wfork()) == 0)
{

/*
**
*/

ft_wclose(listenfd);

/*
** read from client as long as client is writing
*/

while (ft_wreadn(connfd, buff, MAXLINE)) {
if ((argv = ft_strsplit(buff, ' '))) {
if (ft_strequ(argv[0], "pwd")) {
ft_bzero(buff, MAXLINE);
ft_strcpy(buff, ft_wgetcwd());
ft_wwriten(connfd, buff, MAXLINE);
}
}

/*
** use buff to handle client request
*/

ft_handle_request(buff, connfd, root);

}

/*
** kill the process
*/

exit(0);
}

/*
**
*/

ft_wclose(connfd);
}
return (0);
Expand Down
17 changes: 17 additions & 0 deletions src/server/pwd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# include <ft_p.h>

void ft_pwd(char *buff)
{
char *cwd;

cwd = NULL;
if (buff && (cwd = ft_wgetcwd()))
{
ft_strcpy(buff, cwd);
ft_strdel(&cwd);
}
else
{
ft_err_quit("ft_pwd fail");
}
}

0 comments on commit 7cbd052

Please sign in to comment.