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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:47:44+00:00 2026-06-11T21:47:44+00:00

I have a question on fts(3) . I am getting a segmentation fault whenever

  • 0

I have a question on fts(3). I am getting a segmentation fault whenever I try to access any members of the fts_children() function. When I read the man page at http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html it claims to fill itself after the read function runs and returns a linked list linked through the link field in the structure. My suspicion is that the child_function is returning nothing but I feel like that doesn’t line up with the man page. Am I supposed to be adding these files to the child buffer because I thought that was being done automatically? My code is below,
Thanks!

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

int compare (const FTSENT**, const FTSENT**);

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

        FTS* file_system = NULL;
        FTSENT* child = NULL;
        FTSENT* parent = NULL;
        FTSENT* temp = NULL;

        file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);

        while( (parent = fts_read(file_system)) != NULL)
        {

             child = fts_children(file_system,0);
             printf("%s\n", child->fts_path);


        }
//      while (child ->fts_link != NULL)
      //         child = child->fts_link;
        fts_close(file_system);
        return 0;
}

int compare(const FTSENT** one, const FTSENT** two){
        return (strcmp((*one)->fts_name, (*two)->fts_name));
}
"test_fs.c" 43L, 1108C  
  • 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-11T21:47:45+00:00Added an answer on June 11, 2026 at 9:47 pm

    You simply need to add a NULL check.

    You might want to

    • add one for file_system
    • check for command line arguments
    • Add more errorhandling:

      The fts_children() function returns a pointer to an FTSENT structure describing the first entry in a NULL terminated linked list of files in the directory, if successful. The fts_children() function may fail and set errno for any of the errors that the chdir(), malloc(), opendir(), readdir(), and stat() functions specify.

    Update To the new question(s) in the comment:

    • The while loop for linked list traversal was misplaced (outside the outer loop?)
    • The printf displayed only the path… not the filename.

    while you’re at it:

    #include<stdlib.h>
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fts.h>
    #include<string.h>
    #include<errno.h>
    
    int compare (const FTSENT**, const FTSENT**);
    
    int main(int argc, char* const argv[])
    {
        FTS* file_system = NULL;
        FTSENT* child = NULL;
        FTSENT* parent = NULL;
    
        if (argc<2)
        {
            printf("Usage: %s <path-spec>\n", argv[0]);
            exit(255);
        }
    
        file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);
    
        if (NULL != file_system)
        {
            while( (parent = fts_read(file_system)) != NULL)
            {
                child = fts_children(file_system,0);
    
                if (errno != 0)
                {
                    perror("fts_children");
                }
    
                while ((NULL != child)
                    && (NULL != child->fts_link))
                {
                    child = child->fts_link;
                    printf("%s%s\n", child->fts_path, child->fts_name);
                }
            }
            fts_close(file_system);
        }
        return 0;
    }
    
    int compare(const FTSENT** one, const FTSENT** two)
    {
        return (strcmp((*one)->fts_name, (*two)->fts_name));
    }
    

    Sample output fragment:

    ./.profiles/sehe/.opera/icons/cache/g_0000
    ./.profiles/sehe/.opera/icons/cache/g_0000/opr00002.tmp
    ./.profiles/sehe/.opera/icons/cache/g_0000/opr00003.tmp
    ./.profiles/sehe/home/sehe/.mozilla
    fts_children: Permission denied
    ./.vbox-sehe-ipc/lock
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question. I have some app on facebook and getting this error Fatal
I have question regarding the use of function parameters. In the past I have
I have question concerning a function I created. I would like to show the
I have question regarding shared memory segmentation in c using POSIX system calls. Is
I have question about sending emails from MVC3 application. I already read for exmple
I have question about how I can save the result of function I have
I have question will the click event or any other event run in document.ready()
I have question regarding Android Layouts.I have read the documentation of Layouts .My question
I have question on which I can't find any answear. Example: In _root exists
I have question, how write function to select all chceckbox on form, when all

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.