new file creation

When we use open() call by defaults it creates file with mode 644.
Its because the umask on my system is 0022    (0666 & ~0022 = 0644)

Is there any api in C/C++ by means of which I can create a file with desired mode 744.

I don't want to change umask on my system (and of course I don't want to use chmod).

I am just wondering can we set mode during open call itself?
perlperlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gplanaCommented:
The primitive functions for creating files (for example, open or mkdir) take a mode argument, which specifies the file permissions to give the newly created file. This mode is modified by the process's file creation mask, or umask, before it is used

Please take a look at this link: http://www.gnu.org/software/libc/manual/html_node/Setting-Permissions.html
0
perlperlAuthor Commented:
I don't thinks so I can tell open to create a FILE with mode 755 without changing the umask which I don't want to do.

I know with dir we can do something like but again this depends on what the value of umask is: example in this it will create dir with mode 0644 & ~0022 = 0644  
   if (mkdir(argv[1], 0644) == -1) {
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }
There is no way I can create dir with mode 777 using mkdir without changing umask
0
jkrCommented:
You can always use 'mknod()' (http://man7.org/linux/man-pages/man2/mknod.2.html), e.g.

mknod("textfile", S_IFREG | 0644, 0);

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

sarabandeCommented:
the open and creat function both would take the 'mode' as 3rd respectively 2nd argument.

int fd = open("<filepath>", O_CREAT, 0777);

Open in new window


Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Um, gplana alread mentioned that, so you are basically accepting an answer that as been posted three days earlier. Why?
0
sarabandeCommented:
jkr, perlperl's response to gplana's comment was:

I don't thinks so I can tell open to create a FILE with mode 755 without changing the umask which I don't want to do.

because of that you posted an alternative code sample using mknod and I posted a simpler code sample where the open was used. it obviously is an acceptable solution though gplana's comment of course would have deserved a better recognition.

you may reopen the question such that perlperl can make a new decision.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.