printf "width" support for hosts (e.g. %15H)
This commit is contained in:
@@ -106,48 +106,49 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||||||
const void *const *args)
|
const void *const *args)
|
||||||
{
|
{
|
||||||
private_host_t *this = *((private_host_t**)(args[0]));
|
private_host_t *this = *((private_host_t**)(args[0]));
|
||||||
char buffer[INET6_ADDRSTRLEN];
|
char buffer[INET6_ADDRSTRLEN + 16];
|
||||||
void *address;
|
int len;
|
||||||
u_int16_t port;
|
|
||||||
|
|
||||||
if (this == NULL)
|
if (this == NULL)
|
||||||
{
|
{
|
||||||
return fprintf(stream, "(null)");
|
len = sprintf(buffer, "(null)");
|
||||||
}
|
}
|
||||||
|
else if (is_anyaddr(this))
|
||||||
if (is_anyaddr(this))
|
|
||||||
{
|
{
|
||||||
return fprintf(stream, "%%any");
|
len = sprintf(buffer, "%%any");
|
||||||
}
|
|
||||||
|
|
||||||
switch (this->address.sa_family)
|
|
||||||
{
|
|
||||||
case AF_INET:
|
|
||||||
address = &this->address4.sin_addr;
|
|
||||||
port = this->address4.sin_port;
|
|
||||||
break;
|
|
||||||
case AF_INET6:
|
|
||||||
address = &this->address6.sin6_addr;
|
|
||||||
port = this->address6.sin6_port;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return fprintf(stream, "(family not supported)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inet_ntop(this->address.sa_family, address,
|
|
||||||
buffer, sizeof(buffer)) == NULL)
|
|
||||||
{
|
|
||||||
return fprintf(stream, "(address conversion failed)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->alt)
|
|
||||||
{
|
|
||||||
return fprintf(stream, "%s[%d]", buffer, ntohs(port));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return fprintf(stream, "%s", buffer);
|
void *address;
|
||||||
|
u_int16_t port;
|
||||||
|
|
||||||
|
address = &this->address6.sin6_addr;
|
||||||
|
port = this->address6.sin6_port;
|
||||||
|
|
||||||
|
switch (this->address.sa_family)
|
||||||
|
{
|
||||||
|
case AF_INET:
|
||||||
|
address = &this->address4.sin_addr;
|
||||||
|
port = this->address4.sin_port;
|
||||||
|
/* fall */
|
||||||
|
case AF_INET6:
|
||||||
|
|
||||||
|
if (inet_ntop(this->address.sa_family, address,
|
||||||
|
buffer, sizeof(buffer)) == NULL)
|
||||||
|
{
|
||||||
|
len = sprintf(buffer, "(address conversion failed)");
|
||||||
|
}
|
||||||
|
else if (info->alt)
|
||||||
|
{
|
||||||
|
len = sprintf(buffer, "%s[%d]", buffer, ntohs(port));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
len = sprintf(buffer, "(family not supported)");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return fprintf(stream, "%*s", info->width, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user