1

I am learning to write my own Virtual File system but besides the logical error in program something other than that keeps coming i checked all the declarations within the program but couldn't figure it out.

helper function

#include "header.h"

UFDT UFDTArr[50];
SUPERBLOCK SUPERBLOCKobj;
PINODE head=NULL;

void man(char *name)
{
    if(name==NULL) return;

    if(_stricmp(name,"ls")==0)
    {
        printf("Description : Used to list all information of file\n");
        printf("Usage : ls\n");
    }
    else 
    {
        printf("ERROR : No manual entry available\n");
    }
}
    void DisplayHelp()
{
    printf("ls : To List Out all files \n");
    printf("clear : To Clear consol\n");
}

void CreateDILB()
{
    PINODE newn=NULL;
    PINODE temp=head;
    int i=1;

    while(i<=MAXINODE)
    {
        newn=(PINODE)malloc(sizeof(INODE));
        newn->LinkCount=newn->ReferenceCount=0;
        newn->FileType=newn->FileSize=0;
        newn->Buffer=NULL;
        newn->next=NULL;
        newn->InodeNumber=i;

        if(temp==NULL)
        {
            head=newn;
            temp=head;
        }
        else
        {
            temp->next=newn;
            temp=temp->next;
        }
        i++;
    }

}

void InitialiseSuperBlock()
{
    int i=0;
    while(i<50)
    {
        UFDTArr[i].ptrfiletable=NULL;
        i++;
    }

    SUPERBLOCKobj.TotalInodes=MAXINODE;
    SUPERBLOCKobj.FreeInode=MAXINODE;
}
void ls_file()
{
    PINODE temp=head;

    if(SUPERBLOCKobj.FreeInode== MAXINODE)
    {
        printf("Error : There are no files ");
        return;
    }
    printf("\n File Name\tInode Number\tFile Size\tLink count\n");
    printf("------------------------------------------------------------");

    while(temp!=NULL)
    {
        if(temp->FileType!=0)
        {
            printf("%s\t\t%d\t\t%d\t\t%d\n");
        }
        temp=temp->next;
    }
    printf("------------------------------------------------------------");
}

main file

#include "header.h"

int main()
{
    char *ptr=NULL;
    int ret=0,fd=0,count=0;
    char command[4][80],str[80],arr[1024];

    InitialiseSuperBlock();
    CreateDILB();

    while(1)
    {
        fflush(stdin);
        strcpy_s(str,"");

        printf("Sachin VFS :> ");
        fgets(str,80,stdin);

        count=sscanf(str,"%s%s%s %s",command[0],command[1],command[2],command[3]);

        if(count==1)
        {
            if(_stricmp(command[0],"ls")==0)
            {
                ls_file();
            }

            else if(_stricmp(command[0],"clear")==0)
            {
                system("cls");
                continue;
            }
        else
        {
            printf("\n ERROR : Command not found!!! \n");
            continue;
        }

    }
    }
    return 0;
}

header file

#define _CRT_SECURE_NO_WARNINGS
#define MAXINODE 50
#define READ 1
#define WRITE 2
#define MAXFILESIZE 1024
#define REGULAR 1
#define SPECIAL 2
#define START 0
#define CURRENT 1
#define END 2

#include<iostream>
#include <stdlib.h>
#include<string.h>
#include<io.h>

typedef struct superblock
{
    int TotalInodes;
    int FreeInode;

}SUPERBLOCK,*PSUPERBLOCK;

typedef struct inode
{
    char FileName[50];
    int InodeNumber;
    int FileSize;
    int FileActualSize;
    int FileType;
    char *Buffer;
    int LinkCount;
    int ReferenceCount;
    int permission;
    struct inode *next;

}INODE,*PINODE,**PPINODE;

typedef struct filetable
{
    int readoffset;
    int writeoffset;
    int count;
    int mode;
    PINODE ptrinode;

}FILETABLE,*PFILETABLE;

typedef struct ufdt
{
    PFILETABLE ptrfiletable;

}UFDT;

the one solution to this problem i got is declaring all the functions in main file above main to make compiler identify the functions but i still couldn't figure it out why it cant identify the same functions when i declare them in other file?

the default functions are working like system("cls"); but my functions are not working

could anyone help me to understand the reason of this error and possible solution ?

P.S.- I have pasted small part of my code the actual code is too long to post if anyone wants me to post it i will in comment section

1 Answers1

0

In brief - you should declare ls_file() in your header.h:

void ls_file();

This is a common technique to export some objects / functions outside of file where they are defined. Both "implementation" and "client" *.c files must include that header. The former one - in order to guarantee consistency of actual definitions and publicly-visible declarations, the latter one - to provide client code with proper and explicit declarations.

... still couldn't figure it out why it cant identify the same functions when i declare them in other file?

In general compiler should see declarations or definitions of functions / globals before any referencing to them. This is because during compilation process translator works only with one .c source, and knows nothing about another source files and their content.

P.S This answer may enlighten you a bit more.

Community
  • 1
  • 1
Sergio
  • 8,099
  • 2
  • 26
  • 52