-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08ae4cc
commit 7cbd052
Showing
12 changed files
with
274 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# include <ft_p.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |