Skip to content

Commit

Permalink
Look for input files relative to executable path instead of cwd (#11)
Browse files Browse the repository at this point in the history
* Change default input files location to be relative to executable
  • Loading branch information
a3ng7n authored May 28, 2024
1 parent 76fa2a9 commit 9d85769
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Include/42.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ EXTERN long Nmatl;
/* Number of geometric objects */
EXTERN long Ngeom;

#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)

EXTERN char InOutPath[80];
EXTERN char ModelPath[80];
EXTERN char CmdFileName[80];
Expand Down
7 changes: 7 additions & 0 deletions Kit/Include/iokit.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
#include <fcntl.h>
#ifdef _WIN32
#include <winsock2.h>
extern char *_pgmptr;
#define OS_SEP '\\'
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
/* Finesse winsock SOCKET datatype */
#define SOCKET int
#define OS_SEP '/'
#endif
/* #include <sys/un.h> */

Expand All @@ -49,6 +52,10 @@ int FileToString(const char *file_name, char **result_string,
SOCKET InitSocketServer(int Port, int AllowBlocking);
SOCKET InitSocketClient(const char *hostname, int Port, int AllowBlocking);

void SplitPath(char *path_file, char **path, char **file);
void GetExecutablePath(char *path, size_t bufsize);
void AddTrailingSlash(char *path);

/*
** #ifdef __cplusplus
** }
Expand Down
69 changes: 69 additions & 0 deletions Kit/Source/iokit.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,75 @@ SOCKET InitSocketClient(const char *hostname, int Port,int AllowBlocking)
#endif /* _WIN32 */
}

/**
* Splits the `path_file` into a directory part: `path`, and file part: `file`
* If `path_file` is just a filename with no containing folder, `path` will be a 0 length string.
*/
void SplitPath(char *path_file, char **path, char **file) {
*path = (char *)malloc(strlen(path_file));
// file = (char *)malloc(strlen(path_file));
strcpy(*path, path_file);
char *pos = strrchr(*path, OS_SEP);
if (NULL == pos) {
*file = strdup(path_file);
(*path)[0] = '\0';
} else {
*pos = '\0';
*file = strdup(pos + 1);
}
}

/**
* Attempts to retrieve the full path to the currently running executable
* and populates `path` with it. Uses `max_path_len` to avoid overrunning `path`
* `max_path_len` is the total size of `path`, which means it includes the
* null terminating character.
*/
void GetExecutablePath(char *path, size_t max_path_len) {
ssize_t len;
memset(path, 0, max_path_len);
#if defined(_WIN32)
strncpy(path, _pgmptr, max_path_len);
len = strlen(path);
#else
len = readlink("/proc/self/exe", path, max_path_len);
if (len < 0) {
printf("Error: error when getting executable path. Exiting.\n");
exit(EXIT_FAILURE);
}
#endif
if (len >= max_path_len) {
printf("Error: executable path received too long for file path buffer. Exiting.\n");
exit(EXIT_FAILURE);
}
}

/**
* Looks for a trailing slash in `path`; if one isn't found it appends one to `path`
*/
void AddTrailingSlash(char *path) {
char *end = strchr(path, '\0');
if (end == NULL) {
printf(
"Error: path provided contains no null terminating character. "
"Exiting.\n");
exit(EXIT_FAILURE);
}

char *slash = strrchr(path, OS_SEP);
if (slash != NULL) {
size_t diff = end - slash;
if (diff == 1) {
return;
}
}

char *sep = (char *)malloc(3);
sprintf(sep, "%c", OS_SEP);
strcat(path, sep);
free(sep);
}

/* #ifdef __cplusplus
** }
** #endif
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ GMSECFLAG =
RBTFLAG =
#RBTFLAG = -D _ENABLE_RBT_

DEFAULT_CASE_PATH =
#DEFAULT_CASE_PATH = -D _DEFAULT_CASE_PATH_=./InOut

DEFAULT_MODEL_PATH =
#DEFAULT_MODEL_PATH = -D _DEFAULT_MODEL_PATH_=./Model

ifeq ($(strip $(GMSECFLAG)),)
GMSECDIR =
GMSECINC =
Expand Down Expand Up @@ -243,7 +249,7 @@ $(OBJ)AppWriteToSocket.o $(OBJ)AppReadFromSocket.o $(OBJ)AppWriteToFile.o
#ANSIFLAGS = -Wstrict-prototypes -pedantic -ansi -Werror
ANSIFLAGS =

CFLAGS = -fpic -Wall -Wshadow -Wno-deprecated $(XWARN) -g $(ANSIFLAGS) $(GLINC) $(CINC) -I $(INC) -I $(KITINC) -I $(KITSRC) -I $(RBTSRC) $(GMSECINC) -O0 $(ARCHFLAG) $(GUIFLAG) $(GUI_LIB) $(SHADERFLAG) $(CFDFLAG) $(FFTBFLAG) $(GSFCFLAG) $(GMSECFLAG) $(STANDALONEFLAG) $(RBTFLAG)
CFLAGS = -fpic -Wall -Wshadow -Wno-deprecated $(XWARN) -g $(ANSIFLAGS) $(GLINC) $(CINC) -I $(INC) -I $(KITINC) -I $(KITSRC) -I $(RBTSRC) $(GMSECINC) -O0 $(ARCHFLAG) $(GUIFLAG) $(GUI_LIB) $(SHADERFLAG) $(CFDFLAG) $(FFTBFLAG) $(GSFCFLAG) $(GMSECFLAG) $(STANDALONEFLAG) $(RBTFLAG) $(DEFAULT_CASE_PATH) $(DEFAULT_MODEL_PATH)


########################## Rules to link 42 #############################
Expand Down
37 changes: 35 additions & 2 deletions Source/42init.c
Original file line number Diff line number Diff line change
Expand Up @@ -4368,8 +4368,41 @@ void InitSim(int argc, char **argv)
if (argc > 1) sprintf(InOutPath,"../../GSFC/RBT/%s/",argv[1]);
if (argc > 2) sprintf(ModelPath,"../../GSFC/RBT/%s/",argv[2]);
#else
sprintf(InOutPath,"./InOut/");
sprintf(ModelPath,"./Model/");
char *exec_name, *exec_dir, *exec_path;
exec_path = (char *)malloc(FILENAME_MAX);

GetExecutablePath(exec_path, FILENAME_MAX);
SplitPath(exec_path, &exec_dir, &exec_name);

if (strlen(exec_dir) == 0) {
printf("Error finding executable's containing folder. Exiting.");
exit(EXIT_FAILURE);
}

AddTrailingSlash(exec_dir);

#ifndef _DEFAULT_CASE_PATH_
strcpy(InOutPath, exec_dir);
strcat(InOutPath, "examples/InOut");
#else
sprintf(InOutPath, STRINGIZE_VALUE_OF(_DEFAULT_CASE_PATH_));
#endif

AddTrailingSlash((char *)InOutPath);

#ifndef _DEFAULT_MODEL_PATH_
strcpy(ModelPath, exec_dir);
strcat(ModelPath, "Model");
#else
sprintf(ModelPath, STRINGIZE_VALUE_OF(_DEFAULT_MODEL_PATH_));
#endif

AddTrailingSlash((char *)ModelPath);

free(exec_name);
free(exec_dir);
free(exec_path);

if (argc > 1) sprintf(InOutPath,"./%s/",argv[1]);
if (argc > 2) sprintf(ModelPath,"./%s/",argv[2]);
#endif
Expand Down

0 comments on commit 9d85769

Please sign in to comment.