Linux mkdir function mode permission setting

  

When using C to operate Linux directory, encountered a few mkdir small problems, by the way, in the operation of files or directories on Linux, the most likely problems are permission issues, are obvious Mistakes, so these problems are easily exposed and resolved during the development phase. After all, it is not just a Linux platform development, so I will forget it after a few days.

The function prototype of mkdir (includes #include <sys/stat.h>):

int mkdir(const char *path, mode_t mode);

Parameters:

path——directory name, such as abc, /var/www/abc, etc.

mode——directory permissions

Return value:

returns 0 for success, -1 for error, and sets the errno value.


Of course, you can also use macro parameters similar to S_IRWXU, S_IRUSR..., after all, it is difficult to remember, but not as good as the 0421 in octal. The combination style is: owner-group-others, different people are divided into three rules read-write-execute (r-w-x), all licenses are 7.

1. Use mkdir("test",777) when programming.

Try it out:

mkdir("test",0777);

written as mkdir ("test", 777) may not be executed. But vaguely remember, there used to be no mistakes in the previous use of 777, specifically forgot, anyway, it is absolutely correct to write according to the norm.

2, umask command to use

Also assume that your program directory is under /root/abc/, then if your program wants to create a directory under /var/www when executing, there is Maybe you always create a 0777 directory that is always created as a 0755 directory, then try the umask(0) command.

The umask is only valid for the current directory. The default umask value is 0022, so you cannot create a directory of 0777 directly in another location.

It is 0777-0022=0755

3, how to create a user group folder

/etc/passwd and /etc/group to find UID and gid< Br>

mkdir /var/ugroup

We can use chown directly to change the file owner.

chown root:newuser /var/ugroup

Modify permissions

chmod 740 /var/ugroup/*

4, check the permissions of the directory after creation Command:

Locate to the user group directory, execute:

ls -all

will display something like:

drwxrwxr-x

such a result (0775).

Copyright © Windows knowledge All Rights Reserved