#include< stdio.h>
#include< conio.h>
void main()
{
int page[20],n,f,i,j,a[20],pf=0,k=0,match;
clrscr();
printf("enter the length of the reference string");
scanf("%d",&n);
printf("enter the frame size");
scanf("%d",&f);
printf("enter reference string");
for(i=0;i< n;i++)
scanf("%d",&page[i]);
for(j=0;j< f;j++)
a[j]=-1;
printf("\n-1\t-1\t-1\n\n");
k=0;
for(i=0;i< n;i++)
{
match=0;
for(j=0;j< f;j++)
if(a[j]==page[i])
{
match=1;
}
if(match==1)
{
goto x;
}
for(j=0;j< f;j++)
if(a[j]==-1)
{
a[j]=page[i];
pf++;
goto x;
}
a[k]=page[i];
k=(k+1)%f;
pf++;
x:;
for(j=0;j< f;j++)
printf("%d \t",a[j]);
printf("\n\n");
}
printf("no of page faults are %d",pf);
getch();
}
Saturday, May 22, 2010
Optimal Program using C
First In First Out program using C (F.I.F.O)
#include< stdio.h>
#include< conio.h>
void main()
{
int page[20],n,f,i,j,a[20],pf=0,k=0,match;
clrscr();
printf("enter the length of the reference string");
scanf("%d",&n);
printf("enter the frame size");
scanf("%d",&f);
printf("enter reference string");
for(i=0;i< n;i++)
scanf("%d",&page[i]);
for(j=0;j< f;j++)
a[j]=-1;
printf("\n-1\t-1\t-1\n\n");
k=0;
for(i=0;i< n;i++)
{
match=0;
for(j=0;j< f;j++)
if(a[j]==page[i])
{
match=1;
}
if(match==1)
{
goto x;
}
for(j=0;j< f;j++)
if(a[j]==-1)
{
a[j]=page[i];
pf++;
goto x;
}
a[k]=page[i];
k=(k+1)%f;
pf++;
x:;
for(j=0;j< f;j++)
printf("%d \t",a[j]);
printf("\n\n");
}
printf("no of page faults are %d",pf);
getch();
}
Least Recently Used program using C(L.R.U)
#include< stdio.h>
#include< conio.h>
void main()
{
int page[20],n,f,i,j,a[20],pf=0,match,index,counter=0,count[20];
clrscr();
printf("enter the length of the reference string");
scanf("%d",&n);
printf("enter the frame size");
scanf("%d",&f);
printf("enter reference string");
for(i=0;i< n;i++)
scanf("%d",&page[i]);
for(j=0;j< f;j++)
a[j]=-1;
printf("\n-1\t-1\t-1\n\n");
for(i=0;i< n;i++)
{
match=0;
for(j=0;j< f;j++)
if(a[j]==page[i])
{
match=1;
counter++;
count[j]=counter;
}
if(match==1)
{
goto x;
}
for(j=0;j< f;j++)
if(a[j]==-1)
{
a[j]=page[i];
counter++;
count[j]=counter;
pf++;
goto x;
}
index=0;
for(j=0;j< f;j++)
{
if(count[j]< count[index])
{
index=j;
}
}
a[index]=page[i];
counter++;
count[index]=counter;
pf++;
x:;
for(j=0;j< f;j++)
printf("%d \t",a[j]);
printf("\n\n");
}
printf("no of page faults are %d",pf);
getch();
}
Saturday, May 1, 2010
Bit stuffing of data using C
#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
int i,j,a=0;
char sdata[17],stuff[40],rdata[17];
clrscr();
printf("Enter the data in the form of 16 bits : ");
gets(sdata);
printf("\nSender frame : ");
puts(sdata);
for(i=0,j=0;j <16;j++)
{
if(sdata[j]=='1')
{
a=a+1;
if((a==5)&&(sdata[j+1]=='0'))
{
stuff[i]=sdata[j];
i++;
stuff[i]='0';
a=0;
i++;
}
else if(a==6)
{
stuff[i]='0';
a=0;
i++;
j--;
}
else
{
stuff[i]=sdata[j];
i++;
}
}
else
{
stuff[i]=sdata[j];
i++;
a=0;
}
}
stuff[i]='\0';
printf("\nSender frame with bit stuffed : ");
puts(stuff);
a=0;
i=0;
for(j=0;stuff[j]!='\0';j++)
{
if(stuff[j]=='1')
{
a=a+1;
if(a==5)
{
a=0;
rdata[i]=stuff[j];
j++;
i++;
}
else
{
rdata[i]=stuff[j];
i++;
}
}
else
{
a=0;
rdata[i]=stuff[j];
i++;
}
}
rdata[i]='\0';
printf("\nThe reciever data without stuffed bit is : ");
puts(rdata);
if(strcmp(sdata,rdata)==0)
printf("\nTransmission successful");
else
printf("\nTransmission error");
getch();
}
#include< conio.h>
#include< string.h>
void main()
{
int i,j,a=0;
char sdata[17],stuff[40],rdata[17];
clrscr();
printf("Enter the data in the form of 16 bits : ");
gets(sdata);
printf("\nSender frame : ");
puts(sdata);
for(i=0,j=0;j <16;j++)
{
if(sdata[j]=='1')
{
a=a+1;
if((a==5)&&(sdata[j+1]=='0'))
{
stuff[i]=sdata[j];
i++;
stuff[i]='0';
a=0;
i++;
}
else if(a==6)
{
stuff[i]='0';
a=0;
i++;
j--;
}
else
{
stuff[i]=sdata[j];
i++;
}
}
else
{
stuff[i]=sdata[j];
i++;
a=0;
}
}
stuff[i]='\0';
printf("\nSender frame with bit stuffed : ");
puts(stuff);
a=0;
i=0;
for(j=0;stuff[j]!='\0';j++)
{
if(stuff[j]=='1')
{
a=a+1;
if(a==5)
{
a=0;
rdata[i]=stuff[j];
j++;
i++;
}
else
{
rdata[i]=stuff[j];
i++;
}
}
else
{
a=0;
rdata[i]=stuff[j];
i++;
}
}
rdata[i]='\0';
printf("\nThe reciever data without stuffed bit is : ");
puts(rdata);
if(strcmp(sdata,rdata)==0)
printf("\nTransmission successful");
else
printf("\nTransmission error");
getch();
}
Character stuffing of data using C
#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char sdata[17],rdata[17],cstuff[40];
int i,j;
clrscr();
printf("Enter any 16 characters : ");
gets(sdata);
printf("\nOriginal data : %s\n",sdata);
for(i=0,j=0;sdata[i]!='\0';i++)
{
if(sdata[i]=='f'&&sdata[i+1]=='l'&&sdata[i+2]=='a'&&sdata[i+3]=='g')
{
cstuff[j++]='e';
cstuff[j++]='s';
cstuff[j++]='c';
cstuff[j++]=sdata[i];
}
else if(sdata[i]=='e'&&sdata[i+1]=='s'&&sdata[i+2]=='c')
{
cstuff[j++]='e';
cstuff[j++]='s';
cstuff[j++]='c';
cstuff[j++]=sdata[i];
}
else
{
cstuff[j++]=sdata[i];
}
}
cstuff[j]='\0';
printf("\nAfter character stuffing : %s\n",cstuff);
for(i=0,j=0;cstuff[i]!='\0';i++)
{
if(cstuff[i]=='e'&&cstuff[i+1]=='s'&&cstuff[i+2]=='c')
{
i=i+3;
rdata[j++]=cstuff[i];
}
else
{
rdata[j++]=cstuff[i];
}
}
rdata[j]='\0';
printf("\nReceiver data : %s",rdata);
if(strcmp(sdata,rdata)==0)
printf("\n\n\nTransmission successful");
else
printf("\n\n\nTransmission error");
getch();
}
#include< conio.h>
#include< string.h>
void main()
{
char sdata[17],rdata[17],cstuff[40];
int i,j;
clrscr();
printf("Enter any 16 characters : ");
gets(sdata);
printf("\nOriginal data : %s\n",sdata);
for(i=0,j=0;sdata[i]!='\0';i++)
{
if(sdata[i]=='f'&&sdata[i+1]=='l'&&sdata[i+2]=='a'&&sdata[i+3]=='g')
{
cstuff[j++]='e';
cstuff[j++]='s';
cstuff[j++]='c';
cstuff[j++]=sdata[i];
}
else if(sdata[i]=='e'&&sdata[i+1]=='s'&&sdata[i+2]=='c')
{
cstuff[j++]='e';
cstuff[j++]='s';
cstuff[j++]='c';
cstuff[j++]=sdata[i];
}
else
{
cstuff[j++]=sdata[i];
}
}
cstuff[j]='\0';
printf("\nAfter character stuffing : %s\n",cstuff);
for(i=0,j=0;cstuff[i]!='\0';i++)
{
if(cstuff[i]=='e'&&cstuff[i+1]=='s'&&cstuff[i+2]=='c')
{
i=i+3;
rdata[j++]=cstuff[i];
}
else
{
rdata[j++]=cstuff[i];
}
}
rdata[j]='\0';
printf("\nReceiver data : %s",rdata);
if(strcmp(sdata,rdata)==0)
printf("\n\n\nTransmission successful");
else
printf("\n\n\nTransmission error");
getch();
}
Cyclic Redundancy check Using C
#include< stdio.h>
#include< conio.h>
#include< string.h>
char I[40],d[40]="1100000001011";
char a[12]="000000000000",t[13];
int i=0,j,l,n,p;
void main()
{
void division(void);
clrscr();
printf("\n enter a message string : ");
gets(I);
n=strlen(I);
printf("\nOriginal message : %s \n",I);
strcat(I,a);
p=n+12;
division();
printf("\nThe reminder at sender is : %s \n",t);
for(i=n,j=1;i
#include< conio.h>
#include< string.h>
char I[40],d[40]="1100000001011";
char a[12]="000000000000",t[13];
int i=0,j,l,n,p;
void main()
{
void division(void);
clrscr();
printf("\n enter a message string : ");
gets(I);
n=strlen(I);
printf("\nOriginal message : %s \n",I);
strcat(I,a);
p=n+12;
division();
printf("\nThe reminder at sender is : %s \n",t);
for(i=n,j=1;i
{
I[i]=t[j];
}
printf("\nThe padded string to be sent : %s \n",I);
division();
printf("\nThe remainder string at receiver : %s\n",t);
printf("\nThe received message is correct");
getch();
}
void division(void)
{
for(i=0;i <13;i++)
t[i]=I[i];
while(i
{
for(l=0;l <13;l++)
{
if(t[l]==d[l])
t[l]='0';
else
t[l]='1';
}
if(t[0]=='0')
{
for(j=0;j <12;j++)
t[j]=t[j+1];
t[j]=I[i++];
}
}
while(t[0]=='1')
for(l=0;l <13;l++)
{
if(t[l]==d[l])
t[l]='0';
else
t[l]='1';
}
t[l]='\0';
}
Thursday, April 29, 2010
#include < stdio.h >
#include < conio.h >
void main()
{
int a[20],c[20],page[20];
int i,j,k,f,n;
int count=0;fault=0,hit=0;
clrscr();
printf("Enter reference string length : ");
scanf("%d",&n);
printf("Enter reference string");
for(i=0;i < n;i++)
scanf("%d",&page[i]);
printf("Enter frame size ");
scanf("%d",&f);
for(i=0;i < f;i++)
{
for(j=0;j < fault;j++)
{
if(page[i]==a[j])
{
hit++;
c[j]=++count;
goto xyz;
}
}
a[i]=page[i];
fault++;
c[i]=++count;
xyz: ;
}
#include < conio.h >
void main()
{
int a[20],c[20],page[20];
int i,j,k,f,n;
int count=0;fault=0,hit=0;
clrscr();
printf("Enter reference string length : ");
scanf("%d",&n);
printf("Enter reference string");
for(i=0;i < n;i++)
scanf("%d",&page[i]);
printf("Enter frame size ");
scanf("%d",&f);
for(i=0;i < f;i++)
{
for(j=0;j < fault;j++)
{
if(page[i]==a[j])
{
hit++;
c[j]=++count;
goto xyz;
}
}
a[i]=page[i];
fault++;
c[i]=++count;
xyz: ;
}
Round Robin program using C
#include < stdio.h >
#include < conio.h >
void main()
{
int t1=0,wt,twt=0,tat=0,n,i,j,tq,c=0;
int bt[10],a[10],f[10],b[10];
clrscr();
printf("\n enter no of process: ");
scanf("%d",&n);
for(i=0;i < n;i++)
{
f[i]=0; /* indicates process is completed if f[i]==1 */
a[i]=i+1; /* indicates process names */
}
for(i=0;i < n;i++)
{
printf("Enter the Burst time of process %d ",i+1);
scanf("%d",&bt[i]);
b[i]=bt[i];
}
printf("enter the time quantum : ");
scanf("%d",&tq);
printf("given burst times are:\n");
for(i=0;i < n;i++)
{
printf("%d\t",bt[i]);
}
i=0;
while(c < n)
{
if(bt[i]<=tq && f[i]==0)
{
t1=t1+bt[i];
printf("\n turn around time of p%d is : %d",a[i],t1);
wt=t1-b[i];
printf("\n Waiting time of p%d is : %d",a[i],wt);
tat=tat+t1;
twt=twt+wt;
f[i]=1;
c++; /* indicating how many processes are completing */
}
else
{
if(f[i]==0)
{
bt[i]=bt[i]-tq;
t1=t1+tq;
}
}
i=(i+1)%n; */ for rotating value of */
}
printf("\n\n Avg turn around time is : %d\n",tat/n);
printf("\n\n Avg Waiting time is : %d",twt/n);
getch();
}
Output:
Enter no of process : 3
Enter the Burst time of process 1 : 20
Enter the Burst time of process 2 : 5
Enter the Burst time of process 3 : 3
Enter time quantum : 4
Given burst times are :
20 5 3
turn around time of p3 is : 11
Waiting time of p3 is : 8
turn around time of p2 is : 16
Waiting time of p2 is : 11
turn around time of p1 is : 28
Waiting time of p1 is :8
Avg turn arround time is : 18
Avg waiting time is : 9
#include < conio.h >
void main()
{
int t1=0,wt,twt=0,tat=0,n,i,j,tq,c=0;
int bt[10],a[10],f[10],b[10];
clrscr();
printf("\n enter no of process: ");
scanf("%d",&n);
for(i=0;i < n;i++)
{
f[i]=0; /* indicates process is completed if f[i]==1 */
a[i]=i+1; /* indicates process names */
}
for(i=0;i < n;i++)
{
printf("Enter the Burst time of process %d ",i+1);
scanf("%d",&bt[i]);
b[i]=bt[i];
}
printf("enter the time quantum : ");
scanf("%d",&tq);
printf("given burst times are:\n");
for(i=0;i < n;i++)
{
printf("%d\t",bt[i]);
}
i=0;
while(c < n)
{
if(bt[i]<=tq && f[i]==0)
{
t1=t1+bt[i];
printf("\n turn around time of p%d is : %d",a[i],t1);
wt=t1-b[i];
printf("\n Waiting time of p%d is : %d",a[i],wt);
tat=tat+t1;
twt=twt+wt;
f[i]=1;
c++; /* indicating how many processes are completing */
}
else
{
if(f[i]==0)
{
bt[i]=bt[i]-tq;
t1=t1+tq;
}
}
i=(i+1)%n; */ for rotating value of */
}
printf("\n\n Avg turn around time is : %d\n",tat/n);
printf("\n\n Avg Waiting time is : %d",twt/n);
getch();
}
Enter the Burst time of process 1 : 20
Enter the Burst time of process 2 : 5
Enter the Burst time of process 3 : 3
Enter time quantum : 4
Given burst times are :
20 5 3
turn around time of p3 is : 11
Waiting time of p3 is : 8
turn around time of p2 is : 16
Waiting time of p2 is : 11
turn around time of p1 is : 28
Waiting time of p1 is :8
Avg turn arround time is : 18
Avg waiting time is : 9
Monday, March 1, 2010
Program for Prime numbers
1.
main()
{
int a,c=0,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a=0)
{
c=c+1;
}
}
if (c=2)
{ printf("the given number is prime"); }
else
printf("the given number is not prime");
}
main()
{
int a,c=0,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a=0)
{
c=c+1;
}
}
if (c=2)
{ printf("the given number is prime"); }
else
printf("the given number is not prime");
}
2.
main()
{
int i,j=2,ch=0;
clrscr();
printf("\nENTER ANY NUMBER");
scanf("%d",&i);
while(j<=i/2)
{
if(i%j==0)
{
printf("%d IS NOT PRIME",i);
ch=1;
break;
}
else
{
j++;
}
}
if(ch==0)
{
printf("%d IS PRIME",i);
}
}
Fibonacci program using C
This program prints the Fibonacci series
#include
void main(void)
{
int i,j,k,n;
clrscr();
i=0;
j=1;
printf("%d %d ",i,j);
for(n=0;n<=5;n++)
{
k=i+j;
i=j;
j=k;
printf("%d ",k);
}
getch();
}
RSA algorithm using C
/* C program for the Implementation Of RSA
Algorithm */
#include< stdio.h>
#include< conio.h>
int phi,M,n,e,d,C,FLAG;
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}
/*************** OUTPUT *****************
Enter Two Relatively Prime Numbers : 7 17
F(n) = 96
Enter e : 5
Public Key : {5,119}
Private Key : {77,119}
Enter The Plain Text : 19
Encrypted keyword : 66
Enter the Cipher text : 66
Decrypted keyword : 19 */
Making a small password/login form in C
Well, here's a very simple program to write the entries into a file, using the unix/linux/bsd crypt() function to encrypt the password and getpass() function to get the password unix style (no keyboard echo, at all):
#include
#include
#include
#include
int main(int argc, char **argv){
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);
} /* End main() */
And here's how you would check it:
#include
#include
#include
#include
int main(int argc, char **argv){
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);
} /* End main() */
That's basically all there is to it. I used the most basic form--there are better ways (using a variety of encryption algorithms--like ones using stronger encryption than 56 bit, like this one does). Keep in mind, you have to compile in the crypt shared library--like this using gcc:
gcc -lcrypt -o program_name program_name.c
Otherwise, it'll blow up when you run it (coredump).
#include
#include
#include
int main(int argc, char **argv){
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);
} /* End main() */
And here's how you would check it:
#include
#include
#include
#include
int main(int argc, char **argv){
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);
} /* End main() */
That's basically all there is to it. I used the most basic form--there are better ways (using a variety of encryption algorithms--like ones using stronger encryption than 56 bit, like this one does). Keep in mind, you have to compile in the crypt shared library--like this using gcc:
gcc -lcrypt -o program_name program_name.c
Otherwise, it'll blow up when you run it (coredump).