-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex2_inp.c
143 lines (125 loc) · 3.42 KB
/
ex2_inp.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/******************************************
* Student name: Atara Razin
* Student: 327348934
* Course Exercise Group: 8923106
* Exercise name: Exercise 2
******************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#define SIZE 16
void sigIntHandler(int sig);
void signal_hand(int sig);
/***********************************************************************
* function name: main
* input: none
* output: returns 0 if it gets to the end
* operation: makes the sigaction and defines the masks for sigint and sigusr1.
* pauses the program until it gets a sig int.
*************************************************************************/
int main()
{
struct sigaction usr_action;
sigset_t block_mask;
struct sigaction usr_action1;
sigset_t block_mask1;
//for sigusr1
sigfillset (&block_mask);
sigdelset(&block_mask,SIGINT);
usr_action.sa_handler = signal_hand;
usr_action.sa_mask = block_mask;
usr_action.sa_flags = 0;
//for sigint
sigfillset(&block_mask1);
usr_action1.sa_handler = sigIntHandler;
usr_action1.sa_mask = block_mask1;
usr_action1.sa_flags = 0;
sigaction (SIGUSR1, &usr_action, NULL);
sigaction(SIGINT, &usr_action1, NULL);
int didntGetSigInt = 1;
while (didntGetSigInt == 1){
pause();
}
return 0;
}
/***********************************************************************
* function name: signal_hand
* input: int sig
* output: void
* operation: handles a sigusr1. reads in the numbers and prints them in a
* user format.
*************************************************************************/
void signal_hand(int sig){
int endOfGameFlag = 0;
int numbers[SIZE];
int *ptr = numbers;
char comma;
//reads in numbers
int i;
for(i=0;i<SIZE; i++) {
//if last, dont read in comma
if(i == SIZE - 1){
scanf("%i", ptr);
numbers[i] = *ptr;
break;
}
scanf("%i%c", ptr, &comma);
numbers[i] = *ptr;
//checks if the game is over
if(numbers[i] == -1 || numbers[i] == -2 ){
endOfGameFlag = 1;
break;
}
ptr++;
}
//print if the game is over
if(endOfGameFlag == 1){
if(numbers[0] == -1){
printf("Congratulations!\n");
}
else{
printf("Game Over\n");
}
exit(1);
}
//print it in the format
for (i = 0; i < SIZE; i+=4) {
if(numbers[i] != 0){
printf("| %04i |",numbers[i]);
}
else{
printf("| |");
}
if(numbers[i+1] != 0){
printf(" %04i |",numbers[i+1]);
}
else{
printf(" |");
}
if(numbers[i+2] != 0){
printf(" %04i |",numbers[i+2]);
}
else{
printf(" |");
}
if(numbers[i+3] != 0){
printf(" %04i |\n",numbers[i+3]);
}
else{
printf(" |\n");
}
}
printf("\n");
}
/***********************************************************************
* function name: sigIntHandler
* input: int sig
* output: void
* operation: handles a sigint. prints bye bye and exits elegantly
*************************************************************************/
void sigIntHandler(int sig){
printf("BYE BYE\n");
exit(1);
}