- /* Count number of variable arguments provided to this function
- * and determine total number of key-value pairs.
- */
- calc_pairs = 0;
- va_start(ap, vargs);
- ptr = va_arg(ap, char *);
- while (ptr) {
- calc_pairs++;
- ptr = va_arg(ap, char *);
- }
- calc_pairs /= 2;
- va_end(ap);
-
- /* If user provided variable number of arguments, append them as
- * as real key-value "[k v]" into the format string. If the format
- * string is too small, ignore the key-value pair completely.
- */
- if (calc_pairs) {
- char *key, *val;
- size_t pairlen;
- int offset;
-
- /* Calculate bytes available for key-value pairs after reserving
- * bytes for newline character and NULL terminator
- */
- len = MAX_FMT_LEN - strlen(fmt) - KASL_NEWLINE_CHAR_LEN - 1;
- offset = strlen(fmt);
-
- va_start(ap, vargs);
- for (i = 0; i < calc_pairs; i++) {
- key = va_arg(ap, char *);
- val = va_arg(ap, char *);
-
- /* Calculate bytes required to store next key-value pair
- * as "[key val] " including space for '[', ']', and
- * two spaces.
- */
- pairlen = strlen(key) + strlen(val) + 4;
- if (pairlen > len) {
- err = E2BIG;
- break;
- }
-
- /* len + 1 because one byte has been set aside for NULL
- * terminator in calculation of 'len' above
- */
- snprintf((fmt + offset), len + 1, KASL_KEYVAL_FMT,
- key, val);
- offset += pairlen;
- len -= pairlen;
- }
- va_end(ap);
- }
-