+static int ubi_list(const char *var, int numeric)
+{
+ size_t namelen, len, size;
+ char *str, *str2;
+ int i;
+
+ if (!var) {
+ for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
+ if (!ubi->volumes[i])
+ continue;
+ if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
+ continue;
+ printf("%d: %s\n",
+ ubi->volumes[i]->vol_id,
+ ubi->volumes[i]->name);
+ }
+ return 0;
+ }
+
+ len = 0;
+ size = 16;
+ str = malloc(size);
+ if (!str)
+ return 1;
+
+ for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
+ if (!ubi->volumes[i])
+ continue;
+ if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
+ continue;
+
+ if (numeric)
+ namelen = 10; /* strlen(stringify(INT_MAX)) */
+ else
+ namelen = strlen(ubi->volumes[i]->name);
+
+ if (len + namelen + 1 > size) {
+ size = roundup_pow_of_two(len + namelen + 1) * 2;
+ str2 = realloc(str, size);
+ if (!str2) {
+ free(str);
+ return 1;
+ }
+ str = str2;
+ }
+
+ if (len)
+ str[len++] = ' ';
+
+ if (numeric) {
+ len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1;
+ } else {
+ memcpy(str + len, ubi->volumes[i]->name, namelen);
+ len += namelen;
+ str[len] = 0;
+ }
+ }
+
+ env_set(var, str);
+ free(str);
+
+ return 0;
+}
+