Question: CS337 System Programming Lab Exercise 8 Directory Streams in C For this lab, you will get some practice working with directory streams in C. A

CS337 System Programming

Lab Exercise 8

Directory Streams in C

For this lab, you will get some practice working with directory streams in C.

A directory stream in C is tailored for directories as opposed to the file streams we have been working with, which are tailored for use with regular files in UNIX.

You are given the contents of the file main.c :

#include

#include

#include

#include

int main(int args, char* argv[])

{

printf(" ");

DIR* dirp; // type used for a directory stream

dirp = opendir("."); // try to open the current working directory

if ( dirp == NULL )

{

printf(" dir open error ");

exit(1);

}

else

{

struct stat fst; // needed to hold file stats must be allocated (or local var)

struct dirent* dentry; // a pointer to an internal structure

dentry = readdir(dirp); // no allocation needed, unless we need to save each one

while ( dentry != NULL ) // if the dir entry is non NULL use it

{

// Your Work Here

dentry = readdir(dirp); // get the next entry

}

closedir(dirp); // close the directory

}

printf(" ");

return 0;

}

First read through main to understand what it is doing and how.

Compile the program and run it what happens and why?

Add code so that your program:

Displays the directory entry's d_ino

Displays the directory entry's' d_name

Gets the stats on the directory entry

If the directory entry is a regular file display:

file size = entry's st_size

If the directory entry is a directory display:

dir size = entry's st_size

Sample Run:

ino = 287710103

name = .

dir size = 4096

ino = 287710106

name = mains.c

file size = 815

ino = 287181291

name = ..

dir size = 4096

ino = 287710104

name = main.c

file size = 792

ino = 287710113

name = a.out

file size = 7916

Verify that your program is working

See your TA to get checked off.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!