Improved cleanup on close

This commit is contained in:
Daniele Lacamera 2024-11-28 11:43:39 +00:00
parent 32be1bc25b
commit 7aace4e904

View file

@ -73,6 +73,19 @@ void load_slide_content(const char *slides_path, char *output) {
close(fd);
}
/* Clean up client resources */
void cleanup_client(struct Client *client) {
if (client->fd != -1) {
close(client->fd);
client->fd = -1;
}
if (strlen(client->repo_path) > 0) {
char rm_command[CMD_SIZE];
snprintf(rm_command, sizeof(rm_command), "rm -rf %s", client->repo_path);
system(rm_command);
}
}
/* Handle client input and update slides */
void handle_client_input(struct Client *client, char command) {
if (command == ' ') {
@ -81,8 +94,7 @@ void handle_client_input(struct Client *client, char command) {
execute_git_slides(client->repo_path, "prev");
} else if (command == 'q') {
clear_screen(client->fd);
close(client->fd);
client->fd = -1; /* Mark as disconnected */
cleanup_client(client);
return;
} else {
snprintf(client->current_slide, BUFFER_SIZE, "Unknown command '%c'. Use SPACE, BACKSPACE, or Q.\n", command);
@ -101,18 +113,6 @@ void send_welcome_message(struct Client *client) {
write(client->fd, client->current_slide, strlen(client->current_slide));
}
/* Clean up client resources */
void cleanup_client(struct Client *client) {
if (client->fd != -1) {
close(client->fd);
client->fd = -1;
}
if (strlen(client->repo_path) > 0) {
char rm_command[CMD_SIZE];
snprintf(rm_command, sizeof(rm_command), "rm -rf %s", client->repo_path);
system(rm_command);
}
}
int main() {
int server_fd;