"creat" function gives wrong permissions

Viewed 147

My code is as follows:

#include "unistd.h"
#include <fcntl.h>

#define BSIZE 50

int main(int argc, char *argv[]) {
    int f1, f2;
    char buf[BSIZE];

    f1 = open(argv[1], 0x0001, 0);
    f2 = creat(argv[2], 0777);

    read(f1, buf, BSIZE);
    write(f2, buf, BSIZE);

    return 0;
}

When I create a file named 'a' and use the compiled program with command

./a.out a b

It creates a file with the contents of 'a' indeed. But my problem is, its permissions are -rwxr-xr-x. It does not give write permissions to group and others. Moreover, when I change the function to creat(argv[2], 0022); then the permission would become all 0 s ----------.

Is there anything wrong with the creat function? Because chmod works fine.

2 Answers
Related