Debian Codegraph @

src/misc/cross.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: cross.cpp,v 1.7 2009-05-26 17:43:39 qbix79 Exp $ */
20
21 #include "dosbox.h"
22 #include "cross.h"
23 #include "support.h"
24 #include <string>
25 #include <limits.h>
26 #include <stdlib.h>
27
28 #ifdef WIN32
29 #ifndef _WIN32_IE
30 #define _WIN32_IE 0x0400
31 #endif
32 #include <shlobj.h>
33 #endif
34
35 #if defined HAVE_SYS_TYPES_H && defined HAVE_PWD_H
36 #include <sys/types.h>
37 #include <pwd.h>
38 #endif
39
40 #ifdef WIN32
41 static void W32_ConfDir(std::string& in,bool create) {
42 int c = create?1:0;
43 char result[MAX_PATH] = { 0 };
44 BOOL r = SHGetSpecialFolderPath(NULL,result,CSIDL_LOCAL_APPDATA,c);
45 if(!r || result[0] == 0) r = SHGetSpecialFolderPath(NULL,result,CSIDL_APPDATA,c);
46 if(!r || result[0] == 0) {
47 char const * windir = getenv("windir");
48 if(!windir) windir = "c:\\windows";
49 safe_strncpy(result,windir,MAX_PATH);
50 char const* appdata = "\\Application Data";
51 size_t len = strlen(result);
52 if(len + strlen(appdata) < MAX_PATH) strcat(result,appdata);
53 if(create) mkdir(result);
54 }
55 in = result;
56 }
57 #endif
58
59 void Cross::GetPlatformConfigDir(std::string& in) {
60 #ifdef WIN32
61 W32_ConfDir(in,false);
62 in += "\\DOSBox";
63 #elif defined(MACOSX)
64 in = "~/Library/Preferences";
65 ResolveHomedir(in);
66 #else
67 in = "~/.dosbox";
68 ResolveHomedir(in);
69 #endif
70 in += CROSS_FILESPLIT;
71 }
72
73 void Cross::GetPlatformConfigName(std::string& in) {
74 #ifdef WIN32
75 #define DEFAULT_CONFIG_FILE "dosbox-" VERSION ".conf"
76 #elif defined(MACOSX)
77 #define DEFAULT_CONFIG_FILE "DOSBox " VERSION " Preferences"
78 #else /*linux freebsd*/
79 #define DEFAULT_CONFIG_FILE "dosbox-" VERSION ".conf"
80 #endif
81 in = DEFAULT_CONFIG_FILE;
82 }
83
84 void Cross::CreatePlatformConfigDir(std::string& in) {
85 #ifdef WIN32
86 W32_ConfDir(in,true);
87 in += "\\DOSBox";
88 mkdir(in.c_str());
89 #elif defined(MACOSX)
90 in = "~/Library/Preferences/";
91 ResolveHomedir(in);
92 //Don't create it. Assume it exists
93 #else
94 in = "~/.dosbox";
95 ResolveHomedir(in);
96 mkdir(in.c_str(),0700);
97 #endif
98 in += CROSS_FILESPLIT;
99 }
100
101 void Cross::ResolveHomedir(std::string & temp_line) {
102 if(!temp_line.size() || temp_line[0] != '~') return; //No ~
103
104 if(temp_line.size() == 1 || temp_line[1] == CROSS_FILESPLIT) { //The ~ and ~/ variant
105 char * home = getenv("HOME");
106 if(home) temp_line.replace(0,1,std::string(home));
107 #if defined HAVE_SYS_TYPES_H && defined HAVE_PWD_H
108 } else { // The ~username variant
109 std::string::size_type namelen = temp_line.find(CROSS_FILESPLIT);
110 if(namelen == std::string::npos) namelen = temp_line.size();
111 std::string username = temp_line.substr(1,namelen - 1);
112 struct passwd* pass = getpwnam(username.c_str());
113 if(pass) temp_line.replace(0,namelen,pass->pw_dir); //namelen -1 +1(for the ~)
114 #endif // USERNAME lookup code
115 }
116 }
117
118 void Cross::CreateDir(std::string const& in) {
119 #ifdef WIN32
120 mkdir(in.c_str());
121 #else
122 mkdir(in.c_str(),0700);
123 #endif
124 }
125
126 #if defined (WIN32)
127
128 dir_information* open_directory(const char* dirname) {
129 if (dirname == NULL) return NULL;
130
131 size_t len = strlen(dirname);
132 if (len == 0) return NULL;
133
134 static dir_information dir;
135
136 safe_strncpy(dir.base_path,dirname,MAX_PATH);
137
138 if (dirname[len-1] == '\\') strcat(dir.base_path,"*.*");
139 else strcat(dir.base_path,"\\*.*");
140
141 dir.handle = INVALID_HANDLE_VALUE;
142
143 return (access(dirname,0) ? NULL : &dir);
144 }
145
146 bool read_directory_first(dir_information* dirp, char* entry_name, bool& is_directory) {
147 dirp->handle = FindFirstFile(dirp->base_path, &dirp->search_data);
148 if (INVALID_HANDLE_VALUE == dirp->handle) {
149 return false;
150 }
151
152 safe_strncpy(entry_name,dirp->search_data.cFileName,(MAX_PATH<CROSS_LEN)?MAX_PATH:CROSS_LEN);
153
154 if (dirp->search_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) is_directory = true;
155 else is_directory = false;
156
157 return true;
158 }
159
160 bool read_directory_next(dir_information* dirp, char* entry_name, bool& is_directory) {
161 int result = FindNextFile(dirp->handle, &dirp->search_data);
162 if (result==0) return false;
163
164 safe_strncpy(entry_name,dirp->search_data.cFileName,(MAX_PATH<CROSS_LEN)?MAX_PATH:CROSS_LEN);
165
166 if (dirp->search_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) is_directory = true;
167 else is_directory = false;
168
169 return true;
170 }
171
172 void close_directory(dir_information* dirp) {
173 if (dirp->handle != INVALID_HANDLE_VALUE) {
174 FindClose(dirp->handle);
175 dirp->handle = INVALID_HANDLE_VALUE;
176 }
177 }
178
179 #else
180
181 dir_information* open_directory(const char* dirname) {
182 static dir_information dir;
183 dir.dir=opendir(dirname);
184 safe_strncpy(dir.base_path,dirname,CROSS_LEN);
185 return dir.dir?&dir:NULL;
186 }
187
188 bool read_directory_first(dir_information* dirp, char* entry_name, bool& is_directory) {
189 struct dirent* dentry = readdir(dirp->dir);
190 if (dentry==NULL) {
191 return false;
192 }
193
194 // safe_strncpy(entry_name,dentry->d_name,(FILENAME_MAX<MAX_PATH)?FILENAME_MAX:MAX_PATH); // [include stdio.h], maybe pathconf()
195 safe_strncpy(entry_name,dentry->d_name,CROSS_LEN);
196
197 #ifdef DIRENT_HAS_D_TYPE
198 if(dentry->d_type == DT_DIR) {
199 is_directory = true;
200 return true;
201 } else if(dentry->d_type == DT_REG) {
202 is_directory = false;
203 return true;
204 }
205 #endif
206
207 // probably use d_type here instead of a full stat()
208 static char buffer[2*CROSS_LEN] = { 0 };
209 buffer[0] = 0;
210 strcpy(buffer,dirp->base_path);
211 strcat(buffer,entry_name);
212 struct stat status;
213 if (stat(buffer,&status)==0) is_directory = (S_ISDIR(status.st_mode)>0);
214 else is_directory = false;
215
216 return true;
217 }
218
219 bool read_directory_next(dir_information* dirp, char* entry_name, bool& is_directory) {
220 struct dirent* dentry = readdir(dirp->dir);
221 if (dentry==NULL) {
222 return false;
223 }
224
225 // safe_strncpy(entry_name,dentry->d_name,(FILENAME_MAX<MAX_PATH)?FILENAME_MAX:MAX_PATH); // [include stdio.h], maybe pathconf()
226 safe_strncpy(entry_name,dentry->d_name,CROSS_LEN);
227
228 #ifdef DIRENT_HAS_D_TYPE
229 if(dentry->d_type == DT_DIR) {
230 is_directory = true;
231 return true;
232 } else if(dentry->d_type == DT_REG) {
233 is_directory = false;
234 return true;
235 }
236 #endif
237
238 // probably use d_type here instead of a full stat()
239 static char buffer[2*CROSS_LEN] = { 0 };
240 buffer[0] = 0;
241 strcpy(buffer,dirp->base_path);
242 strcat(buffer,entry_name);
243 struct stat status;
244
245 if (stat(buffer,&status)==0) is_directory = (S_ISDIR(status.st_mode)>0);
246 else is_directory = false;
247
248 return true;
249 }
250
251 void close_directory(dir_information* dirp) {
252 closedir(dirp->dir);
253 }
254
255 #endif
256
257 FILE *fopen_wrap(const char *path, const char *mode) {
258 #if defined(WIN32) || defined(OS2)
259 ;
260 #elif defined (MACOSX)
261 ;
262 #else
263 #if defined (HAVE_REALPATH)
264 char work[CROSS_LEN] = {0};
265 strncpy(work,path,CROSS_LEN-1);
266 char* last = strrchr(work,'/');
267
268 if (last) {
269 if (last != work) {
270 *last = 0;
271 //If this compare fails, then we are dealing with files in /
272 //Which is outside the scope, but test anyway.
273 //However as realpath only works for exising files. The testing is
274 //in that case not done against new files.
275 }
276 char* check = realpath(work,NULL);
277 if (check) {
278 if ( ( strlen(check) == 5 && strcmp(check,"/proc") == 0) || strncmp(check,"/proc/",6) == 0) {
279 // LOG_MSG("lst hit %s blocking!",path);
280 free(check);
281 return NULL;
282 }
283 free(check);
284 }
285 }
286
287 #if 0
288 //Lightweight version, but then existing files can still be read, which is not ideal
289 if (strpbrk(mode,"aw+") != NULL) {
290 LOG_MSG("pbrk ok");
291 char* check = realpath(path,NULL);
292 //Will be null if file doesn't exist.... ENOENT
293 //TODO What about unlink /proc/self/mem and then create it ?
294 //Should be safe for what we want..
295 if (check) {
296 if (strncmp(check,"/proc/",6) == 0) {
297 free(check);
298 return NULL;
299 }
300 free(check);
301 }
302 }
303 */
304 #endif //0
305
306 #endif //HAVE_REALPATH
307 #endif
308
309 return fopen(path,mode);
310 }
311
312