]> git.lyx.org Git - lyx.git/blob - src/support/tempname.C
hopefully fix tex2lyx linking.
[lyx.git] / src / support / tempname.C
1 /**
2  * \file tempname.C
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
49 namespace {
50
51 inline
52 int make_tempfile(char * templ)
53 {
54 #if defined(HAVE_MKSTEMP)
55         return ::mkstemp(templ);
56 #elif defined(HAVE_MKTEMP)
57         // This probably just barely works...
58         ::mktemp(templ);
59 # if defined (HAVE_OPEN)
60 # if (!defined S_IRUSR)
61 #   define S_IRUSR S_IREAD
62 #   define S_IWUSR S_IWRITE
63 # endif
64         return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
65 # elif defined (HAVE__OPEN)
66         return ::_open(templ,
67                        _O_RDWR | _O_CREAT | _O_EXCL,
68                        _S_IREAD | _S_IWRITE);
69 # else
70 #  error No open() function.
71 # endif
72 #else
73 #error FIX FIX FIX
74 #endif
75 }
76
77 } // namespace anon
78
79
80 string const lyx::support::tempName(string const & dir, string const & mask)
81 {
82         string const tmpdir(dir.empty() ? package().temp_dir() : dir);
83         string tmpfl(addName(tmpdir, mask));
84 #if defined (HAVE_GETPID)
85         tmpfl += convert<string>(getpid());
86 #elif defined (HAVE__GETPID)
87         tmpfl += convert<string>(_getpid());
88 #else
89 # error No getpid() function
90 #endif
91         tmpfl += "XXXXXX";
92
93         // The supposedly safe mkstemp version
94         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
95         tmpfl.copy(tmpl.get(), string::npos);
96         tmpl[tmpfl.length()] = '\0'; // terminator
97
98         int const tmpf = make_tempfile(tmpl.get());
99         if (tmpf != -1) {
100                 string const t(tmpl.get());
101 #if defined (HAVE_CLOSE)
102                 ::close(tmpf);
103 #elif defined (HAVE__CLOSE)
104                 ::_close(tmpf);
105 #else
106 # error No x() function.
107 #endif
108                 lyxerr[Debug::FILES] << "Temporary file `" << t
109                                      << "' created." << endl;
110                 return t;
111         } else {
112                 lyxerr[Debug::FILES]
113                         << "LyX Error: Unable to create temporary file."
114                         << endl;
115                 return string();
116         }
117 }
118
119
120 } // namespace lyx