Skip to content

Commit

Permalink
Merge pull request msys2#18971 from lazka/some-pathtools-cleanup
Browse files Browse the repository at this point in the history
Some pathtools cleanup
  • Loading branch information
lazka authored Oct 27, 2023
2 parents c8e0146 + 6dd4efa commit c597f84
Show file tree
Hide file tree
Showing 50 changed files with 1,122 additions and 954 deletions.
4 changes: 2 additions & 2 deletions mingw-w64-connect/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ source=(${_realname}-${pkgver}.tar.gz::"${url}/archive/${pkgver}.tar.gz"
"pathtools.h")
sha256sums=('96c50fefe7ecf015cf64ba6cec9e421ffd3b18fef809f59961ef9229df528f3e'
'1e1c89159178349a75665fcb9dfb7639e477b9af32165a377dad54c55e8a59d3'
'08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
'965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6')
'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
'e1944d0dcd7837cb25c31832e785c6176916cd42a446ca2f9057a450afafab4a')

prepare() {
test ! -d "${startdir}/../mingw-w64-pathtools" || {
Expand Down
77 changes: 45 additions & 32 deletions mingw-w64-connect/pathtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include "pathtools.h"

char *
static char *
malloc_copy_string(char const * original)
{
char * result = (char *) malloc (sizeof (char*) * strlen (original)+1);
Expand Down Expand Up @@ -361,7 +361,7 @@ get_dll_path(char * result, unsigned long max_size)
}
#endif

char const *
static char const *
strip_n_prefix_folders(char const * path, size_t n)
{
if (path == NULL)
Expand Down Expand Up @@ -404,7 +404,7 @@ strip_n_suffix_folders(char * path, size_t n)
return;
}

size_t
static size_t
split_path_list(char const * path_list, char split_char, char *** arr)
{
size_t path_count;
Expand Down Expand Up @@ -586,42 +586,55 @@ single_path_relocation_lib(const char *from, const char *to)
#endif
}

char *
pathlist_relocation(const char *from_path, const char *to_path_list)
char const *
msys2_get_relocated_single_path(char const * unix_path)
{
#if defined(__MINGW32__)
static char stored_path[PATH_MAX];
static int stored = 0;
if (stored == 0)
{
char const * relocated = get_relocated_path_list(from_path, to_path_list);
strncpy (stored_path, relocated, PATH_MAX);
stored_path[PATH_MAX-1] = '\0';
free ((void *)relocated);
stored = 1;
}
return stored_path;
#else
return (to_path_list);
#endif
char * unix_part = (char *) strip_n_prefix_folders (unix_path, 1);
char win_part[MAX_PATH];
get_executable_path (NULL, &win_part[0], MAX_PATH);
strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */
char * new_path = (char *) malloc (strlen (unix_part) + strlen (win_part) + 1);
strcpy (new_path, win_part);
strcat (new_path, unix_part);
return new_path;
}

char *
pathlist_relocation_lib(const char *from_path, const char *to_path_list)
msys2_get_relocated_path_list(char const * paths)
{
#if defined(__MINGW32__)
static char stored_path[PATH_MAX];
static int stored = 0;
if (stored == 0)
char win_part[MAX_PATH];
get_executable_path (NULL, &win_part[0], MAX_PATH);
strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */

char **arr = NULL;

size_t count = split_path_list (paths, ':', &arr);
int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */
size_t i;
for (i = 0; i < count; ++i)
{
char const * relocated = get_relocated_path_list_lib(from_path, to_path_list);
strncpy (stored_path, relocated, PATH_MAX);
stored_path[PATH_MAX-1] = '\0';
free ((void *)relocated);
stored = 1;
arr[i] = (char *) strip_n_prefix_folders (arr[i], 1);
result_size += strlen (arr[i]) + strlen (win_part);
}
return stored_path;
char * result = (char *) malloc (result_size);
if (result == NULL)
{
return NULL;
}
result[0] = '\0';
for (i = 0; i < count; ++i)
{
strcat (result, win_part);
strcat (result, arr[i]);
if (i != count-1)
{
#if defined(_WIN32)
strcat (result, ";");
#else
return (to_path_list);
strcat (result, ":");
#endif
}
}
free ((void*)arr);
return result;
}
49 changes: 26 additions & 23 deletions mingw-w64-connect/pathtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,52 @@
#ifndef PATHTOOLS_H
#define PATHTOOLS_H

#include <unistd.h>
#if defined(__APPLE__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>

char * malloc_copy_string(char const * original);
#include <stddef.h>
#include <stdint.h>

/* In-place replaces any '\' with '/' and any '//' with '/' */
void sanitise_path(char * path);
#ifdef __cplusplus
extern "C" {
#endif

/* Uses a host OS specific function to determine the path of the executable,
if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */
int get_executable_path(char const * argv0, char * result, ssize_t max_size);

#if defined(_WIN32)
int get_dll_path(char * result, unsigned long max_size);
#endif

/* Where possible, in-place removes occourances of '.' and 'path/..' */
void simplify_path(char * path);

/* Allocates (via malloc) and returns the path to get from from to to. */
char * get_relative_path(char const * from, char const * to);

size_t split_path_list(char const * path_list, char split_char, char *** arr);

/* Advances path along by the amount that removes n prefix folders. */
char const *
strip_n_prefix_folders(char const * path, size_t n);

/* NULL terminates path to remove n suffix folders. */
void
strip_n_suffix_folders(char * path, size_t n);
void strip_n_suffix_folders(char * path, size_t n);

char * get_relocated_path_list(char const * from, char const * to_path_list);
char * get_relocated_path_list_lib(char const * from, char const * to_path_list);

char * single_path_relocation(const char *from, const char *to);
char * single_path_relocation_lib(const char *from, const char *to);
char * pathlist_relocation(const char *from_path, const char *to_path_list);
char * pathlist_relocation_lib(const char *from_path, const char *to_path_list);

/* Allocates (via malloc) and returns a relocated path from a single Unix path.
This function makes large assumptions regarding PREFIX and is therefore very
much an MSYS2-only function. It operates by removing the first folder of the
input and final folder of the program executable then appending the input to
that.
*/
char const * msys2_get_relocated_single_path(char const * unix_path);

/* Allocates (via malloc) and for each ':' delimited Unix sub-path, returns the
result of applying the procedure detailed for msys2_get_relocated_single_path
on that Unix sub-path with the results joined up again with a ';' delimiter.
It implements the same logic in msys2_get_relocated_single_path to reduce the
the number of mallocs.
*/
char * msys2_get_relocated_path_list(char const * paths);

#ifdef __cplusplus
}
#endif

#endif /* PATHTOOLS_H */
4 changes: 2 additions & 2 deletions mingw-w64-curl/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ source=("https://github.com/curl/curl/releases/download/${_realname}-${pkgver//.
"0004-more-static-fixes.patch")
sha256sums=('16c62a9c4af0f703d28bda6d7bbf37ba47055ad3414d70dec63e2e6336f2a82d'
'SKIP'
'08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
'965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6'
'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
'e1944d0dcd7837cb25c31832e785c6176916cd42a446ca2f9057a450afafab4a'
'c4e6bfd5b58f944d75293128effbd22fe42ee0131b915d9230ceb3c004c0322d'
'3ee9c75a3046f86f91290c143170179230c9adc6eabfbb79eb26f708a165b719'
'7492d019036b5bec251bfbc3c0b40e5f16d3dd6b2515068835e087a6c21f19ad'
Expand Down
77 changes: 45 additions & 32 deletions mingw-w64-curl/pathtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include "pathtools.h"

char *
static char *
malloc_copy_string(char const * original)
{
char * result = (char *) malloc (sizeof (char*) * strlen (original)+1);
Expand Down Expand Up @@ -361,7 +361,7 @@ get_dll_path(char * result, unsigned long max_size)
}
#endif

char const *
static char const *
strip_n_prefix_folders(char const * path, size_t n)
{
if (path == NULL)
Expand Down Expand Up @@ -404,7 +404,7 @@ strip_n_suffix_folders(char * path, size_t n)
return;
}

size_t
static size_t
split_path_list(char const * path_list, char split_char, char *** arr)
{
size_t path_count;
Expand Down Expand Up @@ -586,42 +586,55 @@ single_path_relocation_lib(const char *from, const char *to)
#endif
}

char *
pathlist_relocation(const char *from_path, const char *to_path_list)
char const *
msys2_get_relocated_single_path(char const * unix_path)
{
#if defined(__MINGW32__)
static char stored_path[PATH_MAX];
static int stored = 0;
if (stored == 0)
{
char const * relocated = get_relocated_path_list(from_path, to_path_list);
strncpy (stored_path, relocated, PATH_MAX);
stored_path[PATH_MAX-1] = '\0';
free ((void *)relocated);
stored = 1;
}
return stored_path;
#else
return (to_path_list);
#endif
char * unix_part = (char *) strip_n_prefix_folders (unix_path, 1);
char win_part[MAX_PATH];
get_executable_path (NULL, &win_part[0], MAX_PATH);
strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */
char * new_path = (char *) malloc (strlen (unix_part) + strlen (win_part) + 1);
strcpy (new_path, win_part);
strcat (new_path, unix_part);
return new_path;
}

char *
pathlist_relocation_lib(const char *from_path, const char *to_path_list)
msys2_get_relocated_path_list(char const * paths)
{
#if defined(__MINGW32__)
static char stored_path[PATH_MAX];
static int stored = 0;
if (stored == 0)
char win_part[MAX_PATH];
get_executable_path (NULL, &win_part[0], MAX_PATH);
strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */

char **arr = NULL;

size_t count = split_path_list (paths, ':', &arr);
int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */
size_t i;
for (i = 0; i < count; ++i)
{
char const * relocated = get_relocated_path_list_lib(from_path, to_path_list);
strncpy (stored_path, relocated, PATH_MAX);
stored_path[PATH_MAX-1] = '\0';
free ((void *)relocated);
stored = 1;
arr[i] = (char *) strip_n_prefix_folders (arr[i], 1);
result_size += strlen (arr[i]) + strlen (win_part);
}
return stored_path;
char * result = (char *) malloc (result_size);
if (result == NULL)
{
return NULL;
}
result[0] = '\0';
for (i = 0; i < count; ++i)
{
strcat (result, win_part);
strcat (result, arr[i]);
if (i != count-1)
{
#if defined(_WIN32)
strcat (result, ";");
#else
return (to_path_list);
strcat (result, ":");
#endif
}
}
free ((void*)arr);
return result;
}
49 changes: 26 additions & 23 deletions mingw-w64-curl/pathtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,52 @@
#ifndef PATHTOOLS_H
#define PATHTOOLS_H

#include <unistd.h>
#if defined(__APPLE__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>

char * malloc_copy_string(char const * original);
#include <stddef.h>
#include <stdint.h>

/* In-place replaces any '\' with '/' and any '//' with '/' */
void sanitise_path(char * path);
#ifdef __cplusplus
extern "C" {
#endif

/* Uses a host OS specific function to determine the path of the executable,
if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */
int get_executable_path(char const * argv0, char * result, ssize_t max_size);

#if defined(_WIN32)
int get_dll_path(char * result, unsigned long max_size);
#endif

/* Where possible, in-place removes occourances of '.' and 'path/..' */
void simplify_path(char * path);

/* Allocates (via malloc) and returns the path to get from from to to. */
char * get_relative_path(char const * from, char const * to);

size_t split_path_list(char const * path_list, char split_char, char *** arr);

/* Advances path along by the amount that removes n prefix folders. */
char const *
strip_n_prefix_folders(char const * path, size_t n);

/* NULL terminates path to remove n suffix folders. */
void
strip_n_suffix_folders(char * path, size_t n);
void strip_n_suffix_folders(char * path, size_t n);

char * get_relocated_path_list(char const * from, char const * to_path_list);
char * get_relocated_path_list_lib(char const * from, char const * to_path_list);

char * single_path_relocation(const char *from, const char *to);
char * single_path_relocation_lib(const char *from, const char *to);
char * pathlist_relocation(const char *from_path, const char *to_path_list);
char * pathlist_relocation_lib(const char *from_path, const char *to_path_list);

/* Allocates (via malloc) and returns a relocated path from a single Unix path.
This function makes large assumptions regarding PREFIX and is therefore very
much an MSYS2-only function. It operates by removing the first folder of the
input and final folder of the program executable then appending the input to
that.
*/
char const * msys2_get_relocated_single_path(char const * unix_path);

/* Allocates (via malloc) and for each ':' delimited Unix sub-path, returns the
result of applying the procedure detailed for msys2_get_relocated_single_path
on that Unix sub-path with the results joined up again with a ';' delimiter.
It implements the same logic in msys2_get_relocated_single_path to reduce the
the number of mallocs.
*/
char * msys2_get_relocated_path_list(char const * paths);

#ifdef __cplusplus
}
#endif

#endif /* PATHTOOLS_H */
Loading

0 comments on commit c597f84

Please sign in to comment.