]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/tmpfile.c
2 * tmpfile.c - functions to create and safely open temp files for the shell.
5 /* Copyright (C) 2000 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
25 #include <bashtypes.h>
26 #include <posixstat.h>
27 #include <posixtime.h>
30 #if defined (HAVE_UNISTD_H)
43 #define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL)
45 #define DEFAULT_TMPDIR "." /* bogus default, should be changed */
46 #define DEFAULT_NAMEROOT "shtmp"
48 extern pid_t dollar_dollar_pid
;
50 static char *get_sys_tmpdir
__P((void));
51 static char *get_tmpdir
__P((int));
53 static char *sys_tmpdir
= (char *)NULL
;
55 static int tmpnamelen
= -1;
56 static unsigned long filenum
= 1L;
65 sys_tmpdir
= P_tmpdir
;
66 if (file_iswdir (sys_tmpdir
))
71 if (file_iswdir (sys_tmpdir
))
74 sys_tmpdir
= "/var/tmp";
75 if (file_iswdir (sys_tmpdir
))
78 sys_tmpdir
= "/usr/tmp";
79 if (file_iswdir (sys_tmpdir
))
82 sys_tmpdir
= DEFAULT_TMPDIR
;
93 tdir
= (flags
& MT_USETMPDIR
) ? get_string_value ("TMPDIR") : (char *)NULL
;
94 if (tdir
&& (file_iswdir (tdir
) == 0 || strlen (tdir
) > PATH_MAX
))
98 tdir
= get_sys_tmpdir ();
100 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
101 if (tmpnamelen
== -1)
102 tmpnamelen
= pathconf (tdir
, _PC_NAME_MAX
);
111 sh_mktmpname (nameroot
, flags
)
115 char *filename
, *tdir
, *lroot
;
119 filename
= (char *)xmalloc (PATH_MAX
+ 1);
120 tdir
= get_tmpdir (flags
);
121 tdlen
= strlen (tdir
);
123 lroot
= nameroot
? nameroot
: DEFAULT_NAMEROOT
;
126 sprintf (filename
, "%s/%s.XXXXXX", tdir
, lroot
);
127 if (mktemp (filename
) == 0)
132 #else /* !USE_MKTEMP */
135 filenum
= (filenum
<< 1) ^
136 (unsigned long) time ((time_t *)0) ^
137 (unsigned long) dollar_dollar_pid
^
138 (unsigned long) ((flags
& MT_USERANDOM
) ? get_random_number () : ntmpfiles
++);
139 sprintf (filename
, "%s/%s-%lu", tdir
, lroot
, filenum
);
140 if (tmpnamelen
> 0 && tmpnamelen
< 32)
141 filename
[tdlen
+ 1 + tmpnamelen
] = '\0';
143 r
= lstat (filename
, &sb
);
145 r
= stat (filename
, &sb
);
147 if (r
< 0 && errno
== ENOENT
)
150 #endif /* !USE_MKTEMP */
156 sh_mktmpfd (nameroot
, flags
, namep
)
161 char *filename
, *tdir
, *lroot
;
164 filename
= (char *)xmalloc (PATH_MAX
+ 1);
165 tdir
= get_tmpdir (flags
);
166 tdlen
= strlen (tdir
);
168 lroot
= nameroot
? nameroot
: DEFAULT_NAMEROOT
;
171 sprintf (filename
, "%s/%s.XXXXXX", tdir
, lroot
);
172 fd
= mkstemp (filename
);
173 if (fd
< 0 || namep
== 0)
181 #else /* !USE_MKSTEMP */
184 filenum
= (filenum
<< 1) ^
185 (unsigned long) time ((time_t *)0) ^
186 (unsigned long) dollar_dollar_pid
^
187 (unsigned long) ((flags
& MT_USERANDOM
) ? get_random_number () : ntmpfiles
++);
188 sprintf (filename
, "%s/%s-%lu", tdir
, lroot
, filenum
);
189 if (tmpnamelen
> 0 && tmpnamelen
< 32)
190 filename
[tdlen
+ 1 + tmpnamelen
] = '\0';
191 fd
= open (filename
, BASEOPENFLAGS
| ((flags
& MT_READWRITE
) ? O_RDWR
: O_WRONLY
), 0600);
193 while (fd
< 0 && errno
== EEXIST
);
201 #endif /* !USE_MKSTEMP */
205 sh_mktmpfp (nameroot
, flags
, namep
)
213 fd
= sh_mktmpfd (nameroot
, flags
, namep
);
215 return ((FILE *)NULL
);
216 fp
= fdopen (fd
, (flags
& MT_READWRITE
) ? "w+" : "w");