/*
 * Copyright (c) 2003, Daniel Bryan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met:
 * - Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * - Neither the name of the author nor the names of its contributors
 *   may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <string.h>

#include "common.h"
#include "path.h"

/*
possibilities:
	root  foo  = root/foo
	root  /foo = root/foo
	root/ foo  = root/foo
	root/ /foo = root/foo
*/
int pathdadd(char *master,char *dir) {
	int end = strlen(master)-1;
	
	if(dir == NULL)
		return 0;
	if(pathchsize(master,dir))
		return 1;
	if(master[end] == '/' && *dir == '/')
		dir++;
	else if(master[end] != '/' && *dir != '/') {
		if(master != NULL)
			strcat(master,"/");
	}
	strcat(master,dir);
	if(master[strlen(master)-1] == '/')
		master[strlen(master)-1] = '\0';
	return 0;
}

/* checks string(s) if they are larger then MAXPATHLEN */
int pathchsize(char *path, char *path2) {
	int i = 0;
	int d = 0;
	
	if(path == path2) {
		i = strlen(path);
		if(i<(MAXPATHLEN - 1))
			return 0;
	} else {
		if(path != NULL)
			i = strlen(path);
		if(path2 != NULL)
			d = strlen(path2);
	
		if((i+d)<(MAXPATHLEN - 1))
			return 0;
	}
	pathlong(path,path2);
	return 1;	
}

/* print 20 characters from the string(s),
   verybigstringfoobarr ... 123456789endofstring */
int pathlong(char *path,char *path2) {
	char buf[21],buf2[21];
	int x, l;
	int i = 0;
	int d = 0;
	
	if(path != NULL) {
		if(path == path2)
			i = d = strlen(path);
		else
			i = strlen(path);
	}
	if(path2 != NULL)
		d = strlen(path2);
	
	if(i<21 && i>0)
		strcpy(buf,path);
	else if(i>0){
		for(x=0;x<20;x++)
			buf[x] = path[x+1];
		buf[20] = '\0';
	}
		
	if(d<21 && d>0)
		strcpy(buf2,path2);
	else if(d>0){
		l = strlen(path2) - 20;
		if(path == path2) {
			for(x=0;x<20;x++)
				buf2[x] = path2[x+l];
			buf2[20] = '\0';
		} else {
			for(x=0;x<20;x++)
				buf2[x] = path[x+l];
			buf2[20] = '\0';
		}		
	}
	
	logadds(LOG_ERR,"%s ... %s: Path too long, not copied",buf,buf2);
	return 1;
}

/* returns the filename as int */
int pathnamed(char *path) {
	char *p;
	p = strrchr(path,'/');
	if(path[strlen(path)-1] == '/') {
		p--;
		p = strrchr(path,'/');
	}
	if(p == NULL)
		return 0;
	return p - path;
}

/* returns a pointer to the filename */
char *pathname(char *path) {
	char *p;
	if(path == NULL)
		return path;
	p = strrchr(path,'/');
	if(p == NULL)
		return path;
	p++;
	return p;
}
