简体   繁体   中英

set the execute bits in C

I am writing a program which requires me to create a file using standard fopen fprintf fclose calls.

I want to set the execute bits.

I can do this with chmod but this seems overkill. For the life of me (possibly due to advanced age) I can't seem to find (or remember) an API to do this.

fchmod(2) would be in keeping with your other calls... Given a file descriptor in fd :

struct stat buf;
fstat(fd, &buf);
fchmod(fd, buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);

will add all three execute bits to the file's current mode (error handling left as an exercise for the reader).

You'd use fileno(3) to get the file descriptor from your FILE * structures. Alternatively you could use chmod(2) and pass it the file name.

chmod is an API call, and is the canonical way of changing the mode of a file (referenced by name) from a program; API调用,是从程序更改文件模式(按名称引用)的规范方法; it is not overkill.

You could use fchmod (see fchmod(3)), or umask if you want to apply the same for every file created (extracts below taken from http://www.gnu.org/software/libc/manual/html_node/Setting-Permissions.html and the umask(2) man page ):

The functions in this section are declared in sys/stat.h.

Function: mode_t umask (mode_t mask)

The umask function sets the file creation mask of the current process to mask, and returns the previous value of the file creation mask.

Here is an example showing how to read the mask with umask without changing it permanently:

mode_t
read_umask (void)
{
  mode_t mask = umask (0);
  umask (mask);
  return mask;
}

However, on GNU/Hurd systems it is better to use getumask if you just want to read the mask value, because it is reentrant.

Name

umask - set file mode creation mask

mode_t umask(mode_t mask);

Description

umask() sets the calling process's file mode creation mask (umask) to mask & 0777 (ie, only the file permission bits of mask are used), and returns the previous value of the mask.

The umask is used by open(2), mkdir(2), and other system calls that create files to modify the permissions placed on newly created files or directories. Specifically, permissions in the umask are turned off from the mode argument to open(2) and mkdir(2).

The constants that should be used to specify mask are described under stat(2).

The typical default value for the process umask is S_IWGRP | S_IWOTH (octal 022). In the usual case where the mode argument to open(2) is specified as:

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH

(octal 0666) when creating a new file, the permissions on the resulting file will be:

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH

(because 0666 & ~022 = 0644; ie, rw-r--r--). Return Value This system call always succeeds and the previous value of the mask is returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM