0

I am trying to list files in the parent directory of the current directory, but when I try to execute this program from terminal I get Segmentation Error.. What am I doing wrong? Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
   struct dirent *dirpent;
   DIR *dirp;

   if(argc!=2)
   {
       printf("Cant continue with the program\n");
       return 0;
   }

   dirp= opendir(argv[1]);

   if(dirp)
   {
       while(dirpent=readdir(dirp) !=NULL)
           printf("%s\n",dirpent->d_name);

       closedir(dirp);
   }
   return 0;
}
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
Naruto
  • 1,710
  • 7
  • 28
  • 39

2 Answers2

3
dirpent=readdir(dirp) !=NULL

should be

(dirpent = readdir(dirp)) != NULL

Your current expression is parsed as dirpent = (readdir(dirp) != NULL), which will set dirpent to either 0 or 1.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • But it also lists files in the subdirectory of the given directory, how can I display files of only directory I specified not the files of subdirectory also – Naruto Oct 20 '12 at 14:30
1

If you indent your program with indent rd.c then compile your program with gcc -Wall -g rd.c -o rd you get

 rd.c: In function 'main':
 rd.c:21:22: warning: assignment makes pointer from integer without a cast [enabled by default]
 rd.c:21:7: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

So you forgot parenthesis, your while should be

 while((dirpent=readdir(dirp)) !=NULL)

Please compile your program with all warnings (and improve it till they are all gone) before asking questions. Use the gdb debugger (and its bt command) to find out why a program crash with SIGSEGV.

Don't forget to carefully read documentation like readdir(3) man page and Advanced Linux Programming book.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • readdir() displays files of the subdirectory also. How can I prevent files of the subdirectories to get displayed – Naruto Oct 20 '12 at 14:48
  • Are you really sure of that? According to the `readdir(3)` man page, it should not (and I believe that the GNU Libc `readdir` implementation don't have such a huge bug). Try to `ltrace`, `strace` or `gdb` your program to check. – Basile Starynkevitch Oct 20 '12 at 14:54
  • yes. I am trying to display files on my Desktop which has 10 files and two subdirectories, But my output contains almost 25+ files, some of which are the files of the subdirectory – Naruto Oct 20 '12 at 15:02
  • You should trust much more syscalls and low-level libraries than your desktop. What does the `/bin/ls` command shows you? Inspect the output of `strace yourprog` ... – Basile Starynkevitch Oct 20 '12 at 15:03
  • well. Now I am scratching my head. My desktop displays 10 files, but ls shows 25 files. Why is this so? I am Administrator of the computer and it has no other users.. – Naruto Oct 20 '12 at 15:09
  • I can't help you. I don't really use or trust desktops (just using them to start terminals and firefox). I interact with the system mostly thru `zsh` (and commands). Maybe some files are "backup" (ending with e.g. `~` or `%`) so are not displayed by your desktop... – Basile Starynkevitch Oct 20 '12 at 15:09
  • May be there is a reason for not trusting desktop? – Naruto Oct 20 '12 at 15:14