Hi everyone,
I am brand new to this board and I hope that I do not violate any rules too bad here, although they seem pretty easy to follow.
I am writing a version of "ls -l" on a UNIX platform and although I am not yet finished adding the details of the system call stat(), I do not want to proceed adding file information until I can figure out why I am having the following problem for a reason I have spent hours and hours trying to figure out:
* if my argv[1] is passed nothing, my program assumes we want the "ls -l" of the current working directory. So, no problems there. But, since I added the stat() system call to convert my program from strictly an "ls" program to a "ls -l" program, I can no longer pass a directory name from the command line (i.e. a.out pathname) and read out all of the files in a directory that is not the cwd.
This is the code, which is my first try at ever implementing this, so if it isn't pretty or kosher, my sincere apologies, and i will improve in the future making things neat. (I am pretty new to C) :
Code:
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
DIR *dstream = NULL;
struct dirent *listing;
struct stat fileinfo;
/* Open chosen directory stream */
if(argc == 1){ /*no argument supplied so assume cwd.*/
dstream = opendir(".");
if(dstream == NULL){
printf("Error: Unable to open current directory.\n");
exit(-1);
}
else
{
printf("Current working directory stream opened.\n\n\n");
}
}
else{ /*Argument for dir. supplied.*/
dstream = opendir(argv[1]);
if(dstream == NULL){
printf("Error: Unable to open specified directory.\n");
exit(-1);
}
else
{
printf("%-6s directory stream opened.\n\n\n", argv[1]);
}
}
/*Processing the directory stream. */
while((listing = readdir(dstream)) != NULL)
{
if(stat(listing->d_name, &fileinfo) == 0)
{
printf("%2d\t%-10.10s\n", fileinfo.st_nlink,listing->d_name);
}
else
{
printf("Error: Directory stream unable to be processed.\n");
perror("readdir");
exit(-1);
}
}
/*Closing the directory stream. */
if((closedir(dstream)) == -1) /* closedir() returns -1 on fail. */
{ printf("Error: Directory stream not closed.\n");
exit(-1);
}
return 0; /*otherwise, closedir() succeeded, return.*/
}
I hope I tagged that correctly. So the problem lies, I think anyway, in the stat() system call in the processing section of the program. I attempted to somewhat neatly organize the program into three logical sections, opening the directory, processing the directory, and then closing the directory.
Note: when I pass a subdirectory as the argument into argv[1], I do get the "." and ".." for the present and parent directories, and their number of hard links, which are the two attributes I added to this program before I discovered this bug. After these two are read-out though, I get the errno = 2 error, which is "No such file or directory". Why is the subdirectory after these two iterations no longer readable to readdir()? I used perror() to discover this as the cause, which I'm sure is obvious to everyone else here who certainly knows more than I do.
Can anyone tell me where I went wrong and made a rookie mistake? How do I need to rework the while loop and the stat() call so that I can pass in any subdirectory from the command line without getting an error?
I would be eternally grateful, emphasize, eternally!
Thanks so much in advance to anyone who takes the time to read my code,
CHL