diff options
Diffstat (limited to 'misc.c')
-rw-r--r-- | misc.c | 63 |
1 files changed, 62 insertions, 1 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: misc.c,v 1.104 2016/04/06 06:42:17 djm Exp $ */ | 1 | /* $OpenBSD: misc.c,v 1.105 2016/07/15 00:24:30 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. | 4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. |
@@ -451,6 +451,67 @@ colon(char *cp) | |||
451 | return NULL; | 451 | return NULL; |
452 | } | 452 | } |
453 | 453 | ||
454 | /* | ||
455 | * Parse a [user@]host[:port] string. | ||
456 | * Caller must free returned user and host. | ||
457 | * Any of the pointer return arguments may be NULL (useful for syntax checking). | ||
458 | * If user was not specified then *userp will be set to NULL. | ||
459 | * If port was not specified then *portp will be -1. | ||
460 | * Returns 0 on success, -1 on failure. | ||
461 | */ | ||
462 | int | ||
463 | parse_user_host_port(const char *s, char **userp, char **hostp, int *portp) | ||
464 | { | ||
465 | char *sdup, *cp, *tmp; | ||
466 | char *user = NULL, *host = NULL; | ||
467 | int port = -1, ret = -1; | ||
468 | |||
469 | if (userp != NULL) | ||
470 | *userp = NULL; | ||
471 | if (hostp != NULL) | ||
472 | *hostp = NULL; | ||
473 | if (portp != NULL) | ||
474 | *portp = -1; | ||
475 | |||
476 | if ((sdup = tmp = strdup(s)) == NULL) | ||
477 | return -1; | ||
478 | /* Extract optional username */ | ||
479 | if ((cp = strchr(tmp, '@')) != NULL) { | ||
480 | *cp = '\0'; | ||
481 | if (*tmp == '\0') | ||
482 | goto out; | ||
483 | if ((user = strdup(tmp)) == NULL) | ||
484 | goto out; | ||
485 | tmp = cp + 1; | ||
486 | } | ||
487 | /* Extract mandatory hostname */ | ||
488 | if ((cp = hpdelim(&tmp)) == NULL || *cp == '\0') | ||
489 | goto out; | ||
490 | host = xstrdup(cleanhostname(cp)); | ||
491 | /* Convert and verify optional port */ | ||
492 | if (tmp != NULL && *tmp != '\0') { | ||
493 | if ((port = a2port(tmp)) <= 0) | ||
494 | goto out; | ||
495 | } | ||
496 | /* Success */ | ||
497 | if (userp != NULL) { | ||
498 | *userp = user; | ||
499 | user = NULL; | ||
500 | } | ||
501 | if (hostp != NULL) { | ||
502 | *hostp = host; | ||
503 | host = NULL; | ||
504 | } | ||
505 | if (portp != NULL) | ||
506 | *portp = port; | ||
507 | ret = 0; | ||
508 | out: | ||
509 | free(sdup); | ||
510 | free(user); | ||
511 | free(host); | ||
512 | return ret; | ||
513 | } | ||
514 | |||
454 | /* function to assist building execv() arguments */ | 515 | /* function to assist building execv() arguments */ |
455 | void | 516 | void |
456 | addargs(arglist *args, char *fmt, ...) | 517 | addargs(arglist *args, char *fmt, ...) |