delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/07/07/19:11:01

From: Eric Backus <ericb AT lsid DOT hp DOT com>
Subject: Re: umask() return value
To: dj AT ctron DOT com
Date: Tue, 7 Jul 92 16:11:13 PDT
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp)
Mailer: Elm [revision: 66.25]

> [From the SunOS man page umask(2)]
> 
> RETURN VALUES
>      umask() returns the previous  value  of  the  file  creation
>      mask.

Yes, but it's a mask of bits to TURN OFF when a file is created.  So,
if umask is set to 0644, the file is created with mode 0133.  This
would not give the owner read or write permission.

On the other hand, a umask value of 022 would result in a file with
mode 0755 (read/write/execute by owner, read/execute only by group and
others), which is what you probably want.

From the HP-UX 8.0 man page umask(2):

     DESCRIPTION
          umask sets the process's file mode creation mask to cmask
          and returns the previous value of the mask.  Only the file
          access permission bits of the masks are used.

          The bits set in cmask specify which permission bits to turn
          off in the mode of the created file, and should be specified
          using the symbolic values defined in stat(5).

Try it on SunOS, that's really what it does.  Trust me.

Below is a test program you can use to verify this.  I just
re-verified this on HP-UX 8.0.
--
				Eric Backus
				ericb%hplsla AT hplabs DOT hp DOT com
				(206) 335-2495


#include <stdio.h>		/* For printf() */
#include <unistd.h>		/* For unlink() */
#include <fcntl.h>		/* For open() */
#include <sys/stat.h>		/* For umask() */

void
test_umask(mode_t umask_value)
{
    mode_t	old_um;
    int		fildes, status;
    struct stat	buf;

    old_um = umask(umask_value);
    (void) printf("Old umask was 0%o\n", old_um);
    (void) printf("New umask is now 0%o\n", umask_value);

    /* Make sure the file doesn't already exist */
    (void) unlink("testfile");

    /* Create the file.  Note mode 0777, which will be modified by the
       umask we set above. */
    fildes = open("testfile", O_WRONLY | O_CREAT, 0777);
    if (fildes < 0)
	(void) printf("open() failed!\n");
    else
	(void) close(fildes);

    status = stat("testfile", &buf);
    if (status < 0)
	(void) printf("stat() failed!\n");
    else
	(void) printf("Mode of testfile is 0%o\n", buf.st_mode);
}

int
main(int argc, char **argv)
{
    test_umask(0644);
    test_umask(022);
    return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019