+int lustre_lnet_parse_nids(char *nids, char **array, int size,
+ char ***out_array)
+{
+ int num_nids = 0;
+ char *comma = nids, *cur, *entry;
+ char **new_array;
+ int i, len, start = 0, finish = 0;
+
+ if (nids == NULL || strlen(nids) == 0)
+ return size;
+
+ /* count the number or new nids, by counting the number of commas */
+ while (comma) {
+ comma = strchr(comma, ',');
+ if (comma) {
+ comma++;
+ num_nids++;
+ } else {
+ num_nids++;
+ }
+ }
+
+ /*
+ * if the array is not NULL allocate a large enough array to house
+ * the old and new entries
+ */
+ new_array = calloc(sizeof(char*),
+ (size > 0) ? size + num_nids : num_nids);
+
+ if (!new_array)
+ goto failed;
+
+ /* parse our the new nids and add them to the tail of the array */
+ comma = nids;
+ cur = nids;
+ start = (size > 0) ? size: 0;
+ finish = (size > 0) ? size + num_nids : num_nids;
+ for (i = start; i < finish; i++) {
+ comma = strchr(comma, ',');
+ if (!comma)
+ /*
+ * the length of the string to be parsed out is
+ * from cur to end of string. So it's good enough
+ * to strlen(cur)
+ */
+ len = strlen(cur) + 1;
+ else
+ /* length of the string is comma - cur */
+ len = (comma - cur) + 1;
+
+ entry = calloc(1, len);
+ if (!entry) {
+ finish = i > 0 ? i - 1: 0;
+ goto failed;
+ }
+ strncpy(entry, cur, len - 1);
+ entry[len] = '\0';
+ new_array[i] = entry;
+ if (comma) {
+ comma++;
+ cur = comma;
+ }
+ }
+
+ /* add the old entries in the array and delete the old array*/
+ for (i = 0; i < size; i++)
+ new_array[i] = array[i];
+
+ if (array)
+ free(array);
+
+ *out_array = new_array;
+
+ return finish;
+
+failed:
+ for (i = start; i < finish; i++)
+ free(new_array[i]);
+ if (new_array)
+ free(new_array);
+
+ return size;
+}
+