This commit is contained in:
cozis
2022-08-16 18:54:49 +02:00
parent 4d4e365002
commit 3209afb513
5 changed files with 55 additions and 52 deletions
+30 -31
View File
@@ -82,6 +82,32 @@ const char *Source_GetAbsolutePath(Source *src)
return NULL;
}
const char *makePathAbsolute(const char *path, char *buff, size_t buffsize)
{
if(path[0] == '/')
// It's already absolute.
return path;
if(getcwd(buff, buffsize) == NULL)
return NULL;
size_t written = strlen(buff);
assert(buff[written-1] != '/');
if(written+1 >= buffsize)
return NULL; // No space for the / following the cwd.
buff[written++] = '/';
size_t n = strlen(path);
if(written + n >= buffsize)
return NULL;
memcpy(buff + written, path, n);
buff[written + n] = '\0';
return buff;
}
Source *Source_FromFile(const char *file, Error *error)
{
assert(file != NULL);
@@ -92,37 +118,10 @@ Source *Source_FromFile(const char *file, Error *error)
}
char maybe[1024];
const char *abs_path;
if(file[0] == '/')
abs_path = file;
else {
if(getcwd(maybe, sizeof(maybe)) == NULL) {
Error_Report(error, 0, "Internal buffer is too small");
return NULL;
}
size_t written = strlen(maybe);
assert(maybe[written-1] != '/');
if(written+1 >= sizeof(maybe)) {
Error_Report(error, 0, "Internal buffer is too small");
return NULL;
}
maybe[written] = '/';
written += 1;
size_t n = strlen(file);
if(written + n >= sizeof(maybe)) {
Error_Report(error, 0, "Internal buffer is too small");
return NULL;
}
memcpy(maybe + written, file, n);
maybe[written + n] = '\0';
abs_path = maybe;
const char *abs_path = makePathAbsolute(file, maybe, sizeof(maybe));
if(abs_path == NULL) {
Error_Report(error, 0, "Internal buffer is too small");
return NULL;
}
// Open the file and get it's size.