Code:
#include<io.h>
#include<dir.h>
#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
typedef struct{
char name[25];
unsigned short mode;
char user[10];
int group_id[10];
int size;
char time_mod[26];
}fileSig;
fileSig old_record[50];
fileSig new_record[50];
typedef struct{
char user[30];
char pass[30];
}userSig;
userSig student[50];
FILE *stream;
struct stat statbuf;
int restore(){
int i = -1;
int u = 0;
int p = 0;
int letter;
FILE *retrive;
retrive = fopen("D:/NEW/users.txt","a+");
while(!feof(retrive)){
char character;
fscanf(retrive,"%c",&character);
if(character == '`'){
i++;
letter = 0;
u = 1;
p = 0;
continue;
}
else if(character == ','){
letter = 0;
u = 0;
p = 1;
continue;
}
else {
if(u == 1){
student[i].user[letter] = character;
letter++;
}
else if(p == 1 && character !=';'){
student[i].pass[letter] = character;
letter++;
}
}
}
fclose(retrive);
return i+1;
}
int reg(char u[],char p[],int t){
if(!(strcmp(u,"0"))){
printf("\n\nYou are exiting... Please Wait...");
exit(1);
}
int i = 0;
int compare;
while(i < t){
compare = strcmp(student[i].user,u);
i++;
if(!(compare)){
return 0;
}
}
FILE *entry;
entry = fopen("D:/NEW/users.txt","a+");
fprintf(entry,"`%s,%s;",u,p);
fclose(entry);
strcpy(student[t].user,u);
strcpy(student[t].pass,p);
return 1;
}
int verify(char u[],char p[], int t){
if(!(strcmp(u,"0"))){
printf("\n\nYou are exiting... Please Wait...");
exit(1);
}
int i = 0;
int found_u = 0;
int found_p = 0;
int compare;
while(i < t){
compare = strcmp(student[i].user,u);
if(!(compare)){
found_u = 1;
break;
}
i++;
}
if(found_u == 1){
found_p = !(strcmp(student[i].pass,p));
}
if((found_p == 1) && (found_u == 1)){
return 1;
}
else{
return 0;
}
}
int verify_file_exists(char f[],int t){
int i = 0;
int found = 0;
int compare1,compare2;
while(i < t){
compare1 = strcmp(old_record[i].name,f);
compare2 = strcmp(new_record[i].name,f);
if((!(compare1)) || (!(compare2))){
found = 1;
break;
}
i++;
}
if(found == 1){
return 1;
}
else{
return 0;
}
}
void verifyFiles(int j, char u[]){
fstat(fileno(stream), &statbuf);
printf("File Name\t\t:\t%s\n",new_record[j].name);
old_record[j].mode = new_record[j].mode;
if ((statbuf.st_mode & S_IREAD) && (statbuf.st_mode &S_IWRITE))
new_record[j].mode = 6;
else if (statbuf.st_mode & S_IREAD)
new_record[j].mode = 4;
else if (statbuf.st_mode & S_IWRITE)
new_record[j].mode = 2;
else
new_record[j].mode = 0;
printf("File Mode\t\t:\t%d\n",new_record[j].mode);
if(!(strcmp(new_record[j].user,'\0'))){
strcpy(new_record[j].user,u);
printf("User\t\t\t:\t%s\n",new_record[j].user);
}
else{
strcpy(old_record[j].user,new_record[j].user);
strcpy(new_record[j].user,u);
if(strcmp(old_record[j].user,new_record[j].user)){
printf("Old User\t\t:\t%s\n",old_record[j].user);
printf("New User\t\t:\t%s\n",new_record[j].user);
}
else{
printf("User\t\t\t:\t%s\n",new_record[j].user);
}
}
if(new_record[j].size == 0){
new_record[j].size = statbuf.st_size;
printf("File Size\t\t:\t%d\n",new_record[j].size);
}
else{
old_record[j].size = new_record[j].size;
new_record[j].size = statbuf.st_size;
if(old_record[j].size != new_record[j].size){
printf("Old File Size\t:\t%d\n",old_record[j].size);
printf("New File Size\t:\t%d\n",new_record[j].size);
}
else{
printf("File Size\t\t:\t%d\n",new_record[j].size);
}
}
if(!(strcmp(new_record[j].time_mod,'\0'))){
strcpy(new_record[j].time_mod,ctime(&statbuf.st_ctime));
printf("File Accessed Recently\t:\t%s\n",new_record[j].time_mod);
}
else
{
strcpy(old_record[j].time_mod,new_record[j].time_mod);
strcpy(new_record[j].time_mod,ctime(&statbuf.st_ctime));
if(strcmp(old_record[j].time_mod,new_record[j].time_mod)){
printf("File Accessed Last Time\t:\t%s",old_record[j].time_mod);
printf("File Accessed Recently\t:\t%s\n",new_record[j].time_mod);
}
else{
printf("File Accessed Recently\t:\t%s\n",new_record[j].time_mod);
}
}
}
int main(){
int i;
int j;
int done;
int total;
int check;
int count;
int result;
int choice;
int file_check;
char what_user;
char content;
struct ffblk ffblk;
char file_name[15];
char reg_username[30];
char reg_password[30];
char login_username[30];
char login_password[30];
main_page: ;
done = findfirst("D:/NEW/TEST/*.*",&ffblk,0);
i = 0;
while (!done){
strcpy(old_record[i].name,ffblk.ff_name);
strcpy(new_record[i].name,ffblk.ff_name);
done = findnext(&ffblk);
i++;
}
printf("New User - Press R to Register;\nExisting User - Press L.\n\nInput: ");
scanf("%c",&what_user);
total = restore();
if((what_user == 'R') || (what_user == 'r')){
printf("Please Enter your New Credentials (0 to Exit)...\nUsername\t:\t");
scanf("%s",®_username);
printf("Password\t:\t");
scanf("%s",®_password);
check = reg(reg_username,reg_password,total);
if(check != 0){
printf("\n\nSuccessfully registered..");
goto main_page;
}
else{
printf("\n\nUsername already exists!! Please Try Again!");
goto main_page;
}
}
else if((what_user == 'L') || (what_user == 'l')){
login_page: ;
printf("Please Enter your Credentials (0 to Exit)...\nUsername\t:\t");
scanf("%s",&login_username);
printf("Password\t:\t");
scanf("%s",&login_password);
result = verify(login_username,login_password,total);
if(result == 1){
printf("\n\nL");
printf("o");
printf("g");
printf("g");
printf("i");
printf("n");
printf("g");
printf(" ");
printf("I");
printf("n");
printf(".");
printf(".");
printf(".");
printf("\n\nLogin Successful..\n\n");
verified: ;
printf("Please select an option to perform...\n\n");
printf("1. See File Details;\n2. Edit a file's content to change the size;\n3. Log Out;\n0. Exit");
printf("\n\nInput: ");
scanf("%d",&choice);
if(choice == 0){
printf("\n\nYou are exiting... Please Wait...");
exit(1);
}
else if(choice == 1){
printf("\n\nList of Files in \"D:/NEW/TEST/\"...\n\n");
count = 0;
while(count < i){
printf("%s\n",new_record[count].name);
count++;
}
printf("\n\nEnter the File Name: ");
scanf("%s",file_name);
file_check = verify_file_exists(file_name,i);
if(file_check == 1){
if(!(strcmp(file_name,"001.TXT"))){
if ((stream = fopen("D:/NEW/TEST/001.txt", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
j = 0;
while(strcmp(new_record[j].name,"001.TXT")){
j++;
}
fflush(stream);
verifyFiles(j,login_username);
fclose(stream);
goto verified;
}
else if(!(strcmp(file_name,"002.TXT"))){
if ((stream = fopen("D:/NEW/TEST/002.txt", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
j = 0;
while(strcmp(new_record[j].name,"002.TXT")){
j++;
}
fflush(stream);
verifyFiles(j,login_username);
fclose(stream);
goto verified;
}
else if(!(strcmp(file_name,"003.TXT"))){
if ((stream = fopen("D:/NEW/TEST/003.txt", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
j = 0;
while(strcmp(new_record[j].name,"003.TXT")){
j++;
}
fflush(stream);
verifyFiles(j,login_username);
fclose(stream);
goto verified;
}
}
else{
printf("\n\nFile does not exists...");
goto verified;
}
}
else if(choice == 2){
printf("\n\nList of Files in \"D:/NEW/TEST/\"...\n\n");
count = 0;
while(count < i){
printf("%s\n",new_record[count].name);
count++;
}
printf("\n\nEnter the File Name: ");
scanf("%s",file_name);
file_check = verify_file_exists(file_name,i);
if(file_check == 1){
if(!(strcmp(file_name,"001.TXT"))){
if ((stream = fopen("D:/NEW/TEST/001.TXT", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
printf("Enter the content you want to add to 001.TXT and Press ` with Enter to end...\n\nStart here>\n");
while(content != '`'){
scanf("%c",&content);
fprintf(stream,"%c",content);
}
fclose(stream);
goto verified;
}
if(!(strcmp(file_name,"002.TXT"))){
if ((stream = fopen("D:/NEW/TEST/002.TXT", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
printf("Enter the content you want to add to 002.TXT and Press ` with Enter to end...\n\nStart here>\n");
while(content != '`'){
scanf("%c",&content);
fprintf(stream,"%c",content);
}
fclose(stream);
goto verified;
}
if(!(strcmp(file_name,"003.TXT"))){
if ((stream = fopen("D:/NEW/TEST/003.TXT", "a+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
printf("Enter the content you want to add to 003.TXT and Press ` with Enter to end...\n\nStart here>\n");
while(content != '`'){
scanf("%c",&content);
fprintf(stream,"%c",content);
}
fclose(stream);
goto verified;
}
}
else{
printf("\n\nFile does not exists...");
goto verified;
}
}
else if(choice == 3){
printf("\n\nL");
printf("o");
printf("g");
printf("g");
printf("i");
printf("n");
printf("g");
printf(" ");
printf("O");
printf("u");
printf("t");
printf(".");
printf(".");
printf(".");
goto main_page;
}
/* printf("Following users exist in the system...\n\n");
int i = 0;
while(i < total){
printf("%d. %s - %s\n",i+1,student[i].user,student[i].pass);
i++;
}
*/ }
else{
printf("\n\nL");
printf("o");
printf("g");
printf("g");
printf("i");
printf("n");
printf("g");
printf(" ");
printf("I");
printf("n");
printf(".");
printf(".");
printf(".");
printf("\n\nSorry. Incorrect Username or Password! Please Try Again! :)\n");
goto login_page;
}
}
else{
printf("\n\nWrong Selection... Please Try Again.. Press 0 to exit.");
int ex;
scanf("%d",&ex);
if(ex == 0)
exit(1);
goto main_page;
}
getch();
return 0;
}