8

I am trying to write a simple C program that creates directories (a mkdir clone.). This is what I have so far:

#include <stdlib.h>
#include <sys/stat.h> // mkdir
#include <stdio.h> // perror

mode_t getumask()
{
    mode_t mask = umask(0);
    umask (mask);
    return mask;
}

int main(int argc, const char *argv[])
{
    mode_t mask = getumask();
    printf("%i",mask);

    if (mkdir("trial",mask) == -1) {
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }
    return 0;
}

This code creates directory with d--------- but I want it to create it with drwxr-xr-x like mkdir do? What am I doing wrong here?

Edit: This is the working solution for me:

int main(int argc, const char *argv[])
{
    if (mkdir("trial",0777) == -1) {
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }
    return 0;
}

Setting right permissions according to umask is automatically handled. So I only needed to call mkdir with full permissions, and that gets chopped according to current umask.

Cœur
  • 32,421
  • 21
  • 173
  • 232
yasar
  • 11,262
  • 26
  • 80
  • 154
  • What does the diagnostic print produce? It might work better if you added a newline after the `%i`, and it would be more easily legible if you used `%o` or `%.4o` instead of `%i`. Also, you appear to be missing `#include ` which declares [`umask()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/umask.html) and [`mkdir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html). – Jonathan Leffler Apr 13 '12 at 20:46
  • It produces 22 with %4o. – yasar Apr 13 '12 at 20:47

2 Answers2

10

You seem to be misunderstanding what umask is used for. It sets/retrieves the process's file mode creation mask, which in turn is used to turn off bits in the file mode you specify in calls like mkdir, like this (pseduo-code):

real_mode = requested_mode & ~umask

So in your code, since you pass in the value of the umask itself, you end up specifying permissions as zero, which is exactly what you see.

Instead, you should specify the permissions you want in the call to mkdir, like this:

mkdir("trial", 0755)
Eric Melski
  • 15,052
  • 3
  • 32
  • 48
  • 3
    I think yasar11732 wants to incorporate the current user's umask setting, not hard coding `0755`. Just my guess. – Crend King Apr 13 '12 at 21:10
  • 1
    There is normally no need to mess with the user's umask in an application; libc already does that for you in `mkdir()`. So hardcoding 0755 or 0775 is perfectly fine. – Daniel Roethlisberger Apr 13 '12 at 22:45
  • 4
    What if I `umask 0`? Hardcoding 0755 will make it 0755 instead of desired 0777. Maybe hardcoding 0777 is more correct, as yasar11732 commented, but it is not harmful to `0777 & ~mask`. – Crend King Apr 13 '12 at 23:17
  • I'm trying to create directories with `drwxrwxr-x` permissions, if I've understood the `mkdir` docs correctly then `0775` should give that but it doesn't. Do I need to be su? – airdas Feb 08 '18 at 19:16
1

As Eric says, umask is the complement of the actual permission mode you get. So instead of passing mask itself to mkdir(), you should pass 0777-mask to mkdir().

Crend King
  • 3,525
  • 2
  • 30
  • 41
  • 1
    Thanks, I indeed wanted to incorporate umask to get correct permission. I changed 0777-mask to 0777 & ~mask though. :) – yasar Apr 13 '12 at 21:13
  • 1
    I actually needn't use umask directly, since appearantly, mkdir already does that. So using 0777 results in a directory with `drwxr-xr-x` when my umask is 022 – yasar Apr 13 '12 at 21:19