#include "daemon.h"
-void xwrite (int sock, const void *buf, size_t len);
-
static void usage (void);
/* Also in guestfs.c */
fprintf (stderr, "guestfsd [-f] [-h host -p port]\n");
}
+int
+add_string (char ***argv, int *size, int *alloc, const char *str)
+{
+ char **new_argv;
+ char *new_str;
+
+ if (*size >= *alloc) {
+ *alloc += 64;
+ new_argv = realloc (*argv, *alloc * sizeof (char *));
+ if (new_argv == NULL) {
+ reply_with_perror ("realloc");
+ free_strings (*argv);
+ return -1;
+ }
+ *argv = new_argv;
+ }
+
+ if (str) {
+ new_str = strdup (str);
+ if (new_str == NULL) {
+ reply_with_perror ("strdup");
+ free_strings (*argv);
+ }
+ } else
+ new_str = NULL;
+
+ (*argv)[*size] = new_str;
+
+ (*size)++;
+ return 0;
+}
+
+int
+count_strings (char **argv)
+{
+ int argc;
+
+ for (argc = 0; argv[argc] != NULL; ++argc)
+ ;
+ return argc;
+}
+
+static int
+compare (const void *vp1, const void *vp2)
+{
+ char * const *p1 = (char * const *) vp1;
+ char * const *p2 = (char * const *) vp2;
+ return strcmp (*p1, *p2);
+}
+
+void
+sort_strings (char **argv, int len)
+{
+ qsort (argv, len, sizeof (char *), compare);
+}
+
+void
+free_strings (char **argv)
+{
+ int argc;
+
+ for (argc = 0; argv[argc] != NULL; ++argc)
+ free (argv[argc]);
+ free (argv);
+}
+
+void
+free_stringslen (char **argv, int len)
+{
+ int i;
+
+ for (i = 0; i < len; ++i)
+ free (argv[i]);
+ free (argv);
+}
+
/* This is a more sane version of 'system(3)' for running external
* commands. It uses fork/execvp, so we don't need to worry about
* quoting of parameters, and it allows us to capture any error
}
/* Make sure the output buffers are \0-terminated. Also remove any
- * trailing \n characters.
+ * trailing \n characters from the error buffer (not from stdout).
*/
if (stdoutput) {
*stdoutput = realloc (*stdoutput, so_size+1);
if (*stdoutput == NULL) {
perror ("realloc");
*stdoutput = NULL;
- } else {
+ } else
(*stdoutput)[so_size] = '\0';
- so_size--;
- while (so_size >= 0 && (*stdoutput)[so_size] == '\n')
- (*stdoutput)[so_size--] = '\0';
- }
}
if (stderror) {
*stderror = realloc (*stderror, se_size+1);