#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#define RWXRWXRWX (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH |S_IXUSR | S_IXGRP |S_IXOTH )
int main()
{
struct stat statbuf;
umask(0);
if(creat("foo", RWXRWXRWX) < 0)
printf( "creat error for foo!\n" );
umask( S_IWGRP |S_IWOTH | S_IROTH );
printf("befor creat bar!\n");
if(creat("bar", RWXRWXRWX) < 0 )
printf("creat error for bar!\n");
if( stat( "foo", &statbuf ) < 0 )
printf( "stat foo error !\n" );
if( chmod( "foo", ( statbuf.st_mode & ~S_IRUSR & ~S_IWUSR & ~S_IXUSR) | S_ISUID ) < 0 )
printf( "chmod foo error!\n" );
exit(0);
}
运行结果如下: