0

I have a program that prints out all the directories listed inside a specific directory, by checking that d_type == DT_DIR

the program works, but also prints out the parent directory .. and the curent directory .

I tried to set an if statement to check that d_name != ".." or ".", but it still printed parent and current directory

here is my code with the added if statement to

 directory = opendir("/home/user/adirectory");

    if(directory != NULL)
    {
        while(entry = readdir(directory)) {
            if(entry->d_type == DT_DIR && entry->d_name != ".." && entry->d_name != ".")
                printf("%s\n", entry->d_name);
        }


    }

unfortunately this is the output, where dir2 is a directory inside adirectory

..
dir2
.

I would like instead an output that only shows this directory without dot or two dots

dir2
Mohamad Zein
  • 777
  • 1
  • 8
  • 33
  • Instead of strcmp() use strncmp() to make code safer. – Ravi Mar 22 '16 at 01:25
  • Another approach to list the directories is by using: `int listDir(const char *dirpath) { glob_t gdir = { 0, }; regex_t r1 = { 0, }; regex_t r2 = { 0, }; const char p1[] = "\(^\[\.\]\{1,2\}\/\)"; // RE for filtering const char p2[] = "\(\/$\)"; // RE for inclusion char pwd[PATH_MAX + 1] = { 0, }; getcwd(…); chdir(…); result = regncomp(&1, p1, strlen(p1), REG_EXTENDED); result = regncomp(&2, p2, strlen(p2), REG_EXTENDED); // STUFF code posted in another comment below regfree(&r1); regfree(&r2); chdir(…); return 0; }` – Ravi Mar 22 '16 at 01:46
  • Code to be stuffed inside the above comment: `if ((glob(".*", (…|GLOB_MARK), NULL, &gdir) == 0) && (glob("*", (…|GLOB_MARK | GLOB_APPEND), NULL, &gdir) == 0)) { for (int i = 0; i < gdir.gl_pathc; i ++) { char *ps = gdir.gl_pathv[i]; int sz = strlen(gdir.gl_pathv[i]; if ((REG_NOMATCH == regnexec(&reg1, ps, sz), 0, NULL, 0)) && (REG_NOMATCH != regnexec(&reg2, ps, sz), 0, NULL, 0))) printf(" [%02d] %s\n", (i + 1), ps); } globfree(&gdir); }` – Ravi Mar 22 '16 at 01:47
  • Please note that I have some edits in the above comments/code such as "...", so you may need to change them accordingly to make it work. – Ravi Mar 22 '16 at 01:50

3 Answers3

1

You need to use strcmp. See this post.

Community
  • 1
  • 1
1

String comparison in C can be done using the strcmp function. You can't compare strings using the = sign. Below is your code updated with strcmp.

directory = opendir("/home/user/adirectory");

    if(directory != NULL)
    {
        while(entry = readdir(directory)) {
            if(entry->d_type == DT_DIR && strcmp(entry->d_name,"..")!=0 && strcmp(entry->d_name, ".")!=0)
                printf("%s\n", entry->d_name);
        }


    } 
akinfermo
  • 166
  • 7
1

Problem with your code is that you are using != operator on strings which is NOT valid in C. You have to use strcmp function to compare two strings. If you don't know how to use strcmp function then you can google about it. Here lies the problem,

    if(entry->d_type == DT_DIR && entry->d_name != ".." && entry->d_name != ".")

This may be helpful.

Community
  • 1
  • 1
0x0001
  • 525
  • 4
  • 12