]> git.ipfire.org Git - thirdparty/git.git/blame - ident.c
Merge branch 'jk/rev-input-given-fix'
[thirdparty/git.git] / ident.c
CommitLineData
6aa33f40
LT
1/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
b2141fc1 9#include "config.h"
6aa33f40 10
8587ead7
JK
11static struct strbuf git_default_name = STRBUF_INIT;
12static struct strbuf git_default_email = STRBUF_INIT;
c33ddc2e 13static struct strbuf git_default_date = STRBUF_INIT;
39ab4d09
WH
14static struct strbuf git_author_name = STRBUF_INIT;
15static struct strbuf git_author_email = STRBUF_INIT;
16static struct strbuf git_committer_name = STRBUF_INIT;
17static struct strbuf git_committer_email = STRBUF_INIT;
19ce497c 18static int default_email_is_bogus;
92bcbb9b 19static int default_name_is_bogus;
45280230 20
4d5c2956
DA
21static int ident_use_config_only;
22
45280230
JK
23#define IDENT_NAME_GIVEN 01
24#define IDENT_MAIL_GIVEN 02
25#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
d6991cee
JK
26static int committer_ident_explicitly_given;
27static int author_ident_explicitly_given;
4d5c2956 28static int ident_config_given;
6aa33f40 29
590e081d
RG
30#ifdef NO_GECOS_IN_PWENT
31#define get_gecos(ignored) "&"
32#else
33#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
34#endif
35
92bcbb9b 36static struct passwd *xgetpwuid_self(int *is_bogus)
e850194c
JK
37{
38 struct passwd *pw;
39
40 errno = 0;
41 pw = getpwuid(getuid());
92bcbb9b
JK
42 if (!pw) {
43 static struct passwd fallback;
44 fallback.pw_name = "unknown";
45#ifndef NO_GECOS_IN_PWENT
46 fallback.pw_gecos = "Unknown";
47#endif
48 pw = &fallback;
49 if (is_bogus)
50 *is_bogus = 1;
51 }
e850194c
JK
52 return pw;
53}
54
8587ead7 55static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 56{
8587ead7 57 char *src;
e9bacb4f
JH
58
59 /* Traditionally GECOS field had office phone numbers etc, separated
60 * with commas. Also & stands for capitalized form of the login name.
61 */
62
8587ead7 63 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 64 int ch = *src;
8587ead7
JK
65 if (ch != '&')
66 strbuf_addch(name, ch);
67 else {
e9bacb4f 68 /* Sorry, Mr. McDonald... */
8587ead7
JK
69 strbuf_addch(name, toupper(*w->pw_name));
70 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
71 }
72 }
e9bacb4f
JH
73}
74
8587ead7 75static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
76{
77 FILE *mailname;
dc342a25 78 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8 79
e9d983f1
NTND
80 mailname = fopen_or_warn("/etc/mailname", "r");
81 if (!mailname)
8a55caa8 82 return -1;
e9d983f1 83
1f3b1efd 84 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
8a55caa8 85 if (ferror(mailname))
a26f4ed6 86 warning_errno("cannot read /etc/mailname");
dc342a25 87 strbuf_release(&mailnamebuf);
8a55caa8
JN
88 fclose(mailname);
89 return -1;
90 }
91 /* success! */
dc342a25
JN
92 strbuf_addbuf(buf, &mailnamebuf);
93 strbuf_release(&mailnamebuf);
8a55caa8
JN
94 fclose(mailname);
95 return 0;
96}
97
00bce77f
EP
98static int canonical_name(const char *host, struct strbuf *out)
99{
100 int status = -1;
101
102#ifndef NO_IPV6
103 struct addrinfo hints, *ai;
104 memset (&hints, '\0', sizeof (hints));
105 hints.ai_flags = AI_CANONNAME;
106 if (!getaddrinfo(host, NULL, &hints, &ai)) {
c375a7ef 107 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
00bce77f
EP
108 strbuf_addstr(out, ai->ai_canonname);
109 status = 0;
110 }
111 freeaddrinfo(ai);
112 }
113#else
58d29ece 114 struct hostent *he = gethostbyname(host);
00bce77f
EP
115 if (he && strchr(he->h_name, '.')) {
116 strbuf_addstr(out, he->h_name);
117 status = 0;
118 }
119#endif /* NO_IPV6 */
120
121 return status;
122}
123
19ce497c 124static void add_domainname(struct strbuf *out, int *is_bogus)
8a55caa8 125{
da25bdb7 126 char buf[HOST_NAME_MAX + 1];
8a55caa8 127
5781a9a2 128 if (xgethostname(buf, sizeof(buf))) {
a26f4ed6 129 warning_errno("cannot get host name");
8587ead7 130 strbuf_addstr(out, "(none)");
19ce497c 131 *is_bogus = 1;
8a55caa8
JN
132 return;
133 }
8587ead7 134 if (strchr(buf, '.'))
f8254d32 135 strbuf_addstr(out, buf);
5498c57c 136 else if (canonical_name(buf, out) < 0) {
f8254d32 137 strbuf_addf(out, "%s.(none)", buf);
19ce497c
JK
138 *is_bogus = 1;
139 }
8a55caa8
JN
140}
141
19ce497c
JK
142static void copy_email(const struct passwd *pw, struct strbuf *email,
143 int *is_bogus)
6aa33f40 144{
01754769
JH
145 /*
146 * Make up a fake email address
147 * (name + '@' + hostname [+ '.' + domainname])
148 */
8587ead7
JK
149 strbuf_addstr(email, pw->pw_name);
150 strbuf_addch(email, '@');
151
152 if (!add_mailname_host(email))
8a55caa8 153 return; /* read from "/etc/mailname" (Debian) */
19ce497c 154 add_domainname(email, is_bogus);
01754769
JH
155}
156
9830534e 157const char *ident_default_name(void)
01754769 158{
94425552 159 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
92bcbb9b 160 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
be641abd 161 strbuf_trim(&git_default_name);
01754769 162 }
8587ead7 163 return git_default_name.buf;
bcb2b004 164}
01754769 165
bcb2b004
JK
166const char *ident_default_email(void)
167{
94425552 168 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
46f74f00
MK
169 const char *email = getenv("EMAIL");
170
99178c83 171 if (email && email[0]) {
8587ead7 172 strbuf_addstr(&git_default_email, email);
d6991cee
JK
173 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
174 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
501afcb8
JS
175 } else if ((email = query_user_email()) && email[0]) {
176 strbuf_addstr(&git_default_email, email);
177 free((char *)email);
2f705875 178 } else
92bcbb9b
JK
179 copy_email(xgetpwuid_self(&default_email_is_bogus),
180 &git_default_email, &default_email_is_bogus);
be641abd 181 strbuf_trim(&git_default_email);
01754769 182 }
8587ead7 183 return git_default_email.buf;
6aa33f40
LT
184}
185
dad148c3 186static const char *ident_default_date(void)
6aa33f40 187{
c33ddc2e
JK
188 if (!git_default_date.len)
189 datestamp(&git_default_date);
190 return git_default_date.buf;
6aa33f40
LT
191}
192
4d9c7e6f
JK
193void reset_ident_date(void)
194{
195 strbuf_reset(&git_default_date);
196}
197
6aa33f40
LT
198static int crud(unsigned char c)
199{
f64c81d4
AR
200 return c <= 32 ||
201 c == '.' ||
202 c == ',' ||
203 c == ':' ||
204 c == ';' ||
205 c == '<' ||
206 c == '>' ||
207 c == '"' ||
d404bf02 208 c == '\\' ||
f64c81d4 209 c == '\'';
6aa33f40
LT
210}
211
13b9a24e
JK
212static int has_non_crud(const char *str)
213{
214 for (; *str; str++) {
215 if (!crud(*str))
216 return 1;
217 }
218 return 0;
219}
220
6aa33f40
LT
221/*
222 * Copy over a string to the destination, but avoid special
223 * characters ('\n', '<' and '>') and remove crud at the end
224 */
c96f0c8d 225static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 226{
b0732115 227 size_t i, len;
6aa33f40
LT
228 unsigned char c;
229
230 /* Remove crud from the beginning.. */
231 while ((c = *src) != 0) {
232 if (!crud(c))
233 break;
234 src++;
235 }
236
237 /* Remove crud from the end.. */
238 len = strlen(src);
239 while (len > 0) {
240 c = src[len-1];
241 if (!crud(c))
242 break;
243 --len;
244 }
245
246 /*
247 * Copy the rest to the buffer, but avoid the special
82f9d58a 248 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
249 * an identification line. We can only remove crud, never add it,
250 * so 'len' is our maximum.
6aa33f40 251 */
c96f0c8d 252 strbuf_grow(sb, len);
6aa33f40
LT
253 for (i = 0; i < len; i++) {
254 c = *src++;
255 switch (c) {
256 case '\n': case '<': case '>':
257 continue;
258 }
c96f0c8d 259 sb->buf[sb->len++] = c;
6aa33f40 260 }
c96f0c8d 261 sb->buf[sb->len] = '\0';
6aa33f40
LT
262}
263
4b340cfa
JH
264/*
265 * Reverse of fmt_ident(); given an ident line, split the fields
266 * to allow the caller to parse it.
267 * Signal a success by returning 0, but date/tz fields of the result
268 * can still be NULL if the input line only has the name/email part
269 * (e.g. reading from a reflog entry).
270 */
271int split_ident_line(struct ident_split *split, const char *line, int len)
272{
273 const char *cp;
274 size_t span;
275 int status = -1;
276
277 memset(split, 0, sizeof(*split));
278
279 split->name_begin = line;
280 for (cp = line; *cp && cp < line + len; cp++)
281 if (*cp == '<') {
282 split->mail_begin = cp + 1;
283 break;
284 }
285 if (!split->mail_begin)
286 return status;
287
d9955fd6 288 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
289 if (!isspace(*cp)) {
290 split->name_end = cp + 1;
291 break;
292 }
e27ddb64
JH
293 if (!split->name_end) {
294 /* no human readable name */
295 split->name_end = split->name_begin;
296 }
4b340cfa
JH
297
298 for (cp = split->mail_begin; cp < line + len; cp++)
299 if (*cp == '>') {
300 split->mail_end = cp;
301 break;
302 }
303 if (!split->mail_end)
304 return status;
305
03818a4a
JK
306 /*
307 * Look from the end-of-line to find the trailing ">" of the mail
308 * address, even though we should already know it as split->mail_end.
309 * This can help in cases of broken idents with an extra ">" somewhere
310 * in the email address. Note that we are assuming the timestamp will
311 * never have a ">" in it.
312 *
313 * Note that we will always find some ">" before going off the front of
314 * the string, because will always hit the split->mail_end closing
315 * bracket.
316 */
317 for (cp = line + len - 1; *cp != '>'; cp--)
318 ;
319
320 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
321 ;
322 if (line + len <= cp)
323 goto person_only;
324 split->date_begin = cp;
325 span = strspn(cp, "0123456789");
326 if (!span)
327 goto person_only;
328 split->date_end = split->date_begin + span;
329 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
330 ;
331 if (line + len <= cp || (*cp != '+' && *cp != '-'))
332 goto person_only;
333 split->tz_begin = cp;
334 span = strspn(cp + 1, "0123456789");
335 if (!span)
336 goto person_only;
337 split->tz_end = split->tz_begin + 1 + span;
338 return 0;
339
340person_only:
341 split->date_begin = NULL;
342 split->date_end = NULL;
343 split->tz_begin = NULL;
344 split->tz_end = NULL;
345 return 0;
346}
347
9ed104e5
JH
348
349static void ident_env_hint(enum want_ident whose_ident)
350{
351 switch (whose_ident) {
352 case WANT_AUTHOR_IDENT:
353 fputs(_("Author identity unknown\n"), stderr);
354 break;
355 case WANT_COMMITTER_IDENT:
356 fputs(_("Committer identity unknown\n"), stderr);
357 break;
358 default:
359 break;
360 }
361
362 fputs(_("\n"
363 "*** Please tell me who you are.\n"
364 "\n"
365 "Run\n"
366 "\n"
367 " git config --global user.email \"you@example.com\"\n"
368 " git config --global user.name \"Your Name\"\n"
369 "\n"
370 "to set your account\'s default identity.\n"
371 "Omit --global to set the identity only in this repository.\n"
372 "\n"), stderr);
373}
749be728 374
774751a8 375const char *fmt_ident(const char *name, const char *email,
39ab4d09 376 enum want_ident whose_ident, const char *date_str, int flag)
6aa33f40 377{
c96f0c8d 378 static struct strbuf ident = STRBUF_INIT;
f9bc573f 379 int strict = (flag & IDENT_STRICT);
359b27ad 380 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 381 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 382
39ab4d09
WH
383 if (!email) {
384 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
385 email = git_author_email.buf;
386 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
387 email = git_committer_email.buf;
388 }
862e80a4
JK
389 if (!email) {
390 if (strict && ident_use_config_only
391 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
9ed104e5 392 ident_env_hint(whose_ident);
862e80a4
JK
393 die(_("no email was given and auto-detection is disabled"));
394 }
395 email = ident_default_email();
396 if (strict && default_email_is_bogus) {
9ed104e5 397 ident_env_hint(whose_ident);
862e80a4
JK
398 die(_("unable to auto-detect email address (got '%s')"), email);
399 }
400 }
401
59f92959
JK
402 if (want_name) {
403 int using_default = 0;
39ab4d09
WH
404 if (!name) {
405 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
406 name = git_author_name.buf;
407 else if (whose_ident == WANT_COMMITTER_IDENT &&
408 git_committer_name.len)
409 name = git_committer_name.buf;
410 }
59f92959 411 if (!name) {
734c7789 412 if (strict && ident_use_config_only
d3c06c19 413 && !(ident_config_given & IDENT_NAME_GIVEN)) {
9ed104e5 414 ident_env_hint(whose_ident);
afb6c30b 415 die(_("no name was given and auto-detection is disabled"));
d3c06c19 416 }
59f92959
JK
417 name = ident_default_name();
418 using_default = 1;
419 if (strict && default_name_is_bogus) {
9ed104e5 420 ident_env_hint(whose_ident);
afb6c30b 421 die(_("unable to auto-detect name (got '%s')"), name);
59f92959
JK
422 }
423 }
424 if (!*name) {
425 struct passwd *pw;
426 if (strict) {
427 if (using_default)
9ed104e5 428 ident_env_hint(whose_ident);
afb6c30b 429 die(_("empty ident name (for <%s>) not allowed"), email);
59f92959
JK
430 }
431 pw = xgetpwuid_self(NULL);
432 name = pw->pw_name;
749be728 433 }
13b9a24e
JK
434 if (strict && !has_non_crud(name))
435 die(_("name consists only of disallowed characters: %s"), name);
92bcbb9b
JK
436 }
437
c96f0c8d 438 strbuf_reset(&ident);
c15e1987
JK
439 if (want_name) {
440 strbuf_addstr_without_crud(&ident, name);
441 strbuf_addstr(&ident, " <");
d9ccfe77 442 }
c96f0c8d 443 strbuf_addstr_without_crud(&ident, email);
c15e1987
JK
444 if (want_name)
445 strbuf_addch(&ident, '>');
359b27ad 446 if (want_date) {
c96f0c8d 447 strbuf_addch(&ident, ' ');
c33ddc2e
JK
448 if (date_str && date_str[0]) {
449 if (parse_date(date_str, &ident) < 0)
afb6c30b 450 die(_("invalid date format: %s"), date_str);
c33ddc2e
JK
451 }
452 else
453 strbuf_addstr(&ident, ident_default_date());
d9ccfe77 454 }
c33ddc2e 455
c96f0c8d 456 return ident.buf;
6aa33f40 457}
d289d136 458
39ab4d09 459const char *fmt_name(enum want_ident whose_ident)
d9ccfe77 460{
39ab4d09
WH
461 char *name = NULL;
462 char *email = NULL;
463
464 switch (whose_ident) {
465 case WANT_BLANK_IDENT:
466 break;
467 case WANT_AUTHOR_IDENT:
468 name = getenv("GIT_AUTHOR_NAME");
469 email = getenv("GIT_AUTHOR_EMAIL");
470 break;
471 case WANT_COMMITTER_IDENT:
472 name = getenv("GIT_COMMITTER_NAME");
473 email = getenv("GIT_COMMITTER_EMAIL");
474 break;
475 }
476 return fmt_ident(name, email, whose_ident, NULL,
477 IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
478}
479
774751a8 480const char *git_author_info(int flag)
d289d136 481{
d6991cee
JK
482 if (getenv("GIT_AUTHOR_NAME"))
483 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
484 if (getenv("GIT_AUTHOR_EMAIL"))
485 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 486 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 487 getenv("GIT_AUTHOR_EMAIL"),
39ab4d09 488 WANT_AUTHOR_IDENT,
749be728 489 getenv("GIT_AUTHOR_DATE"),
774751a8 490 flag);
d289d136
EB
491}
492
774751a8 493const char *git_committer_info(int flag)
d289d136 494{
91c38a21 495 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 496 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 497 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 498 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 499 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 500 getenv("GIT_COMMITTER_EMAIL"),
39ab4d09 501 WANT_COMMITTER_IDENT,
749be728 502 getenv("GIT_COMMITTER_DATE"),
774751a8 503 flag);
d289d136 504}
5aeb3a3a 505
d6991cee 506static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
507{
508#ifndef WINDOWS
509 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
510#else
511 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
512#endif
513}
9597921b 514
d6991cee
JK
515int committer_ident_sufficiently_given(void)
516{
517 return ident_is_sufficient(committer_ident_explicitly_given);
518}
519
520int author_ident_sufficiently_given(void)
521{
522 return ident_is_sufficient(author_ident_explicitly_given);
523}
524
39ab4d09 525static int set_ident(const char *var, const char *value)
9597921b 526{
39ab4d09
WH
527 if (!strcmp(var, "author.name")) {
528 if (!value)
529 return config_error_nonbool(var);
530 strbuf_reset(&git_author_name);
531 strbuf_addstr(&git_author_name, value);
532 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
533 ident_config_given |= IDENT_NAME_GIVEN;
534 return 0;
535 }
536
537 if (!strcmp(var, "author.email")) {
538 if (!value)
539 return config_error_nonbool(var);
540 strbuf_reset(&git_author_email);
541 strbuf_addstr(&git_author_email, value);
542 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
543 ident_config_given |= IDENT_MAIL_GIVEN;
544 return 0;
545 }
546
547 if (!strcmp(var, "committer.name")) {
548 if (!value)
549 return config_error_nonbool(var);
550 strbuf_reset(&git_committer_name);
551 strbuf_addstr(&git_committer_name, value);
552 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
553 ident_config_given |= IDENT_NAME_GIVEN;
554 return 0;
555 }
556
557 if (!strcmp(var, "committer.email")) {
558 if (!value)
559 return config_error_nonbool(var);
560 strbuf_reset(&git_committer_email);
561 strbuf_addstr(&git_committer_email, value);
562 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
563 ident_config_given |= IDENT_MAIL_GIVEN;
4d5c2956
DA
564 return 0;
565 }
566
9597921b
JK
567 if (!strcmp(var, "user.name")) {
568 if (!value)
569 return config_error_nonbool(var);
8587ead7
JK
570 strbuf_reset(&git_default_name);
571 strbuf_addstr(&git_default_name, value);
d6991cee
JK
572 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
573 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
4d5c2956 574 ident_config_given |= IDENT_NAME_GIVEN;
9597921b
JK
575 return 0;
576 }
577
578 if (!strcmp(var, "user.email")) {
579 if (!value)
580 return config_error_nonbool(var);
8587ead7
JK
581 strbuf_reset(&git_default_email);
582 strbuf_addstr(&git_default_email, value);
d6991cee
JK
583 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
584 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
4d5c2956 585 ident_config_given |= IDENT_MAIL_GIVEN;
9597921b
JK
586 return 0;
587 }
588
589 return 0;
590}
662cc30c 591
39ab4d09
WH
592int git_ident_config(const char *var, const char *value, void *data)
593{
594 if (!strcmp(var, "user.useconfigonly")) {
595 ident_use_config_only = git_config_bool(var, value);
596 return 0;
597 }
598
599 return set_ident(var, value);
600}
601
fd5a5847
JS
602static void set_env_if(const char *key, const char *value, int *given, int bit)
603{
0640897d 604 if ((*given & bit) || getenv(key))
fd5a5847
JS
605 return; /* nothing to do */
606 setenv(key, value, 0);
607 *given |= bit;
608}
609
610void prepare_fallback_ident(const char *name, const char *email)
611{
612 set_env_if("GIT_AUTHOR_NAME", name,
613 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
614 set_env_if("GIT_AUTHOR_EMAIL", email,
615 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
616 set_env_if("GIT_COMMITTER_NAME", name,
617 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
618 set_env_if("GIT_COMMITTER_EMAIL", email,
619 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
620}
621
662cc30c
JK
622static int buf_cmp(const char *a_begin, const char *a_end,
623 const char *b_begin, const char *b_end)
624{
625 int a_len = a_end - a_begin;
626 int b_len = b_end - b_begin;
627 int min = a_len < b_len ? a_len : b_len;
628 int cmp;
629
630 cmp = memcmp(a_begin, b_begin, min);
631 if (cmp)
632 return cmp;
633
634 return a_len - b_len;
635}
636
637int ident_cmp(const struct ident_split *a,
638 const struct ident_split *b)
639{
640 int cmp;
641
642 cmp = buf_cmp(a->mail_begin, a->mail_end,
643 b->mail_begin, b->mail_end);
644 if (cmp)
645 return cmp;
646
647 return buf_cmp(a->name_begin, a->name_end,
648 b->name_begin, b->name_end);
649}