mirror of
https://github.com/transmission/transmission
synced 2025-02-20 21:26:53 +00:00
(trunk gtk) gtr_get_host_from_url(): avoid a couple of malloc() + free() calls.
This commit is contained in:
parent
e514f93fd1
commit
b93bef2712
1 changed files with 14 additions and 8 deletions
22
gtk/util.c
22
gtk/util.c
|
@ -208,22 +208,28 @@ gtr_mkdir_with_parents( const char * path, int mode )
|
|||
void
|
||||
gtr_get_host_from_url( char * buf, size_t buflen, const char * url )
|
||||
{
|
||||
char * h = NULL;
|
||||
char host[1024];
|
||||
const char * pch;
|
||||
|
||||
tr_urlParse( url, -1, NULL, &h, NULL, NULL );
|
||||
if(( pch = strstr( url, "://" ))) {
|
||||
const size_t hostlen = strcspn( pch+3, ":/" );
|
||||
const size_t copylen = MIN( hostlen, sizeof(host)-1 );
|
||||
memcpy( host, pch+3, copylen );
|
||||
host[copylen] = '\0';
|
||||
} else {
|
||||
*host = '\0';
|
||||
}
|
||||
|
||||
if( tr_addressIsIP( h ) )
|
||||
if( tr_addressIsIP( host ) )
|
||||
g_strlcpy( buf, url, buflen );
|
||||
else {
|
||||
const char * first_dot = strchr( h, '.' );
|
||||
const char * last_dot = strrchr( h, '.' );
|
||||
const char * first_dot = strchr( host, '.' );
|
||||
const char * last_dot = strrchr( host, '.' );
|
||||
if( ( first_dot ) && ( last_dot ) && ( first_dot != last_dot ) )
|
||||
g_strlcpy( buf, first_dot + 1, buflen );
|
||||
else
|
||||
g_strlcpy( buf, h, buflen );
|
||||
g_strlcpy( buf, host, buflen );
|
||||
}
|
||||
|
||||
tr_free( h );
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
|
Loading…
Reference in a new issue