mkstemp_mode.c

来自「在LINUX下实现HA的源代码」· C语言 代码 · 共 42 行

C
42
字号
/* $Id: mkstemp_mode.c,v 1.4.4.1 2004/04/20 08:39:45 alan Exp $ */#include <portability.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <clplumbing/mkstemp_mode.h>/* * A slightly safer version of mkstemp(3) * * In this version, the file is initially created mode 0, and then chmod-ed * to the requested permissions.  This guarantees that the file is never * open to others beyond the specified permissions at any time. */intmkstemp_mode(char* template, mode_t filemode){	mode_t	maskval;	int	fd;	maskval = umask(0777);	/* created file should now be mode 0000 */	fd = mkstemp(template);	umask(maskval);	/* cannot fail :-) */	if (fd >= 0) {		if (chmod(template, filemode) < 0) {			int	save = errno;			close(fd);			errno = save;			fd = -1;		}	}	return fd;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?