0

I'm trying to list all folders and all files of a folder with the language C.

This is the following code:

#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>

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

    if (argc != 2) {
        printf("error");
        return 1;
    }

    if ((dirp = opendir(argv[1])) == NULL) {
        printf("error");
        return 1;
    }

    while ((direnp = readdir(dirp)) != NULL)
    {
        stat(direnp->d_name, &filestat);
        printf("%s\n", direnp->d_name); 
    }

    return 0;
}

After entering the cmd ./file.c folder

The output from this code is:

folder1
folder2
file1.txt
..
.
file2.txt

I wish to remove this part:

..
.

So the output I wish is:

folder1
folder2
file1.txt
file2.txt

How do I hide the 3 dots?

(Edit: There were some mistakes in the code. I corrected it)

Lord Rixuel
  • 1,173
  • 6
  • 24
  • 43
  • 3
    AFAIK the only way to do this is manually compare each entry's name with the strings "." and ".." and skip if they match – Drew McGowen Feb 01 '14 at 20:13
  • If (strcmp(dirp->dname,"..")) ... – Joe DF Feb 01 '14 at 20:13
  • Were ".." and "." really the 4th and 5th entries? – chux - Reinstate Monica Feb 01 '14 at 23:03
  • @chux well the positions of ".." and "." are random. this is why i want to hide them. In my case, they were at 4th and 5th for me. – Lord Rixuel Feb 01 '14 at 23:09
  • In some _poor_ code, code have taken advantage that the first 2 entries are often "." and ".." and simply skipped the first 2 entries. Good you are not doing the same. Note: a folder might not have a ".". – chux - Reinstate Monica Feb 01 '14 at 23:16
  • @chux Just out of curiosity: Can you please direct me where to read more on folders not having a `.`? – Palec Feb 03 '14 at 04:28
  • @Palec Fat finger mistake on my part. I should have said: Note: a folder might not have a "..". e.g. a top-level folder – chux - Reinstate Monica Feb 03 '14 at 04:30
  • @chux That explains it. BTW POSIX forbids this. Base Definitions §4.12 Path Resolution says `The special filename dot-dot shall refer to the parent directory of its predecessor directory. As a special case, in the root directory, dot-dot may refer to the root directory itself.` and Rationale §4.12 says `Each directory has exactly one parent directory which is represented by the name dot-dot in the first directory. No other directory … is considered the parent directory by POSIX.1-2008.` and `What the filename dot-dot refers to relative to the root directory is implementation-defined.`. – Palec Feb 03 '14 at 05:48

1 Answers1

6

Simply filter them in your while loop:

if (strcmp(direnp->d_name, ".") != 0 && strcmp(direnp->d_name, "..") != 0) {
   printf("%s\n", direnp->d_name);    
}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • IMO the correct way would be to use the library functions( dirent.h ) to determine if the file is not a file or directory. – this Feb 01 '14 at 20:17
  • 3
    OP wants to list files **and** directories, but not the two special ones - afaik "." and ".." **are** normal directories (not pipes, not device files) – Andreas Fester Feb 01 '14 at 20:19
  • I said to determine if **not a file or directory** == anything else but a file or dir. So you can filter them. – this Feb 01 '14 at 20:21
  • 1
    @self But how would that allow to distinguish "." and ".."? – Andreas Fester Feb 01 '14 at 20:21
  • Read the library dirent.h. and look at types. – this Feb 01 '14 at 20:22
  • So, what do you think `type` will be for "." and ".."? – Andreas Fester Feb 01 '14 at 20:24
  • The code works correctly. Thank you Mr. Andreas. Just a little note, I forgot to type correctly d_name and the dirent name. Otherwise, everything works fine. – Lord Rixuel Feb 01 '14 at 20:52
  • @LordRixuel Yes, I recognized the same when I compiled your code, but then you already fixed it in the question - I also adjusted my answer now to match your code. Hint: ideally post exactly the code which you run through your compiler - simply do a copy&paste from your code editor – Andreas Fester Feb 01 '14 at 21:01