]> git.lyx.org Git - lyx.git/blob - src/support/tempname.C
* output_plaintext.C: cosmetics in comment: line length cannot be < 0
[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 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         // FIXME UNICODE encoding of package().temp_dir() is probably wrong
84         string const tmpdir(dir.empty() ? package().temp_dir() : dir.toFilesystemEncoding());
85         string tmpfl(addName(tmpdir, mask));
86 #if defined (HAVE_GETPID)
87         tmpfl += convert<string>(getpid());
88 #elif defined (HAVE__GETPID)
89         tmpfl += convert<string>(_getpid());
90 #else
91 # error No getpid() function
92 #endif
93         tmpfl += "XXXXXX";
94
95         // The supposedly safe mkstemp version
96         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
97         tmpfl.copy(tmpl.get(), string::npos);
98         tmpl[tmpfl.length()] = '\0'; // terminator
99
100         int const tmpf = make_tempfile(tmpl.get());
101         if (tmpf != -1) {
102                 string const t(tmpl.get());
103 #if defined (HAVE_CLOSE)
104                 ::close(tmpf);
105 #elif defined (HAVE__CLOSE)
106                 ::_close(tmpf);
107 #else
108 # error No x() function.
109 #endif
110                 lyxerr[Debug::FILES] << "Temporary file `" << t
111                                      << "' created." << endl;
112                 return FileName(t);
113         } else {
114                 lyxerr[Debug::FILES]
115                         << "LyX Error: Unable to create temporary file."
116                         << endl;
117                 return FileName();
118         }
119 }
120
121 } // namespace support
122 } // namespace lyx