Thread: C programming with Unix functions for manipulation of a virtual file system

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    C programming with Unix functions for manipulation of a virtual file system

    Hi all,

    This virtual file system is implemented as a binary file and I have a problem with some of the functions as it needs to incorporate with Unix and I don't know how/where to start. The basic functions (i.e. manipulation) I had to perform are:

    1. Display a directory listing
    2. Renaming a file
    3. Removing a file
    4. Display the contents of each file

    The starting point for the program is as follows:

    Code:
    #define FILE_SYS_SIZE 1024 
    #define BLOCK_SIZE 32 
    #define DATA_SIZE (BLOCK_SIZE - 2 * sizeof(unsigned char) - sizeof(short)) 
    #define FILE_NAME_SIZE (DATA_SIZE-2*sizeof(short)) 
    #define HEADER_BLOCK 11 
    #define FENTRY_BLOCK 13 
    #define DATA_BLOCK   15 
    #define FREE_BLOCK   17 
    typedef struct 
    { 
            short  size; 
            short  first_block; 
            char name[FILE_NAME_SIZE]; 
    } FILE_ENTRY; 
    typedef struct 
    { 
            short first_free; 
            short first_used; 
    } HEADER; 
    typedef struct 
    { 
            unsigned char num; 
            unsigned char type; 
            short next_block; 
            union { 
                    HEADER header; 
                    FILE_ENTRY fent; 
                    char file_content[DATA_SIZE]; 
            } data; 
    } BLOCK;
    I already have a skeleton code:

    Code:
    int main() 
    { 
        /*
         * Directory listing & rename calls
         */
        ls(); 
        rename_file("File 1", "File ONE");
        ls();
    
        /*
         * Displaying the contents of the files
         */
        display( "File TWO" ); 
        display( "File THREE" ); 
        display( "MY FILE" );
    
        /*
         * Removing a File & Directory Listing
         */
        remove( "File TWO" );
        ls();
    
        return 0; 
    }
    My questions are:

    1. How and where do I insert the mandatory Unix system functions i.e. open(), read(), write(), lseek(), close() into the program for it to work?

    2. How and where do I insert the command to read from a given binary .dat file for it to work?

    3. Do I use the rename method as above or the C method:
    "int rename(const char *old, const char *new);

    Thank you all for your attention and help!

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Start with "The basic function 1: Display a directory listing"

    What processes do you need to perform?
    In what order do you need to do them?

    Write it out on paper, then look up the C and Unix functions to accomplish the tasks.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    hi walt,

    thanks for your advice. am i right to assume that for various unix functions, i use the relevant c functions instead?

    e.g.

    remove() --> int remove
    rename() --> int rename

    thanks

  4. #4
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187

    Re: C programming with Unix functions for manipulation of a virtual file system

    >>1. Display a directory listing
    look into <dirent.h>
    DIR *dp;
    struct dirent *dirp;
    dp=opendir("dirname");
    while((dirp=readdir(dp))!=NULL)
    printf("%s\n",dirp->d_name);

    >>2. Renaming a file
    <stdio.h>
    int rename(const char *oldname,const char *newname);

    >>3. Removing a file
    <stdio.h>
    int remove(const char *pathname);

    >>4. Display the contents of each file
    no direct method
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by zagelle
    hi walt,

    thanks for your advice. am i right to assume that for various unix functions, i use the relevant c functions instead?

    e.g.

    remove() --> int remove
    rename() --> int rename

    thanks
    In general, you use the C functions that do the job. Some of them are named the same as the unix function, but don't necessarily use that as a guide. You're best bet is to look up the relevant C function. in the MAN pages.

    For example, you made reference to open() and lseek(). But to accomplish your objective, the lseek() function is not necessary, and fopen() may be easier to use than open(). You will need to decide in C which functions you need.

    In your prompts the program displays, it would make sense to reference the Unix command being simulated.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM