/
Inventory.c
908 lines (794 loc) · 24.7 KB
/
Inventory.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
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
Name: Rajan Palaniya & Naresh Chopda
Date: 05/11/11 18:18
Description: This project is about Inventory Management System.
*/
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
/* List of Global variables used in the application*/
int mboxbrdrclr,mboxbgclr,mboxfgclr; /* To set colors for all message boxes in the application*/
int menutxtbgclr,menutxtfgclr,appframeclr; /* To set the frame and color's for menu items's*/
int section1_symb,section1_bgclr,section1_fgclr; /* To set color of section 1, the region around the menu options*/
int section2_symb,section2_bgclr,section2_fgclr; /* To set color of section 2, the section on the right of the menu options*/
int fEdit;
int animcounter;
static struct struct_stock /* Main database structure*/
{
char itemcode[8];
char itemname[50];
float itemrate;
float itemqty;
int minqty; /*Used for Reorder level, which is the minimum no of stock*/
}inv_stock;
struct struct_bill
{
char itemcode[8];
char itemname[50];
float itemrate;
float itemqty;
float itemtot;
}item_bill[100];
void password();
void check_password();
char password[8];
const long int stocksize=sizeof(inv_stock); /*stocksize stores the size of the struct_stock*/
float tot_investment;
int numItems; /*To count the no of items in the stock*/
int button,column,row; /*To allow mouse operations in the application*/
FILE *dbfp; /*To perform database file operations on "inv_stock.dat"*/
int main(void)
{
float issued_qty;
char userchoice,code[8];
int flag,i,itemsold;
float getInvestmentInfo(void);
FILE *ft;
int result;
getConfiguration();
/* Opens & set 'dbfp' globally so that it is accessible from anywhere in the application*/
dbfp=fopen("d:\invstoc.dat","r+");
if(dbfp==NULL)
{
clrscr();
printf("\nDatabase does not exists.\nPress Enter key to create it. To exit, press any other key.\n ");
fflush(stdin);
if(getch()==13)
{
dbfp=fopen("d:\invstoc.dat","w+");
printf("\nThe database for the application has been created.\nYou must restart the application.\nPress any key to continue.\n");
fflush(stdin);
getch();
exit(0);
}
else
{
exit(0);
}
}
/* Application control will reach here only if the database file has been opened successfully*/
if(initmouse()==0)
messagebox(10,33,"Mouse could not be loaded.","Error ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
showmouseptr();
_setcursortype(_NOCURSOR);
password();
check_password();
while(1)
{
clrscr();
fEdit=FALSE;
ShowMenu();
numItems=0;
rewind(dbfp);
/* To calculate the number of records in the database*/
while(fread(&inv_stock,stocksize,1,dbfp)==1)
++numItems;
textcolor(menutxtfgclr);
textbackground(menutxtbgclr);
gotopos(23,1);
cprintf("Total Items in Stock: %d",numItems);
textcolor(BLUE);
textbackground(BROWN);
fflush(stdin);
/*The application will wait for user response */
userchoice=getUserResponse();
switch(userchoice)
{
/* To Close the application*/
case '0':
BackupDatabase(); /*Backup the Database file to secure data*/
flushall();
fclose(dbfp);
fcloseall();
print2screen(12,40,"Thanks for Using the application.",BROWN,BLUE,0);
sleep(1);
setdefaultmode();
exit(0);
/* To Add an item*/
case '1':
if(getdata()==1)
{
fseek(dbfp,0,SEEK_END);
/*Write the item information into the database*/
fwrite(&inv_stock,stocksize,1,dbfp);
print2screen(13,33,"The item has been successfully added. ",BROWN,BLUE,0);
getch();
}
break;
/* To edit the item information*/
case '2':
print2screen(2,33,"Enter Item Code>",BROWN,BLUE,0);gotopos(2,54);fflush(stdin);
scanf("%s",&code);
fEdit=TRUE;
if(CheckId(code)==0)
{
if(messagebox(0,33,"Press Enter key to edit the item.","Confirm",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0)!=13)
{
messagebox(10,33,"The item information could not be modified. Please try again.","Edit ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
fEdit=FALSE;
break;
}
fEdit=TRUE;
getdata();
fflush(stdin);
fseek(dbfp,-stocksize,SEEK_CUR);
fwrite(&inv_stock,stocksize,1,dbfp);
}
else
messagebox(10,33,"The item is not available in the database.","No records found",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
fEdit=FALSE;
break;
/* To show information about an an Item*/
case '3':
print2screen(2,33,"Enter Item Code: ",BROWN,BLUE,0);gotopos(2,55);fflush(stdin);
scanf("%s",&code);
flag=0;
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
{
if(strcmp(inv_stock.itemcode,code)==0)
{
DisplayItemInfo();
flag=1;
}
}
if(flag==0)
messagebox(10,33,"The item is not available.","No records found ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
break;
/* To show information about all items in the database*/
case '4':
if(numItems==0)
messagebox(10,33,"No items are available. ","Error ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
textcolor(BLUE);
textbackground(BROWN);
gotopos(3,33);
cprintf("Number of Items Available in Stock: %d",numItems);
gotopos(4,33);
getInvestmentInfo();
cprintf("Total Investment :Rs.%.2f",tot_investment);
gotopos(5,33);
cprintf("Press Enter To View. Otherwise Press Any Key...");fflush(stdin);
if(getch()==13)
{
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1); /*List All records*/
DisplayItemRecord(inv_stock.itemcode);
}
textcolor(BLUE);
break;
/* To issue Items*/
case '5':
itemsold=0;
i=0;
top:
print2screen(3,33,"Enter Item Code: ",BROWN,BLUE,0);fflush(stdin);gotopos(3,55);
scanf("%s",&code);
if(CheckId(code)==1)
if(messagebox(10,33,"The item is not available.","No records found ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0)==13)
goto top;
else
goto bottom;
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
{
if(strcmp(inv_stock.itemcode,code)==0) /*To check if the item code is available in the database*/
{
issued_qty=IssueItem();
if(issued_qty > 0)
{
itemsold+=1;
strcpy(item_bill[i].itemcode,inv_stock.itemcode);
strcpy(item_bill[i].itemname,inv_stock.itemname);
item_bill[i].itemqty=issued_qty;
item_bill[i].itemrate=inv_stock.itemrate;
item_bill[i].itemtot=inv_stock.itemrate*issued_qty;
i+=1;
}
print2screen(19,33,"Would you like to issue another item(Y/N)?",BROWN,BLUE,0);fflush(stdin);gotopos(19,45);
if(toupper(getch())=='Y')
goto top;
bottom:
break;
}
}
break;
/* Items to order*/
case '6':
if(numItems<=0)
{
messagebox(10,33,"No items are available. ","Items Not Found ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
break;
}
print2screen(3,33,"Stock of these items is on the minimum level:",BROWN,RED,0);fflush(stdin);
flag=0;
fflush(stdin);
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
{
if(inv_stock.itemqty <= inv_stock.minqty)
{
DisplayItemInfo();
flag=1;
}
}
if(flag==0)
messagebox(10,33,"No item is currently at reorder level.","Reorder Items",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
break;
default:
messagebox(10,33,"The option you have entered is not available.","Invalid Option ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
break;
}
}
}
/*Display Menu & Skins that the user will see*/
ShowMenu()
{
if(section1_bgclr != BROWN || section1_symb != ' ')
fillcolor(2,1,23,39,section1_symb,section1_bgclr,section1_fgclr,0);
if(section2_bgclr != BROWN || section2_symb != ' ')
fillcolor(2,40,23,79,section2_symb,section2_bgclr,section2_fgclr,0);
print2screen(2,2,"1: Add an Item",menutxtbgclr,menutxtfgclr,0);
print2screen(4,2,"2: Edit Item Information",menutxtbgclr,menutxtfgclr,0);
print2screen(6,2,"3: Show Item Information",menutxtbgclr,menutxtfgclr,0);
print2screen(8,2,"4: View Stock Report",menutxtbgclr,menutxtfgclr,0);
print2screen(10,2,"5: Issue Items from Stock",menutxtbgclr,menutxtfgclr,0);
print2screen(12,2,"6: View Items to be Ordered ",menutxtbgclr,menutxtfgclr,0);
print2screen(14,2,"0: Close the application",menutxtbgclr,menutxtfgclr,0);
htskin(0,0,' ',80,appframeclr,LIGHTGREEN,0);
htskin(1,0,' ',80,appframeclr,LIGHTGREEN,0);
vtskin(0,0,' ',24,appframeclr,LIGHTGREEN,0);
vtskin(0,79,' ',24,appframeclr,LIGHTGREEN,0);
htskin(24,0,' ',80,appframeclr,LIGHTGREEN,0);
vtskin(0,31,' ',24,appframeclr,LIGHTGREEN,0);
return;
}
/*Wait for response from the user & returns choice*/
getUserResponse()
{
int ch,i;
animcounter=0;
while(!kbhit())
{
getmousepos(&button,&row,&column);
/*To show Animation*/
BlinkText(0,27,"Inventory Management System",1,YELLOW,RED,LIGHTGRAY,0,50);
animcounter+=1;
i++;
if(button==1 && row==144 && column>=16 && column<=72) /*Close*/
return('0');
if(button==1 && row==16 && column>=16 && column<=136) /*Add New Item*/
return('1');
if(button==1 && row==32 && column>=16 && column<=144) /*Edit Item*/
return('2');
if(button==1 && row==48 && column>=16 && column<=160) /*Show an Item*/
return('3');
if(button==1 && row==64 && column>=16 && column<=104) /*Stock Report*/
return('4');
if(button==1 && row==80 && column>=16 && column<=144) /*Issue an Item*/
return('5');
if(button==1 && row==96 && column>=16 && column<=152) /*Items to order*/
return('6');
}
ch=getch();
return ch;
}
/*Reads a valid id and its information,returns 0 if id already exists*/
getdata()
{
char tmp[8];
float tst;
_setcursortype(_NORMALCURSOR);
print2screen(3,33,"Enter Item Code: ",BROWN,BLUE,0);fflush(stdin);gotopos(3,53);
scanf("%s",&tmp);
if(CheckId(tmp)==0 && fEdit == FALSE)
{
messagebox(10,33,"The id already exists. ","Error ",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
return 0;
}
strcpy(inv_stock.itemcode,tmp); /*Means got a correct item code*/
print2screen(4,33,"Name of the Item: ",BROWN,BLUE,0);fflush(stdin);gotopos(4,53);
gets(inv_stock.itemname);
print2screen(5,33,"Price of Each Unit: ",BROWN,BLUE,0);fflush(stdin);gotopos(5,53);
scanf("%f",&inv_stock.itemrate);
print2screen(6,33,"Quantity: ",BROWN,BLUE,0);fflush(stdin);gotopos(6,53);
scanf("%f",&inv_stock.itemqty);
print2screen(7,33,"Reorder Level: ",BROWN,BLUE,0);fflush(stdin);gotopos(7,53);
scanf("%d",&inv_stock.minqty);
_setcursortype(_NOCURSOR);
return 1;
}
/*Returns 0 if the id already exists in the database, else returns 1*/
int CheckId(char item[8])
{
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
if(strcmp(inv_stock.itemcode,item)==0)
return(0);
return(1);
}
/*Displays an Item*/
DisplayItemRecord(char idno[8])
{
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
if(strcmp(idno,inv_stock.itemcode)==0)
DisplayItemInfo();
return;
}
/*Displays an Item information*/
DisplayItemInfo()
{
int r=7;
textcolor(menutxtfgclr);
textbackground(menutxtbgclr);
gotopos(r,33);
cprintf("Item Code: %s"," ");
gotopos(r,33);
cprintf("Item Code: %s",inv_stock.itemcode);
gotopos(r+1,33);
cprintf("Name of the Item: %s"," ");
gotopos(r+1,33);
cprintf("Name of the Item: %s",inv_stock.itemname);
gotopos(r+2,33);
cprintf("Price of each unit: %.2f"," ");
gotopos(r+2,33);
cprintf("Price of each unit: %.2f",inv_stock.itemrate);
gotopos(r+3,33);
cprintf("Quantity in Stock: %.4f"," ");
gotopos(r+3,33);
cprintf("Quantity in Stock: %.4f",inv_stock.itemqty);
gotopos(r+4,33);
cprintf("Reorder Level: %d"," ");
gotopos(r+4,33);
cprintf("Reorder Level: %d",inv_stock.minqty);
gotopos(r+5,33);
cprintf("\nPress Any Key...");fflush(stdin);getch();
textbackground(BROWN);
textcolor(BLUE);
return;
}
/*This function will return 0 if an item cannot issued, else issues the item*/
IssueItem()
{
float issueqnty;
DisplayItemInfo();
print2screen(15,33,"Enter Quantity: ",BROWN,BLUE,0);fflush(stdin);gotopos(15,49);
scanf("%f",&issueqnty);
/*If the stock of the item is greater than minimum stock*/
if((inv_stock.itemqty - issueqnty) >= inv_stock.minqty)
{
textcolor(BLUE);
textbackground(BROWN);
gotopos(18,33);
cprintf("%.4f Item(s) issued.",issueqnty);
gotopos(19,33);
cprintf("You should pay RS. %.2f",issueqnty*inv_stock.itemrate);getch();
textcolor(BLUE);
inv_stock.itemqty-=issueqnty; /*Updating quantity for the item in stock*/
fseek(dbfp,-stocksize,SEEK_CUR);
fwrite(&inv_stock,stocksize,1,dbfp);
return issueqnty;
}
/* If the stock of the item is less than minimum stock.ie Reorder level*/
else
{
messagebox(10,33,"Insufficient quantity in stock.","Insufficient Stock",' ',mboxbrdrclr,mboxbgclr,mboxfgclr,0);
gotopos(17,33);
textcolor(BLUE);
textbackground(BROWN);
cprintf("ONLY %.4f pieces of the Item can be issued.",inv_stock.itemqty-inv_stock.minqty);
gotopos(18,33);
cprintf("Press Any Key...");getch();
textcolor(BLUE);
textbackground(BROWN);
return 0;
}
}
/* Calculates the total investment amount for the stock available*/
float getInvestmentInfo(void)
{
tot_investment=0;
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
tot_investment+=(inv_stock.itemrate*inv_stock.itemqty);
return tot_investment;
}
/* Creates a backup file "Bakckup" of "inv_stock.dat"*/
BackupDatabase(void)
{
FILE *fback;
fback=fopen("d:/Backup.dat","w");
rewind(dbfp);
while(fread(&inv_stock,stocksize,1,dbfp)==1)
fwrite(&inv_stock,stocksize,1,fback);
fclose(fback);
return;
}
/*This structure is used color settings for the application*/
struct colors
{
char cfg_name[10];
int mboxbrdrclr;
int mboxbgclr;
int mboxfgclr;
int menutxtbgclr;
int menutxtfgclr;
int appframeclr;
int section1_symb;
int section1_bgclr;
int section1_fgclr;
int section2_symb;
int section2_bgclr;
int section2_fgclr;
}clr;
const long int clrsize=sizeof(clr);
/* Gets the display configuration for the application*/
getConfiguration()
{
FILE *flast;
flast=fopen("lastcfg","r+");
if(flast==NULL)
{
SetDefaultColor();
return 0;
}
rewind(flast);
/*Reads the first record.*/
fread(&clr,clrsize,1,flast);
#ifdef OKAY
if(strcmp(clr.cfg_name,"lastclr")!=0)
{
SetDefaultColor();
fclose(flast);
return 0;
}
#endif
mboxbrdrclr=clr.mboxbrdrclr;mboxbgclr=clr.mboxbgclr;mboxfgclr=clr.mboxfgclr;
menutxtbgclr=clr.menutxtbgclr;menutxtfgclr=clr.menutxtfgclr;appframeclr=clr.appframeclr;
section1_symb=clr.section1_symb;section1_bgclr=clr.section1_bgclr;section1_fgclr=clr.section1_fgclr;
section2_symb=clr.section2_symb;section2_bgclr=clr.section2_bgclr;section2_fgclr=clr.section2_fgclr;
fclose(flast);
return 1;
}
/* Sets the default color settings for the application*/
SetDefaultColor()
{
mboxbrdrclr=BLUE,mboxbgclr=GREEN,mboxfgclr=WHITE;
menutxtbgclr=BROWN,menutxtfgclr=BLUE,appframeclr=CYAN;
section1_symb=' ',section1_bgclr=BROWN,section1_fgclr=BLUE;
section2_symb=' ',section2_bgclr=BROWN,section2_fgclr=BLUE;
return 1;
}
/* Adds animation to a text */
BlinkText(const int r,const int c,char txt[],int bgclr,int fgclr,int BGCLR2,int FGCLR2,int blink,const int dly)
{
int len=strlen(txt);
BGCLR2=bgclr;FGCLR2=BLUE;
htskin(r,c,' ',len,bgclr,bgclr,0);
print2screen(r,c,txt,bgclr,fgclr,blink);
write2screen(r,c+animcounter+1,txt[animcounter],BGCLR2,FGCLR2,0);
write2screen(r,c+animcounter+2,txt[animcounter+1],BGCLR2,FGCLR2,0);
write2screen(r,c+animcounter+3,txt[animcounter+2],BGCLR2,FGCLR2,0);
write2screen(r,c+animcounter+4,txt[animcounter+3],BGCLR2,FGCLR2,0);
write2screen(r,c+animcounter+5,txt[animcounter+4],BGCLR2,FGCLR2,0);
write2screen(r,c+animcounter+6,txt[animcounter+5],BGCLR2,FGCLR2,0);
delay(dly*2);
write2screen(r,c+animcounter+1,txt[animcounter],bgclr,fgclr,0);
write2screen(r,c+animcounter+2,txt[animcounter+1],bgclr,fgclr,0);
write2screen(r,c+animcounter+3,txt[animcounter+2],bgclr,fgclr,0);
write2screen(r,c+animcounter+4,txt[animcounter+3],bgclr,fgclr,0);
write2screen(r,c+animcounter+5,txt[animcounter+4],bgclr,fgclr,0);
write2screen(r,c+animcounter+6,txt[animcounter+5],bgclr,fgclr,0);
animcounter+=1;
if(animcounter+5 >= len) animcounter=0;
return;
}
/* Displays a single charector with its attrribute*/
write2screen(int row,int col,char ch,int bg_color,int fg_color,int blink)
{
int attr;
char far *v;
char far *ptr=(char far*)0xB8000000;
if(blink!=0)
blink=128;
attr=bg_color+blink;
attr=attr<<4;
attr+=fg_color;
attr=attr|blink;
v=ptr+row*160+col*2; /*Calculates the video memory address corresponding to row & column*/
*v=ch;
v++;
*v=attr;
return 0;
}
/* Prints text with color attribute direct to the screen*/
print2screen(int row,int col,char string[],int bg_color,int fg_color,int blink)
{
int i=row,j=col,strno=0,len;
len=strlen(string);
while(j<80)
{
j++;
if(j==79)
{
j=0;
i+=1;
}
write2screen(i,j,string[strno],bg_color,fg_color,blink); /*See below function*/
strno+=1;
if(strno > len-1)
break;
}
return;
}
/* Prints text horizondally*/
htskin(int row,int column,char symb,int no,int bg_color,int fg_color,int blink)
{
int i;
for(i=0;i<no;i++)
write2screen(row,column++,symb,bg_color,fg_color,blink); /*Print one symbol*/
return;
}
/*Print text vertically*/
vtskin(int row,int column,char symb,int no,int bg_color,int fg_color,int blink)
{
int i;
for(i=0;i<no;i++)
write2screen(row++,column,symb,bg_color,fg_color,blink); /*Print one symbol*/
return;
}
/* Shows a message box*/
messagebox(int row,int column,char message[50],char heading[10],char symb,int borderclr,int bg_color,int fg_color,int blink)
{
int len;
char key,image[1000];
len=strlen(message);
capture_image(row,column,row+3,column+len+7,&image);
draw_mbox(row,column,row+3,column+len+7,symb,symb,borderclr,YELLOW,blink,borderclr,YELLOW,blink);
fillcolor(row+1,column+1,row+2,column+len+6,' ',bg_color,bg_color,0);
print2screen(row+1,column+2,message,bg_color,fg_color,blink);
print2screen(row+2,column+2,"Press Any Key... ",bg_color,fg_color,blink);
print2screen(row,column+1,heading,borderclr,fg_color,blink);
sound(400);
delay(200);
nosound();
fflush(stdin);
key=getch();
put_image(row,column,row+3,column+len+7,&image);
return key;
}
/* Fills color in a region*/
fillcolor(int top_row,int left_column,int bottom_row,int right_column,char symb,int bg_color,int fg_color,int blink)
{
int i,j;
for(i=top_row;i<=bottom_row;i++)
htskin(i,left_column,symb,right_column-left_column+1,bg_color,fg_color,blink);
return;
}
/* Prints a message box with an appropriate message*/
draw_mbox(int trow,int tcolumn,int brow,int bcolumn,char hsymb,char vsymb,int hbg_color,int hfg_color,int hblink,int vbg_color,int vfg_color,int vblink)
{
htskin(trow,tcolumn,hsymb,bcolumn-tcolumn,hbg_color,hfg_color,hblink);
htskin(brow,tcolumn,hsymb,bcolumn-tcolumn,hbg_color,hfg_color,hblink);
vtskin(trow,tcolumn,vsymb,brow-trow+1,vbg_color,vfg_color,vblink);
vtskin(trow,bcolumn,vsymb,brow-trow+1,vbg_color,vfg_color,vblink);
return;
}
/* Copies the txt mode image below the messagebox*/
capture_image(int toprow,int leftcolumn,int bottomrow,int rightcolumn,int *image)
{
char far *vidmem;
int i,j,count;
count=0;
for(i=toprow;i<=bottomrow;i++)
for(j=leftcolumn;j<=rightcolumn;j++)
{
vidmem=(char far*)0xB8000000+(i*160)+(j*2); /*Calculates the video memory address corresponding to row & column*/
image[count]=*vidmem;
image[count+1]=*(vidmem+1);
count+=2;
}
return;
}
/* Places an image on the screen*/
put_image(int toprow,int leftcolumn,int bottomrow,int rightcolumn,int image[])
{
char far *ptr=(char far*)0xB8000000;
char far *vid;
int i,j,count;
count=0;
for(i=toprow;i<=bottomrow;i++)
for(j=leftcolumn;j<=rightcolumn;j++)
{
vid=ptr+(i*160)+(j*2); /*Calculates the video memory address corresponding to row & column*/
*vid=image[count];
*(vid+1)=image[count+1];
count+=2;
}
return;
}
/* To move the curser position to derired position*/
gotopos(int r,int c)
{
union REGS i,o;
i.h.ah=2;
i.h.bh=0;
i.h.dh=r;
i.h.dl=c;
int86(16,&i,&o);
return 0;
}
union REGS i,o;
/* Initialize the mouse*/
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
/* Shows the mouse pointer*/
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
return;
}
/* Get the mouse position*/
getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.dx;
*y=o.x.cx;
return 0;
}
/* Restores the default text mode*/
setdefaultmode()
{
set25x80();
setdefaultcolor();
return;
}
/* Sets the default color and cursor of screen*/
setdefaultcolor()
{
int i;
char far *vidmem=(char far*)0xB8000000;
window(1,1,80,25);
clrscr();
for (i=1;i<4000;i+=2)
*(vidmem+i)=7;
_setcursortype(_NORMALCURSOR);
return;
}
/* Sets 25x80 Text mode*/
set25x80()
{
asm mov ax,0x0003;
asm int 0x10;
return;
}
void password()
{
char salt[2]; /* Salt for the crypt() function */
const char *salt_chars = "abcdefghijklmnopqrstuvwxyz" /* Range of character supported */
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* as a value for salt in crypt() */
"0123456789";
char username[BUFSIZ], password1[BUFSIZ], /* Buffers for user input and comparison */
password2[BUFSIZ], *buf;
char filename[BUFSIZ]; /* Buffer for filename */
FILE *outfile; /* File handle */
/* Build salt */
srand(time(NULL));
salt[0] = salt_chars[rand() % 62];
salt[1] = salt_chars[rand() % 62];
/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);
/* Get the username */
printf("Username: ");
scanf("%s", username);
do {
/* Get the password */
buf = getpass("Password: ");
/* Copy to a "stable" pointer */
sprintf(password1, "%s", buf);
/* Get the password */
buf = getpass("Enter Again: ");
/* Copy to a "stable" pointer */
sprintf(password2, "%s", buf);
/* See if the passwords are the same */
if(strcmp(password1, password2) != 0)
printf("\nPasswords do not match!\nTry again.\n\n");
} while(strcmp(password1, password2) != 0);
/* Encrypt the password */
buf = crypt(password1, salt);
/* Open the output file for append */
if((outfile = fopen(filename, "a+")) == NULL){
printf("\nFile error!\nAborting...\n");
} else {
/* Print the record to the file */
fprintf(outfile, "%s:%s\n", username, buf);
} /* End if */
/* Close the file */
fclose(outfile);
}
void check_password()
{
char username[BUFSIZ], *password; /* User input buffers */
char buf[BUFSIZ]; /* File input buffer */
char *user_file, *pass_file; /* Buffers for values in file */
char filename[BUFSIZ]; /* Filename buffer */
FILE *infile; /* File handle */
/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);
/* Get the username from the user */
printf("Username: ");
scanf("%s", username);
/* Get the password from the user */
password = getpass("Password: ");
/* Open the file */
if((infile = fopen(filename, "r")) == NULL){
printf("\nFile error!\nAborting...\n");
} else {
/* Loop throught the file */
while (!feof(infile)) {
/* Initialize with empty string */
buf[0] = '\0';
/* Read in one line */
fscanf(infile, "%s", buf);
/* If it's an empty line, continue */
if(strlen(buf) == 0) continue;
/* Point to the buffer */
user_file = buf;
/* Point to the delimiter in the buffer */
pass_file = strchr(buf, ':');
/* Change the delimiter to a nul character */
pass_file[0] = '\0';
/* Move to the next character */
pass_file++;
/* See if this matches the name the user entered */
if(strcmp(user_file, username) == 0){
/* See if the passwords match */
if(strcmp(crypt(password, pass_file), pass_file) == 0){
printf("Correct password...\n");
} else {
printf("Invalid password!\n\n");
} /* End if */
/* We found what we wanted; get out of loop */
break;
} /* End if */
} /* End while */
} /* End if */
/* Close the file */
fclose(infile);
}