You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>mktemp (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="mktemp (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="mktemp (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdio.html#Stdio" rel="up" title="Stdio">
  16. <link href="open_005fmemstream.html#open_005fmemstream" rel="next" title="open_memstream">
  17. <link href="getwchar.html#getwchar" rel="prev" title="getwchar">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="mktemp"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="open_005fmemstream.html#open_005fmemstream" accesskey="n" rel="next">open_memstream</a>, Previous: <a href="getwchar.html#getwchar" accesskey="p" rel="prev">getwchar</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="mktemp_002c-mkstemp_002c-mkostemp_002c-mkstemps_002c"></a>
  54. <h3 class="section">4.42 <code>mktemp</code>, <code>mkstemp</code>, <code>mkostemp</code>, <code>mkstemps</code>,</h3>
  55. <a name="index-mktemp"></a>
  56. <a name="index-mkdtemp"></a>
  57. <a name="index-mkstemp"></a>
  58. <a name="index-mkstemps"></a>
  59. <a name="index-mkostemp"></a>
  60. <a name="index-mkostemps"></a>
  61. <a name="index-_005fmktemp_005fr"></a>
  62. <a name="index-_005fmkdtemp_005fr"></a>
  63. <a name="index-_005fmkstemp_005fr"></a>
  64. <a name="index-_005fmkstemps_005fr"></a>
  65. <a name="index-_005fmkostemp_005fr"></a>
  66. <a name="index-_005fmkostemps_005fr"></a>
  67. <p><strong>Synopsis</strong>
  68. </p><div class="example">
  69. <pre class="example">#include &lt;stdlib.h&gt;
  70. char *mktemp(char *<var>path</var>);
  71. char *mkdtemp(char *<var>path</var>);
  72. int mkstemp(char *<var>path</var>);
  73. int mkstemps(char *<var>path</var>, int <var>suffixlen</var>);
  74. int mkostemp(char *<var>path</var>, int <var>flags</var>);
  75. int mkostemps(char *<var>path</var>, int <var>suffixlen</var>, int <var>flags</var>);
  76. char *_mktemp_r(struct _reent *<var>reent</var>, char *<var>path</var>);
  77. char *_mkdtemp_r(struct _reent *<var>reent</var>, char *<var>path</var>);
  78. int *_mkstemp_r(struct _reent *<var>reent</var>, char *<var>path</var>);
  79. int *_mkstemps_r(struct _reent *<var>reent</var>, char *<var>path</var>, int <var>len</var>);
  80. int *_mkostemp_r(struct _reent *<var>reent</var>, char *<var>path</var>,
  81. int <var>flags</var>);
  82. int *_mkostemps_r(struct _reent *<var>reent</var>, char *<var>path</var>, int <var>len</var>,
  83. int <var>flags</var>);
  84. </pre></div>
  85. <p><strong>Description</strong><br>
  86. <code>mktemp</code>, <code>mkstemp</code>, and <code>mkstemps</code> attempt to generate a file name
  87. that is not yet in use for any existing file. <code>mkstemp</code> and <code>mkstemps</code>
  88. create the file and open it for reading and writing; <code>mktemp</code> simply
  89. generates the file name (making <code>mktemp</code> a security risk). <code>mkostemp</code>
  90. and <code>mkostemps</code> allow the addition of other <code>open</code> flags, such
  91. as <code>O_CLOEXEC</code>, <code>O_APPEND</code>, or <code>O_SYNC</code>. On platforms with a
  92. separate text mode, <code>mkstemp</code> forces <code>O_BINARY</code>, while <code>mkostemp</code>
  93. allows the choice between <code>O_BINARY</code>, <code>O_TEXT</code>, or 0 for default.
  94. <code>mkdtemp</code> attempts to create a directory instead of a file, with a
  95. permissions mask of 0700.
  96. </p>
  97. <p>You supply a simple pattern for the generated file name, as the string
  98. at <var>path</var>. The pattern should be a valid filename (including path
  99. information if you wish) ending with at least six &lsquo;<code>X</code>&rsquo;
  100. characters. The generated filename will match the leading part of the
  101. name you supply, with the trailing &lsquo;<code>X</code>&rsquo; characters replaced by some
  102. combination of digits and letters. With <code>mkstemps</code>, the &lsquo;<code>X</code>&rsquo;
  103. characters end <var>suffixlen</var> bytes before the end of the string.
  104. </p>
  105. <p>The alternate functions <code>_mktemp_r</code>, <code>_mkdtemp_r</code>, <code>_mkstemp_r</code>,
  106. <code>_mkostemp_r</code>, <code>_mkostemps_r</code>, and <code>_mkstemps_r</code> are reentrant
  107. versions. The extra argument <var>reent</var> is a pointer to a reentrancy
  108. structure.
  109. </p>
  110. <br>
  111. <p><strong>Returns</strong><br>
  112. <code>mktemp</code> returns the pointer <var>path</var> to the modified string
  113. representing an unused filename, unless it could not generate one, or
  114. the pattern you provided is not suitable for a filename; in that case,
  115. it returns <code>NULL</code>. Be aware that there is an inherent race between
  116. generating the name and attempting to create a file by that name;
  117. you are advised to use <code>O_EXCL|O_CREAT</code>.
  118. </p>
  119. <p><code>mkdtemp</code> returns the pointer <var>path</var> to the modified string if the
  120. directory was created, otherwise it returns <code>NULL</code>.
  121. </p>
  122. <p><code>mkstemp</code>, <code>mkstemps</code>, <code>mkostemp</code>, and <code>mkostemps</code> return a file
  123. descriptor to the newly created file, unless it could not generate an
  124. unused filename, or the pattern you provided is not suitable for a
  125. filename; in that case, it returns <code>-1</code>.
  126. </p>
  127. <br>
  128. <p><strong>Notes</strong><br>
  129. Never use <code>mktemp</code>. The generated filenames are easy to guess and
  130. there&rsquo;s a race between the test if the file exists and the creation
  131. of the file. In combination this makes <code>mktemp</code> prone to attacks
  132. and using it is a security risk. Whenever possible use <code>mkstemp</code>
  133. instead. It doesn&rsquo;t suffer the race condition.
  134. </p>
  135. <br>
  136. <p><strong>Portability</strong><br>
  137. ANSI C does not require either <code>mktemp</code> or <code>mkstemp</code>; the System
  138. V Interface Definition requires <code>mktemp</code> as of Issue 2. POSIX 2001
  139. requires <code>mkstemp</code>, and POSIX 2008 requires <code>mkdtemp</code> while
  140. deprecating <code>mktemp</code>. <code>mkstemps</code>, <code>mkostemp</code>, and <code>mkostemps</code>
  141. are not standardized.
  142. </p>
  143. <p>Supporting OS subroutines required: <code>getpid</code>, <code>mkdir</code>, <code>open</code>, <code>stat</code>.
  144. </p>
  145. <br>
  146. <hr>
  147. <div class="header">
  148. <p>
  149. Next: <a href="open_005fmemstream.html#open_005fmemstream" accesskey="n" rel="next">open_memstream</a>, Previous: <a href="getwchar.html#getwchar" accesskey="p" rel="prev">getwchar</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  150. </div>
  151. </body>
  152. </html>