diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-03-13 08:19:11 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-03-13 08:19:11 +0000 |
commit | 14df5fa5d1731a502332e9d4a41f54ee3d4a4dac (patch) | |
tree | 72313cef36867c63ae09e1d23bcd5d7c380fdf88 /daemon/9p.c | |
parent | d66dd2260c724bdfe57a8595aac37c8e9173cee5 (diff) | |
download | libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.tar.gz libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.tar.xz libguestfs-14df5fa5d1731a502332e9d4a41f54ee3d4a4dac.zip |
daemon: Implement a growable strings buffer type.
Previously a lot of daemon code used three variables (a string list,
'int size' and 'int alloc') to track growable strings buffers. This
commit implements a simple struct containing the same variables, but
using size_t instead of int:
struct stringsbuf {
char **argv;
size_t size;
size_t alloc;
};
Use it like this:
DECLARE_STRINGSBUF (ret);
//...
if (add_string (&ret, str) == -1)
return NULL;
//...
if (end_stringsbuf (&ret) == -1)
return NULL;
return ret.argv;
Diffstat (limited to 'daemon/9p.c')
-rw-r--r-- | daemon/9p.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/daemon/9p.c b/daemon/9p.c index 8476c45c..6243919d 100644 --- a/daemon/9p.c +++ b/daemon/9p.c @@ -40,8 +40,7 @@ static char *read_whole_file (const char *filename); char ** do_list_9p (void) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (r); DIR *dir; @@ -55,10 +54,10 @@ do_list_9p (void) * the virtio driver isn't loaded. Don't return an error * in this case, but return an empty list. */ - if (add_string (&r, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&r) == -1) return NULL; - return r; + return r.argv; } while (1) { @@ -79,7 +78,7 @@ do_list_9p (void) if (mount_tag == 0) continue; - if (add_string (&r, &size, &alloc, mount_tag) == -1) { + if (add_string (&r, mount_tag) == -1) { free (mount_tag); closedir (dir); return NULL; @@ -92,7 +91,7 @@ do_list_9p (void) /* Check readdir didn't fail */ if (errno != 0) { reply_with_perror ("readdir: /sys/block"); - free_stringslen (r, size); + free_stringslen (r.argv, r.size); closedir (dir); return NULL; } @@ -100,19 +99,19 @@ do_list_9p (void) /* Close the directory handle */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /sys/block"); - free_stringslen (r, size); + free_stringslen (r.argv, r.size); return NULL; } - /* Sort the tags. Note that r might be NULL if there are no tags. */ - if (r != NULL) - sort_strings (r, size); + /* Sort the tags. */ + if (r.size > 0) + sort_strings (r.argv, r.size); /* NULL terminate the list */ - if (add_string (&r, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&r) == -1) return NULL; - return r; + return r.argv; } /* Read whole file into dynamically allocated array. If there is an |