Submit your source code via email attachment to dannellys@winthrop.edu with the subject line CSCI325 - HW 04 by 2:00pm on February 22.
For example, assuming the current directory has three entries - two files and a directory named testdir - and testdir has one file named bob:
> lstree . 2
myls1.c
myls2.c
testdir
bob
Here is the myls2 program that we discussed in class.
/*******************************************
myls2 - Print a director with inode numbers
and mark directories
Dannelly - February 2011
********************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
DIR *dp; // ptr to directory
struct dirent *dirp; // prt to next dir entry
struct stat stat_info; // file status information
/***** check for correct command line *****/
if (argc != 2)
{
printf ("must provide a directory name\n");
exit (1);
}
/***** can that directory really be opened??? *****/
if ( ( dp = opendir(argv[1])) == NULL)
{
printf ("cannot open %s\n",argv[1]);
exit (1);
}
/***** process all directory entries *****/
while ( ( dirp = readdir(dp)) != NULL)
{
printf ( "%20s %d", dirp->d_name,dirp->d_ino);
stat (dirp->d_name, &stat_info);
if (S_ISDIR(stat_info.st_mode)) // if directory
printf (" directory\n"); // label it
else
printf ("\n"); // else print nothing else
}
/***** done *****/
closedir (dp);
}
|