Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:57:25+00:00 2026-06-17T17:57:25+00:00

I’m trying to list up my local file-system recursively using dirent.h . In order

  • 0

I’m trying to list up my local file-system recursively using dirent.h. In order to prevent from following sym-links, I’m using the sys/stat.h header. In the following you can find my SSCCE program.

/**
 * coding: utf-8
 *
 * Copyright (C) 2013, Niklas Rosenstein
 *
 * listdir.c - List up directories and file-content recursively.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

void list_dir(const char* directory_name) {
    DIR* directory_handle = opendir(directory_name);
    if (directory_handle == NULL) {
        fprintf(stderr, "Could not open directory %s.\n", directory_name);
        return;
    }

    char buffer[1024];
    struct dirent* dentry;
    int directory_name_length = strlen(directory_name);

    memcpy(buffer, directory_name, directory_name_length);
    buffer[directory_name_length] = '/';

    while ((dentry = readdir(directory_handle)) != NULL) {
        char* name = dentry->d_name;
        int length = strlen(name);

        // Skip the dotted elements.
        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;

        // Concatenate the directory name with the element name.
        memcpy(buffer + directory_name_length + 1, name, length);
        buffer[directory_name_length + 1 + length] = 0;

        printf("%s\n", buffer);

        // Proceed recursively if the element is a directory.
        struct stat s;
        if (stat(buffer, &s) != 0) {
            fprintf(stderr, "WARNING: stat() failed on %s\n", buffer);
            continue;
        }

        mode_t mode = s.st_mode;
        if (mode & S_IFDIR && !(mode & S_IFLNK)) {
            list_dir(buffer);
        }

    }
    closedir(directory_handle);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "Expected exactly 2 arguments.\n");
        return -1;
    }
    list_dir(argv[1]);
    return 0;
}

I just can’t get it to work to detect symbolic links correctly. When it comes accross a symbolic link, linking to it’s parent directory, it goes on and on and on. It seems like there are a few folders like this on my system, eg. /usr/bin/X11:

/usr/bin/X11/
    X11/ -> .

This line must not be entirely correct: if (mode & S_IFDIR && !(mode & S_IFLNK)) {. Is it propabaly an issue with the stat() function or am I missing something obvious here?

Here’s a picture of my terminal after invoking ./listdir /usr/bin/X11, stopping the program after about a second by pressing ^C.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:57:26+00:00Added an answer on June 17, 2026 at 5:57 pm

    Try lstat instead of stat: the latter, when done on symbolic link, returns information on its target (the final one, which is not a symbolic link).

    S_IFDIR and S_IFLNK are not supposed to be used as if they were exclusive bit flags. For directory, use S_ISDIR(mode); for symbolic link, you’ll need no test: lstat won’t report symbolic link as a directory, and that’s enough when all you want is to skip symbolic links.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am using jsonparser to parse data and images obtained from json response. When
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.