Skip to content

Commit

Permalink
Handle backslashes in paths on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
elasota committed Dec 26, 2023
1 parent 3f25349 commit 58bd360
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ long int unshield_get_path_max(Unshield* unshield)
char *unshield_get_base_directory_name(Unshield *unshield) {
long int path_max = unshield_get_path_max(unshield);
char *p = strrchr(unshield->filename_pattern, '/');
#ifdef WIN32
char *pbs = strrchr(unshield->filename_pattern, '\\');
#endif
char *dirname = malloc(path_max);

#ifdef WIN32
if (NULL != pbs && (NULL == p || pbs > p))
p = pbs;
#endif

if (p) {
strncpy(dirname, unshield->filename_pattern, path_max);
if ((unsigned int) (p - unshield->filename_pattern) > path_max) {
Expand Down Expand Up @@ -91,11 +99,20 @@ FILE* unshield_fopen_for_reading(Unshield* unshield, int index, const char* suff
char* filename = get_filename(unshield, index, suffix);
char* dirname = unshield_get_base_directory_name(unshield);
const char *q;
#ifdef WIN32
const char *qbs;
#endif
struct dirent *dent = NULL;
DIR *sourcedir = NULL;
long int path_max = unshield_get_path_max(unshield);

q=strrchr(filename,'/');
#ifdef WIN32
qbs = strrchr(filename, '\\');
if (NULL != qbs && (NULL == q || qbs > q))
q = qbs;
#endif

if (q)
q++;
else
Expand Down
9 changes: 9 additions & 0 deletions lib/libunshield.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ static bool unshield_create_filename_pattern(Unshield* unshield, const char* fil
char pattern[256];
char* prefix = strdup(filename);
char* p = strrchr(prefix, '/');
#ifdef WIN32
char *pbs = strrchr(prefix, '\\');
#endif

#ifdef WIN32
if (NULL != pbs && (NULL == p || pbs > p))
p = pbs;
#endif

if (!p)
p = prefix;

Expand Down
22 changes: 21 additions & 1 deletion src/unshield.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,35 @@ static bool make_sure_directory_exists(const char* directory)/*{{{*/
p+=2;
else if (0 == strncmp(p, "../", 3))
p+=3;
#ifdef WIN32
if ('\\' == *p)
p++;
else if (0 == strncmp(p, ".\\", 2))
p += 2;
else if (0 == strncmp(p, "..\\", 3))
p += 3;
#endif
else
{
int is_win_root = 0;
const char* slash = strchr(p, '/');
#ifdef WIN32
const char* backslash = strchr(p, '/');
if (NULL != backslash && (NULL == slash || backslash < slash))
slash = backslash;
#endif

current = strdup(directory);

if (slash)
current[slash-directory] = '\0';

if (stat(current, &dir_stat) < 0)
#ifdef WIN32
if (slash - directory == 2 && current[1] == ':')
is_win_root = 1;
#endif

if (!is_win_root && stat(current, &dir_stat) < 0)
{
#if defined (__MINGW32__) || defined (_WIN32)
if (_mkdir(current) < 0)
Expand Down

0 comments on commit 58bd360

Please sign in to comment.