]> git.lyx.org Git - lyx.git/blob - src/support/tempname.C
* filetools.[Ch]: Make functions that start with a capital
[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         return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
58 # elif defined (HAVE__OPEN)
59         return ::_open(templ,
60                        _O_RDWR | _O_CREAT | _O_EXCL,
61                        _S_IREAD | _S_IWRITE);
62 # else
63 #  error No open() function.
64 # endif
65 #else
66 #error FIX FIX FIX
67 #endif
68 }
69
70 } // namespace anon
71
72
73 string const lyx::support::tempName(string const & dir, string const & mask)
74 {
75         string const tmpdir(dir.empty() ? package().temp_dir() : dir);
76         string tmpfl(addName(tmpdir, mask));
77 #if defined (HAVE_GETPID)
78         tmpfl += convert<string>(getpid());
79 #elif defined (HAVE__GETPID)
80         tmpfl += convert<string>(_getpid());
81 #else
82 # error No getpid() function
83 #endif
84         tmpfl += "XXXXXX";
85
86         // The supposedly safe mkstemp version
87         scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
88         tmpfl.copy(tmpl.get(), string::npos);
89         tmpl[tmpfl.length()] = '\0'; // terminator
90
91         int const tmpf = make_tempfile(tmpl.get());
92         if (tmpf != -1) {
93                 string const t(tmpl.get());
94 #if defined (HAVE_CLOSE)
95                 ::close(tmpf);
96 #elif defined (HAVE__CLOSE)
97                 ::_close(tmpf);
98 #else
99 # error No close() function.
100 #endif
101                 lyxerr[Debug::FILES] << "Temporary file `" << t
102                                      << "' created." << endl;
103                 return t;
104         } else {
105                 lyxerr[Debug::FILES]
106                         << "LyX Error: Unable to create temporary file."
107                         << endl;
108                 return string();
109         }
110 }