/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/



#include	"memory_debug.h"
#include	"long_char.h"
#include	"utils.h"
#include	"netutils.h"

L_CHAR *
url_regulation(L_CHAR * url)
{
URL u;
char * d;
char * _ret;
L_CHAR * ret;
char * ptr;
int len_server,len_d;
char * _server;
	get_url2(&u,url);
	if ( u.server == 0 ) {
		ret = ll_copy_str(url);
		goto end0;
	}
	d = get_localdomainname();
	if ( d == 0 ) {
		ret = ll_copy_str(url);
		goto end0;
	}
	_server = ln_copy_str(std_cm,u.server);
	for ( ptr = _server ; *ptr && *ptr != '.' ; ptr ++ );
	if ( *ptr == 0 ) {
		len_server = strlen(_server);
		len_d = strlen(d);
		_ret = d_alloc(len_server + len_d + 20);
		sprintf(_ret,"%s.%s",_server,d);
		ret = nl_copy_str(std_cm,_ret);
	}
	else {
		*ptr = 0;
		ptr ++;
		len_server = strlen(ptr);
		len_d = strlen(d);
		if ( len_d <= len_server ) {
			ret = ll_copy_str(url);
			goto end2;
		}
		if ( d[len_server] != '.' ) {
			ret = ll_copy_str(url);
			goto end2;
		}
		if ( memcmp(ptr,d,len_server) ) {
			ret = ll_copy_str(url);
			goto end2;
		}
		_ret = d_alloc(strlen(_server) + len_d + 20);
		sprintf(_ret,"%s.%s",_server,d);
		ret = nl_copy_str(std_cm,_ret);
	}
	d_f_ree(_ret);
	if ( u.server )
		d_f_ree(u.server);
	u.server = ret;
	ret = ll_copy_str(get_url_str2(&u));
end2:
	d_f_ree(_server);
end0:
	free_url(&u);
	return ret;
}
