(standard c libraries translation )mkstemp

mkstemp, mkostemp, mkstemps, mkostemps - create a unique temporary file
mkstemp, mkostemp, mkstemps, mkostemps - 创建一个唯一的临时文件

所需头文件
#include <stdlib.h>
int mkstemp(char *template);
int mkostemp(char *template, int flags);
int mkstemps(char *template, int suffixlen);
int mkostemps(char *template, int suffixlen, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

mkstemp():
   _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
   || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200112L

mkostemp(): _GNU_SOURCE
mkstemps(): _BSD_SOURCE || _SVID_SOURCE
mkostemps(): _GNU_SOURCE

The  mkstemp()  function  generates  a unique temporary filename from template, creates and opens the file, and returns an open file descriptor for the file.
mkstemp函数从template中创建一个临时文件,创建并打开这个文件,返回打开文件的文件描述符

The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique.  Since it will  be  modified, template must not be a string constant, but should be declared as a character array.
template的最后六个自负必须是"XXXXXX",这些被字符串替换以创建唯一的文件名,必须是可以修改的,template不能是字符串常量,因此必须声明为字符数组

The file is created with permissions 0600, that is, read plus write for owner only.  (In glibc versions 2.06 and earlier, the file is created with permissions 0666, that is, read and write for all users.)  The returned file descriptor provides both read and write access to  the  file.   The  file  is opened with the open(2) O_EXCL flag, guaranteeing that the caller is the process that creates the file.
文件以0600权限创建,这也就是说,仅仅是owner有读写权限(glibc 2.06或者以前,文件以0666创建,这就是说所有用户都有读写权限),返回的文件描述符提供读写权限,文件以O_EXCL选项打开,确保调用者是打开这个文件的进程

The mkostemp() function is like mkstemp(), with the difference that flags as for open(2) may be specified in flags (e.g., O_APPEND, O_SYNC).
mkostemp函数类似与mkstemp,区别是可以指定打开的标志位
The mkstemps() function is like mkstemp(), except that the string in template contains a suffix of suffixlen characters.  Thus, template is of the form prefixXXXXXXsuffix, and the string XXXXXX is modified as for mkstemp().
mkstemps函数类似与mkstemp,区别是template包含suffixlen长度的后缀,因此template是prefixXXXXXXsuffix,字符串XXXXXX在mkstemp中修改
The mkostemps() function is to mkstemps() as mkostemp() is to mkstemp().
mkostemps与mkstemps的关系就像mkostemp和mkstemp的关系


On success, these functions return the file descriptor of the temporary file.  On error, -1 is returned, and errno is set appropriately.
成功的时候返回临时文件的文件描述符,错误的时候返回-1,errno被设置成合适值

EEXIST Could not create a unique temporary filename.  Now the contents of template are undefined.
不能创建唯一的临时文件,template的内容是为定义的
EINVAL For mkstemp() and mkostemp(): The last six characters of template were not XXXXXX; now template is unchanged.
template的后六个自负不是XXXXXX,template没有被改变

For mkstemps() and mkostemps(): template is less than (6 + suffixlen) characters long, or the last 6 characters before the  suffix  in  template were not XXXXXX.
template小于六个字符长度,或者前缀之后的六个字节不是XXXXXX

These functions may also fail with any of the errors described for open(2).
这些文件有open所有的错误(因为创建临时文件的过程中也会打开文件)

mkstemp(): 4.3BSD, POSIX.1-2001.
mkstemps(): unstandardized, but appears on several other systems.
mkostemp() and mkostemps(): are glibc extensions.


The  old  behavior  of creating a file with mode 0666 may be a security risk, especially since other UNIX flavors use 0600, and somebody might overlook this detail when porting programs.
老版本的以0666权限创建文件存在安全隐患,尤其是其他unix版本使用0600的时候,一些人在移植程序的时候可能会忽略这一点

More generally, the POSIX specification of mkstemp() does not say anything about file modes, so the application should make sure its file mode creation mask (see umask(2)) is set appropriately before calling mkstemp() (and mkostemp()).
一般来说,mkstemp的POSIX协议并没有说到任何文件模式相关的内容,所以在调用mkstemp之前,应用程序必须确保文件模式的创建掩码被设置成适当的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值