Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / lib / sh / tmpfile.c
1 /*
2  * tmpfile.c - functions to create and safely open temp files for the shell.
3  */
4
5 /* Copyright (C) 2000 Free Software Foundation, Inc.
6
7    This file is part of GNU Bash, the Bourne Again SHell.
8
9    Bash is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation, either version 3 of the License, or
12    (at your option) any later version.
13
14    Bash is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <config.h>
24
25 #include <bashtypes.h>
26 #include <posixstat.h>
27 #include <posixtime.h>
28 #include <filecntl.h>
29
30 #if defined (HAVE_UNISTD_H)
31 #  include <unistd.h>
32 #endif
33
34 #include <bashansi.h>
35
36 #include <stdio.h>
37 #include <errno.h>
38
39 #include <shell.h>
40
41 #ifndef errno
42 extern int errno;
43 #endif
44
45 #define BASEOPENFLAGS   (O_CREAT | O_TRUNC | O_EXCL | O_BINARY)
46
47 #define DEFAULT_TMPDIR          "."     /* bogus default, should be changed */
48 #define DEFAULT_NAMEROOT        "shtmp"
49
50 extern pid_t dollar_dollar_pid;
51
52 static char *get_sys_tmpdir __P((void));
53 static char *get_tmpdir __P((int));
54
55 static char *sys_tmpdir = (char *)NULL;
56 static int ntmpfiles;
57 static int tmpnamelen = -1;
58 static unsigned long filenum = 1L;
59
60 static char *
61 get_sys_tmpdir ()
62 {
63   if (sys_tmpdir)
64     return sys_tmpdir;
65
66 #ifdef P_tmpdir
67   sys_tmpdir = P_tmpdir;
68   if (file_iswdir (sys_tmpdir))
69     return sys_tmpdir;
70 #endif
71
72   sys_tmpdir = "/tmp";
73   if (file_iswdir (sys_tmpdir))
74     return sys_tmpdir;
75
76   sys_tmpdir = "/var/tmp";
77   if (file_iswdir (sys_tmpdir))
78     return sys_tmpdir;
79
80   sys_tmpdir = "/usr/tmp";
81   if (file_iswdir (sys_tmpdir))
82     return sys_tmpdir;
83
84   sys_tmpdir = DEFAULT_TMPDIR;
85
86   return sys_tmpdir;
87 }
88
89 static char *
90 get_tmpdir (flags)
91      int flags;
92 {
93   char *tdir;
94
95   tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
96   if (tdir && (file_iswdir (tdir) == 0 || strlen (tdir) > PATH_MAX))
97     tdir = 0;
98
99   if (tdir == 0)
100     tdir = get_sys_tmpdir ();
101
102 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
103   if (tmpnamelen == -1)
104     tmpnamelen = pathconf (tdir, _PC_NAME_MAX);
105 #else
106   tmpnamelen = 0;
107 #endif
108
109   return tdir;
110 }
111
112 char *
113 sh_mktmpname (nameroot, flags)
114      char *nameroot;
115      int flags;
116 {
117   char *filename, *tdir, *lroot;
118   struct stat sb;
119   int r, tdlen;
120
121   filename = (char *)xmalloc (PATH_MAX + 1);
122   tdir = get_tmpdir (flags);
123   tdlen = strlen (tdir);
124
125   lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
126
127 #ifdef USE_MKTEMP
128   sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
129   if (mktemp (filename) == 0)
130     {
131       free (filename);
132       filename = NULL;
133     }
134 #else  /* !USE_MKTEMP */
135   while (1)
136     {
137       filenum = (filenum << 1) ^
138                 (unsigned long) time ((time_t *)0) ^
139                 (unsigned long) dollar_dollar_pid ^
140                 (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
141       sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
142       if (tmpnamelen > 0 && tmpnamelen < 32)
143         filename[tdlen + 1 + tmpnamelen] = '\0';
144 #  ifdef HAVE_LSTAT
145       r = lstat (filename, &sb);
146 #  else
147       r = stat (filename, &sb);
148 #  endif
149       if (r < 0 && errno == ENOENT)
150         break;
151     }
152 #endif /* !USE_MKTEMP */
153
154   return filename;
155 }
156
157 int
158 sh_mktmpfd (nameroot, flags, namep)
159      char *nameroot;
160      int flags;
161      char **namep;
162 {
163   char *filename, *tdir, *lroot;
164   int fd, tdlen;
165
166   filename = (char *)xmalloc (PATH_MAX + 1);
167   tdir = get_tmpdir (flags);
168   tdlen = strlen (tdir);
169
170   lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
171
172 #ifdef USE_MKSTEMP
173   sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
174   fd = mkstemp (filename);
175   if (fd < 0 || namep == 0)
176     {
177       free (filename);
178       filename = NULL;
179     }
180   if (namep)
181     *namep = filename;
182   return fd;
183 #else /* !USE_MKSTEMP */
184   do
185     {
186       filenum = (filenum << 1) ^
187                 (unsigned long) time ((time_t *)0) ^
188                 (unsigned long) dollar_dollar_pid ^
189                 (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
190       sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
191       if (tmpnamelen > 0 && tmpnamelen < 32)
192         filename[tdlen + 1 + tmpnamelen] = '\0';
193       fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600);
194     }
195   while (fd < 0 && errno == EEXIST);
196
197   if (namep)
198     *namep = filename;
199   else
200     free (filename);
201
202   return fd;
203 #endif /* !USE_MKSTEMP */
204 }
205
206 FILE *
207 sh_mktmpfp (nameroot, flags, namep)
208      char *nameroot;
209      int flags;
210      char **namep;
211 {
212   int fd;
213   FILE *fp;
214
215   fd = sh_mktmpfd (nameroot, flags, namep);
216   if (fd < 0)
217     return ((FILE *)NULL);
218   fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w");
219   if (fp == 0)
220     close (fd);
221   return fp;
222 }