Index: uspace/app/nettest2/nettest2.c
===================================================================
--- uspace/app/nettest2/nettest2.c	(revision 5f69504acd57bfcce8f4f6c1d97b5ce32635bc21)
+++ uspace/app/nettest2/nettest2.c	(revision 987930f81cb7f02ccc80523f1057b0a6f7209712)
@@ -44,4 +44,5 @@
 #include <time.h>
 #include <arg_parse.h>
+#include <bool.h>
 
 #include <net/in.h>
@@ -57,174 +58,220 @@
 #define NETTEST2_TEXT	"Networking test 2 - transfer"
 
+static size_t size;
+static bool verbose;
+static sock_type_t type;
+static int sockets;
+static int messages;
+static int family;
+static uint16_t port;
+
 static void nettest2_print_help(void)
 {
 	printf(
-		"Network Networking test 2 aplication - UDP transfer\n" \
-		"Usage: echo [options] numeric_address\n" \
-		"Where options are:\n" \
-		"-f protocol_family | --family=protocol_family\n" \
-		"\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
-		"\n" \
-		"-h | --help\n" \
-		"\tShow this application help.\n"
-		"\n" \
-		"-m count | --messages=count\n" \
-		"\tThe number of messages to send and receive per socket. The default is 10.\n" \
-		"\n" \
-		"-n sockets | --sockets=count\n" \
-		"\tThe number of sockets to use. The default is 10.\n" \
-		"\n" \
-		"-p port_number | --port=port_number\n" \
-		"\tThe port number the application should send messages to. The default is 7.\n" \
-		"\n" \
-		"-s packet_size | --size=packet_size\n" \
-		"\tThe packet data size the application sends. The default is 29 bytes.\n" \
-		"\n" \
-		"-v | --verbose\n" \
-		"\tShow all output messages.\n"
-	);
+	    "Network Networking test 2 aplication - UDP transfer\n"
+	    "Usage: echo [options] address\n"
+	    "Where options are:\n"
+	    "-f protocol_family | --family=protocol_family\n"
+	    "\tThe listenning socket protocol family. Only the PF_INET and "
+	    "PF_INET6 are supported.\n"
+	    "\n"
+	    "-h | --help\n"
+	    "\tShow this application help.\n"
+	    "\n"
+	    "-m count | --messages=count\n"
+	    "\tThe number of messages to send and receive per socket. The "
+	    "default is 10.\n"
+	    "\n"
+	    "-n sockets | --sockets=count\n"
+	    "\tThe number of sockets to use. The default is 10.\n"
+	    "\n"
+	    "-p port_number | --port=port_number\n"
+	    "\tThe port number the application should send messages to. The "
+	    "default is 7.\n"
+	    "\n"
+	    "-s packet_size | --size=packet_size\n"
+	    "\tThe packet data size the application sends. The default is 29 "
+	    "bytes.\n"
+	    "\n"
+	    "-v | --verbose\n"
+	    "\tShow all output messages.\n");
 }
 
-/** Refreshes the data.
+/** Fill buffer with the NETTEST1_TEXT pattern.
  *
- * Fills the data block with the NETTEST1_TEXT pattern.
- *
- * @param[out] data The data block.
- * @param[in] size The data block size in bytes.
- */
-static void nettest2_refresh_data(char *data, size_t size)
+ * @param buffer	Data buffer.
+ * @param size		Buffer size in bytes.
+ */
+static void nettest2_fill_buffer(char *buffer, size_t size)
 {
 	size_t length;
 
-	// fill the data
 	length = 0;
 	while (size > length + sizeof(NETTEST2_TEXT) - 1) {
-		memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1);
+		memcpy(buffer + length, NETTEST2_TEXT,
+		    sizeof(NETTEST2_TEXT) - 1);
 		length += sizeof(NETTEST2_TEXT) - 1;
 	}
-	memcpy(data + length, NETTEST2_TEXT, size - length);
-	data[size] = '\0';
+
+	memcpy(buffer + length, NETTEST2_TEXT, size - length);
+	buffer[size] = '\0';
 }
 
-
-int main(int argc, char *argv[])
+/** Parse one command-line option.
+ *
+ * @param argc		Number of all command-line arguments.
+ * @param argv		All command-line arguments.
+ * @param index		Current argument index (in, out).
+ */
+static int nettest2_parse_opt(int argc, char *argv[], int *index)
 {
-	size_t size = 28;
-	int verbose = 0;
-	sock_type_t type = SOCK_DGRAM;
-	int sockets = 10;
-	int messages = 10;
-	int family = PF_INET;
-	uint16_t port = 7;
-
-	socklen_t max_length = sizeof(struct sockaddr_in6);
-	uint8_t address_data[max_length];
-	struct sockaddr *address = (struct sockaddr *) address_data;
-	struct sockaddr_in *address_in = (struct sockaddr_in *) address;
-	struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
-	socklen_t addrlen;
-	uint8_t *address_start;
-
-	int *socket_ids;
-	char *data;
 	int value;
-	int index;
-	struct timeval time_before;
-	struct timeval time_after;
-
 	int rc;
 
-	// parse the command line arguments
-	// stop before the last argument if it does not start with the minus sign ('-')
-	for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index) {
-		// options should start with the minus sign ('-')
-		if (argv[index][0] == '-') {
-			switch(argv[index][1]){
-			// short options with only one letter
-			case 'f':
-				rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family);
-				if (rc != EOK)
-					return rc;
-				break;
-			case 'h':
-				nettest2_print_help();
-				return EOK;
-				break;
-			case 'm':
-				rc = arg_parse_int(argc, argv, &index, &messages, 0);
-				if (rc != EOK)
-					return rc;
-				break;
-			case 'n':
-				rc = arg_parse_int(argc, argv, &index, &sockets, 0);
-				if (rc != EOK)
-					return rc;
-				break;
-			case 'p':
-				rc = arg_parse_int(argc, argv, &index, &value, 0);
-				if (rc != EOK)
-					return rc;
-				port = (uint16_t) value;
-				break;
-			case 's':
-				rc = arg_parse_int(argc, argv, &index, &value, 0);
-				if (rc != EOK)
-					return rc;
-				size = (value >= 0) ? (size_t) value : 0;
-				break;
-			case 't':
-				rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type);
-				if (rc != EOK)
-					return rc;
-				type = (sock_type_t) value;
-				break;
-			case 'v':
-				verbose = 1;
-				break;
-			// long options with the double minus sign ('-')
-			case '-':
-				if (str_lcmp(argv[index] + 2, "family=", 7) == 0) {
-					rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family);
-					if (rc != EOK)
-						return rc;
-				} else if (str_lcmp(argv[index] + 2, "help", 5) == 0) {
-					nettest2_print_help();
-					return EOK;
-				} else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) {
-					rc = arg_parse_int(argc, argv, &index, &messages, 8);
-					if (rc != EOK)
-						return rc;
-				} else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) {
-					rc = arg_parse_int(argc, argv, &index, &sockets, 8);
-					if (rc != EOK)
-						return rc;
-				} else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) {
-					rc = arg_parse_int(argc, argv, &index, &value, 7);
-					if (rc != EOK)
-						return rc;
-					port = (uint16_t) value;
-				} else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) {
-					rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type);
-					if (rc != EOK)
-						return rc;
-					type = (sock_type_t) value;
-				} else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) {
-					verbose = 1;
-				} else {
-					nettest2_print_help();
-					return EINVAL;
-				}
-				break;
-			default:
-				nettest2_print_help();
-				return EINVAL;
-			}
+	rc = EOK;
+
+	switch (argv[*index][1]) {
+	/*
+	 * Short options with only one letter
+	 */
+	case 'f':
+		rc = arg_parse_name_int(argc, argv, index, &family, 0,
+		    socket_parse_protocol_family);
+		if (rc != EOK)
+			return rc;
+		break;
+	case 'h':
+		nettest2_print_help();
+		return EOK;
+		break;
+	case 'm':
+		rc = arg_parse_int(argc, argv, index, &messages, 0);
+		if (rc != EOK)
+			return rc;
+		break;
+	case 'n':
+		rc = arg_parse_int(argc, argv, index, &sockets, 0);
+		if (rc != EOK)
+			return rc;
+		break;
+	case 'p':
+		rc = arg_parse_int(argc, argv, index, &value, 0);
+		if (rc != EOK)
+			return rc;
+		port = (uint16_t) value;
+		break;
+	case 's':
+		rc = arg_parse_int(argc, argv, index, &value, 0);
+		if (rc != EOK)
+			return rc;
+		size = (value >= 0) ? (size_t) value : 0;
+		break;
+	case 't':
+		rc = arg_parse_name_int(argc, argv, index, &value, 0,
+		    socket_parse_socket_type);
+		if (rc != EOK)
+			return rc;
+		type = (sock_type_t) value;
+		break;
+	case 'v':
+		verbose = true;
+		break;
+	/*
+	 * Long options with double dash ('-')
+	 */
+	case '-':
+		if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
+			rc = arg_parse_name_int(argc, argv, index, &family, 9,
+			    socket_parse_protocol_family);
+			if (rc != EOK)
+				return rc;
+		} else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
+			nettest2_print_help();
+			return EOK;
+		} else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
+			rc = arg_parse_int(argc, argv, index, &messages, 8);
+			if (rc != EOK)
+				return rc;
+		} else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
+			rc = arg_parse_int(argc, argv, index, &sockets, 8);
+			if (rc != EOK)
+				return rc;
+		} else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
+			rc = arg_parse_int(argc, argv, index, &value, 7);
+			if (rc != EOK)
+				return rc;
+			port = (uint16_t) value;
+		} else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
+			rc = arg_parse_name_int(argc, argv, index, &value, 7,
+			    socket_parse_socket_type);
+			if (rc != EOK)
+				return rc;
+			type = (sock_type_t) value;
+		} else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
+			verbose = 1;
 		} else {
 			nettest2_print_help();
 			return EINVAL;
 		}
-	}
-
-	// if not before the last argument containing the address
+		break;
+	default:
+		nettest2_print_help();
+		return EINVAL;
+	}
+
+	return EOK;
+}
+
+int main(int argc, char *argv[])
+{
+	socklen_t max_length;
+	uint8_t address_data[sizeof(struct sockaddr_in6)];
+	struct sockaddr *address;
+	struct sockaddr_in *address_in;
+	struct sockaddr_in6 *address_in6;
+	socklen_t addrlen;
+	uint8_t *address_start;
+
+	int *socket_ids;
+	char *data;
+	int index;
+	struct timeval time_before;
+	struct timeval time_after;
+
+	int rc;
+
+	size = 28;
+	verbose = false;
+	type = SOCK_DGRAM;
+	sockets = 10;
+	messages = 10;
+	family = PF_INET;
+	port = 7;
+
+	max_length = sizeof(address_data);
+	address = (struct sockaddr *) address_data;
+	address_in = (struct sockaddr_in *) address;
+	address_in6 = (struct sockaddr_in6 *) address;
+
+	/*
+	 * Parse the command line arguments.
+	 *
+	 * Stop before the last argument if it does not start with dash ('-')
+	 */
+	for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
+	    (argv[index][0] == '-')); ++index) {
+
+		/* Options should start with dash ('-') */
+		if (argv[index][0] == '-') {
+			rc = nettest2_parse_opt(argc, argv, &index);
+			if (rc != EOK)
+				return rc;
+		} else {
+			nettest2_print_help();
+			return EINVAL;
+		}
+	}
+
+	/* If not before the last argument containing the address */
 	if (index >= argc) {
 		printf("Command line error: missing address\n");
@@ -233,6 +280,7 @@
 	}
 
-	// prepare the address buffer
+	/* Prepare the address buffer */
 	bzero(address_data, max_length);
+
 	switch (family) {
 	case PF_INET:
@@ -253,5 +301,5 @@
 	}
 
-	// parse the last argument which should contain the address
+	/* Parse the last argument which should contain the address. */
 	rc = inet_pton(family, argv[argc - 1], address_start);
 	if (rc != EOK) {
@@ -260,12 +308,15 @@
 	}
 
-	// check the buffer size
+	/* Check data buffer size. */
 	if (size <= 0) {
-		fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size);
+		fprintf(stderr, "Data buffer size too small (%d). Using 1024 "
+		    "bytes instead.\n", size);
 		size = 1024;
 	}
 
-	// prepare the buffer
-	// size plus terminating null (\0)
+	/*
+	 * Prepare the buffer. Allocate size bytes plus one for terminating
+	 * null character.
+	 */
 	data = (char *) malloc(size + 1);
 	if (!data) {
@@ -273,14 +324,19 @@
 		return ENOMEM;
 	}
-	nettest2_refresh_data(data, size);
-
-	// check the socket count
+
+	/* Fill buffer with a pattern. */
+	nettest2_fill_buffer(data, size);
+
+	/* Check socket count. */
 	if (sockets <= 0) {
-		fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets);
+		fprintf(stderr, "Socket count too small (%d). Using "
+		    "2 instead.\n", sockets);
 		sockets = 2;
 	}
 
-	// prepare the socket buffer
-	// count plus the terminating null (\0)
+	/*
+	 * Prepare the socket buffer.
+	 * Allocate count entries plus the terminating null (\0)
+	 */
 	socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
 	if (!socket_ids) {
@@ -298,5 +354,6 @@
 
 	if (type == SOCK_STREAM) {
-		rc = sockets_connect(verbose, socket_ids, sockets, address, addrlen);
+		rc = sockets_connect(verbose, socket_ids, sockets,
+		    address, addrlen);
 		if (rc != EOK)
 			return rc;
@@ -312,5 +369,6 @@
 	}
 
-	rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages);
+	rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
+	    &addrlen, data, size, messages);
 	if (rc != EOK)
 		return rc;
@@ -325,5 +383,6 @@
 		printf("\tOK\n");
 
-	printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before));
+	printf("sendto + recvfrom tested in %d microseconds\n",
+	    tv_sub(&time_after, &time_before));
 
 	rc = gettimeofday(&time_before, NULL);
@@ -333,9 +392,11 @@
 	}
 
-	rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages);
+	rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
+	    data, size, messages);
 	if (rc != EOK)
 		return rc;
 
-	rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages);
+	rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
+	    data, size, messages);
 	if (rc != EOK)
 		return rc;
@@ -350,5 +411,6 @@
 		printf("\tOK\n");
 
-	printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before));
+	printf("sendto, recvfrom tested in %d microseconds\n",
+	    tv_sub(&time_after, &time_before));
 
 	rc = sockets_close(verbose, socket_ids, sockets);
