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

  • Home
  • SEARCH
  • 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 7947481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:26:38+00:00 2026-06-04T01:26:38+00:00

Just starting to learn some basic C and messing around with the stat() system

  • 0

Just starting to learn some basic C and messing around with the stat() system call on Linux.

So st_mode returned in the struct from stat() is a bitfield. And I can get the octal permissions by simply printf("octal %o", st.st_mode) but I’m only interested in certain flags in the bitfield, namely S_IRWXU, S_IRWXG and S_IRWXO to send that value to another function, eg: mkdir.

Here’s my sample program.
Ignore the fact that this is a rubbish program with the directories hard-coded, not passed as an argument to the create_dir() function and with no error checking.

#include <stdio.h>
#include <sys/stat.h>

static int create_dir(mode_t mode) {
  mkdir("/home/user/blahnew", mode);
}

main() {
  struct stat st;
  int res;
  res = stat("/home/user/blah", &st);

  printf("user %o\n", st.st_mode & S_IRWXU);
  printf("group %o\n", st.st_mode & S_IRWXG);
  printf("other %o\n", st.st_mode & S_IRWXO);

  create_dir(mode);

  return 0;
}

How can I take only the above flages in st.st_mode and pass it as an octal to create_dir() to be used by an mkdir() function?

If I just did create_dir(st.st_mode) would that not also pass all the other flags that mkdir() doesn’t need… uid, gid, sticky bit etc?

Cheers, B

  • 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-04T01:26:39+00:00Added an answer on June 4, 2026 at 1:26 am

    You already did 99% of the work in your code. When you say

    st.st_mode & S_IRWXU
    

    only the bits in st.st_mode that are present in the S_IRWXU bitmask remain. So what you want is a different bitmask that combines the other three:

    st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting to learn jQuery and have gotten some of the basic
I'm just starting to learn T-SQL and could use some help in understanding what's
I'm just starting to learn Xcode, and have run in to some problems... I've
I am starting to learn Perl and this is just a basic little for
I am just starting to learn Python, but I have already run into some
I'm just starting to learn Android doing the Hello World app, and some of
I am just starting to learn jQuery, and want to load content from a
Just starting to learn the CakePHP framework. I'm using the Auth component, and all
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
im just starting to learn flex and im trying to understand how Flex does

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.