Link to home
Start Free TrialLog in
Avatar of perlperl
perlperl

asked on

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?
Avatar of gplana
gplana
Flag of Spain image

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
Avatar of perlperl
perlperl

ASKER

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
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

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Um, gplana alread mentioned that, so you are basically accepting an answer that as been posted three days earlier. Why?
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