]> git.lyx.org Git - lyx.git/blob - src/support/tempname.C
fix doxygen operation ; stop #warning causing errors on Sun CC 6.0
[lyx.git] / src / support / tempname.C
1 #include <config.h>
2
3 #include <cstdlib>
4 #include <unistd.h>
5
6 #include "LString.h"
7 #include "support/lyxlib.h"
8 #include "support/filetools.h"
9 #include "debug.h"
10
11 using std::endl;
12
13 extern string system_tempdir;
14
15 namespace {
16
17 inline
18 int make_tempfile(char * templ) 
19 {
20 #ifdef HAVE_MKSTEMP
21         return ::mkstemp(templ);
22 #else
23 #ifdef HAVE_MKTEMP
24         // This probably just barely works...
25         ::mktemp(templ);
26         return ::open(templ, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
27 #else
28 #ifdef WITH_WARNINGS
29 #warning FIX FIX FIX
30 #endif
31 #endif
32 #endif
33 }
34
35 } // namespace anon
36
37
38 string const lyx::tempName(string const & dir, string const & mask)
39 {
40         string const tmpdir(dir.empty() ? system_tempdir : dir);
41         string tmpfl(AddName(tmpdir, mask));
42         tmpfl += tostr(getpid());
43         tmpfl += "XXXXXX";
44
45         // The supposedly safe mkstemp version
46         char * tmpl = new char[tmpfl.length() + 1]; // + 1 for '\0'
47         tmpfl.copy(tmpl, string::npos);
48         tmpl[tmpfl.length()] = '\0'; // terminator
49         
50         int const tmpf = make_tempfile(tmpl);
51         if (tmpf != -1) {
52                 string const t(tmpl);
53                 ::close(tmpf);
54                 delete [] tmpl;
55                 lyxerr[Debug::FILES] << "Temporary file `" << t
56                                      << "' created." << endl;
57                 return t;
58         } else {
59                 lyxerr[Debug::FILES]
60                         << "LyX Error: Unable to create temporary file."
61                         << endl;
62                 delete [] tmpl;
63                 return string();
64         }
65 }