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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:31:02+00:00 2026-06-13T11:31:02+00:00

For homework I have to write a C program and one of the things

  • 0

For homework I have to write a C program and one of the things it has to do is check to see a file exists and if it is executable by the owner.

Using (stat(path[j], &sb) >= 0 I’m able to see if the file indicated by path[j] exists.

I’ve looked through man pages, a lot of questions and answers on stackoverflow, and several websites but I’m not able to wrap my head around exactly how to check if a file is executable using stat.
I thought it would be as simple as ((stat(path[j], &sb) >= 0) && (sb.st_mode > 0) && (S_IEXEC) but as far as I can tell by testing it, it seems to ignore the fact that these files aren’t executable.

I think that perhaps stat doesn’t work the way I think it does. Assuming I use stat, how can I go about fixing this?

  • 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-13T11:31:03+00:00Added an answer on June 13, 2026 at 11:31 am

    You can indeed use stat to do this. You just have to use S_IXUSR (S_IEXEC is an old synonym of S_IXUSR) to check if you have execute permission. Bitwise AND operator (&) checks whether the bits of S_IXUSR are set or not.

    if (stat(file, &sb) == 0 && sb.st_mode & S_IXUSR) 
        /* executable */
    else  
        /* non-executable */
    

    Example:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
        if (argc > 1) {
            struct stat sb;
            printf("%s is%s executable.\n", argv[1], stat(argv[1], &sb) == 0 &&
                                                     sb.st_mode & S_IXUSR ? 
                                                     "" : " not");
        }
        return 0;
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my homework for my C class, we have to write a program that
This is a homework assignment, I have to write a program that reads a
This is a homework question. I have to write a program forking itself 20
I have a homework to do that says the following: Write a program in
I have two questions about this homework assignment: Write an assembly language program in
I have a homework assignment where I need to take input from a file
I have homework that should use fork() and wait() system calls: 1) Write two
The homework problem is the user enters a number. Then you have to write
This is homework, not going to lie. I need to write a program which
So I am doing homework and I am stuck on one spot. I have

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.