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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:20:50+00:00 2026-06-13T03:20:50+00:00

I am trying to find whether a certain path is a directory or not.

  • 0

I am trying to find whether a certain path is a directory or not.

I’m using the stat function present in <sys/stat.h> using the following code:

int  isDir(char *path){
    printf("%s\n", path);
    struct stat file;
    stat(path, &file);
    printf("%i\n", file.st_mode);
    int x; 
    x = S_ISDIR(file.st_mode);
    return x;
}
// I tried this function with the following code.
    printf("%i\n",  isDir("/home/aladdin"));
    printf("%i\n",  isDir("/home/aladdn"));

My username on the PC is aladdin so, the first one should work and equal 1, and the second should print a 0, as there are no user named aladdn.

So running the previous code gives

/home/aladdin
16832
1
/home/aladdn
16832
1

Any idea about what’s wrong?

  • 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-13T03:20:51+00:00Added an answer on June 13, 2026 at 3:20 am

    You must check stat return code. If stat fails, it will not modify your file struct contents, and in that case it IS_DIR seems to return true.

    So, instead of your current line stat(path, &file);, have line:

    if (stat(path, &file) == -1) return 0;

    …of course you should probably report the error somehow, depending on your requirements, but that simple change will fix your function.

    As to why you get identical results, I think C leaves structs like that uninitialized unless you explicitly initialize them, and with that kind of usage, the struct will be in exactly the same spot in stack on 2nd call, so it still has the values from 1st call. But this is just chance, change the code and behaviour changes unpredicably. C is fun that way 🙂

    To zero the buf, do this before calling stat with &file:

    memset(&file, 0, sizeof file);

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

Sidebar

Related Questions

I trying to find whether the elements of 2 arrayLists are match or not.
I am using UICatalog example and trying to find whether it is possible to
I want to find out whether or not a certain process is sleeping or
I am trying to find whether number is prime or not for 1000 digit
I'm trying to find out whether I should be using business critical logic in
I am trying to find out whether a database has a catalog or not.
I am trying to find out whether local press releases may have affected companies'
I'm trying to find out whether a Socket is currently connected - according to
I am trying to find out whether it is possible to start a gen_server
What I'm trying to find out is whether there is some sort of equivalence

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.