aboutsummaryrefslogtreecommitdiffstats
path: root/libcoopgamma_get_method_and_site.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-02-10 17:50:58 +0100
committerMattias Andrée <m@maandree.se>2025-02-10 17:52:46 +0100
commitec1bcdcd0dd6e196303e8d9a30b3b2740e32c502 (patch)
treedcc759aaf897c915827659e00644f12503cf1268 /libcoopgamma_get_method_and_site.c
parentImprove makefile (diff)
downloadlibcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.gz
libcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.bz2
libcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.xz
Minor code improvements and split into multiple c files
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'libcoopgamma_get_method_and_site.c')
-rw-r--r--libcoopgamma_get_method_and_site.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/libcoopgamma_get_method_and_site.c b/libcoopgamma_get_method_and_site.c
new file mode 100644
index 0000000..73de048
--- /dev/null
+++ b/libcoopgamma_get_method_and_site.c
@@ -0,0 +1,76 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Get the adjustment method and site
+ *
+ * SIGCHLD must not be ignored or blocked
+ *
+ * @param method The adjustment method, `NULL` for automatic
+ * @param site The site, `NULL` for automatic
+ * @param methodp Output pointer for the selected adjustment method,
+ * which cannot be `NULL`. It is safe to call
+ * this function with this parameter set to `NULL`.
+ * @param sitep Output pointer for the selected site, which will
+ * be `NULL` the method only supports one site or if
+ * `site == NULL` and no site can be selected
+ * automatically. It is safe to call this function
+ * with this parameter set to `NULL`.
+ * @return Zero on success, -1 on error
+ */
+int
+libcoopgamma_get_method_and_site(const char *restrict method, const char *restrict site,
+ char **restrict methodp, char **restrict sitep)
+{
+ int saved_errno;
+ char *raw;
+ char *p;
+ char *q;
+
+ raw = libcoopgamma_query__(method, site, "-q");
+ if (!raw)
+ return -1;
+
+ if (methodp) *methodp = NULL;
+ if (sitep) *sitep = NULL;
+
+ p = strchr(raw, '\n');
+ if (!p) {
+ errno = EBADMSG;
+ goto fail;
+ }
+ *p++ = '\0';
+
+ if (methodp) {
+ *methodp = malloc(strlen(raw) + 1U);
+ if (!*methodp)
+ goto fail;
+ strcpy(*methodp, raw);
+ }
+
+ if (site && *(q = &strchr(p, '\0')[-1])) {
+ if (*q != '\n') {
+ errno = EBADMSG;
+ goto fail;
+ }
+ *q = '\0';
+ *sitep = malloc(strlen(p) + 1U);
+ if (!*sitep)
+ goto fail;
+ strcpy(*sitep, p);
+ }
+
+ free(raw);
+ return 0;
+
+fail:
+ saved_errno = errno;
+ if (methodp) {
+ free(*methodp);
+ *methodp = NULL;
+ }
+ free(raw);
+ errno = saved_errno;
+ return -1;
+}