| 1 |
/** |
| 2 |
* Filesystem helpers |
| 3 |
|
| 4 |
* Copyright (C) 2005, 2007, 2008, 2009 Sylvain Beucler |
| 5 |
|
| 6 |
* This file is part of GNU FreeDink |
| 7 |
|
| 8 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License as |
| 10 |
* published by the Free Software Foundation; either version 3 of the |
| 11 |
* License, or (at your option) any later version. |
| 12 |
|
| 13 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
|
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see |
| 20 |
* <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <strings.h> /* strcasecmp */ |
| 31 |
#include <dirent.h> |
| 32 |
#include <errno.h> |
| 33 |
|
| 34 |
/* stat(2) */ |
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <unistd.h> |
| 38 |
|
| 39 |
#include "SDL.h" |
| 40 |
#ifdef HAVE_LIBZIP |
| 41 |
# include "SDL_rwops_libzip.h" |
| 42 |
#else |
| 43 |
# ifdef HAVE_ZZIPLIB |
| 44 |
#include "SDL_rwops_zzip.h" |
| 45 |
# endif |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "paths.h" |
| 49 |
|
| 50 |
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ || defined __EMSCRIPTEN__ |
| 51 |
#else |
| 52 |
/* Returns a pointer to the end of the current path element (file or |
| 53 |
directory) */ |
| 54 |
static char* |
| 55 |
end_of_elt(char *str) |
| 56 |
{ |
| 57 |
char *p = str; |
| 58 |
while ((*p != '/') && (*p != '\\') && (*p != '\0')) |
| 59 |
p++; |
| 60 |
return p; |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
/** |
| 65 |
* Look for filename case-insensitively, to mimic MS Woe's |
| 66 |
* case-insensitive file system. It modifies the 'filename' buffer |
| 67 |
* in-place. Also converts '\' to '/'. I REPEAT: the filename buffer |
| 68 |
* is modified, so it cannot be "a constant string", or you'll get a |
| 69 |
* segfault. */ |
| 70 |
void |
| 71 |
ciconvert (char *filename) |
| 72 |
{ |
| 73 |
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ |
| 74 |
/* For __EMSCRIPTEN__ remember to -s CASE_INSENSITIVE_FS=1 */ |
| 75 |
return; |
| 76 |
#elif defined __EMSCRIPTEN__ |
| 77 |
/* convert windows-style backslashes in paths */ |
| 78 |
char *pcur_elt = filename; |
| 79 |
while (*pcur_elt) |
| 80 |
{ |
| 81 |
if (*pcur_elt == '\\') |
| 82 |
*pcur_elt = '/'; |
| 83 |
pcur_elt++; |
| 84 |
} |
| 85 |
#else |
| 86 |
/* Parse all the directories that composes filename */ |
| 87 |
char *cur_dir = NULL; |
| 88 |
char *pcur_elt, *pend_of_elt, *pend_of_cur_dir; |
| 89 |
int error = 0; |
| 90 |
|
| 91 |
/* First simply try to open the file, constant complexity. Permits |
| 92 |
decent speed for careful authors who write exact filenames. */ |
| 93 |
FILE *f; |
| 94 |
if ((f = fopen(filename, "r")) != NULL) |
| 95 |
{ |
| 96 |
fclose(f); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
/* Else, check each path element of the filename */ |
| 101 |
/* No need to support volumes ("C:\"...) because this function |
| 102 |
already returned in case-insensitive environments (woe&dos) */ |
| 103 |
if (filename[0] == '/') |
| 104 |
{ |
| 105 |
cur_dir = (char*)malloc(strlen("/") + strlen(filename) + 1); |
| 106 |
strcpy(cur_dir, "/"); |
| 107 |
pend_of_cur_dir = cur_dir + 1; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
cur_dir = (char*)malloc(strlen("./") + strlen(filename) + 1); |
| 112 |
strcpy(cur_dir, "./"); |
| 113 |
pend_of_cur_dir = cur_dir + 2; |
| 114 |
} |
| 115 |
pcur_elt = filename; |
| 116 |
do |
| 117 |
{ |
| 118 |
char end_of_elt_backup; |
| 119 |
|
| 120 |
/* skip leading or multiple slashes */ |
| 121 |
while (*pcur_elt == '/' || *pcur_elt == '\\') |
| 122 |
{ |
| 123 |
*pcur_elt = '/'; |
| 124 |
pcur_elt++; |
| 125 |
} |
| 126 |
|
| 127 |
pend_of_elt = end_of_elt(pcur_elt); |
| 128 |
end_of_elt_backup = *pend_of_elt; |
| 129 |
*pend_of_elt = '\0'; |
| 130 |
/* pcur_elt is now a \0-terminated string containing the current |
| 131 |
path element. */ |
| 132 |
|
| 133 |
/* Now check if there's a matching entry in the directory */ |
| 134 |
DIR *list; |
| 135 |
struct dirent *entry; |
| 136 |
int found = 0; |
| 137 |
|
| 138 |
list = opendir (cur_dir); |
| 139 |
if (list != NULL) |
| 140 |
{ |
| 141 |
/* note: we may want to sort list first */ |
| 142 |
while ((entry = readdir (list)) != NULL && !found) |
| 143 |
{ |
| 144 |
if (strcasecmp(pcur_elt, entry->d_name) == 0) |
| 145 |
{ |
| 146 |
/* Good case-insensitive match: replace the |
| 147 |
user-provided filename with it */ |
| 148 |
strcpy(pcur_elt, entry->d_name); |
| 149 |
found = 1; |
| 150 |
} |
| 151 |
} |
| 152 |
closedir (list); |
| 153 |
} |
| 154 |
else if (errno == EACCES) |
| 155 |
{ |
| 156 |
/* Maybe attempting to read a private prefix dir such as |
| 157 |
/data/data on Android, try to use it as-is. */ |
| 158 |
found = 1; |
| 159 |
} |
| 160 |
if (!found) |
| 161 |
error = 1; |
| 162 |
|
| 163 |
/* Prepare parsing next path element, unless the current element |
| 164 |
was the last one */ |
| 165 |
if (end_of_elt_backup != '\0') |
| 166 |
{ |
| 167 |
*pend_of_elt = '/'; /* restore */ |
| 168 |
|
| 169 |
/* Prepare next directory */ |
| 170 |
{ |
| 171 |
int cur_elt_len = pend_of_elt - pcur_elt; |
| 172 |
strncpy(pend_of_cur_dir, pcur_elt, cur_elt_len + 1); |
| 173 |
pend_of_cur_dir += cur_elt_len + 1; |
| 174 |
*pend_of_cur_dir = '\0'; |
| 175 |
} |
| 176 |
|
| 177 |
/* go to the next path element */ |
| 178 |
pcur_elt = pend_of_elt + 1; |
| 179 |
} |
| 180 |
} |
| 181 |
while(*pend_of_elt != '\0' && !error); |
| 182 |
free(cur_dir); |
| 183 |
|
| 184 |
/* If there was an error, we return a half-converted path (maybe the |
| 185 |
file didn't exist yet, but leading directories still needed to be |
| 186 |
converted); otherwise, filename contains the fully-converted |
| 187 |
path, ready to be opened on a case-sensitive filesystem. */ |
| 188 |
#endif /* !_WIN32 !__EMSCRIPTEN__*/ |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Does this file exist and can be opened? |
| 193 |
* Note: this is case-sensitive. |
| 194 |
*/ |
| 195 |
int exist(char *name) |
| 196 |
{ |
| 197 |
FILE *fp = fopen(name, "rb"); |
| 198 |
if (!fp) |
| 199 |
return 0; |
| 200 |
fclose(fp); |
| 201 |
return 1; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Is it a directory that exists? |
| 206 |
*/ |
| 207 |
int is_directory(const char *name) |
| 208 |
{ |
| 209 |
char *tmp_filename = strdup(name); |
| 210 |
int accessible = 0; |
| 211 |
int retval = 0; |
| 212 |
|
| 213 |
struct stat buf; |
| 214 |
/* ciconvert(tmp_filename); */ |
| 215 |
accessible = stat(tmp_filename, &buf); |
| 216 |
free(tmp_filename); |
| 217 |
|
| 218 |
if (accessible < 0) |
| 219 |
retval = 0; |
| 220 |
else |
| 221 |
retval = S_ISDIR(buf.st_mode); |
| 222 |
|
| 223 |
return retval; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* So-called "portable" dirname - that is, it supports backslash and |
| 228 |
* forward-slash. That way, it can process filenames from dink.ini, in |
| 229 |
* particular. Return a newly allocated string. |
| 230 |
*/ |
| 231 |
char* |
| 232 |
pdirname (const char* filename) |
| 233 |
{ |
| 234 |
char *retval = strdup(filename); |
| 235 |
char *pc = retval + strlen(retval); |
| 236 |
while (pc >= retval && *pc != '/' && *pc != '\\') |
| 237 |
pc--; |
| 238 |
if (pc >= retval) |
| 239 |
*pc = '\0'; |
| 240 |
return retval; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Read integer portably (same result with MSB and LSB |
| 246 |
* endianness). Source data is a file with little-endian data. |
| 247 |
*/ |
| 248 |
int read_lsb_int(FILE *f) |
| 249 |
{ |
| 250 |
unsigned char buf[4]; |
| 251 |
fread(buf, 4, 1, f); |
| 252 |
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
* Write integer portably (same result with MSB and LSB |
| 258 |
* endianness). Will write little-endian data to file. |
| 259 |
*/ |
| 260 |
void write_lsb_int(int n, FILE *f) |
| 261 |
{ |
| 262 |
unsigned char buf[4]; |
| 263 |
buf[0] = n & 0xFF; |
| 264 |
buf[1] = (n >> (1*8)) & 0xFF; |
| 265 |
buf[2] = (n >> (2*8)) & 0xFF; |
| 266 |
buf[3] = (n >> (3*8)) & 0xFF; |
| 267 |
fwrite(buf, 4, 1, f); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Read unsigned integer portably (same result with MSB and LSB |
| 272 |
* endianness). Source data is a file with little-endian data. |
| 273 |
*/ |
| 274 |
unsigned int read_lsb_uint(FILE *f) |
| 275 |
{ |
| 276 |
unsigned char buf[4]; |
| 277 |
fread(buf, 4, 1, f); |
| 278 |
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Write integer portably (same result with MSB and LSB |
| 283 |
* endianness). Will write little-endian data to file. |
| 284 |
*/ |
| 285 |
void write_lsb_uint(unsigned int n, FILE *f) |
| 286 |
{ |
| 287 |
unsigned char buf[4]; |
| 288 |
buf[0] = n & 0xFF; |
| 289 |
buf[1] = (n >> (1*8)) & 0xFF; |
| 290 |
buf[2] = (n >> (2*8)) & 0xFF; |
| 291 |
buf[3] = (n >> (3*8)) & 0xFF; |
| 292 |
fwrite(buf, 4, 1, f); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Read short portably (same result with MSB and LSB |
| 298 |
* endianness). Source data is a file with little-endian data. |
| 299 |
*/ |
| 300 |
short read_lsb_short(FILE *f) |
| 301 |
{ |
| 302 |
unsigned char buf[2]; |
| 303 |
fread(buf, 2, 1, f); |
| 304 |
return (buf[1] << 8) | (buf[0]); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* Write short portably (same result with MSB and LSB |
| 310 |
* endianness). Will write little-endian data to file. |
| 311 |
*/ |
| 312 |
void write_lsb_short(short n, FILE *f) |
| 313 |
{ |
| 314 |
unsigned char buf[2]; |
| 315 |
buf[0] = n & 0xFF; |
| 316 |
buf[1] = (n >> (1*8)) & 0xFF; |
| 317 |
fwrite(buf, 2, 1, f); |
| 318 |
} |