Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit de6be98

Browse files
committed
fix: coding style compliance
1 parent 36f40a1 commit de6be98

5 files changed

Lines changed: 67 additions & 51 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CFLAGS += -Wshadow
1515
CFLAGS += -Wunreachable-code
1616
CFLAGS += -Wmissing-prototypes
1717
CFLAGS += -pedantic
18-
CFLAGS += -I./include/ -g3 -O0
18+
CFLAGS += -I./include/
1919
BDIR = .build
2020

2121
NAME = myftp

‎include/network.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
void write_msg(const client_t *client, const char *code, const char *fmt,
1414
...);
1515
void write_templated_msg(const client_t *client, const char *template);
16-
16+
void parse_client_input(client_t *client);
17+
void process_client(client_t *client);
1718

1819
void close_data_sock(client_t *client);
1920
int accept_data_sock(client_t *client);

‎include/server.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ typedef struct {
5555
} command_t;
5656

5757
int init_server(server_t *server);
58+
bool client_cmd_handler(const command_t *command, const char *buffer,
59+
client_t *client);
5860
void write_msg_to_client(int client_control_fd, const char *str);
5961
void events_loop(server_t *server);
6062

‎src/network/input_buffer.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** myftp
4+
** File description:
5+
** input_buffer.c
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
#include "network.h"
12+
13+
static void parse_client_buffer(client_t *client, char *buff_not_read)
14+
{
15+
const size_t buff_len = strlen(client->cmd_buffer);
16+
size_t idx = 0;
17+
18+
for (; idx < buff_len; idx++) {
19+
if (client->cmd_buffer[idx] == '\r' && idx + 1 < buff_len
20+
&& client->cmd_buffer[idx + 1] == '\n') {
21+
idx += 2;
22+
strncpy(client->cmd, client->cmd_buffer, idx - 1);
23+
client->cmd[idx - 1] = '\0';
24+
client->cmd[idx - 2] = '\0';
25+
client->should_be_processed = true;
26+
break;
27+
}
28+
}
29+
if (idx < buff_len)
30+
strcat(buff_not_read, &client->cmd_buffer[idx]);
31+
if (!client->should_be_processed)
32+
strcat(buff_not_read, client->cmd_buffer);
33+
}
34+
35+
void parse_client_input(client_t *client)
36+
{
37+
char buff_not_read[BUFSIZ] = {0};
38+
39+
memset(client->cmd, 0, BUFSIZ);
40+
parse_client_buffer(client, buff_not_read);
41+
memset(client->cmd_buffer, 0, BUFSIZ);
42+
strcpy(client->cmd_buffer, buff_not_read);
43+
client->cmd_buffer_offset = strlen(buff_not_read);
44+
}
45+
46+
void process_client(client_t *client)
47+
{
48+
bool cmd_found = false;
49+
50+
for (size_t i = 0; i < COMMANDS_SIZE && !cmd_found; i++)
51+
if (client_cmd_handler(&COMMANDS[i], client->cmd, client) == true) {
52+
cmd_found = true;
53+
}
54+
if (!cmd_found)
55+
write_msg_to_client(client->control_fd, "500 Syntax Error");
56+
client->should_be_processed = false;
57+
parse_client_input(client);
58+
}

‎src/server.c‎

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static void searching_new_clients(server_t *server)
5757
}
5858
}
5959

60-
static bool client_cmd_handler(const command_t *command, const char *buffer,
60+
bool client_cmd_handler(const command_t *command, const char *buffer,
6161
client_t *client)
6262
{
6363
if (strncasecmp(command->name, buffer, strlen(command->name)) != 0)
@@ -74,60 +74,15 @@ static bool client_cmd_handler(const command_t *command, const char *buffer,
7474
return true;
7575
}
7676

77-
static void parse_client_buffer(client_t *client, char *buff_not_read)
78-
{
79-
80-
const size_t buff_len = strlen(client->cmd_buffer);
81-
size_t idx = 0;
82-
83-
for (; idx < buff_len; idx++) {
84-
if (client->cmd_buffer[idx] == '\r' && idx + 1 < buff_len && client->cmd_buffer[idx + 1] == '\n') {
85-
idx += 2;
86-
strncpy(client->cmd, client->cmd_buffer, idx - 1);
87-
client->cmd[idx - 1] = '\0';
88-
client->cmd[idx - 2] = '\0';
89-
client->should_be_processed = true;
90-
break;
91-
}
92-
}
93-
if (idx < buff_len)
94-
strcat(buff_not_read, &client->cmd_buffer[idx]);
95-
if (!client->should_be_processed)
96-
strcat(buff_not_read, client->cmd_buffer);
97-
}
98-
99-
static void parse_client_input(client_t *client)
100-
{
101-
char buff_not_read[BUFSIZ] = {0};
102-
103-
client->should_be_processed = false;
104-
memset(client->cmd, 0, BUFSIZ);
105-
parse_client_buffer(client, buff_not_read);
106-
memset(client->cmd_buffer, 0, BUFSIZ);
107-
strcpy(client->cmd_buffer, buff_not_read);
108-
client->cmd_buffer_offset = strlen(buff_not_read);
109-
//printf("[INFO] Current command: %s\n", client->cmd);
110-
//printf("[INFO] Command buffer: %s\n", client->cmd_buffer);
111-
}
112-
11377
static void reply_client(client_t *client)
11478
{
115-
bool cmd_found = false;
116-
117-
//printf("Writing in buffer with offset of: %lu\n", client->cmd_buffer_offset);
11879
if (read(client->control_fd, client->cmd_buffer +
11980
client->cmd_buffer_offset, BUFSIZ - client->cmd_buffer_offset) < 1)
12081
return;
121-
printf("[ <- ] '%s'\n", client->cmd_buffer);
82+
printf("[ <- ] %s", client->cmd_buffer);
12283
parse_client_input(client);
123-
while (client->should_be_processed) {
124-
for (size_t i = 0; i < COMMANDS_SIZE && !cmd_found; i++)
125-
if (client_cmd_handler(&COMMANDS[i], client->cmd, client) == true)
126-
cmd_found = true;
127-
if (!cmd_found)
128-
write_msg_to_client(client->control_fd, "500 Syntax Error");
129-
parse_client_input(client);
130-
}
84+
while (client->should_be_processed)
85+
process_client(client);
13186
}
13287

13388
void events_loop(server_t *server)

0 commit comments

Comments
 (0)