summaryrefslogtreecommitdiffstats
path: root/src/format.c
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-07-31 15:05:29 -0400
committerNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-07-31 15:05:29 -0400
commit62dfaaf510bc1d86a5036451b549d5bb1afd813b (patch)
tree053dd43f35f7e26887a5dd56a26a36af4ac55b73 /src/format.c
parent82fd474ce71e2c5ac539eb9b5ac0832240e3be26 (diff)
- make %first sort the values it's given and return the first in sort order,
not returned-by-the-directory order, which isn't really guaranteed
Diffstat (limited to 'src/format.c')
-rw-r--r--src/format.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/format.c b/src/format.c
index 0abefb8..700d320 100644
--- a/src/format.c
+++ b/src/format.c
@@ -561,7 +561,7 @@ format_first(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
struct format_choice **outbuf_choices,
char ***ref_attrs, struct format_inref_attr ***inref_attrs)
{
- int ret, i, argc;
+ int ret, i, argc, first, common_length;
char **argv, **values;
const char *value_format, *default_value;
unsigned int *lengths;
@@ -601,11 +601,24 @@ format_first(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
ret = i;
}
} else {
- if ((int) lengths[0] > outbuf_len) {
+ first = 0;
+ for (i = 1; values[i] != NULL; i++) {
+ /* Check if this entry sorts "earlier" than the current
+ * "first" entry. If it does, note its position as the
+ * "first". */
+ common_length = lengths[first] < lengths[i] ?
+ lengths[first] : lengths[i];
+ ret = memcmp(values[i], values[first], common_length);
+ if ((ret < 0) ||
+ ((ret == 0) && (lengths[i] < lengths[first]))) {
+ first = i;
+ }
+ }
+ if ((int) lengths[first] > outbuf_len) {
ret = -ENOSPC;
} else {
- memcpy(outbuf, values[0], lengths[0]);
- ret = lengths[0];
+ memcpy(outbuf, values[first], lengths[first]);
+ ret = lengths[first];
}
format_free_data_set(values, lengths);
}