]> git.tuebingen.mpg.de Git - dss.git/blobdiff - string.c
dss.ggo: Minor documentation improvements.
[dss.git] / string.c
index cf6d0b1a365aa378720ba1882aa5c83cc39a0df6..38fb1ee3f83a00b08b5a93af5260df4e092bc05f 100644 (file)
--- a/string.c
+++ b/string.c
@@ -13,6 +13,7 @@
 #include "gcc-compat.h"
 #include "log.h"
 #include "error.h"
+#include "string.h"
 
 __noreturn void clean_exit(int status);
 
@@ -183,15 +184,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.
  *
@@ -244,3 +236,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;
+}