-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoL_main.c
97 lines (83 loc) · 1.86 KB
/
GoL_main.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
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include "GoL_pattern.h"
#include "GoL_func.h"
#define ARG_ERR -1
#define MALLOC_ERR -2
#define SIGNAL_ERR -3
#define AUTHOR "Alfredo Gonzalez Calvin"
extern char world[M_ROW][M_COL];
extern int aux_world[M_ROW][M_COL];
extern const char kernel[K_ROW][K_COL];
extern const char *entities[MAX_ENTITIES];
int end_proccess = 0;
void clean_terminal()
{
printf("\033[2J\033[1;1H");
}
void sig_int(int arg)
{
end_proccess = 1;
}
void sig_term(int arg)
{
end_proccess = 1;
}
void print_author()
{
printf("Game of Life. %s (Ctrl + C to quit)", AUTHOR);
}
int main(int argc, char *argv[])
{
srand(time(NULL));
struct sigaction sa_int, sa_term;
if(argc != 2){
fprintf(stderr, "Usage: %s <number_of_entities>\n", argv[0]);
return ARG_ERR;
}
/* One handler per signal */
sa_int.sa_handler = sig_int; sa_term.sa_handler = sig_term;
sigemptyset(&sa_int.sa_mask); sigemptyset(&sa_term.sa_mask);
sa_int.sa_flags = 0; sa_term.sa_flags = 0;
if(sigaction(SIGINT, &sa_int, NULL) == -1){
perror("Sig_int() error ");
return SIGNAL_ERR;
}
if(sigaction(SIGTERM, &sa_term, NULL) == -1){
perror("Sig_term() error ");
return SIGNAL_ERR;
}
struct timespec *req = malloc(sizeof(struct timespec));
if(req == NULL){
fprintf(stderr, "malloc() error\n");
return MALLOC_ERR;
}
/* Sleep for tv_sec + tv_nsec */
req->tv_sec = 0;
req->tv_nsec = 90000000L;
init_world(atoi(argv[1]));
while(!end_proccess){
clean_terminal();
show_world();
print_border();
print_author();
fflush(stdout);
world_step();
if(nanosleep(req, NULL) == -1){
int errsv = errno;
if((errsv == EFAULT) || (errsv == EINVAL)){
perror("Error with nanosleep ");
free(req);
return -1;
}
/* Else: we sent signal */
}
}
clean_terminal();
free(req);
return 0;
}