umask

From RAD Studio
Jump to: navigation, search

Go Up to io.h Index


Header File

io.h, sys\stat.h

Category

Input/output Routines

Prototype

unsigned umask(unsigned mode);

Description

Sets file read/write permission mask.

The umask function sets the access permission mask used by open and creat. Bits that are set in mode will be cleared in the access permission of files subsequently created by open and creat.

The mode can have one of the following values, defined in sys\stat.h:

S_IWRITE

Permission to write

S_IREAD

Permission to read

S_IREAD|S_IWRITE

Permission to read and write



Return Value

The previous value of the mask. There is no error return.

Example

#include <io.h>
#include <stdio.h>
#include <sys\stat.h>
#define FILENAME "TEST.$$$"
int main(void)
{
    unsigned oldmask;
    FILE *f;
    struct stat statbuf;
    /* Cause subsequent files to be created as read-only */
    oldmask = umask(S_IWRITE);
    printf("Old mask = 0x%x\n",oldmask);
    /* Create a zero-length file */
    if ((f = fopen(FILENAME,"w+")) == NULL)
    {
        perror("Unable to create output file");
        return (1);
    }
    fclose(f);
    /* Verify that the file is read-only */
    if (stat(FILENAME,&statbuf) != 0)
    {
        perror("Unable to get information about output file");
        return (1);
    }
    if (statbuf.st_mode & S_IWRITE)
        printf("Error! %s is writable!\n",FILENAME);
    else
        printf("Success! %s is not writable.\n",FILENAME);
    return (0);
}

Portability

POSIX Win32 ANSI C ANSI C++

+

+