Skip to content

Instantly share code, notes, and snippets.

@user454322
Created June 21, 2014 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save user454322/9f32f69dd8caa8c972eb to your computer and use it in GitHub Desktop.
Save user454322/9f32f69dd8caa8c972eb to your computer and use it in GitHub Desktop.
umask chmod open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#define RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH| S_IWOTH)
int
main(int argc, char **argv, char **env) {
const char *path = "/tmp/test";
const mode_t process_umask = umask(S_IWGRP );
umask(S_IWGRP);
const mode_t mode = (S_IRUSR | S_IWUSR);
// final permission: 666 - 022 = 644
// RW_UGO ^ process_umask = -rw-r--r--
if (open(path, O_RDWR | O_CREAT | O_EXCL, mode) == -1)
err(1, "open for '%s' failed", path);
struct stat sb;
if (stat(path, &sb) != 0)
err(2, "stat failed");
const mode_t file_mode = sb.st_mode & ACCESSPERMS;
const mode_t umasked_mode = (RW_UGO ^ process_umask) & mode;
if (umasked_mode != file_mode)
printf("umasked mode (%o) and file_mode (%o) are different\n",
umasked_mode, file_mode);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment