aboutsummaryrefslogtreecommitdiff
path: root/ssh/util.c
blob: a8f3c419618694a8af51fe5293d41d6a9d027680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"


bool parse_host_port(const char *arg, const char **server_host, int *port) {
	const char *ptr = strchr(arg, ':');
	if (ptr == NULL) {
		*server_host = arg;
	} else {
		size_t length = ptr - arg;
		char *host = malloc(length + 1);
		if (!host) {
			fprintf(stderr, "Cannot allocate memory!\n");
			exit(1);
		}
		memcpy(host, arg, length);
		host[length] = '\0';
		*server_host = host;

		char *endp;
		*port = strtol(ptr + 1, &endp, 10);
		if (endp == ptr || *endp != '\0') {
			return false;
		}
	}
	return true;
}