/*======================================================================
                    U T I L S . C 
                    doc: Mon Mar 23 11:42:27 1992
                    dlm: Fri Jul 23 16:05:28 1993
                    (c) 1992 ant@ips.id.ethz.ch
                    uE-Info: 19 0 T 0 0 72 2 2 8 ofnI
======================================================================*/

#include	<stdio.h>
#include	<pwd.h>
#include	<string.h>
#include	<sys/types.h>
#include	<sys/param.h>
#include	<rpc/rpc.h>
#include	"inetray.h"
#include	"config.h"
#include	"common.h"

void VersionPrint() {}			/* the raylibs need that */
	
void stripHome(uid,path)		/* home -> ~ */
uid_t uid; char *path;
{
	int	i = 0,t = 0;
	char	cwd[MAXPATHLEN],home[MAXPATHLEN];
	struct passwd *p;

	if (path[0] != '/')		/* errmes from getwd */
		return;
	getcwd(cwd,MAXPATHLEN);		/* save current wdir */
	p = getpwuid(geteuid());	/* get real home dir */
	if (p == NULL) return;
	if (chdir(p->pw_dir) < 0)	/* can't access home */ 
		strncpy(home,cwd,MAXPATHLEN);
	else 				/* real name of home */
		getcwd(home,MAXPATHLEN);/* automount ... */
	if (chdir(cwd) < 0) 		/* shouldn't happen at all! */
		getcwd(path,MAXPATHLEN);

	while ((home[i] != '\0') &&	/* strip out home */
	       (path[i] == home[i])) i++;
	if (home[i] != '\0') return;

	path[t++] = '~';		/* add tilde */
	while ((path[t++] = path[i++]) != '\0') ; /* copy rest */
}

void stripHead(head,path)		/* for AutoMount ... */
char *path,*head;
{
	int	t = 0, s = 0;

	while ((head[s] != '\0') &&
               (path[s] != '\0') &&
	       (path[s] == head[s])) s++;
	if ((s == 0) || (head[s] != '\0')) return;
	while ((path[t++] = path[s++]) != '\0') ;
	if (chdir(path) < 0) getcwd(path,MAXPATHLEN);
}

void addHome(uid,pIn,pOut)		/* ~ -> home */
uid_t uid; char *pIn,*pOut;
{
	int	s = 0,t = 0,i = 0;
	struct passwd *p;

	if (pIn[s] == '~') {		/* do subst */
		s++;
		p = getpwuid(geteuid());
		if (p == NULL) 
			pOut[t++] = '.';
		else
			while (p->pw_dir[i] != '\0')
				pOut[t++] = p->pw_dir[i++];
	}
	while ((pOut[t++] = pIn[s++]) != '\0') ;
}
		
