Skip to content

Commit

Permalink
group similar cmd logic
Browse files Browse the repository at this point in the history
  • Loading branch information
selbetar committed Apr 9, 2020
1 parent 3ccd636 commit 4fd0ddc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
64 changes: 48 additions & 16 deletions ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,54 @@ int main (int argc, char *argv[])

int scode = execute_command (command, parameter);

if (scode == -1) {
// flush();
if (scode <= 0) {
cleanup();
break;
}
}
return 0;
}

// returns -1 on error [connection need to be closed and client]
// returns 0 on conncetion closure [client exit with no error]
// returns 1 otherwise
int execute_command (const char *command, const char *parameter)
{
if (strcasecmp (command, "quit") == 0) {
// todo
if (check_argc (parameter, 0))
return quit_cmd();
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "user") == 0) {
if (check_argc (parameter, 1))
return user_cmd (parameter);
return two_cmd ("user", parameter);
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "pass") == 0) {
if (check_argc (parameter, 1))
return pass_cmd (parameter);
return two_cmd ("pass", parameter);
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "get") == 0) {
// todo
}
else if (strcasecmp (command, "features") == 0) {
// todo
if (check_argc (parameter, 0))
return one_cmd("feat");
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "cd") == 0) {
// todo
if (check_argc (parameter, 1))
return two_cmd ("cwd", parameter);
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "nlist") == 0) {
// todo
}
else if (strcasecmp (command, "pwd") == 0) {
// todo
if (check_argc (parameter, 0))
return one_cmd("pwd");
return printErrorInvalidParameter (stdout, parameter);
}
else if (strcasecmp (command, "put") == 0) {
// todo
Expand All @@ -105,7 +116,7 @@ int execute_command (const char *command, const char *parameter)
// todo
}
else {
printErrorInvalidCommand (stdout, command);
return printErrorInvalidCommand (stdout, command);
}
return 0;
}
Expand All @@ -115,11 +126,11 @@ int execute_command (const char *command, const char *parameter)
* length of server response to the msg
* -1 on error
* */
int user_cmd (const char *parameter)
int one_cmd (const char *cmd)
{
char buffer[BUFSIZ];
memset (buffer, 0, BUFSIZ);
snprintf (buffer, BUFSIZ, "%s%s", "user ", parameter);
snprintf (buffer, BUFSIZ, "%s", cmd);

ssize_t length = send_msg (buffer);

Expand All @@ -129,11 +140,11 @@ int user_cmd (const char *parameter)
return read_response (buffer);
}

int pass_cmd (const char *parameter)
int two_cmd (const char *cmd, const char *param)
{
char buffer[BUFSIZ];
memset (buffer, 0, BUFSIZ);
snprintf (buffer, BUFSIZ, "%s%s", "pass ", parameter);
snprintf (buffer, BUFSIZ, "%s%s%s", cmd, " ", param);

ssize_t length = send_msg (buffer);

Expand All @@ -142,6 +153,23 @@ int pass_cmd (const char *parameter)

return read_response (buffer);
}

int quit_cmd()
{
char buffer[BUFSIZ];
memset (buffer, 0, BUFSIZ);
snprintf (buffer, BUFSIZ, "%s", "quit");

ssize_t length = send_msg (buffer);

if (length < 0)
return -1;

read_response (buffer);
cleanup();
return 0;
}

/*
* returns -1 on error, number of bytes sent otherwise
*/
Expand Down Expand Up @@ -211,6 +239,10 @@ int check_argc (const char *args, unsigned int exepcted)
// {
// }

// void flush()
// {
// }
void cleanup()
{
if (sfd != -1) {
close (sfd);
sfd = -1;
}
}
12 changes: 7 additions & 5 deletions ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ typedef struct pasv_response {

} pasvr_t;
int execute_command (const char *command, const char *parameter);
int user_cmd(const char* parameter);
int pass_cmd(const char* parameter);
int get_cmd(const char* file);
int one_cmd(const char* parameter);
int two_cmd(const char* cmd, const char* parameter);
int quit_cmd ();
int feat_cmd();
int cd_cmd(const char* path);
int pwd_cmd();

int get_cmd(const char* file);

int pasv_cmd();


int read_response (char *buf);
int send_msg (const char *msg);

int check_argc (const char *args, unsigned int exepcted);
void cleanup();
#endif
2 changes: 1 addition & 1 deletion network.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int create_connection (const char *host, const char *port)
fprintf (stderr, "Could not connect\n");
return -1;
}

freeaddrinfo (result); /* No longer needed */

return sfd;
Expand Down

0 comments on commit 4fd0ddc

Please sign in to comment.