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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:24:38+00:00 2026-05-22T23:24:38+00:00

I want to list regular files in a directory. However, stat fails for every

  • 0

I want to list regular files in a directory. However, stat fails for every file.

DIR* dp = NULL;
struct dirent* entry = NULL;
dp = opendir(directory);

if (!dp) { log_err("Could not open directory"); return -1; }

while (entry = readdir(dp))
{
    struct stat s;
    char path[1024]; path[0] = 0;
    strcat(path, directory);
    strcat(path, entry->d_name);

    int status = 0;

    if (status = stat(path, &s))
    {
        if (S_ISREG(s.st_mode))
        {
            printf("%s\n", entry->d_name);
        }
    }
    else
    {
        fprintf(stderr, "Can't stat: %s\n", strerror(errno));
    }
}

closedir(dp);

The output is

Can’t stat: Resource temporarily
unavailable

Can’t stat: Resource temporarily
unavailable

Can’t stat: Resource temporarily
unavailable

(… many times)

errno is set to E_AGAIN (11).

Now, if I printf the resulting path, they are indeed valid file and directory names. The directory is readable, the user I run with does have the rights to do so (it’s the directory where I write the program in).

What is causing this problem, and how can I do this correctly?

  • 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-22T23:24:39+00:00Added an answer on May 22, 2026 at 11:24 pm

    stat and many other system calls return 0 on success and -1 on failure. You are incorrectly testing the return value of stat.

    Your code should be:

    if (!stat(path, &s))
    {
        if (S_ISREG(s.st_mode))
        {
            printf("%s\n", entry->d_name);
        }
    }
    else
    {
        fprintf(stderr, "Can't stat: %s\n", strerror(errno));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to list all files on an FTP server using PHP. According to
i want to list files from dev end at tty bettwen 15 and 24...should
I want to extract all tags in a xml file. However, I cannot write
I have a huge list of mp3 files, the names of which I want
I have a list of file names which I want to change to include
I have a list of regular expressions I want to run over a single
I have a list of words in a text file. What I want is
I want a list of hyperlinks on a basic html page, which point to
I want that list, because if something horrible happens, and I'll have to reinstall
I want to list (a sorted list) all my entries from an attribute called

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.