/* Turn list 'excludes' into list of " --excludes=..." strings, all * properly quoted. Caller must free the returned string. */ static char * make_excludes_args (char *const *excludes) { DECLARE_STRINGSBUF (strings); size_t i; char *s, *ret; for (i = 0; excludes[i] != NULL; ++i) { if (asprintf_nowarn (&s, " --exclude=%Q", excludes[i]) == -1) { reply_with_perror ("asprintf"); free_stringslen (strings.argv, strings.size); return NULL; } if (add_string_nodup (&strings, s) == -1) { free (s); return NULL; } } if (end_stringsbuf (&strings) == -1) return NULL; ret = concat_strings (strings.argv); if (!ret) { reply_with_perror ("concat"); free_stringslen (strings.argv, strings.size); return NULL; } free_stringslen (strings.argv, strings.size); return ret; }
static int add_partitions(const char *device, char ***const r, int *const size, int *const alloc) { char devdir[256]; /* Open the device's directory under /sys/block */ snprintf (devdir, sizeof devdir, "/sys/block/%s", device); DIR *dir = opendir (devdir); if (!dir) { reply_with_perror ("opendir: %s", devdir); free_stringslen (*r, *size); return -1; } /* Look in /sys/block/<device>/ for entries starting with <device> * e.g. /sys/block/sda/sda1 */ errno = 0; struct dirent *d; while ((d = readdir (dir)) != NULL) { if (STREQLEN (d->d_name, device, strlen (device))) { char part[256]; snprintf (part, sizeof part, "/dev/%s", d->d_name); if (add_string (r, size, alloc, part) == -1) { closedir (dir); return -1; } } } /* Check if readdir failed */ if(0 != errno) { reply_with_perror ("readdir: %s", devdir); free_stringslen(*r, *size); closedir (dir); return -1; } /* Close the directory handle */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /sys/block/%s", device); free_stringslen (*r, *size); return -1; } return 0; }
int add_string (struct stringsbuf *sb, const char *str) { char *new_str = NULL; if (str) { new_str = strdup (str); if (new_str == NULL) { reply_with_perror ("strdup"); free_stringslen (sb->argv, sb->size); sb->argv = NULL; return -1; } } return add_string_nodup (sb, new_str); }
int add_sprintf (struct stringsbuf *sb, const char *fs, ...) { va_list args; char *str; int r; va_start (args, fs); r = vasprintf (&str, fs, args); va_end (args); if (r == -1) { reply_with_perror ("vasprintf"); free_stringslen (sb->argv, sb->size); sb->argv = NULL; return -1; } return add_string_nodup (sb, str); }
int add_string_nodup (struct stringsbuf *sb, char *str) { char **new_argv; if (sb->size >= sb->alloc) { sb->alloc += 64; new_argv = realloc (sb->argv, sb->alloc * sizeof (char *)); if (new_argv == NULL) { reply_with_perror ("realloc"); free_stringslen (sb->argv, sb->size); sb->argv = NULL; return -1; } sb->argv = new_argv; } sb->argv[sb->size] = str; sb->size++; return 0; }