This commit is contained in:
HexPloR
2024-06-23 23:59:58 +02:00
parent d5bc369618
commit 60eef6cebe
98 changed files with 2196 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 16:51:00 by yantoine #+# #+# */
/* Updated: 2024/05/17 16:13:47 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void pausing(int signum)
{
(void)signum;
}
static void send_bits(int pid, char c)
{
int bit;
usleep(50);
bit = 0;
while (bit < 8)
{
if ((c & (0x01 << bit)) != 0)
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
signal(SIGUSR1, pausing);
pause();
bit++;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 3 || !ft_strlen(argv[2]) || ft_atoi(argv[1]) <= 0)
return (1);
i = 0;
while (argv[2][i] != '\0')
{
send_bits(ft_atoi(argv[1]), argv[2][i]);
i++;
}
send_bits(ft_atoi(argv[1]), '\n');
send_bits(ft_atoi(argv[1]), '\0');
}
+127
View File
@@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <hexplor@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 22:48:59 by yantoine #+# #+# */
/* Updated: 2024/05/13 21:22:13 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
char *g_str;
static void print_hello(void)
{
ft_printf(" , ----.\n");
ft_printf(" - - `\n");
ft_printf(" ,__.,' \\\n");
ft_printf(" .' *`\\\n");
ft_printf(" / | | / **\\\n");
ft_printf(" . / ****.\n");
ft_printf(" | mm | ****|\n");
ft_printf(" \\ | ****|\n");
ft_printf(" ` ._______ \\ ****/\n");
ft_printf(" \\ /`---'\n");
ft_printf(" \\___(\n");
ft_printf(" /~~~~\\\n");
ft_printf(" / \\\n");
ft_printf(" / | \\\n");
ft_printf(" | | \\\n");
ft_printf(" , ~~ . |, ~~ . | |\\\n");
ft_printf(" ( |||| ) ( |||| )(,,,)`\n");
ft_printf(" ( |||||| )-( |||||| ) | ^\n");
ft_printf(" ( |||||| ) ( |||||| ) |'/\n");
ft_printf(" ( |||||| )-( |||||| )___,'-\n");
ft_printf(" ( |||| ) ( |||| )\n");
ft_printf(" ` ~~ ' ` ~~ '\n");
}
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
static char *add_char_to_string(char *str, char c)
{
int len;
char *new_str;
len = 0;
new_str = NULL;
if (str == NULL)
{
str = (char *)malloc(2 * sizeof(char));
str[0] = c;
str[1] = '\0';
}
else
{
len = ft_strlen(str);
new_str = (char *)malloc((len + 2) * sizeof(char));
ft_strcpy(new_str, str);
new_str[len] = c;
new_str[len + 1] = '\0';
free(str);
str = new_str;
}
return (str);
}
static void action(int signum, siginfo_t *info, void *context)
{
static int current_byte;
static int bit_count;
(void)context;
if (signum == SIGUSR1)
current_byte |= (0x01 << bit_count);
bit_count++;
if (bit_count == 8)
{
if (current_byte == '\0')
{
ft_printf("%s", g_str);
free(g_str);
g_str = NULL;
kill(info->si_pid, SIGUSR1);
current_byte = 0;
bit_count = 0;
return ;
}
g_str = add_char_to_string(g_str, current_byte);
current_byte = 0;
bit_count = 0;
}
usleep(50);
kill(info->si_pid, SIGUSR1);
}
int main(void)
{
struct sigaction sa;
print_hello();
sa.sa_sigaction = action;
sa.sa_flags = SA_SIGINFO;
ft_printf("pid: %d\n", getpid());
while (1)
{
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
pause();
}
return (0);
}