Skip to content

Commit

Permalink
added cd, exit and ls
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 15, 2017
1 parent 7cbd052 commit d7c1517
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 49 deletions.
1 change: 1 addition & 0 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

Expand Down
6 changes: 4 additions & 2 deletions libftp/inc/libftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
# define SA struct sockaddr /* generic socket address */
# define SERV_PORT 8000 /* default server port */
# define BACKLOG 15 /* default client BACKLOG */
# define MAXLINE 1024 /* max text line length */
# define BUFFSIZE 8192 /* buffer size for reads and writes */
# define MAXLINE 4096 /* max text line length */

/*
** Read / Write to a socket
Expand All @@ -61,13 +60,16 @@ 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_wchdir(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 *);
void ft_set_environ(char *, char *);
char *ft_path(char *);
char **ft_get_argv(char *);

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

int ft_wchdir(char *path)
{
char *cwd;
int n;

n = 0;
cwd = ft_wgetcwd();
if (path && cwd)
{
if (!(n = chdir(path)))
{
ft_set_environ("OLDPWD", cwd);
}
}
return (n);
}
60 changes: 57 additions & 3 deletions libftp/src/environ.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# include <libftp.h>

/*
** get value of curr
*/

char *search_environ(char *curr, char *target)
{
char *end;
Expand All @@ -13,7 +17,7 @@ char *search_environ(char *curr, char *target)
/*
** find the pointer to the '=' delimeter
*/

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

Expand All @@ -37,21 +41,28 @@ char *search_environ(char *curr, char *target)
if (ft_strequ(name, target))
{


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

if ((value = ft_strdup(end)))
{
ft_strdel(&name);
return (value);
}

}
ft_strdel(&name);
}
}
return (value);
}

/*
** Get value of environ variable targer
*/

char *ft_get_environ(char *target)
{
extern char **environ;
Expand All @@ -71,7 +82,7 @@ char *ft_get_environ(char *target)
{

/*
** check for the = delimeter
** check for the target
*/

if ((res = search_environ(environ[count], target)))
Expand All @@ -83,11 +94,54 @@ char *ft_get_environ(char *target)
*/

value = ft_strdup(res + 1);

/*
** free res (value prefixed by '=')
*/

ft_strdel(&res);
return (value);
}
count++;
}
ft_err_quit("environ variable not found");
return (value);
}
}

/*
** change value of environ variable target
*/

void ft_set_environ(char *target, char *value)
{
extern char **environ;
char *res;
char *tmp;
int count;

count = 0;
res = NULL;
tmp = NULL;

/*
** loop through the environment
*/

while (environ[count])
{

/*
** check for the target
*/

if ((res = search_environ(environ[count], target)))
{
tmp = ft_strjoin("=", value);
environ[count] = ft_strjoin(target, tmp);
ft_strdel(&res);
ft_strdel(&tmp);
break;
}
count++;
}
}
31 changes: 31 additions & 0 deletions libftp/src/path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# include <libftp.h>

char *ft_path(char *name)
{
if (name)
{
if (ft_strlen(name) == 1)
{
if (ft_strnequ(name, "~", 1))
{
return (ft_get_environ("HOME"));
}
else if (ft_strnequ(name, "-", 1))
{
return (ft_get_environ("OLDPWD"));
}
}
else
{
if (!ft_strchr(name, '/'))
{
return (ft_strjoin("./", name));
}
}
}
else
{
name = ft_path("~");
}
return (name);
}
2 changes: 1 addition & 1 deletion src/client/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void ft_check_exit(char *line)
if (line)
{
argv = ft_get_argv(line);
if (ft_strequ(argv[0], "exit"))
if (ft_strequ(argv[0], "quit"))
{
exit(EXIT_SUCCESS);
}
Expand Down
51 changes: 28 additions & 23 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,43 @@ int main(int argc, char **argv)
while ((cmd = ft_wreadline()))
{

/*
** set buff with cmd contents, free cmd for next read
*/
if (ft_strlen(cmd) > 0)
{

ft_fill_buffer(buff, cmd);
/*
** set buff with cmd contents, free cmd for next read
*/

/*
** check exit
*/
ft_fill_buffer(buff, cmd);

ft_check_exit(buff);
/*
** check exit
*/

/*
** write buff to server socket
*/
ft_check_exit(buff);

ft_wwriten(sockfd, buff, MAXLINE);
/*
** write buff to server socket
*/

/*
** clean buff
*/
ft_wwriten(sockfd, buff, MAXLINE);

ft_bzero(buff, MAXLINE);
/*
** clean buff
*/

/*
** read server response into buff
*/
ft_bzero(buff, MAXLINE);

if (ft_wreadn(sockfd, buff, MAXLINE))
{
ft_putstr(buff);
ft_putstr("\n");
/*
** read server response into buff
*/

if (ft_wreadn(sockfd, buff, MAXLINE))
{
ft_putstr(buff);
ft_putstr("\n");
}

}
ft_display_prompt();
}
Expand Down
53 changes: 53 additions & 0 deletions src/server/cd.c
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# include <ft_p.h>

/*
** Change the server directory
*/

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

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

/*
** check if there is a second argument
*/

if ((argv + 1))
{

/*
** check path for ~ or -
*/

path = ft_path(argv[1]);

/*
** attempt to chang the directory
*/

if (!ft_wchdir(path))
{
resp = ft_strdup("COMPLETE");
}
else
{
resp = ft_strdup("INCOMPLETE");
}

/*
** set the buffer to be used for client response
*/

ft_fill_buffer(buff, resp);
}
}
else
{
ft_err_quit("ft_cd fail");
}
}
34 changes: 14 additions & 20 deletions src/server/handle_request.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
# 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)
Expand All @@ -42,10 +23,23 @@ void ft_handle_request(char *line, int connfd, char *root)

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

/*
** check if a built in function
*/

if (ft_strequ(argv[0], "pwd"))
ft_pwd(buff);
else if (ft_strequ(argv[0], "cd"))
ft_cd(buff, root, argv);
else if (ft_strequ(argv[0], "ls"))
ft_ls(buff);
else
ft_invalid(buff);

/*
** using buff send response to client
*/

ft_send_response(buff, connfd);
}
Loading

0 comments on commit d7c1517

Please sign in to comment.