Imported from ../bash-2.05.tar.gz.
[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 it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13
14    Bash is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Bash; see the file COPYING.  If not, write to the Free
21    Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 #include <config.h>
24
25 #include <bashtypes.h>
26 #include <posixstat.h>
27 #include <filecntl.h>
28
29 #if defined (HAVE_UNISTD_H)
30 #  include <unistd.h>
31 #endif
32
33 #include <stdio.h>
34 #include <errno.h>
35
36 #include <shell.h>
37
38 #ifndef errno
39 extern int errno;
40 #endif
41
42 #define BASEOPENFLAGS   (O_CREAT | O_TRUNC | O_EXCL)
43
44 #define DEFAULT_TMPDIR          "."     /* bogus default, should be changed */
45 #define DEFAULT_NAMEROOT        "shtmp"
46
47 extern int dollar_dollar_pid;
48
49 static char *sys_tmpdir = (char *)NULL;
50 static int ntmpfiles;
51 static int tmpnamelen = -1;
52 static unsigned long filenum = 1L;
53
54 static char *
55 get_sys_tmpdir ()
56 {
57   struct stat sb;
58
59   if (sys_tmpdir)
60     return sys_tmpdir;
61
62   sys_tmpdir = "/tmp";
63   if (stat (sys_tmpdir, &sb) == 0)
64     return sys_tmpdir;
65
66   sys_tmpdir = "/var/tmp";
67   if (stat (sys_tmpdir, &sb) == 0)
68     return sys_tmpdir;
69
70   sys_tmpdir = "/usr/tmp";
71   if (stat (sys_tmpdir, &sb) == 0)
72     return sys_tmpdir;
73
74 #ifdef P_tmpdir
75   sys_tmpdir = P_tmpdir;
76 #else
77   sys_tmpdir = DEFAULT_TMPDIR;
78 #endif
79
80   return sys_tmpdir;
81 }
82
83 static char *
84 get_tmpdir (flags)
85      int flags;
86 {
87   char *tdir;
88
89   tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
90   if (tdir == 0)
91     tdir = get_sys_tmpdir ();
92
93 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
94   if (tmpnamelen == -1)
95     tmpnamelen = pathconf (tdir, _PC_NAME_MAX);
96 #else
97   tmpnamelen = 0;
98 #endif
99
100   return tdir;
101 }
102
103 char *
104 sh_mktmpname (nameroot, flags)
105      char *nameroot;
106      int flags;
107 {
108   char *filename, *tdir;
109   struct stat sb;
110   int r, tdlen;
111
112   filename = xmalloc (PATH_MAX + 1);
113   tdir = get_tmpdir (flags);
114   tdlen = strlen (tdir);
115
116   if (nameroot == 0)
117     nameroot = DEFAULT_NAMEROOT;
118
119   while (1)
120     {
121       filenum *= (int)time ((time_t *)0) * dollar_dollar_pid *
122                 ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
123       sprintf (filename, "%s/%s-%lu", tdir, nameroot, filenum);
124       if (tmpnamelen > 0 && tmpnamelen < 32)
125         filename[tdlen + 1 + tmpnamelen] = '\0';
126 #ifdef HAVE_LSTAT
127       r = lstat (filename, &sb);
128 #else
129       r = stat (filename, &sb);
130 #endif
131       if (r < 0 && errno == ENOENT)
132         break;
133     }
134
135   return filename;
136 }
137
138 int
139 sh_mktmpfd (nameroot, flags, namep)
140      char *nameroot;
141      int flags;
142      char **namep;
143 {
144   char *filename, *tdir;
145   int fd, tdlen;
146
147   filename = xmalloc (PATH_MAX + 1);
148   tdir = get_tmpdir (flags);
149   tdlen = strlen (tdir);
150
151   if (nameroot == 0)
152     nameroot = DEFAULT_NAMEROOT;
153
154   do
155     {
156       filenum *= (int)time ((time_t *)0) * dollar_dollar_pid *
157                 ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
158       sprintf (filename, "%s/%s-%lu", tdir, nameroot, filenum);
159       if (tmpnamelen > 0 && tmpnamelen < 32)
160         filename[tdlen + 1 + tmpnamelen] = '\0';
161       fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600);
162     }
163   while (fd < 0 && errno == EEXIST);
164
165   if (namep)
166     *namep = filename;
167   else
168     free (filename);
169
170   return fd;
171 }
172
173 FILE *
174 sh_mktmpfp (nameroot, flags, namep)
175      char *nameroot;
176      int flags;
177      char **namep;
178 {
179   int fd;
180
181   fd = sh_mktmpfd (nameroot, flags, namep);
182   return ((fd >= 0) ? (fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w")) : (FILE *)NULL);
183 }