-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
259 lines (235 loc) · 5.28 KB
/
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include<curses.h>
#include<time.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#ifndef __APPLE__
#include<stdio_ext.h>
#else
#endif
#define SIZE 2048
struct user
{
int state;
/*
State of preparedness:
~ -> No connection
19 -> Connected. Not ready.
23 -> Connected. Ready.
1 -> Message Broadcasted
2 -> Message Recieved
3 -> Quit
*/
char msg[SIZE], usrname[16];
};
int main()
{
void *shm[2];
struct user *usr[2];
int me, you, shmid[2], y=1, /*end=1,*/ j, name, ch, prex, prey;
char buf[SIZE];
shm[0]=shm[1]=(void *)0;
usr[0]=usr[1]=(struct user *)NULL;
name=-1;
shmid[0]=shmget((key_t)12345, sizeof(struct user), 0666|IPC_CREAT);
shmid[1]=shmget((key_t)54321, sizeof(struct user), 0666|IPC_CREAT);
if(shmid[0]==-1||shmid[1]==-1)
{
fprintf(stderr, "\nshmget() failed!!!\n");
return 0;
}
shm[0]=shmat(shmid[0], (void *)0, 0);
shm[1]=shmat(shmid[1], (void *)0, 0);
if(shm[0]==(void *)-1||shm[1]==(void *)-1)
{
fprintf(stderr, "\nshmat() failed!!!\n");
return 0;
}
printf("\nMemories attached at %lu\t&\t%lu\n", (unsigned long)shm[0], (unsigned long)shm[1]);
usr[0]=(struct user*)shm[0];
if(usr[0]->state==19||usr[0]->state==23)
{
me=1;
you=0;
usr[you]=(struct user*)shm[you];
usr[me]=(struct user*)shm[me];
usr[me]->state=19;
printf("\nYou were the second user to connect\n");
}
else
{
me=0;
you=1;
usr[you]=(struct user*)shm[you];
usr[me]=(struct user*)shm[me];
usr[me]->state=19;
printf("\nYou are the first user to connect\n");
}
printf("\nEnter your username: ");
#ifndef __APPLE__
__fpurge(stdin);
#else
fpurge(stdin);
#endif
scanf("%s", buf);
strncpy(usr[me]->usrname, buf, 15);
usr[me]->state=23;
if(usr[you]->state!=19&&usr[you]->state!=23)
{
printf("\nWaiting for the other user to connect...");
while(usr[you]->state!=19)
{
sleep(1);
printf(".");
}
printf("\nThe other user connected!!!\n");
}
if(usr[you]->state!=23)
{
printf("\nWaiting for the other user to log in...");
while(usr[you]->state!=23)
{
sleep(1);
printf(".");
}
printf("\nThe other user logged in!!!\n");
}
WINDOW *chat, *txtbox/*, *status*/;
time_t now;
initscr();
clear();
chat=newwin(LINES-4, COLS, 0, 0);
txtbox=newwin(3, COLS, LINES-3, 0);
/****************************************************************************************************
status=newwin(1, COLS, LINES-4, 0);
****************************************************************************************************/
box(chat, 0, 0);
box(txtbox, 0, 0);
halfdelay(1);
do
{
wattron(txtbox, A_BOLD);
mvwprintw(txtbox, 1, 1, "%s: ", usr[me]->usrname);
wattroff(txtbox, A_BOLD);
wrefresh(chat);
wrefresh(txtbox);
wmove(txtbox, 1, strlen(usr[me]->usrname)+3);
j=0;
while(1)
{
box(txtbox, 0, 0);
box(chat, 0, 0);
ch=wgetch(txtbox);
if(usr[you]->state==20)
{
mvwprintw(chat, LINES-6, 2, "%s is typing...", usr[you]->usrname);
wrefresh(chat);
}
else if(usr[you]->state!=20)
{
wmove(chat, LINES-6, 0);
wclrtoeol(chat);
}
if(ch=='\n')
break;
if(ch==127&&j!=0)
{
j--;
if(j==0)
usr[me]->state=2;
getyx(txtbox, prey, prex);
if(prex>(strlen(usr[me]->usrname)+2))
{
mvwdelch(txtbox, prey, prex-3);
mvwdelch(txtbox, prey, prex-3);
mvwdelch(txtbox, prey, prex-3);
wclrtoeol(txtbox);
}
}
else if(ch==127&&j==0)
{
usr[me]->state=2;
getyx(txtbox, prey, prex);
mvwdelch(txtbox, prey, prex-2);
mvwdelch(txtbox, prey, prex-2);
wclrtoeol(txtbox);
}
if((ch!=-1)&&(ch!=127))
buf[j++]=ch;
if(usr[you]->state==3)
break;
if(usr[you]->state==1)
{
time(&now);
wattron(chat, A_BOLD);
if(name!=you)
{
mvwaddstr(chat, y, 2, usr[you]->usrname);
mvwprintw(chat, y++, COLS-26, "%s", ctime(&now));
}
wattroff(chat, A_BOLD);
mvwprintw(chat, y++, 2, "%s", usr[you]->msg);
box(chat, 0, 0);
wrefresh(chat);
wrefresh(txtbox);
usr[you]->state=2;
name=you;
}
if(y>LINES-6)
{
wclear(chat);
box(chat, 0, 0);
wrefresh(chat);
y=1;
}
if(j>0)
usr[me]->state=20;
/*
else if(j==0)
usr[me]->state=2;
*/
/****************************************************************************************************
mvwprintw(status, 1, 5, "Test area. Blank area.");
*****************************************************************************************************/
}
buf[j]='\0';
if(j>0)
{
strcpy(usr[me]->msg, buf);
time(&now);
wattron(chat, A_BOLD);
if(name!=me)
{
mvwaddstr(chat, y, 2, usr[me]->usrname);
mvwprintw(chat, y++, COLS-26, "%s", ctime(&now));
}
wattroff(chat, A_BOLD);
mvwaddstr(chat, y++, 2, usr[me]->msg);
box(chat, 0, 0);
wmove(txtbox, 1, 3);
wclrtoeol(txtbox);
usr[me]->state=1;
name=me;
}
}while(strcasecmp("end", buf)&&(usr[you]->state!=3));
if((usr[you]->state!=3))
usr[me]->state=3;
endwin();
if(usr[me]->state==3)
{
if((shmdt(shm[0])==-1)||(shmdt(shm[1])==-1))
{
fprintf(stderr, "shmdt() failed!!!\n");
return 0;
}
if((shmctl(shmid[0], IPC_RMID, 0)==-1)||(shmctl(shmid[1], IPC_RMID, 0)==-1))
{
fprintf(stderr, "shmctl() failed!!!\n");
return 0;
}
}
printf("\nChat successfully ended\n");
return 0;
}