X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=libiberty%2Fmkstemps.c;h=a0e68a73b4915d27d97b3b9d5d3779f6c3f7023f;hb=bfb81bedaaf85c6c4cbb7119e17909b4696ea38c;hp=5963f58baf4ed5f5455d2fd5a3e1252eda70f54f;hpb=f8d9d6a03d24c6a42a1a813a25e63ac308062869;p=pf3gnuchains%2Fgcc-fork.git diff --git a/libiberty/mkstemps.c b/libiberty/mkstemps.c index 5963f58baf4..a0e68a73b49 100644 --- a/libiberty/mkstemps.c +++ b/libiberty/mkstemps.c @@ -13,8 +13,8 @@ You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ + write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, + Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -49,19 +49,23 @@ typedef unsigned long gcc_uint64_t; #define TMP_MAX 16384 #endif +#ifndef O_BINARY +# define O_BINARY 0 +#endif + /* -@deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len}) +@deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len}) -Generate a unique temporary file name from @var{template}. -@var{template} has the form: +Generate a unique temporary file name from @var{pattern}. +@var{pattern} has the form: @example @var{path}/ccXXXXXX@var{suffix} @end example @var{suffix_len} tells us how long @var{suffix} is (it can be zero -length). The last six characters of @var{template} before @var{suffix} +length). The last six characters of @var{pattern} before @var{suffix} must be @samp{XXXXXX}; they are replaced with a string that makes the filename unique. Returns a file descriptor open on the file for reading and writing. @@ -71,7 +75,7 @@ reading and writing. */ int -mkstemps (char *template, int suffix_len) +mkstemps (char *pattern, int suffix_len) { static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; @@ -83,15 +87,15 @@ mkstemps (char *template, int suffix_len) size_t len; int count; - len = strlen (template); + len = strlen (pattern); if ((int) len < 6 + suffix_len - || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6)) + || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6)) { return -1; } - XXXXXX = &template[len - 6 - suffix_len]; + XXXXXX = &pattern[len - 6 - suffix_len]; #ifdef HAVE_GETTIMEOFDAY /* Get some more or less random data. */ @@ -119,10 +123,17 @@ mkstemps (char *template, int suffix_len) v /= 62; XXXXXX[5] = letters[v % 62]; - fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600); + fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600); if (fd >= 0) /* The file does not exist. */ return fd; + if (errno != EEXIST +#ifdef EISDIR + && errno != EISDIR +#endif + ) + /* Fatal error (EPERM, ENOSPC etc). Doesn't make sense to loop. */ + break; /* This is a random value. It is only necessary that the next TMP_MAX values generated by adding 7777 to VALUE are different @@ -131,6 +142,6 @@ mkstemps (char *template, int suffix_len) } /* We return the null string if we can't find a unique file name. */ - template[0] = '\0'; + pattern[0] = '\0'; return -1; }