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 6580321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:00:46+00:00 2026-05-25T16:00:46+00:00

If you recursively traverse a directory tree by the obvious method, you’ll run into

  • 0

If you recursively traverse a directory tree by the obvious method, you’ll run into trouble with infinite recursion when a symlink points to a parent directory.

An obvious solution would be to just check for symlinks and not follow them at all. But that might be an unpleasant surprise for a user who doesn’t expect what behaves for other purposes like a perfectly normal directory to be silently ignored.

An alternative solution might be to keep a hash table of all directories visited so far, and use this to check for loops. But this would require there to be some canonical representation, some way to get the identity, of the directory you are currently looking at (regardless of the path by which you reached it).

Would Unix users typically regard the second solution as less surprising?

If so, is there a way to obtain such a canonical representation/identity of a directory, that’s portable across Unix systems? (I’d like it to work across Linux, BSD, Mac OS, Solaris etc. I expect to have to write separate code for Windows.)

  • 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-05-25T16:00:46+00:00Added an answer on May 25, 2026 at 4:00 pm

    The most frequently ignored API in this field would be

    nftw

    Nftw has options to avoid it traversing symlinks. It has much more advanced capabilities than that. Here is a simple sample from the man page itself:

    #define _XOPEN_SOURCE 500
    #include <ftw.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    
    static int
    display_info(const char *fpath, const struct stat *sb,
                 int tflag, struct FTW *ftwbuf)
    {
        printf("%-3s %2d %7jd   %-40s %d %s\n",
               (tflag == FTW_D) ?   "d"   : (tflag == FTW_DNR) ? "dnr" :
               (tflag == FTW_DP) ?  "dp"  : (tflag == FTW_F) ?   "f" :
               (tflag == FTW_NS) ?  "ns"  : (tflag == FTW_SL) ?  "sl" :
               (tflag == FTW_SLN) ? "sln" : "???",
               ftwbuf->level, (intmax_t) sb->st_size,
               fpath, ftwbuf->base, fpath + ftwbuf->base);
        return 0;           /* To tell nftw() to continue */
    }
    
    int
    main(int argc, char *argv[])
    {
        int flags = 0;
    
        if (argc > 2 && strchr(argv[2], 'd') != NULL)
            flags |= FTW_DEPTH;
        if (argc > 2 && strchr(argv[2], 'p') != NULL)
            flags |= FTW_PHYS;
    
        if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
                == -1)
        {
            perror("nftw");
            exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
    }
    

    See also

    • Directory recursion
    • http://rosettacode.org/wiki/Walk_a_directory/Recursively
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to recursively traverse a directory and create a tree to be used
If you need to recursively traverse a directory tree, there are two ways to
How can I traverse an n -ary tree without using recursion? Recursive way: traverse(Node
The following code recursively processes a list of dictionaries into a tree while building
How to recursively list all the files in a directory and child directories in
I can't figure out how to write a Binary Search Tree to file recursively.
I want to create a function to recursively traverse a multidimensional dictionary, where the
So I am trying to traverse a directory and find all the instances of
I'm trying to use the two following methods to recursively traverse arrays of arrays
We're building a Windows-based application that traverses a directory structure recursively, looking for files

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.