]> git.lyx.org Git - lyx.git/blob - src/support/tempname.cpp
use LYXERR macro to avoid unneeded argument evaluations...
[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_DECL_MKSTEMP)
30 extern "C" int mkstemp(char *);
31 #endif
32
33 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
34 # include <fcntl.h>
35 # ifdef HAVE_SYS_STAT_H
36 #  include <sys/stat.h>
37 # endif
38 # ifdef HAVE_IO_H
39 #  include <io.h>
40 # endif
41 # ifdef HAVE_PROCESS_H
42 #  include <process.h>
43 # endif
44 #endif
45
46 using boost::scoped_array;
47
48 using std::string;
49
50 namespace lyx {
51 namespace support {
52
53 namespace {
54
55 inline
56 int make_tempfile(char * templ)
57 {
58 #if defined(HAVE_MKSTEMP)
59         return ::mkstemp(templ);
60 #elif defined(HAVE_MKTEMP)
61         // This probably just barely works...
62         ::mktemp(templ);
63 # if defined (HAVE_OPEN)
64 # if (!defined S_IRUSR)
65 #   define S_IRUSR S_IREAD
66 #   define S_IWUSR S_IWRITE
67 # endif
68         return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
69 # elif defined (HAVE__OPEN)
70         return ::_open(templ,
71                        _O_RDWR | _O_CREAT | _O_EXCL,
72                        _S_IREAD | _S_IWRITE);
73 # else
74 #  error No open() function.
75 # endif
76 #else
77 #error FIX FIX FIX
78 #endif
79 }
80
81 } // namespace anon
82
83
84 FileName const tempName(FileName const & dir, string const & mask)
85 {
86         string const tmpdir = dir.empty()
87                 ? package().temp_dir().absFilename() : dir.absFilename();
88         string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, mask)));
89 #if defined (HAVE_GETPID)
90         tmpfl += convert<string>(getpid());
91 #elif defined (HAVE__GETPID)
92         tmpfl += convert<string>(_getpid());
93 #else
94 # error No getpid() function
95 #endif
96         tmpfl += "XXXXXX";
97
98         // The supposedly safe mkstemp version
99         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
100         tmpfl.copy(tmpl.get(), string::npos);
101         tmpl[tmpfl.length()] = '\0'; // terminator
102
103         int const tmpf = make_tempfile(tmpl.get());
104         if (tmpf != -1) {
105                 string const t(to_utf8(from_filesystem8bit(tmpl.get())));
106 #if defined (HAVE_CLOSE)
107                 ::close(tmpf);
108 #elif defined (HAVE__CLOSE)
109                 ::_close(tmpf);
110 #else
111 # error No x() function.
112 #endif
113                 LYXERR(Debug::FILES, "Temporary file `" << t << "' created.");
114                 return FileName(t);
115         }
116         LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
117         return FileName();
118 }
119
120 } // namespace support
121 } // namespace lyx