-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.c
More file actions
190 lines (175 loc) · 4.54 KB
/
function.c
File metadata and controls
190 lines (175 loc) · 4.54 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "function.h"
#include "time.h"
int counter = 0;
void sighandler(int signal){
if(signal == SIGQUIT)
{
if(counter < 3)
{
printf("Warning! You are trying to force end the program!\n");
printf("This may cause a disaster!\n");
printf("To end the program safely, follow Disaster Protocol until step 2\n");
printf("Press Control + \\ %d more time(s) to force end the program\n", 3 - counter);
counter ++;
}
else
{
printf("You are about to force end the program.\n");
printf("This creates a disaster.\n");
printf("To obtain protocol for repairing a disaster,\n");
printf("Type \"make run\"\n");
int status;
exit(status);
}
}
else if(signal == SIGINT)
{
printf("If you are trying to exit the program, type \"exit\" for your guess\n");
}
}
int shmsetup(key_t key, void ** pointer){
int shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0644);
if(shmid < 0)
{
shmid = shmget(key, SIZE, 0644);
}
* pointer = shmat(shmid, 0, 0);//checking array to be shared between players
return shmid;
}
int semsetup(){
int semid = semget(SEMKEY, 1, IPC_CREAT | IPC_EXCL | 0644);
int semidExistedBefore = 0;
if(semid < 0)
{
semid = semget(SEMKEY, 1, 0644);
printf("\n");
if(semid >= 0)
semidExistedBefore = 1;
}
if(!semidExistedBefore)
{
struct sembuf sb;
sb.sem_num = 0;
sb.sem_op = 1;
semop(semid, &sb, 1);
int semval = semctl(semid, 0, GETVAL, 0);
}
else
{
int semval = semctl(semid, 0, GETVAL, 0);
}
return semid;
}
void clear(){
char ** args = parse_args("clear");
int child = fork();
int * status;
if(child < 0)
{
printf("error %d: %s\n", errno, strerror(errno));
}
else if(child == 0)
{
execvp("clear", args);
exit(*status);
}
else if(child > 0)
{
wait(status);
}
}
int draw(int lives){
if (lives <= 6){
printf("\n" );
if (lives <= 5){
printf(" O\n" );
if (lives <= 4){
printf("/" );
if (lives <= 3){
printf("|" );
if (lives <= 2){
printf("\\\n" );
if (lives <= 1){
printf("/ " );
if (lives <= 0){
printf("\\\n" );
}
}
}
}
}
}
}
printf("\n");
}
char * generate_word(){
FILE * f = fopen("words.txt", "r");
if (f < 0) {
printf("%s\n", strerror(errno));
}
int nLines = 0;
char line[1024];
int randLine;
int i;
while(!feof(f)) {
fgets(line, 1024, f);
nLines++;
}
randLine = rand() % nLines;
fseek(f, 0, SEEK_SET);
for(i = 0; !feof(f) && i <= randLine; i++){
fgets(line, 1024, f);
}
char * word = line;
int x = strlen(word) - 2;//this is supposed to be strlen(word) - 1 but it is hardcoded because for some reason it won't work
word[x] = '\0';
printf("while inside generate_word\n");
printf("%s\n", word);
printf("strlen(word) is %ld\n", strlen(word));
fclose(f);
return word;
}
char ** parse_args( char * line ){
char ** args = calloc(6, sizeof(char));
char * current = line;
char * holder;
int i = 0;
while (current != NULL){
holder = strsep(¤t, "\n");
args[i] = holder;
i++;
}
args[i] = NULL;
return args;
}
void words(char * wordbank){
int x = 0;
printf("{");
printf("%s", wordbank);
printf("}\n");
}
void disasterPreventionText(){
printf("To properly exit the program, follow Disaster Protocol to step 2.\n\n");
printf("Disaster Protocol:\n");
printf("1. All players press Control + C until they reach the guessing screen.\n");
printf("2. All players type \"exit\" for their guess to exit their programs.\n");
printf("3. Recompile the program using the following steps:\n");
printf(" a. Type \"make clean\"\n");
printf(" b. Type \"make\"\n");
printf(" c. Type \"make run\"\n\n");
printf("***To force exit the program (not recommended), press Control + \\ 3 times***\n\n");
printf("******************************************************************************************\n");
printf(" HANGMAN:\n\n");
}