Skip to content

Commit

Permalink
Fix HELP command
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Mar 8, 2024
1 parent 752cc44 commit 4cfaf0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/messages/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ static char *clear_msg(const struct msg_s *msg, char *buffer)
return buffer;
}

static int compare(const char *buffer, const struct msg_s *msg)
{
return (buffer == NULL || msg == NULL || msg->cmd == NULL)
? 1
: strncmp(buffer, msg->cmd, strlen(msg->cmd));
}

static void data_fork_child(
client_t *client, const char *buffer,
void (*func)(int, client_t *, const char *))
Expand Down Expand Up @@ -79,16 +72,28 @@ void fork_data_sock(
}
}

static int compare(const char *buffer, const struct msg_s *msg)
{
return (buffer == NULL || msg == NULL || msg->cmd == NULL)
? 1
: strncmp(buffer, msg->cmd, strlen(msg->cmd));
}

struct msg_s *get_message(const char *cmd)
{
return bsearch(
cmd, INCOMMING_MSG, LEN_OF(INCOMMING_MSG), sizeof(*INCOMMING_MSG),
(int (*)(const void *, const void *))compare);
}

void client_process_message(client_t *client, char *buffer)
{
const struct msg_s *msg = NULL;

if (client == NULL || buffer == NULL)
return;
DEBUG("client said: %s", buffer);
msg = bsearch(
buffer, INCOMMING_MSG, LEN_OF(INCOMMING_MSG), sizeof(*INCOMMING_MSG),
(int (*)(const void *, const void *))compare);
msg = get_message(buffer);
if (msg == NULL) {
client_write(client, MSG_500);
return;
Expand Down
1 change: 1 addition & 0 deletions src/messages/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "myftp.h"

struct msg_s *get_message(const char *cmd);
void client_process_message(client_t *client, char *buffer);
bool client_logged(client_t *client);
void fork_data_sock(
Expand Down
26 changes: 19 additions & 7 deletions src/messages/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,31 @@ void msg_noop(client_t *client, UNUSED const char *buffer)
client_write(client, MSG_200);
}

void msg_help(client_t *client, UNUSED const char *buffer)
static const char *desc(const struct msg_s *msg)
{
static char buf[BUFSIZ];

if (msg == NULL || msg->help == NULL)
return "No description available";
strcpy(buf, msg->cmd);
strcat(buf, " - ");
strcat(buf, msg->help);
return buf;
}

void msg_help(client_t *client, const char *buffer)
{
const struct msg_s *msg = NULL;

if (client == NULL)
return;
if (buffer[0] != '\0') {
client_write(client, MSG_214, desc(get_message(buffer)));
return;
}
for (size_t i = 0; i < LEN_OF(INCOMMING_MSG); i++) {
msg = &INCOMMING_MSG[i];
if (strcmp(buffer, msg->cmd) != 0)
continue;
client_write(
client, MSG_214, (msg->help != NULL) ? msg->help : "(null)");
return;
if (msg->func != NULL)
client_write(client, MSG_214, desc(msg));
}
client_write(client, MSG_500);
}

0 comments on commit 4cfaf0a

Please sign in to comment.