#include <stdio.h>
#include <string.h>
+#define nw_string_vformat(s, format, ...) \
+ __nw_string_vformat(s, sizeof(s), format, __VA_ARGS__)
+
static inline int __nw_string_vformat(char* s, const size_t length,
const char* format, va_list args) {
// Write string to buffer
return __nw_string_format(s, length, "%s", value);
}
+/*
+ Paths
+*/
+
+#define nw_path_join(s, first, second) \
+ __nw_path_join(s, sizeof(s), first, second)
+
+static inline int __nw_path_join(char* s, const size_t length,
+ const char* first, const char* second) {
+ if (!first)
+ return __nw_string_format(s, length, "%s", second);
+
+ if (!second)
+ return __nw_string_format(s, length, "%s", first);
+
+ // Remove leading slashes from second argument
+ while (*second == '/')
+ second++;
+
+ return __nw_string_format(s, length, "%s/%s", first, second);
+}
+
#endif /* NETWORKD_STRING_H */
# #
#############################################################################*/
+#include <limits.h>
#include <stdlib.h>
#include "config.h"
struct nw_config *config;
};
+#define nw_zone_path(zone, path, format, ...) \
+ __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
+
+static int __nw_zone_path(struct nw_zone* zone, char* p, const size_t length,
+ const char* format, ...) {
+ char prefix[NAME_MAX];
+ char suffix[NAME_MAX];
+ va_list args;
+ int r;
+
+ // Format the prefix
+ r = nw_string_format(prefix, "%s/zones/%s", CONFIG_DIR, zone->name);
+ if (r)
+ return r;
+
+ // Format the suffix
+ va_start(args, format);
+ r = nw_string_vformat(suffix, format, args);
+ va_end(args);
+ if (r)
+ return r;
+
+ // Join the two parts together
+ return __nw_path_join(p, length, prefix, suffix);
+}
+
static void nw_zone_free(struct nw_zone* zone) {
if (zone->config)
nw_config_unref(zone->config);
free(zone);
}
+static int nw_zone_setup(struct nw_zone* zone) {
+ char path[PATH_MAX];
+ int r;
+
+ // Compose the path to the main configuration file
+ r = nw_zone_path(zone, path, "%s", "settings");
+ if (r)
+ return r;
+
+ // Initialize the configuration
+ r = nw_config_create(&zone->config, path);
+ if (r)
+ return r;
+
+ return 0;
+}
+
int nw_zone_create(struct nw_zone** zone, const char* name) {
int r;
if (r)
goto ERROR;
+ // Setup the zone
+ r = nw_zone_setup(z);
+ if (r)
+ goto ERROR;
+
*zone = z;
return 0;