+/**
+ * Free an array of words created by create_argv().
+ *
+ * \param argv A pointer previously obtained by \ref create_argv().
+ */
+void free_argv(char **argv)
+{
+ int i;
+
+ for (i = 0; argv[i]; i++)
+ free(argv[i]);
+ free(argv);
+}
+
+/**
+ * Split a line into words which are separated by whitespace.
+ *
+ * In contrast to gengetopt's string parser, double quotes, backslash-escaped
+ * characters and special characters like \p \\n are honored. The result
+ * contains pointers to copies of the words contained in \a line and has to be
+ * freed by using \ref free_argv().
+ *
+ * \param line The line to be split.
+ * \param result The array of words is returned here.
+ *
+ * \return Number of words in \a line, negative on errors.
+ */
+int create_argv(const char *line, char ***result)
+{
+ char *word, **argv = adu_malloc(2 * sizeof(char *));
+ const char *p;
+ int ret, num_words;
+
+ argv[0] = adu_strdup(line);
+ for (p = line, num_words = 1; ; p += ret, num_words++) {
+ ret = get_next_word(p, &word);
+ if (ret < 0)
+ goto err;
+ if (!ret)
+ break;
+ argv = adu_realloc(argv, (num_words + 2) * sizeof(char*));
+ argv[num_words] = word;
+ }
+ argv[num_words] = NULL;
+ *result = argv;
+ return num_words;
+err:
+ while (num_words > 0)
+ free(argv[--num_words]);
+ free(argv);
+ return ret;
+}
+
+char *absolute_path(const char *path)
+{
+ char *cwd, *ap;
+ long int path_max;
+
+ if (!path || !path[0])
+ return NULL;
+ if (path[0] == '/')
+ return adu_strdup(path);
+
+#ifdef PATH_MAX
+ path_max = PATH_MAX;
+#else
+ /*
+ * The result of pathconf(3) may be huge and unsuitable for mallocing
+ * memory. OTOH pathconf(3) may return -1 to signify that PATH_MAX is
+ * not bounded.
+ */
+ path_max = pathconf(name, _PC_PATH_MAX);
+ if (path_max <= 0 || path_max >= 4096)
+ path_max = 4096;
+#endif
+ cwd = adu_malloc(path_max);
+ if (!getcwd(cwd, path_max)) {
+ free(cwd);
+ return NULL;
+ }
+ ap = make_message("%s/%s", cwd, path);
+ free(cwd);
+ return ap;
+}