]> git.lyx.org Git - features.git/blob - src/support/tempname.C
changes read the dirr and changelog, something should be remvoed/changed
[features.git] / src / support / tempname.C
1 #include <config.h>
2
3 #include "LString.h"
4
5 #include <cstdlib>
6
7 #include <unistd.h>
8
9 #include "lyxlib.h"
10 #include "debug.h"
11 #include "filetools.h"
12
13 extern string system_tempdir;
14
15 string const lyx::tempName(string const & dir, string const & mask)
16 {
17 #if 0
18         // the tmpnam version...
19         char const * const tmp = ::tmpnam(0);
20         return (tmp) ? tmp : string ();
21 #else
22         string tmpdir;
23         if (dir.empty())
24                 tmpdir = system_tempdir;
25         else
26                 tmpdir = dir;
27         string tmpfl(AddName(tmpdir, mask));
28         tmpfl += tostr(getpid());
29
30         // the supposedly safer mkstemp version
31         char * tmpl = new char[256];
32         tmpfl += ".XXXXXX";
33         ::strcpy(tmpl, tmpfl.c_str());
34         int tmpf = ::mkstemp(tmpl);
35         if (tmpf != -1) {
36                 string const t(tmpl);
37                 ::close(tmpf);
38                 delete [] tmpl;
39                 lyxerr << "Temporary file `" << t << "' created." << endl;
40                 return t;
41         } else {
42                 lyxerr << "LyX Error: Unable to create temporary file."
43                        << endl;
44                 delete [] tmpl;
45                 return string();
46         }
47 #endif
48 }