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