Searching a folder presents everything inside and outside

I'm running my code from the terminal in Ubuntu, for searching for a specific file, but the search produces folders and files inside that folder but also outside. Here's the code:

(compiles and runnable)

#include <stdio.h>     // For perror
#include <stdlib.h>
#include <sys/types.h> // For stat, opendir, readdir
#include <sys/stat.h>  // For stat
#include <unistd.h>    // For stat
#include <dirent.h>    // For opendir, readdir


int displayAllFiles(char * directory)
{

    DIR *dir;
    struct dirent *ent;
        dir = opendir (directory);
    if (dir != NULL) {

      /* print all the files and directories within directory */
      while ((ent = readdir (dir)) != NULL) {
        printf ("%s\n", ent->d_name);
      }
      closedir (dir);
    } else {
      /* could not open directory */
      perror ("");
      return EXIT_FAILURE;
    }

    return 1;

}



int main(int argc , char * argv[])
{

    DIR *dir;
    struct dirent *entry;
    int result;
    struct stat status;
    char path[PATH_MAX];

    dir = opendir(argv[1]);
    if (!dir)
    {
        perror("opendir");
        exit(1);
    }

    entry = readdir(dir);
    while (entry)
    {
        result = snprintf(path, sizeof(path), "%s", argv[1]);
        snprintf(&path[result], sizeof(path) - result, "/%s", entry->d_name);

        int out = displayAllFiles(path);
        printf("%s", path);

        result = lstat(path, &status);
        if (-1 == result)
        {
            printf("\n");
            perror("stat");
            exit(2);
        }

        if (S_ISLNK(status.st_mode))
        {
            printf("%s", " is a symbolic link");
        }

        printf("\n");

        entry = readdir(dir);
    }


    return(0);
}

I hit from terminal ./exer4 check, that folder, check is the father folder, and then:

  • insideCheck is inside check
  • moreInside is inside insideCheck
  • and david.txt is inside moreInside

But, what I get is:

a@ubuntu:~/Desktop$ ./exer4 check
check
..
Link to workspace2
eclipse 4 linux
fol
.
basherFolder
Link to eclipse
test
eclipse
exer4
doc.txt~
check/..
..
.
insideCheck
check/.
..
moreInside
.
david.txt~
check/insideCheck

Something is wrong, I can see, but have no idea what.

I'd appreciate your help.

Answers


It is caused because . and .. are also listed in the current directory, where .. means parent diretory.

In answer to your comment: comparing path with . or .. doesn't work out, because as you see, path is Directory/. and Directory/..

while (entry)
{
    result = snprintf(path, sizeof(path), "%s", argv[1]);
    snprintf(&path[result], sizeof(path) - result, "/%s", entry->d_name);

    char cur_str[80];
    char par_str[80];
    strcpy ( cur_str , argv[1] );
    strcat ( cur_str , "/." );

    strcpy ( par_str , argv[1] );
    strcat ( par_str , "/.." );

    if (strcmp(path,cur_str) == 0 || strcmp(path ,par_str) == 0) {
     break;
    }
    int out = displayAllFiles(path);
    printf("%s", path);

    result = lstat(path, &status);
    if (-1 == result)
    {
        printf("\n");
        perror("stat");
        exit(2);
    }
    if (S_ISLNK(status.st_mode))
    {
        printf("%s", " is a symbolic link");
    }

    printf("\n");

    entry = readdir(dir);
}

Above code works. (Though it prints "Not a directory" errors on files that are not directories).


Need Your Help

How do I move a TFS file with c# API?

c# api tfs

I have been googling for a good time on how to move a file with c# using the TFS API. The idea is to have a folder on which the developers drop database upgrade scripts and the build process get's ...

Asp.net web application tool - application settings. SQL credentials?

asp.net application-settings

Just wandering if its a good idea to put username and password in the application settings?