Fix absolute path error on Windows

This commit is contained in:
Ashton Fagg 2024-10-06 12:55:03 -04:00
parent 06233e5218
commit ceefacca7f
3 changed files with 28 additions and 1 deletions

View file

@ -71,9 +71,15 @@ char* currentdir(void) {
return append_trail_slash(q_(getcwd(buf,2048)));
}
int is_valid_path(const char *path) {
return path[0] == '/';
}
#else
char* homedir(void);
char* currentdir(void);
int is_valid_path(const char *);
#endif
char* configdir(void) {
@ -82,9 +88,10 @@ char* configdir(void) {
if (env) /* note: env can be a NULL */
{
if (env[0] != DIRSEP[0]) /* note: DIRSEP == \\ on windows, / on unix */
if (!is_valid_path(env))
{
cond_printf(0,"Error: %s must be absolute. Got: %s \n",c,env);
abort();
}
s(c); /* note : this frees c. */
return append_trail_slash(q(env));
@ -143,3 +150,4 @@ char* basedir(void) {
s(cd_);
return configdir();
}

View file

@ -64,4 +64,21 @@ char* currentdir(void) {
char buf[2048];
return append_trail_slash(q_(_getcwd(buf,2048)));
}
int is_valid_path(const char *path) {
//
// On Windows, an absolute path can technically
// start with a drive letter (i.e. c:\roswell),
// but could also be a UNC path (i.e. \\OTHERMACHINE\roswell).
//
// But, if invoked inside an msys2 shell we could get a UNIX-style path.
//
// It appears however that the latter two scenarios are not supported
// by other parts of the codebase, so we shall enforce here that only
// regular Windows-style paths are allowed.
//
return (isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'));
}
#endif

View file

@ -13,6 +13,7 @@
#include <string.h>
#include <stdarg.h>
#ifndef HAVE_WINDOWS_H
#include <ctype.h>
#include <pwd.h>
#include <unistd.h>
#include <grp.h>
@ -142,6 +143,7 @@ int directory_exist_p (char* path);
int change_directory(const char* path);
int delete_directory(char* pathspec,int recursive);
char* impldir(char* arch,char* os,char* impl,char* version);
int is_valid_path(const char *path);
/*util_file.c */
int delete_file(char* pathspec);
int rename_file(char* file,char* new_name);