No specific user configuration
[platform/upstream/bash.git] / lib / sh / tmpfile.c
index d51ac2c..c06ad65 100644 (file)
@@ -6,30 +6,33 @@
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   Bash is free software; you can redistribute it and/or modify it
-   under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   Bash is distributed in the hope that it will be useful, but WITHOUT
-   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
-   License for more details.
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with Bash; see the file COPYING.  If not, write to the Free
-   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include <config.h>
 
 #include <bashtypes.h>
 #include <posixstat.h>
+#include <posixtime.h>
 #include <filecntl.h>
 
 #if defined (HAVE_UNISTD_H)
 #  include <unistd.h>
 #endif
 
+#include <bashansi.h>
+
 #include <stdio.h>
 #include <errno.h>
 
 extern int errno;
 #endif
 
-#define BASEOPENFLAGS  (O_CREAT | O_TRUNC | O_EXCL)
+#define BASEOPENFLAGS  (O_CREAT | O_TRUNC | O_EXCL | O_BINARY)
 
 #define DEFAULT_TMPDIR         "."     /* bogus default, should be changed */
 #define DEFAULT_NAMEROOT       "shtmp"
 
 extern pid_t dollar_dollar_pid;
 
+static char *get_sys_tmpdir __P((void));
+static char *get_tmpdir __P((int));
+
 static char *sys_tmpdir = (char *)NULL;
 static int ntmpfiles;
 static int tmpnamelen = -1;
@@ -54,27 +60,25 @@ static unsigned long filenum = 1L;
 static char *
 get_sys_tmpdir ()
 {
-  struct stat sb;
-
   if (sys_tmpdir)
     return sys_tmpdir;
 
 #ifdef P_tmpdir
   sys_tmpdir = P_tmpdir;
-  if (stat (sys_tmpdir, &sb) == 0)
+  if (file_iswdir (sys_tmpdir))
     return sys_tmpdir;
 #endif
 
   sys_tmpdir = "/tmp";
-  if (stat (sys_tmpdir, &sb) == 0)
+  if (file_iswdir (sys_tmpdir))
     return sys_tmpdir;
 
   sys_tmpdir = "/var/tmp";
-  if (stat (sys_tmpdir, &sb) == 0)
+  if (file_iswdir (sys_tmpdir))
     return sys_tmpdir;
 
   sys_tmpdir = "/usr/tmp";
-  if (stat (sys_tmpdir, &sb) == 0)
+  if (file_iswdir (sys_tmpdir))
     return sys_tmpdir;
 
   sys_tmpdir = DEFAULT_TMPDIR;
@@ -89,6 +93,9 @@ get_tmpdir (flags)
   char *tdir;
 
   tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
+  if (tdir && (file_iswdir (tdir) == 0 || strlen (tdir) > PATH_MAX))
+    tdir = 0;
+
   if (tdir == 0)
     tdir = get_sys_tmpdir ();
 
@@ -130,7 +137,7 @@ sh_mktmpname (nameroot, flags)
       filenum = (filenum << 1) ^
                (unsigned long) time ((time_t *)0) ^
                (unsigned long) dollar_dollar_pid ^
-               (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
+               (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
       sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
       if (tmpnamelen > 0 && tmpnamelen < 32)
        filename[tdlen + 1 + tmpnamelen] = '\0';
@@ -179,7 +186,7 @@ sh_mktmpfd (nameroot, flags, namep)
       filenum = (filenum << 1) ^
                (unsigned long) time ((time_t *)0) ^
                (unsigned long) dollar_dollar_pid ^
-               (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
+               (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
       sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
       if (tmpnamelen > 0 && tmpnamelen < 32)
        filename[tdlen + 1 + tmpnamelen] = '\0';