]> git.lyx.org Git - lyx.git/blob - src/support/tempname.cpp
Possibly fix this bug:
[lyx.git] / src / support / tempname.cpp
1 /**
2  * \file tempname.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14
15 #include "support/convert.h"
16 #include "support/filetools.h"
17 #include "support/Package.h"
18
19 #include "debug.h"
20
21 #include <boost/scoped_array.hpp>
22
23 #include <cstdlib>
24
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28
29 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
30 # include <fcntl.h>
31 # ifdef HAVE_SYS_STAT_H
32 #  include <sys/stat.h>
33 # endif
34 # ifdef HAVE_IO_H
35 #  include <io.h>
36 # endif
37 # ifdef HAVE_PROCESS_H
38 #  include <process.h>
39 # endif
40 #endif
41
42 using boost::scoped_array;
43
44 using std::string;
45 using std::endl;
46
47 namespace lyx {
48 namespace support {
49
50 namespace {
51
52 inline
53 int make_tempfile(char * templ)
54 {
55 #if defined(HAVE_MKSTEMP)
56         return ::mkstemp(templ);
57 #elif defined(HAVE_MKTEMP)
58         // This probably just barely works...
59         ::mktemp(templ);
60 # if defined (HAVE_OPEN)
61 # if (!defined S_IRUSR)
62 #   define S_IRUSR S_IREAD
63 #   define S_IWUSR S_IWRITE
64 # endif
65         return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
66 # elif defined (HAVE__OPEN)
67         return ::_open(templ,
68                        _O_RDWR | _O_CREAT | _O_EXCL,
69                        _S_IREAD | _S_IWRITE);
70 # else
71 #  error No open() function.
72 # endif
73 #else
74 #error FIX FIX FIX
75 #endif
76 }
77
78 } // namespace anon
79
80
81 FileName const tempName(FileName const & dir, string const & mask)
82 {
83         string const tmpdir(dir.empty() ?
84                         package().temp_dir().absFilename() :
85                         dir.absFilename());
86         string tmpfl(to_filesystem8bit(from_utf8(addName(tmpdir, mask))));
87 #if defined (HAVE_GETPID)
88         tmpfl += convert<string>(getpid());
89 #elif defined (HAVE__GETPID)
90         tmpfl += convert<string>(_getpid());
91 #else
92 # error No getpid() function
93 #endif
94         tmpfl += "XXXXXX";
95
96         // The supposedly safe mkstemp version
97         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
98         tmpfl.copy(tmpl.get(), string::npos);
99         tmpl[tmpfl.length()] = '\0'; // terminator
100
101         int const tmpf = make_tempfile(tmpl.get());
102         if (tmpf != -1) {
103                 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
104 #if defined (HAVE_CLOSE)
105                 ::close(tmpf);
106 #elif defined (HAVE__CLOSE)
107                 ::_close(tmpf);
108 #else
109 # error No x() function.
110 #endif
111                 LYXERR(Debug::FILES) << "Temporary file `" << t
112                                      << "' created." << endl;
113                 return FileName(t);
114         } else {
115                 LYXERR(Debug::FILES)
116                         << "LyX Error: Unable to create temporary file."
117                         << endl;
118                 return FileName();
119         }
120 }
121
122 } // namespace support
123 } // namespace lyx