]> git.lyx.org Git - lyx.git/blob - src/support/tempname.C
Use lyxlex to parse rgb.txt + small compilation fixes
[lyx.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 using std::endl;
14
15 extern string system_tempdir;
16
17 string const lyx::tempName(string const & dir, string const & mask)
18 {
19 #if 0
20         // the tmpnam version...
21         char const * const tmp = ::tmpnam(0);
22         return (tmp) ? tmp : string ();
23 #else
24         string tmpdir;
25         if (dir.empty())
26                 tmpdir = system_tempdir;
27         else
28                 tmpdir = dir;
29         string tmpfl(AddName(tmpdir, mask));
30         tmpfl += tostr(getpid());
31
32         // the supposedly safer mkstemp version
33         char * tmpl = new char[256];
34         tmpfl += ".XXXXXX";
35         ::strcpy(tmpl, tmpfl.c_str());
36         int tmpf = ::mkstemp(tmpl);
37         if (tmpf != -1) {
38                 string const t(tmpl);
39                 ::close(tmpf);
40                 delete [] tmpl;
41                 lyxerr << "Temporary file `" << t << "' created." << endl;
42                 return t;
43         } else {
44                 lyxerr << "LyX Error: Unable to create temporary file."
45                        << endl;
46                 delete [] tmpl;
47                 return string();
48         }
49 #endif
50 }