epicsGetExecDir() paths relative to executable

For linux, enable softIoc to find .dbd relative to the executable
location.

The same could be done for other targets

*bsd
  may have symlink /proc/curproc/file
  fallback to sysctl() with KERN_PROC_PATHNAME

solaris
  getexecname()

mac
  _NSGetExecutablePath()

WIN32
  GetModuleFileName(NULL)

others
  out of luck...
This commit is contained in:
Michael Davidsaver
2018-04-25 22:57:03 -07:00
parent 4ee3cbf382
commit 32340584b4
7 changed files with 170 additions and 2 deletions

View File

@@ -62,23 +62,59 @@
#include "epicsThread.h"
#include "epicsExit.h"
#include "epicsStdio.h"
#include "epicsString.h"
#include "dbStaticLib.h"
#include "subRecord.h"
#include "dbAccess.h"
#include "asDbLib.h"
#include "iocInit.h"
#include "iocsh.h"
#include "osiFileName.h"
#include "epicsInstallDir.h"
extern "C" int softIoc_registerRecordDeviceDriver(struct dbBase *pdbbase);
#define DBD_FILE EPICS_BASE "/dbd/softIoc.dbd"
#define EXIT_FILE EPICS_BASE "/db/softIocExit.db"
#define DBD_BASE "dbd/softIoc.dbd"
#define EXIT_BASE "db/softIocExit.db"
#define DBD_FILE_REL "../../" DBD_BASE
#define EXIT_FILE_REL "../../" EXIT_BASE
#define DBD_FILE EPICS_BASE "/" DBD_BASE
#define EXIT_FILE EPICS_BASE "/" EXIT_BASE
const char *arg0;
const char *base_dbd = DBD_FILE;
const char *exit_db = EXIT_FILE;
static void preparePath(void)
{
FILE *fp;
char *prefix = epicsGetExecDir();
char *dbd, *exit;
if(!prefix) return;
dbd = (char*)malloc(strlen(prefix) + strlen(DBD_FILE_REL) + 1);
if(dbd) {
dbd[0] = '\0';
strcat(dbd, prefix);
strcat(dbd, DBD_FILE_REL);
printf("Testing '%s'\n", dbd);
if((fp = fopen(dbd, "rb"))!=NULL) {
fclose(fp);
base_dbd = dbd;
}
}
exit = (char*)malloc(strlen(prefix) + strlen(EXIT_FILE_REL) + 1);
if(exit) {
exit[0] = '\0';
strcat(exit, prefix);
strcat(exit, EXIT_FILE_REL);
if((fp = fopen(exit, "rb"))!=NULL) {
fclose(fp);
exit_db = exit;
}
}
}
static void exitSubroutine(subRecord *precord) {
epicsExitLater((precord->a == 0.0) ? EXIT_SUCCESS : EXIT_FAILURE);
@@ -96,6 +132,7 @@ static void usage(int status) {
int main(int argc, char *argv[])
{
preparePath();
char *dbd_file = const_cast<char*>(base_dbd);
char *macros = NULL;
char xmacro[PVNAME_STRINGSZ + 4];