1313#include "util.h"
1414
1515/*
16- * get_pex_dir stores the path to the .pex file in pex_dir. It returns NULL on success and an error
17- * on failure.
16+ * get_pex_dir stores the canonical absolute path to the .pex file in pex_dir. It returns NULL on
17+ * success and an error on failure.
1818 */
1919err_t * get_pex_dir (char * * pex_dir ) {
2020 char * exe_path = NULL ;
21+ char * exe_realpath = NULL ;
2122 char * exe_dir = NULL ;
2223 err_t * err = NULL ;
2324
2425#if defined(__linux__ )
25- if ((exe_path = realpath ("/proc/self/exe" , NULL )) == NULL ) {
26- err = err_from_errno ("realpath /proc/self/exe" );
27- goto end ;
28- }
26+ exe_path = "/proc/self/exe" ;
2927#elif defined(__APPLE__ )
3028 uint32_t len = 0 ;
3129
@@ -56,14 +54,22 @@ err_t *get_pex_dir(char **pex_dir) {
5654#error "Unsupported operating system"
5755#endif
5856
59- exe_dir = dirname (exe_path );
57+ if ((exe_realpath = realpath (exe_path , NULL )) == NULL ) {
58+ err = err_from_errno ("realpath" );
59+ goto end ;
60+ }
61+
62+ exe_dir = dirname (exe_realpath );
6063 if (((* pex_dir ) = strdup (exe_dir )) == NULL ) {
6164 err = err_from_errno ("strdup" );
6265 goto end ;
6366 }
6467
6568end :
69+ #ifndef __linux__
6670 FREE (exe_path );
71+ #endif
72+ FREE (exe_realpath );
6773
6874 return err ;
6975}
@@ -82,10 +88,9 @@ err_t *get_pex_dir(char **pex_dir) {
8288err_t * get_plz_bin_path (char * * path ) {
8389 char * pex_dir = NULL ;
8490 char * tmp_dir = NULL ;
85- char * pex_dir_realpath = NULL ;
8691 char * tmp_dir_realpath = NULL ;
87- size_t pex_len = 0 ;
88- size_t tmp_len = 0 ;
92+ size_t pex_dir_len = 0 ;
93+ size_t tmp_dir_len = 0 ;
8994 err_t * err = NULL ;
9095
9196 if ((err = get_pex_dir (& pex_dir )) != NULL ) {
@@ -106,23 +111,19 @@ err_t *get_plz_bin_path(char **path) {
106111 }
107112
108113 // Identify whether the .pex file exists inside the build environment.
109- if ((pex_dir_realpath = realpath (pex_dir , NULL )) == NULL ) {
110- err = err_from_errno ("realpath pex_dir" );
111- goto end ;
112- }
113114 if ((tmp_dir_realpath = realpath (tmp_dir , NULL )) == NULL ) {
114115 err = err_from_errno ("realpath tmp_dir" );
115116 goto end ;
116117 }
117118
118- pex_len = strlen (pex_dir_realpath );
119- tmp_len = strlen (tmp_dir_realpath );
119+ pex_dir_len = strlen (pex_dir );
120+ tmp_dir_len = strlen (tmp_dir_realpath );
120121
121122 if (
122- strncmp (tmp_dir_realpath , pex_dir_realpath , tmp_len ) == 0 &&
123+ strncmp (tmp_dir_realpath , pex_dir , tmp_dir_len ) == 0 &&
123124 (
124- (pex_len == tmp_len ) ||
125- (pex_len > tmp_len && pex_dir_realpath [ tmp_len ] == '/' )
125+ (pex_dir_len == tmp_dir_len ) ||
126+ (pex_dir_len > tmp_dir_len && pex_dir [ tmp_dir_len ] == '/' )
126127 )
127128 ) {
128129 if (((* path ) = strdup (tmp_dir_realpath )) == NULL ) {
@@ -152,7 +153,7 @@ err_t *get_plz_bin_path(char **path) {
152153 }
153154
154155end :
155- FREE (pex_dir_realpath );
156+ FREE (pex_dir );
156157 FREE (tmp_dir_realpath );
157158
158159 return err ;
0 commit comments