-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.cpp
734 lines (658 loc) · 21.3 KB
/
validator.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <errno.h>
#include "utilities.h"
#include "cryptogram.h"
/*{
return 0;
}
int process_server_comm(vector<string> tokens, int num_of_tokens, char res[MAX_STR_SIZE], char* dirnm)
{
return 0;
}
int process_command(char command[MAX_STR_SIZE], char res[MAX_STR_SIZE], char* dirnm)
{
int num_of_tokens;
//char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
vector<string> tokens = mytokenizer(command, " \n");
cerr<<tokens[0]<<" DD voter/server DD"<<endl;
//if(num_of_tokens < 0)
//return -1;
if(tokens[0] == "server"){
cerr<<"FAHMIDE SERVER E"<<endl;
return process_server_comm(tokens, num_of_tokens, res, dirnm);
}
else if(tokens[0] == "voter"){
cerr<<"fahmide vvvvvoter e"<<endl;
return process_voter_comm(tokens, num_of_tokens, res, dirnm);
}
return -1;
}*/
/*int process_command(char command[MAX_STR_SIZE], char res[MAX_STR_SIZE], char* dirnm)
{
return 0;
}*/
int main(int argn, char** args)
{
if(argn < 2){
cout<<"Valid format is :\n./validator port_number"<<endl;
return 0;
}
int port_number = atoi(args[1]);
//save_ca_port(args[1].c_str());
if(found_in(args[1], read_available_ca_ports()) < 0)
{
int fd_to_save_port = open("./DB/validators.txt", O_APPEND | O_RDWR);
cerr<<"args[1] is: "<<args[1]<< " in " <<strlen(args[1])<<endl;
int dumm = write(fd_to_save_port, args[1], strlen(args[1]));
dumm = write(fd_to_save_port, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_port);
}
const int num_of_connection = 5;
// make directories
char directory_name[] = "DB";
int mkdir_status = create_directories(directory_name);
if(mkdir_status != 0){
cout<<"Error while creating directory"<<endl;
return 0;
}
cout<<"Directory created"<<endl;
//creating socket
int server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port_number);
struct sockaddr_in client_addr;
//binding
int binding_st = bind(server_fd,(struct sockaddr*) &server_addr, sizeof(server_addr));
if(binding_st == -1)
{
write(STDOUTFD, "binding error\n", sizeof("binding error\n"));
return -1;
//exit
}
else
write(STDOUTFD, "binding successful\n", sizeof("binding successful\n"));
//listenning
int listening_st = listen(server_fd, num_of_connection);
if(listening_st == -1)
write(STDOUTFD, "listening error\n", sizeof("listening error"));
else
write(STDOUTFD, "listening successful\n", sizeof("listening successful"));
fd_set read_fdset, temp_fdset;
struct timeval tv;
int ret_val;
int new_sock_fd, it_fd;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&read_fdset);
FD_SET(server_fd, &read_fdset);
FD_SET(STDINFD, &read_fdset);
char publicPath[MAX_STR_SIZE];
clear_buff(publicPath,MAX_STR_SIZE);
char privatePath[MAX_STR_SIZE];
clear_buff(privatePath,MAX_STR_SIZE);
strcat( publicPath,"./DB/");
strcat( privatePath,"./DB/");
struct stat st;
if (stat(publicPath, &st) == -1) {
mkdir(publicPath, 0700);
}
strcat( publicPath,"publicCA");
strcat( privatePath,"privateCA");
if( !create_RSA_key(publicPath,privatePath)) {
cout<<"error in creating key" << endl;
}else{
cout << "key create successfully! " << endl;
}
/* Wait up to five seconds. */
tv.tv_sec = 10 * 60;
tv.tv_usec = 0;
unsigned int size_of_client_addr = sizeof(client_addr);
int status;
while(1)
{
memcpy(&temp_fdset, &read_fdset, sizeof(temp_fdset));
status = select(FD_SETSIZE, &temp_fdset, (fd_set *)0, (fd_set *)0, (struct timeval*) &tv);
if(status < 1)
{
write(STDOUTFD, "Select Error\n", sizeof("Select error\n"));
break;
}
for(it_fd = 0; it_fd<FD_SETSIZE; it_fd++)
{
if(FD_ISSET(it_fd, &temp_fdset))
{
if(it_fd == STDINFD)
{
char buff_read [MAX_STR_SIZE], response_buff[MAX_STR_SIZE];
clear_buff(buff_read, MAX_STR_SIZE);
clear_buff(response_buff, MAX_STR_SIZE);
read(it_fd, buff_read, MAX_STR_SIZE-1);
//process_server_command(buff_read, response_buff, directory_name);
write(STDOUTFD, buff_read, MAX_STR_SIZE);
write(STDOUTFD, "OK", strlen("OK"));
}
if(it_fd == server_fd)
{
new_sock_fd = accept(server_fd, NULL, NULL);
if(new_sock_fd < 0)
{
write(STDOUTFD,"Error on accepting\n", sizeof("Error on accepting\n"));
break;
}
else write(STDOUTFD,"accepting successful\n", sizeof("accepting successful\n"));
FD_SET(new_sock_fd, &read_fdset);
}
else
{
int n, m;
char buff_read [MAX_STR_SIZE], response_buff[MAX_STR_SIZE];
clear_buff(buff_read, MAX_STR_SIZE);
clear_buff(response_buff, MAX_STR_SIZE);
n = read(it_fd, buff_read, MAX_STR_SIZE-1);
if(n == 0)
{
close(it_fd);
FD_CLR(it_fd, &read_fdset);
write(STDOUTFD, "Removing One Client_fd\n", sizeof("Removing One Client_fd\n"));
}
else if(n < 0)
{
write(STDOUTFD, "Error on reading\n", sizeof("Error on reading\n"));
}
//after reading successfully
else
{
if(buff_read != "DC")
{
if( strncmp (buff_read,"Register",8)==0 ){
// char publicPath[MAX_STR_SIZE];
// clear_buff(publicPath,MAX_STR_SIZE);
// strcat( publicPath,"./DB/");
// struct stat st;
// if (stat(publicPath, &st) == -1) {
// mkdir(publicPath, 0700);
// }
// strcat( publicPath,"certificate/");
// if (stat(publicPath, &st) == -1) {
// mkdir(publicPath, 0700);
// }
vector<string> rcommand = mytokenizer(buff_read," ");
////open file
string s = "./DB/certificate/"+ rcommand[1];
int fd_c = creat(s.c_str(),O_RDWR| O_APPEND);
if(fd_c > 0){
chmod(s.c_str(),S_IRUSR | S_IWUSR);
}
close(fd_c);
show_encrypted_massage(publicPath,rcommand);
if( create_certificate(privatePath,rcommand[2],rcommand[1]))
{
int s = write(it_fd, "certificate created successfully\n", strlen("certificate created successfully\n"));
if(s < 0) write(STDOUTFD, "Error on writing\n", sizeof("Error on writing\n"));
}else{
int s = write(it_fd, "error in creating certificate\n", strlen("error in creating certificate\n"));
if(s < 0) write(STDOUTFD, "Error on writing\n", sizeof("Error on writing\n"));
}
//open file
// for(int i=0; i< rcommand.size(); i++)
// cerr << rcommand[i] << ' ';
// cerr << endl;
//open file
}
int s = write(it_fd, "OK\n", strlen("OK\n"));
if(s < 0) write(STDOUTFD, "Error on writing\n", sizeof("Error on writing\n"));
}
else //if(buff_read == "DC")
{
cerr<<"varede dasture DC DC DC shode"<<endl;
write(it_fd, "Disconnecting in Progress ...\n",
sizeof("Disconnecting in Progress ...\n"));
close(it_fd);
FD_CLR(it_fd, &read_fdset);
write(STDOUTFD, "Removing One Client_fd\n", sizeof("Removing One Client_fd\n"));
}
}
}
}
}
}
close(server_fd);
return 0;
}
/*
void set_compn_data(char* path_name, company* this_company)
{
int op_fd = open(path_name, O_RDWR | O_APPEND);
if(op_fd < 0) write(STDOUTFD, "Error In Opening File to Read Data!\n", sizeof("Error In Opening File to Read Data!\n"));
int path_tkn_num;
//char path_tkns[MAX_ARRAY_SIZE][MAX_STR_SIZE];
vector<string> pathtkns = mytokenizer(path_name, "./ \n");
strcpy(this_company->name, path_tkns[path_tkn_num - 2]);
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int rd_st = read(op_fd, read_buff, MAX_STR_SIZE-1);
if(rd_st > 0)
{
int tkn_num;
char tkns[MAX_ARRAY_SIZE][MAX_STR_SIZE];
tokenizer(read_buff, " \n", &tkn_num, tkns);
char tmp_stcknum[MAX_ARRAY_SIZE];
clear_buff(tmp_stcknum, MAX_ARRAY_SIZE);
strcpy(tmp_stcknum, tkns[3]);
this_company->stocks_number = atoi(tmp_stcknum);//set number of company's stocks
clear_buff(tmp_stcknum, MAX_ARRAY_SIZE);
strncpy(tmp_stcknum, tkns[6], strlength(tkns[6])-1);
this_company->price_of_stock = atoi(tmp_stcknum);//set price of each stock
this_company->budget = (this_company->stocks_number)*(this_company->price_of_stock);
int i = 7;
this_company->invstr_num = 0;
while(i<tkn_num && strlength(tkns[i])!=0)
{
strcpy(this_company->investors[(i-7)/6], tkns[i]);
i++; i++;//passing bought
this_company->investors_stocks_num[(i-7)/6] = atoi(tkns[i]);
i++; i++; i++;//passing stocks on
strcpy(this_company->investing_date[(i-7)/6], tkns[i]);
i++;
this_company->invstr_num++;
}
}
close(op_fd);
}*/
/*void record_company_data(char path_name[], company this_company)
{
int file_fd = open(path_name, O_RDWR);
if(file_fd < 0) write(STDOUTFD, "Error In Opening File to Write!\n", sizeof("Error In Opening File to Write!\n"));
int w_st = write(file_fd, "Main stocks Num ", sizeof("Main stocks Num "));
char strtmp[20];
clear_buff(strtmp, 20);
int_to_str(this_company.stocks_number, strtmp, 10);
write(file_fd, strtmp, strlength(strtmp));
write(file_fd, "\n", sizeof("\n"));
write(file_fd, "stocks Price ", sizeof("stocks Price "));
clear_buff(strtmp, 20);
int_to_str(this_company.price_of_stock, strtmp, 10);
write(file_fd, strtmp, strlength(strtmp));
write(file_fd, "$\n", sizeof("$\n"));
int i;
for(i=0; i<this_company.invstr_num; i++)
{
write(file_fd, this_company.investors[i], strlength(this_company.investors[i]));//investor name
write(file_fd, " bought ", sizeof(" bought "));
clear_buff(strtmp, 20);
int_to_str(this_company.investors_stocks_num[i], strtmp, 10);
write(file_fd, strtmp, strlength(strtmp));//number of stocks
write(file_fd, " stocks on ", sizeof(" stocks on "));
write(file_fd, this_company.investing_date[i], strlength(this_company.investing_date[i]));//date
write(file_fd, "\n", sizeof("\n"));
}
close(file_fd);
}*/
/* client* this_customer, char res[MAX_STR_SIZE], char* path_name)
{
clear_buff(res, MAX_STR_SIZE);
if(num_of_tokens >= 6 && mystrcmp(tokens[2], "Introduction") == 0 && mystrcmp(tokens[3], "Customer") == 0)
{
int cr_fd = creat(path_name, O_RDWR | O_APPEND);
if(cr_fd > 0)
{
chmod(path_name,S_IRUSR | S_IWUSR);
char w_buff[MAX_STR_SIZE];
clear_buff(w_buff, MAX_STR_SIZE);
strcpy(w_buff , "Main budget ");
strcat(w_buff, tokens[5]);
strcat(w_buff, "$\n");
int w_st = write(cr_fd, w_buff, strlength(w_buff));
if(w_st < 0)
{
write(STDOUTFD, "Error in Writing File!\n", sizeof("Error in Writing File!\n"));
return -1;
}
strcpy(res, "You Registered Successfully!\n");
}
else
return -1;
close(cr_fd);
}
else if( num_of_tokens >= 5 && mystrcmp(tokens[2], "Get") == 0 &&
mystrcmp(tokens[3], "Stocks") == 0 && mystrcmp(tokens[4], "List") == 0)
{
int file_num;
char file_names[MAX_ARRAY_SIZE][MAX_STR_SIZE];
char sources_path[MAX_STR_SIZE];
clear_buff(sources_path, MAX_STR_SIZE);
char old_path_tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
int old_path_tokens_num;
tokenizer(path_name, "/", &old_path_tokens_num, old_path_tokens);
strcpy(sources_path, old_path_tokens[0]);
strcat(sources_path, "/Company/");
print(sources_path);
//list_files(sources_path, file_names, &file_num);
char date_p[30];
clear_buff(date_p, 30);
get_date(date_p);
strcpy(res, "------------Stocks List-------------\nTime:");
strcat(res, date_p);
strcat(res, "\nCompany---StocksNum-Price\n");
int fd, nread;
char buf[MAX_STR_SIZE];
struct linux_dirent *d;
int bpos;
char d_type;
fd = open(sources_path, O_RDONLY | O_DIRECTORY);
while(1)
{
nread = syscall(SYS_getdents, fd, buf, MAX_STR_SIZE);
if (nread == 0){
break;
}
for (bpos = 0; bpos < nread;)
{
d = (struct linux_dirent *) (buf + bpos);
print(d->d_name);
print("\n");
if(mystrcmp(d->d_name, ".") != 0 && mystrcmp(d->d_name, "..")){
company this_cmp;
char cmp_path[MAX_STR_SIZE];
clear_buff(cmp_path, MAX_STR_SIZE);
strcpy(cmp_path, sources_path);
strcat(cmp_path, d->d_name);
set_compn_data(cmp_path, &this_cmp);
strcat(res, this_cmp.name);
strcat(res, "\t");
char stcks [MAX_STR_SIZE];
int_to_str(this_cmp.stocks_number, stcks, 10);
strcat(res, stcks);
strcat(res, "\t");
int_to_str(this_cmp.price_of_stock, stcks, 10);
strcat(res, stcks);
strcat(res, "\n");
}
bpos += d->d_reclen;
}
break;
}
}
else if(num_of_tokens >= 5 && mystrcmp(tokens[2], "Buy") == 0 && this_customer != NULL)
{
return 1;
}
else if(num_of_tokens >= 4 && mystrcmp(tokens[2], "Get") == 0 && mystrcmp(tokens[3], "Status") == 0
&& this_customer != NULL)
{
char date_p[30];
clear_buff(date_p, 30);
get_date(date_p);
strcpy(res, "---------status---------\nTime : ");
strcat(res, date_p);
strcat(res, "\n");
strcat(res, "Budget : ");
char budget_str[20];
clear_buff(budget_str, 20);
int_to_str(this_customer->budget, budget_str, 10);
strcat(res, budget_str);
strcat(res, "$\n");
strcat(res, "Company---Stocks Num\n");
int i;
for(i = 0; i < this_customer -> invstd_num; i++){
strcat(res, this_customer -> invested[i]);
strcat(res, " ");
char tmp[20];
clear_buff(tmp, 20);
int_to_str(this_customer -> invested_stocks_num[i], tmp, 10);
strcat(res, tmp);
strcat(res, " stocks on ");
strcat(res, this_customer -> invested_date[i]);
strcat(res, "\n");
}
strcat(res, "\0");
}
else if(num_of_tokens >= 3 && mystrcmp(tokens[2], "Unregister") == 0 && this_customer != NULL)
{
unlink(path_name);
strcpy(res, "You Are Unregistered!\n");
}
else if(num_of_tokens >= 3 && mystrcmp(tokens[2], "DC") == 0)
{
}
else
return -1;
return 0;
}*/
/*int record_customer_data(char path_name[], client this_customer)
{
int file_fd = open(path_name, O_RDWR);
if(file_fd < 0) write(STDOUTFD, "Error In Opening File to Write!\n", sizeof("Error In Opening File to Write!\n"));
int w_st = write(file_fd, "Main budget ", sizeof("Main budget "));
char strtmp[20];
clear_buff(strtmp, 20);
int_to_str(this_customer.budget, strtmp, 10);
write(file_fd, strtmp, strlength(strtmp));
write(file_fd, "$\n", sizeof("$\n"));
int i;
for(i=0; i<this_customer.invstd_num; i++)
{
write(file_fd, this_customer.invested[i], strlength(this_customer.invested[i]));//company name
write(file_fd, " ", sizeof(" "));
clear_buff(strtmp, 20);
int_to_str(this_customer.invested_stocks_num[i], strtmp,10);
write(file_fd, strtmp, strlength(strtmp));//number of stocks
write(file_fd, this_customer.invested_date[i], strlength(this_customer.invested_date[i]));//date
write(file_fd, "\nbudget ", sizeof("\nbudget "));
clear_buff(strtmp, 20);
int_to_str(this_customer.remained_budget[i], strtmp, 10);
write(file_fd, strtmp, strlength(strtmp));//remained budget after buying above stocks
write(file_fd, "$\n", sizeof("$\n"));
}
close(file_fd);
return 0;
}*/
/*int process_customer_comm(char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE], int num_of_tokens, char res[MAX_STR_SIZE], char* dirnm){
client this_customer;
strcpy(this_customer.name, tokens[1]);
char path_name[MAX_STR_SIZE];
clear_buff(path_name, MAX_STR_SIZE);
strcpy(path_name, dirnm);
strcat(path_name, "/Customer/");
strcat(path_name, tokens[1]);
strcat(path_name, ".txt");
int file_fd = open(path_name, O_RDWR | O_APPEND);
if(file_fd < 0)
{
return (customer_response(tokens, num_of_tokens, NULL , res, path_name));
}
else
{
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int r_st = read(file_fd, read_buff, MAX_STR_SIZE-1);
//tokenize read_file_lines
if(r_st > 0)
{
int tkn_num;
char tkns[MAX_ARRAY_SIZE][MAX_STR_SIZE];
tokenizer(read_buff, " \n", &tkn_num, tkns);
char tmp_budget[MAX_ARRAY_SIZE];
clear_buff(tmp_budget, MAX_ARRAY_SIZE);
strncpy(tmp_budget, tkns[2], strlength(tkns[2])-1);
this_customer.budget = atoi(tmp_budget);
int i = 3;
this_customer.invstd_num = 0;
while(i < tkn_num && strlength(tkns[i]) != 0)
{
strcpy(this_customer.invested[(i-3)/7], tkns[i]);//company name
print("company name:");
print (tkns[i]);
i++;
this_customer.invested_stocks_num[(i-3)/7] = atoi(tkns[i]);//num of stocks
print("\n num of stocks:");
print (tkns[i]);
i++; i++; i++;// for passing stocks on
strcpy(this_customer.invested_date[(i-3)/7], tkns[i]);//date
print("\n date:");
print (tkns[i]);
i++; i++;//passing budget
char tmp_budget[MAX_ARRAY_SIZE];
clear_buff(tmp_budget, MAX_ARRAY_SIZE);
strncpy(tmp_budget, tkns[i], strlength(tkns[i])-1);
this_customer.remained_budget[(i-3)/7] = atoi(tmp_budget);
print("\n budget:");
print (tkns[i]);
i++;
this_customer.invstd_num++;
}
close(file_fd);
int response_stat = customer_response(tokens, tkn_num, &this_customer, res, path_name);
if(response_stat == 1)
record_customer_data(path_name, this_customer);
return response_stat;
}
else
close(file_fd);
}
return -1;
}*/
/*int company_response(char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE], int num_of_tokens,
company* this_company, char res[MAX_STR_SIZE], char* path_name)
{
clear_buff(res, MAX_STR_SIZE);
if(num_of_tokens >= 7 && mystrcmp(tokens[2], "Introduction") == 0 && mystrcmp(tokens[3], "Company") == 0)
{
int cr_fd = creat(path_name, O_RDWR | O_APPEND);
if(cr_fd > 0)
{
chmod(path_name, S_IRUSR | S_IWUSR );
char w_buff[MAX_STR_SIZE];
clear_buff(w_buff, MAX_STR_SIZE);
strcpy(w_buff, "Main stocks Num ");
strcat(w_buff, tokens[5]);
strcat(w_buff, "\n");
strcat(w_buff, "stocks Price ");
strcat(w_buff, tokens[6]);
strcat(w_buff, "$\n");
int w_st = write(cr_fd, w_buff, strlength(w_buff));
if(w_st < 0)
{
write(STDOUTFD, "Error in Writing File!\n", sizeof("Error in Writing File!\n"));
return -1;
}
strcpy(res, "You Registered Successfully!\n");
}
else
return -1;
}
else if(num_of_tokens >= 4 && mystrcmp(tokens[2], "Get") == 0 && mystrcmp(tokens[3], "Status") == 0
&& this_company != NULL)
{
clear_buff(res, MAX_ARRAY_SIZE);
char date_p[30];
clear_buff(date_p, 30);
get_date(date_p);//get date
strcpy(res, "------------Company Status -------------\nCompanyName : ");
strcat(res, this_company->name);
strcat(res, "\nStatus Time : ");
strcat(res, date_p);
strcat(res, "\nBudget ");
char tmp_str[20];
clear_buff(tmp_str, 20);
int_to_str(this_company->budget, tmp_str, 10);
strcat(res, tmp_str);
strcat(res, "$\nPrice of each stock : ");
clear_buff(tmp_str, 20);
int_to_str(this_company->price_of_stock,tmp_str, 10);
strcat(res, tmp_str);
strcat(res, "$\nRemaining stocks num : ");
clear_buff(tmp_str, 20);
int_to_str(this_company->stocks_number, tmp_str, 10);
strcat(res, tmp_str);
strcat(res, "\n");
int i;
for(i=0; i<this_company->invstr_num; i++)
{
strcat(res, this_company->investors[i]);
strcat(res, " bought ");
clear_buff(tmp_str, 20);
int_to_str(this_company->investors_stocks_num[i], tmp_str, 10);
strcat(res, tmp_str);
strcat(res, " stocks on ");
strcat(res, this_company->investing_date[i]);
strcat(res, "\n");
}
strcat(res, "\0");
}
else
return -1;
return 0;
}*/
/*int process_company_comm(char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE], int num_of_tokens, char res[MAX_STR_SIZE], char* dirnm)
{
company this_company;
strcpy(this_company.name, tokens[1]);
char path_name[MAX_STR_SIZE];
clear_buff(path_name, MAX_STR_SIZE);
strcpy(path_name, dirnm);
strcat(path_name, "/Company/");
strcat(path_name, tokens[1]);
strcat(path_name, ".txt");
int file_fd = open(path_name, O_RDWR | O_APPEND);
if(file_fd < 0)
{
return company_response(tokens, num_of_tokens, NULL, res, path_name);
}
else
{
set_compn_data(path_name, &this_company);
return company_response(tokens, num_of_tokens, &this_company, res, path_name);
}
}*/
/*int process_command(char command[MAX_STR_SIZE], char res[MAX_STR_SIZE], char* dirnm)
{
int num_of_tokens;
char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
tokenizer(command, " \n", &num_of_tokens, tokens);
if(num_of_tokens < 0)
return -1;
if(mystrcmp(tokens[0], "company") == 0)
return process_company_comm(tokens, num_of_tokens, res, dirnm);
else if(mystrcmp(tokens[0], "customer") == 0)
return process_customer_comm(tokens, num_of_tokens, res, dirnm);
return -1;
}*/
/*int process_server_command(char buff_read[MAX_STR_SIZE], char response_buff[MAX_STR_SIZE], char* dirnm){
int num_of_tokens;
clear_buff(response_buff, MAX_STR_SIZE);
char tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
tokenizer(buff_read, " \n", &num_of_tokens, tokens);
if(num_of_tokens < 4)
return -1;
char pathname[MAX_STR_SIZE];
clear_buff(pathname, MAX_STR_SIZE);
strcpy(pathname, dirnm);
strcat(pathname, "/");
strcat(pathname, tokens[2]);
strcat(pathname, "/");
strcat(pathname, tokens[3]);
strcat(pathname, ".txt");
if(mystrcmp(tokens[0],"Change") == 0 && mystrcmp(tokens[1], "FileOwner") == 0){
if(chmod(pathname, S_IRUSR | S_IWUSR | S_IWOTH | S_IROTH) != 0) strcpy(response_buff, "File address Incorrect\n");
else strcpy(response_buff, "File mode changed\n");
}
else if(mystrcmp(tokens[0],"Show") == 0 && mystrcmp(tokens[1], "FileOwner") == 0){
struct stat sb;
if (stat(pathname, &sb) == -1) strcpy(response_buff, "Stat can not be recognized\n");
else
{
char modestr[MAX_STR_SIZE] ;
int_to_str(sb.st_mode, modestr, 10);
strcpy(response_buff, modestr);
}
}
return 0;
}*/