2016-05-09 4 views
0

私は少しのFTPサーバーを作成し、いくつかのコマンドを用意しました。ここで私のFTPサーバーの<CRLF>を確認する方法

は、私はそれが私のリンクリストの私のコマンドのいずれかと一致するかどうかを確認するために、ユーザーからの入力をチェックする方法は次のとおりです。

int check_cmd(char *buff, char *cmd) 
{ 
    int end; 

    if (strstr(buff, cmd) != buff) 
    return (-1); 
    end = strlen(cmd); 
    if (buff[end] != '\0' && buff[end] != ' ' && buff[end] != '\n') 
    return (-1); 
    return (0); 
} 

void read_command(t_client *client, t_cmd *lexer) 
{ 
    t_cmd *current; 

    bzero(client->buff, MAX_READ + 1); 
    server_read(client); 
    current = lexer; 
    while (current != NULL) // Go through the linked list, checking if it matches 
    { 
     if (check_cmd(client->buff, current->cmd) == 0) // It matches a command ! 
     { 
      current->ptr(client); // Calls the appropriate function 
      return ; 
     } 
     current = current->next; 
    } 
    server_write(client, "Invalid command.\n"); 
} 

しかしnetcatため-Cオプションを使用すると、すべてのコマンドにデフォルトで\r\nをお送りしますまだ、私はそれをチェックしていません。

<CRLF>がコマンドラインで渡されるかどうかを確認するにはどうすればよいですか?

答えて

0

私が見る最初のものは、端部が実際にあるべきであるということである:Cにおける配列は0から始まるインデックスでアクセスされるため

end = strlen(cmd) - 1; 

はない1

はの終わりにするためにチェックしますstring end - 1が '\ r'で終わりが '\ n'であるかどうかを調べる:

if(buff[end-1] == '\r' && buff[end] == '\n') 
{ 
    // do something 
} 
関連する問題