From 16092c007e8767b32c107d74d3ea3d238f5e65ec Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 18:10:14 +0100 Subject: Preparing forks --- src/ft_exit.c | 5 +---- src/ft_music.c | 14 ++++++-------- src/ft_treat_args.c | 4 ++-- src/ft_warp_level.c | 9 ++------- 4 files changed, 11 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/ft_exit.c b/src/ft_exit.c index ca5201c..c7b2275 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -16,8 +16,6 @@ #include #include #include -#include -#include static void ft_free_lists(t_cub *clist) @@ -67,8 +65,7 @@ int } if (clist->isoldmus && clist->wlist.inited) { - pthread_cancel(clist->tid); - pthread_join(clist->tid, NULL); + /* kill fork() here */ } ft_free_lists(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_music.c b/src/ft_music.c index aec5e58..b9e9d89 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -14,17 +14,15 @@ #include #include #include -#include void - *ft_music_thread(void *vargp) + ft_music_fork(t_cub *cl) { - t_cub *cl; - - cl = (t_cub *)vargp; cl->isoldmus = 1; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); while (1) - system(cl->mlist.music_cmd); - return (NULL); + { + ft_printf("qweqwe\n"); + } + /* system(cl->mlist.music_cmd); */ + /* execve here */ } diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index 2571709..b588b46 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -14,7 +14,6 @@ #include #include #include -#include #include uint8_t @@ -38,7 +37,8 @@ uint8_t return (ft_exit(FT_RET_FAILED_MLX, clist)); ft_draw_scene(clist); if (clist->mlist.ismusic) - pthread_create(&clist->tid, NULL, ft_music_thread, clist); + ft_music_fork(cl); + /* create music fork() here */ ft_hooks_and_loops(&clist->wlist, clist); } else if (argc == 3 && !ft_strncmp("--save", argv[2], 7)) diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 808b346..4d1ca12 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -16,8 +16,6 @@ #include #include #include -#include -#include static void ft_del_map(t_map *ml) @@ -66,16 +64,13 @@ static void { if (isoldmus && !cl->mlist.ismusic) { - pthread_cancel(cl->tid); - pthread_join(cl->tid, NULL); + /* stop fork() here */ cl->isoldmus = 0; } else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - pthread_cancel(cl->tid); - pthread_join(cl->tid, NULL); - pthread_create(&cl->tid, NULL, ft_music_thread, cl); + /* stop and recreate fork() here */ } else if (isoldmus && cl->mlist.ismusic && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) -- cgit v1.2.3 From a35b258841cde7884f5bb71d5ae0951d5e3f27b7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 19:03:24 +0100 Subject: fork(3) going well, freed a leak --- src/ft_exit.c | 3 ++- src/ft_music.c | 10 ++++++---- src/ft_parse_map.c | 10 +++++----- src/ft_treat_args.c | 7 ++++++- src/ft_warp_level.c | 1 + 5 files changed, 20 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/ft_exit.c b/src/ft_exit.c index c7b2275..3ba1237 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,6 +15,7 @@ #include #include #include +#include #include static void @@ -65,7 +66,7 @@ int } if (clist->isoldmus && clist->wlist.inited) { - /* kill fork() here */ + kill(clist->mpid, SIGTERM); } ft_free_lists(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_music.c b/src/ft_music.c index b9e9d89..70bb1a1 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -12,17 +12,19 @@ #include #include -#include #include +#include + void - ft_music_fork(t_cub *cl) + ft_music_fork(char *music_cmd) { - cl->isoldmus = 1; + (void)music_cmd; while (1) { ft_printf("qweqwe\n"); + sleep(1); } - /* system(cl->mlist.music_cmd); */ /* execve here */ + /* system(cl->mlist.music_cmd); */ } diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 3c1fd9c..36d1ec5 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -75,13 +75,13 @@ static int8_t } void - ft_save_name(const char *map_path, t_map *mlist, t_cub *clist) + ft_save_name(const char *map_path, t_cub *clist) { - ft_memdel((void**)&mlist->filename); - if (!(mlist->filename = + ft_memdel((void**)&clist->mlist.filename); + if (!(clist->mlist.filename = (char*)malloc((ft_strlen(map_path) + 1) * sizeof(char)))) ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist); - ft_sprintf(mlist->filename, "%s", map_path); + ft_sprintf(clist->mlist.filename, "%s", map_path); } void @@ -95,7 +95,7 @@ void fd = open(map_path, O_RDONLY); if (fd < 0) ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist); - ft_save_name(map_path, &clist->mlist, clist); + ft_save_name(map_path, clist); ret = 1; while (ret != 12 && ret >= 0) ret = ft_parse_it(fd, clist); diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index b588b46..accf3aa 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -37,8 +37,13 @@ uint8_t return (ft_exit(FT_RET_FAILED_MLX, clist)); ft_draw_scene(clist); if (clist->mlist.ismusic) - ft_music_fork(cl); + { /* create music fork() here */ + clist->isoldmus = 1; + clist->mpid = fork(); + if (clist->mpid == 0) + ft_music_fork(clist->mlist.music_cmd); + } ft_hooks_and_loops(&clist->wlist, clist); } else if (argc == 3 && !ft_strncmp("--save", argv[2], 7)) diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 4d1ca12..0d126c1 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -20,6 +20,7 @@ static void ft_del_map(t_map *ml) { + ft_memdel((void**)&ml->filename); ft_memdel((void**)&ml->no_tex_path); ft_memdel((void**)&ml->so_tex_path); ft_memdel((void**)&ml->ea_tex_path); -- cgit v1.2.3 From f3aab6429d673342f4819f8a3c2857e48610689a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 19:12:10 +0100 Subject: it's going well --- src/ft_exit.c | 2 ++ src/ft_music.c | 3 +-- src/ft_warp_level.c | 12 ++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ft_exit.c b/src/ft_exit.c index 3ba1237..973abca 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -16,6 +16,7 @@ #include #include #include +#include #include static void @@ -67,6 +68,7 @@ int if (clist->isoldmus && clist->wlist.inited) { kill(clist->mpid, SIGTERM); + wait(&clist->mpid); } ft_free_lists(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_music.c b/src/ft_music.c index 70bb1a1..4d25a3a 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -19,10 +19,9 @@ void ft_music_fork(char *music_cmd) { - (void)music_cmd; while (1) { - ft_printf("qweqwe\n"); + ft_printf("%s\n", music_cmd); sleep(1); } /* execve here */ diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 0d126c1..febff7e 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include static void @@ -65,13 +68,18 @@ static void { if (isoldmus && !cl->mlist.ismusic) { - /* stop fork() here */ + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); cl->isoldmus = 0; } else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - /* stop and recreate fork() here */ + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); + cl->mpid = fork(); + if (cl->mpid == 0) + ft_music_fork(cl->mlist.music_cmd); } else if (isoldmus && cl->mlist.ismusic && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) -- cgit v1.2.3 From 6a89966eed6110d0e7a7ec53727c4473d18ad441 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 20:43:42 +0100 Subject: Fuck execve --- src/ft_exit.c | 1 + src/ft_get_music.c | 11 +++++++---- src/ft_init_map.c | 5 ++++- src/ft_music.c | 22 ++++++++++++---------- src/ft_treat_args.c | 2 +- src/ft_warp_level.c | 11 ++++++----- 6 files changed, 31 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/ft_exit.c b/src/ft_exit.c index 973abca..be4d46e 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -34,6 +34,7 @@ static void ft_memdel((void**)&clist->mlist.music_cmd); ft_memdel((void**)&clist->mlist.mapl); ft_free_words(clist->mlist.map); + ft_free_words(clist->mlist.mcmd_words); if (!clist->wlist.inited) ft_memdel((void**)&clist->wlist.winptr); } diff --git a/src/ft_get_music.c b/src/ft_get_music.c index cdd7039..2383e0a 100644 --- a/src/ft_get_music.c +++ b/src/ft_get_music.c @@ -15,7 +15,7 @@ #include #include -static void +static int8_t ft_set_music_cmd(t_map *mlist) { uint8_t len; @@ -24,9 +24,13 @@ static void len += ft_strlen(FT_MUS_CMD) - 2; ft_memdel((void**)&mlist->music_cmd); if (!(mlist->music_cmd = (char *)malloc((len + 1) * sizeof(char)))) - return ; + return (-1); ft_sprintf(mlist->music_cmd, FT_MUS_CMD, mlist->music_path); + ft_free_words(mlist->mcmd_words); + if (!(mlist->mcmd_words = ft_split(mlist->music_cmd, ' '))) + return (-1); + return (0); } int8_t @@ -54,6 +58,5 @@ int8_t return (-1); } clist->mlist.ismusic = 1; - ft_set_music_cmd(&clist->mlist); - return (0); + return ((ft_set_music_cmd(&clist->mlist) == 0) ? (0) : (-1)); } diff --git a/src/ft_init_map.c b/src/ft_init_map.c index 4fa127c..1c12189 100644 --- a/src/ft_init_map.c +++ b/src/ft_init_map.c @@ -30,7 +30,9 @@ static int8_t !(mlist->music_cmd = (char*)ft_calloc(1, sizeof(char))) || !(mlist->mapl = (char*)ft_calloc(1, sizeof(char))) || !(mlist->map = (char**)ft_calloc(2, sizeof(char*))) || - !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char)))) + !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->mcmd_words = (char**)ft_calloc(2, sizeof(char*))) || + !(mlist->mcmd_words[0] = (char*)ft_calloc(1, sizeof(char)))) return (-1); return (0); } @@ -41,6 +43,7 @@ int8_t if (ft_init_map_callocs(mlist) < 0) return (-1); mlist->map[1] = 0; + mlist->mcmd_words[1] = 0; mlist->map_w = 0; mlist->map_h = 0; mlist->mapl_len = 0; diff --git a/src/ft_music.c b/src/ft_music.c index 4d25a3a..f1801f9 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,19 +11,21 @@ /* ************************************************************************** */ #include -#include +#include #include - #include void - ft_music_fork(char *music_cmd) + ft_music_fork(char **mcmd_words) { - while (1) - { - ft_printf("%s\n", music_cmd); - sleep(1); - } - /* execve here */ - /* system(cl->mlist.music_cmd); */ + /* execve(2) here */ + + char *arg[4]; + + arg[0] = "/bin/sh"; + arg[1] = "-c"; + arg[2] = "aplay ./media/sound/DEVANT-LES-KAISSONS.wav"; + arg[3] = NULL; + (void)mcmd_words; + execve("/bin/sh", &arg[0], NULL); } diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index accf3aa..c4ffc98 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -42,7 +42,7 @@ uint8_t clist->isoldmus = 1; clist->mpid = fork(); if (clist->mpid == 0) - ft_music_fork(clist->mlist.music_cmd); + ft_music_fork(clist->mlist.mcmd_words); } ft_hooks_and_loops(&clist->wlist, clist); } diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index febff7e..9bd8d92 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -35,6 +35,7 @@ static void ft_memdel((void**)&ml->music_cmd); ft_memdel((void**)&ml->mapl); ft_free_words(ml->map); + ft_free_words(ml->mcmd_words); } static void @@ -75,11 +76,11 @@ static void else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - kill(cl->mpid, SIGTERM); - wait(&cl->mpid); - cl->mpid = fork(); - if (cl->mpid == 0) - ft_music_fork(cl->mlist.music_cmd); + /* kill(cl->mpid, SIGTERM); */ + /* wait(&cl->mpid); */ + /* cl->mpid = fork(); */ + /* if (cl->mpid == 0) */ + /* ft_music_fork(cl->mlist.mcmd_words); */ } else if (isoldmus && cl->mlist.ismusic && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) -- cgit v1.2.3 From 874d8d3bbafb3d1919cfc6f21fdb780382f0d015 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 21:03:07 +0100 Subject: boy --- src/ft_music.c | 14 ++++++-------- src/ft_treat_args.c | 4 ++-- src/ft_warp_level.c | 4 ++-- src/main.c | 4 ++-- 4 files changed, 12 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ft_music.c b/src/ft_music.c index f1801f9..a3e2120 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -16,16 +16,14 @@ #include void - ft_music_fork(char **mcmd_words) + ft_music_fork(char **mcmd_words, char *const envp[]) { /* execve(2) here */ + char *arg[3]; - char *arg[4]; - - arg[0] = "/bin/sh"; - arg[1] = "-c"; - arg[2] = "aplay ./media/sound/DEVANT-LES-KAISSONS.wav"; - arg[3] = NULL; + arg[0] = "/usr/bin/aplay"; + arg[1] = "./media/sound/DEVANT-LES-KAISSONS.wav"; + arg[2] = NULL; (void)mcmd_words; - execve("/bin/sh", &arg[0], NULL); + execve("/usr/bin/aplay", &arg[0], envp); } diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index c4ffc98..79eedae 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -29,7 +29,7 @@ uint8_t } uint8_t - ft_use_args(int argc, const char *argv[], t_cub *clist) + ft_use_args(int argc, const char *argv[], char *const envp[], t_cub *clist) { if (argc < 3) { @@ -42,7 +42,7 @@ uint8_t clist->isoldmus = 1; clist->mpid = fork(); if (clist->mpid == 0) - ft_music_fork(clist->mlist.mcmd_words); + ft_music_fork(clist->mlist.mcmd_words, envp); } ft_hooks_and_loops(&clist->wlist, clist); } diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 9bd8d92..2721aca 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -76,8 +76,8 @@ static void else if (isoldmus && cl->mlist.ismusic && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) { - /* kill(cl->mpid, SIGTERM); */ - /* wait(&cl->mpid); */ + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); /* cl->mpid = fork(); */ /* if (cl->mpid == 0) */ /* ft_music_fork(cl->mlist.mcmd_words); */ diff --git a/src/main.c b/src/main.c index b55e051..43b482a 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ #include int - main(int argc, const char *argv[]) + main(int argc, const char *argv[], char *const envp[]) { t_cub clist; @@ -27,6 +27,6 @@ int if (ft_init_winlx(&clist) < 0) return (ft_exit(FT_RET_FAILED_MLX, &clist)); ft_wall_tex_init(&clist); - ft_use_args(argc, argv, &clist); + ft_use_args(argc, argv, envp, &clist); return (FT_RET_FINE); } -- cgit v1.2.3 From 3048aae5d3f50359f19e0d440376ae422a16a7b7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 21:47:09 +0100 Subject: Good enough --- src/ft_music.c | 25 +++++++++++++++---------- src/ft_treat_args.c | 9 ++++----- src/ft_warp_level.c | 4 +--- 3 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/ft_music.c b/src/ft_music.c index a3e2120..0667ecb 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -10,20 +10,25 @@ /* */ /* ************************************************************************** */ -#include -#include -#include +#include #include void ft_music_fork(char **mcmd_words, char *const envp[]) { - /* execve(2) here */ - char *arg[3]; + if (FT_OS == 1) + execve("/usr/bin/afplay", mcmd_words, envp); + else + execve("/usr/bin/aplay", mcmd_words, envp); +} - arg[0] = "/usr/bin/aplay"; - arg[1] = "./media/sound/DEVANT-LES-KAISSONS.wav"; - arg[2] = NULL; - (void)mcmd_words; - execve("/usr/bin/aplay", &arg[0], envp); +void + ft_enable_music(t_cub *cl) +{ + cl->isoldmus = 1; + cl->mpid = fork(); + if (cl->mpid == 0) + { + ft_music_fork(cl->mlist.mcmd_words, cl->envp); + } } diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c index 79eedae..a47f9f7 100644 --- a/src/ft_treat_args.c +++ b/src/ft_treat_args.c @@ -31,18 +31,17 @@ uint8_t uint8_t ft_use_args(int argc, const char *argv[], char *const envp[], t_cub *clist) { + clist->envp = envp; if (argc < 3) { if (ft_init_winptr(clist) < 0) + { return (ft_exit(FT_RET_FAILED_MLX, clist)); + } ft_draw_scene(clist); if (clist->mlist.ismusic) { - /* create music fork() here */ - clist->isoldmus = 1; - clist->mpid = fork(); - if (clist->mpid == 0) - ft_music_fork(clist->mlist.mcmd_words, envp); + ft_enable_music(clist); } ft_hooks_and_loops(&clist->wlist, clist); } diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c index 2721aca..37789a5 100644 --- a/src/ft_warp_level.c +++ b/src/ft_warp_level.c @@ -78,9 +78,7 @@ static void { kill(cl->mpid, SIGTERM); wait(&cl->mpid); - /* cl->mpid = fork(); */ - /* if (cl->mpid == 0) */ - /* ft_music_fork(cl->mlist.mcmd_words); */ + ft_enable_music(cl); } else if (isoldmus && cl->mlist.ismusic && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) -- cgit v1.2.3 From a14c6b476a36f68ba75eb4da651062bdbf8f0dc0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 21:47:52 +0100 Subject: Good --- src/ft_music.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/ft_music.c b/src/ft_music.c index 0667ecb..3104978 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -17,9 +17,13 @@ void ft_music_fork(char **mcmd_words, char *const envp[]) { if (FT_OS == 1) + { execve("/usr/bin/afplay", mcmd_words, envp); + } else + { execve("/usr/bin/aplay", mcmd_words, envp); + } } void -- cgit v1.2.3 From d68996487117fee33bd6301f66c06611e57f0c71 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Mar 2020 22:08:37 +0100 Subject: okok --- src/ft_music.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/ft_music.c b/src/ft_music.c index 3104978..c3b90b1 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -13,17 +13,11 @@ #include #include +#include void ft_music_fork(char **mcmd_words, char *const envp[]) { - if (FT_OS == 1) - { - execve("/usr/bin/afplay", mcmd_words, envp); - } - else - { - execve("/usr/bin/aplay", mcmd_words, envp); - } + execve(*(mcmd_words + 0), mcmd_words, envp); } void -- cgit v1.2.3