dss.ggo: Disable inode monitoring by default.
[dss.git] / string.c
index cf6d0b1a365aa378720ba1882aa5c83cc39a0df6..91ad3e78e6925fd41eda7d9d20dd1e0eb24a8943 100644 (file)
--- a/string.c
+++ b/string.c
@@ -1,5 +1,6 @@
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <stdarg.h>
 #include <assert.h>
 #include <limits.h>
 #include <unistd.h>
 
 
-#include "cmdline.h"
 #include "gcc-compat.h"
 #include "log.h"
 #include "error.h"
-
-__noreturn void clean_exit(int status);
+#include "string.h"
 
 /**
  * Write a message to a dynamically allocated string.
@@ -70,7 +69,7 @@ __must_check __malloc void *dss_realloc(void *p, size_t size)
        if (!(p = realloc(p, size))) {
                DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
                        size);
-               clean_exit(EXIT_FAILURE);
+               exit(EXIT_FAILURE);
        }
        return p;
 }
@@ -95,7 +94,7 @@ __must_check __malloc void *dss_malloc(size_t size)
        if (!p) {
                DSS_EMERG_LOG("malloc failed (size = %zu),  aborting\n",
                        size);
-               clean_exit(EXIT_FAILURE);
+               exit(EXIT_FAILURE);
        }
        return p;
 }
@@ -141,7 +140,7 @@ __must_check __malloc char *dss_strdup(const char *s)
        if ((ret = strdup(s? s: "")))
                return ret;
        DSS_EMERG_LOG("strdup failed, aborting\n");
-       clean_exit(EXIT_FAILURE);
+       exit(EXIT_FAILURE);
 }
 
 /**
@@ -165,12 +164,6 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
        return msg;
 }
 
-__printf_1_2 void make_err_msg(const char* fmt,...)
-{
-       free(dss_error_txt);
-       VSPRINTF(fmt, dss_error_txt);
-}
-
 /**
  * Get the home directory of the current user.
  *
@@ -183,15 +176,6 @@ __must_check __malloc char *get_homedir(void)
        return dss_strdup(pw? pw->pw_dir : "/tmp");
 }
 
-/** \cond LLONG_MAX and LLONG_LIN might not be defined. */
-#ifndef LLONG_MAX
-#define LLONG_MAX (1 << (sizeof(long) - 1))
-#endif
-#ifndef LLONG_MIN
-#define LLONG_MIN (-LLONG_MAX - 1LL)
-#endif
-/** \endcond */
-
 /**
  * Convert a string to a 64-bit signed integer value.
  *
@@ -209,22 +193,14 @@ int dss_atoi64(const char *str, int64_t *value)
 
        errno = 0; /* To distinguish success/failure after call */
        tmp = strtoll(str, &endptr, 10);
-       if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN)) {
-               make_err_msg("%s", str);
+       if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
                return -E_ATOI_OVERFLOW;
-       }
-       if (errno != 0 && tmp == 0) { /* other error */
-               make_err_msg("%s", str);
+       if (errno != 0 && tmp == 0) /* other error */
                return -E_STRTOLL;
-       }
-       if (endptr == str) {
-               make_err_msg("%s", str);
+       if (endptr == str)
                return -E_ATOI_NO_DIGITS;
-       }
-       if (*endptr != '\0') { /* Further characters after number */
-               make_err_msg("%s", str);
+       if (*endptr != '\0') /* Further characters after number */
                return -E_ATOI_JUNK_AT_END;
-       }
        *value = tmp;
        return 1;
 }
@@ -244,3 +220,52 @@ __must_check __malloc char *dss_logname(void)
        return dss_strdup(pw? pw->pw_name : "unknown_user");
 }
 
+/**
+ * Split string and return pointers to its parts.
+ *
+ * \param args The string to be split.
+ * \param argv_ptr Pointer to the list of substrings.
+ * \param delim Delimiter.
+ *
+ * This function modifies \a args by replacing each occurance of \a delim by
+ * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
+ * and these pointers are initialized to point to the broken-up substrings
+ * within \a args. A pointer to this array is returned via \a argv_ptr.
+ *
+ * \return The number of substrings found in \a args.
+ */
+__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
+{
+       char *p = args;
+       char **argv;
+       size_t n = 0, i, j;
+
+       p = args + strspn(args, delim);
+       for (;;) {
+               i = strcspn(p, delim);
+               if (!i)
+                       break;
+               p += i;
+               n++;
+               p += strspn(p, delim);
+       }
+       *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
+       argv = *argv_ptr;
+       i = 0;
+       p = args + strspn(args, delim);
+       while (p) {
+               argv[i] = p;
+               j = strcspn(p, delim);
+               if (!j)
+                       break;
+               p += strcspn(p, delim);
+               if (*p) {
+                       *p = '\0';
+                       p++;
+                       p += strspn(p, delim);
+               }
+               i++;
+       }
+       argv[n] = NULL;
+       return n;
+}