0

Working on code that will open up a directory, read its contents, and print out info about each file. It examines the command line and will print out the contents of the specified directory or the current directory if no arguments provided. The code below is almost the exact same as the code used to print out the current directory. The only differece is that in the first if statement instead of argv[1] there is ".". The code below hits the first if statement in the while loop(The one after I set result equal to the current file) and then says for every file in the directory

Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
Error while loading statBuffer: : No such file or directory
. thaddueus thaddueus 4096 Wed May 1 18:34:42 2013

Error while loading statBuffer: : No such file or directory

Here is the code I use.

else if(argc > 1){
    if((dir = opendir(argv[1])) == NULL){
    perror("opendir() error");
}else{
    while((entry = readdir(dir)) != NULL){
    result = stat(entry->d_name, &statBuf);
    if(result == -1)perror("Error while loading statBuffer: ");
        else{
            /*Line of code I added directly below this based upon David's suggestion*/
             snprint(pathname, sizeof(pathname), "%s/%s", argv[1], entry->d_name);
            /*Addition cause Segmentation fault core dumped*/
    if(strncmp(entry->d_name, "..",100) == 0 ||strncmp(entry->d_name, ".",100))
        continue;
    errno = 0;
    pws = getpwuid(statBuf.st_uid);
    if(errno != 0)
    perror("Error while retriving username: ");         
    errno = 0;
    grp = getgrgid(statBuf.st_gid);
    if(errno != 0) perror("Error while retriving groupname: ");                 
    /*Prints the filename*/
    printf("%s", entry->d_name);
        /*Prints the username*/
    printf("\t%s", pws->pw_name);
    /*Prints the groupname*/
    printf("\t%s", grp->gr_name);
    /*Prints the file size*/
    printf("\t%ld", (long)statBuf.st_size);
    /*Prints the last modification time*/
    printf("\t%s\n", ctime(&(statBuf.st_mtime)));
    }   
}
closedir(dir);
}
}

EDIT: That

. thaddueus thaddueus 4096 Wed May 1 18:34:42 2013

is a hidden file. Not sure while that will print and the others won't.

EDIT1: Sorry. Forgot to add that it is getting the filenames.

EDIT2: When I remove the error statement it stops printing out the error statements and just prints out the hidden file.

thad
  • 167
  • 1
  • 4
  • 16

1 Answers1

1

It appears that you are opening the directory (dir = opendir(argv[1])) and reading it (entry = readdir(dir)), but I don't see that you are ever making it your current directory.

The filename contained in entry->d_name is not a full pathname, it's just the name of the file within the directory. You either need to build a pathname (dir/filename), or make the directory you're looking at your current directory (chdir).

EDIT You could build a pathname with something like:

char pathname[1024]; // must be large enough for dirname+filename
snprintf(pathname, sizeof(pathname), "%s/%s", argv[1], entry->d_name);

Note that you need to be careful to (a) define a variable large enough to hold a maximum-length directory name + filename, and (b) copy into that variable using a function (snprintf , in this case) that lets you specify the maximum length, to make sure you never write past the end of the variable, no matter what.

ps. you mention that

The code below is almost the exact same as the code used to print out the current directory

You might want to think about whether there is a way you can use the same code for both the current directory and a specified directory, rather than duplicating it.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84