This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
94 lines (84 loc) · 2.12 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
** init.c for C in /home/delaco_c/rendu/PSU_2015_lemipc
**
** Made by Delacour Benjamin
** Login <delaco_c@epitech.net>
**
** Started on Tue Mar 22 18:20:17 2016 Delacour Benjamin
** Last update Fri Mar 25 17:53:39 2016 Delacour Benjamin
*/
#include "lemipc.h"
static void catch_sigint(int sign)
{
if (sign == SIGINT)
g_exit = 1;
}
static void init_map(struct s_case *map)
{
int i;
i = 0;
while (i < WIDTH * HEIGHT)
{
map[i].x = i % WIDTH;
map[i].y = i / WIDTH;
map[i].nteam = 0;
i++;
}
}
static void init_player(const key_t key, t_ipcs *ipcs, const int team)
{
void *addr;
if ((addr = shmat(ipcs->shm_id, NULL, SHM_R | SHM_W)) == (void *)-1)
return ;
if ((ipcs->sem_id = semget(key, 1, SHM_R | SHM_W)) == -1)
return ;
if ((ipcs->msg_id = msgget(key, SHM_R | SHM_W)) == -1)
return ;
ipcs->sops.sem_num = 0;
ipcs->sops.sem_op = -1;
ipcs->sops.sem_flg = 0;
play(addr, team, ipcs);
}
static void *first_init(const key_t key, t_ipcs *ipcs)
{
void *addr;
if (signal(SIGINT, catch_sigint) == SIG_ERR)
return (NULL);
if ((ipcs->shm_id = shmget(key, WIDTH * HEIGHT * sizeof(struct s_case),
IPC_CREAT | SHM_R | SHM_W)) == -1)
return (NULL);
if ((ipcs->sem_id = semget(key, 1, IPC_CREAT | SHM_R | SHM_W)) == -1 ||
semctl(ipcs->sem_id, 0, SETVAL, 1) == -1)
return (NULL);
ipcs->sops.sem_num = 0;
ipcs->sops.sem_op = -1;
ipcs->sops.sem_flg = 0;
if ((ipcs->msg_id = msgget(key, IPC_CREAT | SHM_R | SHM_W)) == -1)
return (NULL);
if ((addr = shmat(ipcs->shm_id, NULL, SHM_R | SHM_W)) == (void *)-1)
return (NULL);
init_map(addr);
return (addr);
}
void init_lemipc(const char *path, const int team)
{
key_t key;
t_ipcs ipcs;
void *addr;
pid_t pid;
if ((key = ftok(path, 0)) == -1)
return ;
if ((ipcs.shm_id = shmget(key, WIDTH * HEIGHT * sizeof(struct s_case),
SHM_R | SHM_W)) == -1)
{
if ((addr = first_init(key, &ipcs)) == NULL ||
(pid = fork()) == -1)
return ;
if (pid == 0)
play(addr, team, &ipcs);
else
screen(addr, &ipcs);
}
else
init_player(key, &ipcs, team);
}