Add FILEPATH_HOME

This commit is contained in:
Pradana Aumars 2021-06-12 11:02:56 +02:00
parent fc0f657774
commit fce41edbe5
2 changed files with 16 additions and 2 deletions

View File

@ -32,6 +32,15 @@ file_init(const char *filename, filepath_t FILEPATH, exception_t *e)
switch (FILEPATH) {
case FILEPATH_ABSOLUTE: {
realpath(filename, path);
break;
}
case FILEPATH_HOME: {
char* home = getenv("HOME");
size_t pathlen = strlen(home) + strlen(filename);
path = malloc(pathlen + 1);
strcpy(home, path);
strcat(path, filename);
path[pathlen] = '\0';
}
case FILEPATH_RELATIVE: path = (char*) filename; break;
}
@ -55,7 +64,11 @@ file_init(const char *filename, filepath_t FILEPATH, exception_t *e)
void
file_close(file_t *f)
{
if (f->fp == FILEPATH_ABSOLUTE) free(f->name);
switch (f->fp) {
case FILEPATH_ABSOLUTE:
case FILEPATH_HOME: free(f->name);
default: break;
}
if (f->hash_str != NULL) free(f->hash_str);
free(f);
}

View File

@ -7,7 +7,8 @@
typedef enum filepath_t {
FILEPATH_ABSOLUTE,
FILEPATH_RELATIVE
FILEPATH_RELATIVE,
FILEPATH_HOME
} filepath_t;
/**