From 505a0285b81883442d0ce84994c7502ad6a7dd6d Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 2 Apr 2018 23:02:56 +0200 Subject: [PATCH] wip --- src/prog/domod/src/main.cpp | 81 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/src/prog/domod/src/main.cpp b/src/prog/domod/src/main.cpp index d3b0bb67..4552ca99 100644 --- a/src/prog/domod/src/main.cpp +++ b/src/prog/domod/src/main.cpp @@ -23,6 +23,7 @@ * */ + /*-------------------------------- INCLUDES ---------------------------------*/ #include @@ -34,6 +35,10 @@ extern "C" { #include } +/*-------------------------------- DEFINES ---------------------------------*/ + +#define k_path_max 1024 + /*! ---------------------------------------------------------------------------- * @fn setup_ubus @@ -67,6 +72,38 @@ static struct ubus_context *setup_ubus (void) } } +/*! ---------------------------------------------------------------------------- + * @fn is_path_absolute + * + * @brief return true if the patch is absolute. + */ +static bool is_path_absolute (const char *a_path) +{ + return a_path != NULL && a_path[0] == '/'; +} + + +char * app_path (char * path, const char * argv0) +{ + char buf[PATH_MAX]; + char * pos; + if (argv0[0] == '/') { // run with absolute path + strcpy(buf, argv0); + } else { // run with relative path + if(NULL == getcwd(buf, PATH_MAX)) { + perror("getcwd error"); + return NULL; + } + strcat(buf, "/"); + strcat(buf, argv0); + } + if (NULL == realpath(buf, path)) { + perror("realpath error"); + return NULL; + } + return path; +} + /*! ---------------------------------------------------------------------------- * @fn main @@ -76,14 +113,18 @@ static struct ubus_context *setup_ubus (void) int main (void) { int the_ret = 0; + char path[k_path_max] = ""; struct ubus_context *the_ctx = NULL; + /* Get the Path of the binary to load configs. */ + + /* Setup the Ubus context. */ the_ctx = setup_ubus(); if (the_ctx == NULL) { return -1; } - + /* Main Event Loop. */ uloop_run(); @@ -92,7 +133,45 @@ int main (void) return the_ret; } +#if 0 +tatic void +set_absolute_path(char *options[], + const char *option_name, + const char *path_to_civetweb_exe) +{ + char path[PATH_MAX] = "", absolute[PATH_MAX] = ""; + const char *option_value; + const char *p; + /* Check whether option is already set */ + option_value = get_option(options, option_name); + + /* If option is already set and it is an absolute path, + leave it as it is -- it's already absolute. */ + if (option_value != NULL && !is_path_absolute(option_value)) { + /* Not absolute. Use the directory where civetweb executable lives + be the relative directory for everything. + Extract civetweb executable directory into path. */ + if ((p = strrchr(path_to_civetweb_exe, DIRSEP)) == NULL) { + IGNORE_UNUSED_RESULT(getcwd(path, sizeof(path))); + } else { + snprintf(path, + sizeof(path) - 1, + "%.*s", + (int)(p - path_to_civetweb_exe), + path_to_civetweb_exe); + path[sizeof(path) - 1] = 0; + } + + strncat(path, "/", sizeof(path) - strlen(path) - 1); + strncat(path, option_value, sizeof(path) - strlen(path) - 1); + + /* Absolutize the path, and set the option */ + IGNORE_UNUSED_RESULT(abs_path(path, absolute, sizeof(absolute))); + set_option(options, option_name, absolute); + } +} +#endif #if 0 #include "ubus/capabilities.h" #include "ubus/capabilities_lights.h"