]> git.lizzy.rs Git - dragonfireclient.git/blob - src/porting.cpp
merged fix to readlink ignored return value warning
[dragonfireclient.git] / src / porting.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21         Random portability stuff
22
23         See comments in porting.h
24 */
25
26 #include "porting.h"
27 #include "config.h"
28 #include "debug.h"
29
30 namespace porting
31 {
32
33 /*
34         Signal handler (grabs Ctrl-C on POSIX systems)
35 */
36
37 bool g_killed = false;
38
39 bool * signal_handler_killstatus(void)
40 {
41         return &g_killed;
42 }
43
44 #if !defined(_WIN32) // POSIX
45         #include <signal.h>
46
47 void sigint_handler(int sig)
48 {
49         if(g_killed == false)
50         {
51                 dstream<<DTIME<<"INFO: sigint_handler(): "
52                                 <<"Ctrl-C pressed, shutting down."<<std::endl;
53                 
54                 dstream<<DTIME<<"INFO: sigint_handler(): "
55                                 <<"Printing debug stacks"<<std::endl;
56                 debug_stacks_print();
57
58                 g_killed = true;
59         }
60         else
61         {
62                 (void)signal(SIGINT, SIG_DFL);
63         }
64 }
65
66 void signal_handler_init(void)
67 {
68         dstream<<"signal_handler_init()"<<std::endl;
69         (void)signal(SIGINT, sigint_handler);
70 }
71
72 #else // _WIN32
73
74 void signal_handler_init(void)
75 {
76         // No-op
77 }
78
79 #endif
80
81 /*
82         Path mangler
83 */
84
85 std::string path_data = "../data";
86 std::string path_userdata = "../";
87
88 void pathRemoveFile(char *path, char delim)
89 {
90         // Remove filename and path delimiter
91         int i;
92         for(i = strlen(path)-1; i>=0; i--)
93         {
94                 if(path[i] == delim)
95                         break;
96         }
97         path[i] = 0;
98 }
99
100 void initializePaths()
101 {
102 #ifdef RUN_IN_PLACE
103         /*
104                 Use relative paths if RUN_IN_PLACE
105         */
106
107         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
108
109         /*
110                 Windows
111         */
112         #if defined(_WIN32)
113                 #include <windows.h>
114
115         const DWORD buflen = 1000;
116         char buf[buflen];
117         DWORD len;
118         
119         // Find path of executable and set path_data relative to it
120         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
121         assert(len < buflen);
122         pathRemoveFile(buf, '\\');
123
124         // Use "./bin/../data"
125         path_data = std::string(buf) + "/../data";
126                 
127         // Use "./bin/../"
128         path_userdata = std::string(buf) + "/../";
129
130         /*
131                 Linux
132         */
133         #elif defined(linux)
134                 #include <unistd.h>
135         
136         char buf[BUFSIZ];
137         memset(buf, 0, BUFSIZ);
138         // Get path to executable
139         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
140         
141         pathRemoveFile(buf, '/');
142
143         // Use "./bin/../data"
144         path_data = std::string(buf) + "/../data";
145                 
146         // Use "./bin/../"
147         path_userdata = std::string(buf) + "/../";
148         
149         /*
150                 OS X
151         */
152         #elif defined(__APPLE__)
153         
154         //TODO: Get path of executable. This assumes working directory is bin/
155         dstream<<"WARNING: Relative path not properly supported on OS X"
156                         <<std::endl;
157         path_data = std::string("../data");
158         path_userdata = std::string("../");
159
160         #endif
161
162 #else // RUN_IN_PLACE
163
164         /*
165                 Use platform-specific paths otherwise
166         */
167
168         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
169
170         /*
171                 Windows
172         */
173         #if defined(_WIN32)
174                 #include <windows.h>
175
176         const DWORD buflen = 1000;
177         char buf[buflen];
178         DWORD len;
179         
180         // Find path of executable and set path_data relative to it
181         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
182         assert(len < buflen);
183         pathRemoveFile(buf, '\\');
184         
185         // Use "./bin/../data"
186         path_data = std::string(buf) + "/../data";
187         //path_data = std::string(buf) + "/../share/" + APPNAME;
188                 
189         // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
190         len = GetEnvironmentVariable("APPDATA", buf, buflen);
191         assert(len < buflen);
192         path_userdata = std::string(buf) + "/" + APPNAME;
193
194         /*
195                 Linux
196         */
197         #elif defined(linux)
198                 #include <unistd.h>
199         
200         char buf[BUFSIZ];
201         memset(buf, 0, BUFSIZ);
202         // Get path to executable
203         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
204         
205         pathRemoveFile(buf, '/');
206
207         path_data = std::string(buf) + "/../share/" + APPNAME;
208         //path_data = std::string(INSTALL_PREFIX) + "/share/" + APPNAME;
209         
210         path_userdata = std::string(getenv("HOME")) + "/." + APPNAME;
211
212         /*
213                 OS X
214         */
215         #elif defined(__APPLE__)
216                 #include <unistd.h>
217
218         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + APPNAME;
219         path_data = std::string("minetest-mac.app/Contents/Resources/data/");
220
221         #endif
222
223 #endif // RUN_IN_PLACE
224
225         dstream<<"path_data = "<<path_data<<std::endl;
226         dstream<<"path_userdata = "<<path_userdata<<std::endl;
227 }
228
229 } //namespace porting
230