<commit_before>#include "ftp.h"

int INIT_SEED = 0;
int PASSIVE_PIPE_FD[2] = {0, 0};
int CONN_FD = 0;

int parse_command(char* command)
{
    // Remove "\r\n" from command
    command = strtok(command, "\r\n");
    // Split string on " "
    char* token = strsep(&command, " ");
    // Print result
    //printf("Token: %s\nArgument: %s\n", token, command);

    char* return_msg = NULL;
    if(!strncmp(token, "USER", 4)) {
        return_msg = response_msg(331, "Whatever user ;)");
    } else if(!strncmp(token, "PASS", 4)) {
        return_msg = response_msg(231, "Whatever pass ;)");
    } else if(!strncmp(token, "PWD", 3) || !strncmp(token, "XPWD", 4)) {
        char cwd[1024];
        if(getcwd(cwd, sizeof(cwd)) != NULL) {
            asprintf(&return_msg, "\"%s\" is the current working directory", cwd);
            return_msg = response_msg(257, return_msg);
        } else {
            return_msg = response_msg(451, "Could not get current working directory");
        }
    } else if(!strncmp(token, "CWD", 3)) {
        if(chdir(command) == -1) {
            return_msg = response_msg(451, strerror(errno));
        } else {
            return_msg = response_msg(250, "OK");
        }
    } else if(!strncmp(token, "PASV", 4)){
        int childpid;
        int port = random_number(1024, 65535);
        char* current_ip = get_socket_ip(CONN_FD);
        
        if(current_ip == NULL) {
            return -1;
        }

        // The child will start a new process, to handle data connection
        // from passive mode
        pipe(PASSIVE_PIPE_FD);
        if ((childpid = fork()) == 0) {
            return start_passive_mode(INADDR_ANY, port);
        }

        asprintf(&return_msg, "Entering Passive Mode (%s,%s,%s,%s,%d,%d)",
                 strsep(&current_ip, "."), strsep(&current_ip, "."),
                 strsep(&current_ip, "."), strsep(&current_ip, "."),
                 port / 256, port % 256);
        
        return_msg = response_msg(227, return_msg);
    } else if (!strncmp(token, "LIST", 4)) {
        set_passive_mode_operation(LIST);
        return_msg = response_msg(150, "BINARY data connection established");
    } else if (!strncmp(token, "SYST", 4)) {
        return_msg = response_msg(215, "UNIX Type: L8");
    } else if(!strncmp(token, "QUIT", 4)) {
        return_msg = response_msg(221, "Bye bye T-T...");
        return 1;
    }
    else {
        return_msg = response_msg(500, "Command not found");
    }

    printf("SERVER RESPONSE: %s", return_msg);
    write(CONN_FD, return_msg, strlen(return_msg));<commit_msg>Added some unimplemented modes for better error check<commit_after>#include "ftp.h"

int INIT_SEED = 0;
int PASSIVE_PIPE_FD[2] = {0, 0};
int CONN_FD = 0;

int parse_command(char* command)
{
    // Remove "\r\n" from command
    command = strtok(command, "\r\n");
    // Split string on " "
    char* token = strsep(&command, " ");
    // Print result
    //printf("Token: %s\nArgument: %s\n", token, command);

    char* return_msg = NULL;
    if(!strncmp(token, "USER", 4)) {
        return_msg = response_msg(331, "Whatever user ;)");
    } else if(!strncmp(token, "PASS", 4)) {
        return_msg = response_msg(231, "Whatever pass ;)");
    } else if(!strncmp(token, "PWD", 3) || !strncmp(token, "XPWD", 4)) {
        char cwd[1024];
        if(getcwd(cwd, sizeof(cwd)) != NULL) {
            asprintf(&return_msg, "\"%s\" is the current working directory", cwd);
            return_msg = response_msg(257, return_msg);
        } else {
            return_msg = response_msg(451, "Could not get current working directory");
        }
    } else if(!strncmp(token, "CWD", 3)) {
        if(chdir(command) == -1) {
            return_msg = response_msg(451, strerror(errno));
        } else {
            return_msg = response_msg(250, "OK");
        }
    } else if(!strncmp(token, "PASV", 4)){
        int childpid;
        int port = random_number(1024, 65535);
        char* current_ip = get_socket_ip(CONN_FD);
        
        if(current_ip == NULL) {
            return -1;
        }

        // The child will start a new process, to handle data connection
        // from passive mode
        pipe(PASSIVE_PIPE_FD);
        if ((childpid = fork()) == 0) {
            return start_passive_mode(INADDR_ANY, port);
        }

        asprintf(&return_msg, "Entering Passive Mode (%s,%s,%s,%s,%d,%d)",
                 strsep(&current_ip, "."), strsep(&current_ip, "."),
                 strsep(&current_ip, "."), strsep(&current_ip, "."),
                 port / 256, port % 256);
        
        return_msg = response_msg(227, return_msg);
    } else if(!strncmp(token, "LIST", 4)) {
        set_passive_mode_operation(LIST);
        return_msg = response_msg(150, "BINARY data connection established");
    } else if(!strncmp(token, "SYST", 4)) {
        return_msg = response_msg(215, "UNIX Type: L8");
    } else if(!strncmp(token, "PORT", 4)) {
        return_msg = response_msg(502, "This server doesn't support active mode");
    } else if(!strncmp(token, "LPRT", 4) || !strncmp(token, "LPSV", 4)) {
        return_msg = response_msg(502, "This server doesn't support long mode");
    } else if(!strncmp(token, "QUIT", 4)) {
        return_msg = response_msg(221, "Bye bye T-T...");
        return 1;
    } else {
        return_msg = response_msg(500, "Command not found");
    }

    printf("SERVER RESPONSE: %s", return_msg);
    write(CONN_FD, return_msg, strlen(return_msg));
{'fraction_non_alphanumeric': 0.15759678597516436, 'fraction_numerical': 0.027574872169466764, 'mean_word_length': 2.479669631512071, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1471069', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1815, 'n_words': 429}