2020#include <sys/types.h>
2121#include <sys/stat.h>
2222#include <unistd.h>
23+ #include <limits.h>
2324#include <errno.h>
2425#include <fcntl.h>
2526
@@ -354,7 +355,7 @@ ly_ctx_unset_trusted(struct ly_ctx *ctx)
354355API int
355356ly_ctx_set_searchdir (struct ly_ctx * ctx , const char * search_dir )
356357{
357- char * cwd = NULL , * new = NULL ;
358+ char * new = NULL ;
358359 int index = 0 ;
359360 void * r ;
360361 int rc = EXIT_FAILURE ;
@@ -365,14 +366,13 @@ ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
365366 }
366367
367368 if (search_dir ) {
368- cwd = get_current_dir_name ();
369- if (chdir (search_dir )) {
369+ if (access (search_dir , R_OK | X_OK )) {
370370 LOGERR (ctx , LY_ESYS , "Unable to use search directory \"%s\" (%s)" ,
371371 search_dir , strerror (errno ));
372- goto cleanup ;
372+ return EXIT_FAILURE ;
373373 }
374374
375- new = get_current_dir_name ( );
375+ new = realpath ( search_dir , NULL );
376376 if (!ctx -> models .search_paths ) {
377377 ctx -> models .search_paths = malloc (2 * sizeof * ctx -> models .search_paths );
378378 LY_CHECK_ERR_GOTO (!ctx -> models .search_paths , LOGMEM (ctx ), cleanup );
@@ -394,18 +394,13 @@ ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir)
394394 ctx -> models .search_paths [index + 1 ] = NULL ;
395395
396396success :
397- if (chdir (cwd )) {
398- LOGWRN (ctx , "Unable to return back to working directory \"%s\" (%s)" ,
399- cwd , strerror (errno ));
400- }
401397 rc = EXIT_SUCCESS ;
402398 } else {
403399 /* consider that no change is not actually an error */
404400 return EXIT_SUCCESS ;
405401 }
406402
407403cleanup :
408- free (cwd );
409404 free (new );
410405 return rc ;
411406}
0 commit comments