#include "defines.h"
/*     Bed a Binary EDitor for Linux and for Rxvt running under Linux.       */
/*     Copyright (C) 1998  Jaap Korthals Altes <jkaltes@hetnet.nl>      */
/*                                                                           */
/*     Bed is free software; you can redistribute it and/or modify           */
/*     it under the terms of the GNU General Public License as published by  */
/*     the Free Software Foundation; either version 2 of the License, or     */
/*     (at your option) any later version.                                   */
/*                                                                           */
/*     Bed 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.  See the         */
/*     GNU General Public License for more details.                          */
/*                                                                           */
/*     You should have received a copy of the GNU General Public License     */
/*     along with bed; if not, write to the Free Software                    */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
/*                                                                           */
/* Sun Dec  6 18:34:27 1998                                                  */

#include <features.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/param.h>

#include <limits.h>
#include "system.h"
#ifdef __CYGWIN__
#include <windows.h>
#include <sys/cygwin.h>
#endif
#ifdef __CYGWIN__
char *expandfilename(char *filename,const char *name) {
char tmp[MAX_PATH];
char *ptr;
if(name[0]=='\\') {
		strcpy(filename,name);
		return filename;
		}
cygwin_conv_to_win32_path(name,tmp);
return (GetFullPathName(tmp,MAXPATHLEN,filename,&ptr),filename);
}
#else
#ifdef x__CYGWIN__
char *expandfilename(char *filename,const char *name) 
{
	char *ptr;
	return (GetFullPathName(name,MAXPATHLEN,filename,&ptr),filename);
}
#else
#ifndef HAS_GOODREALPATH
char *expandfilename(char *filename,const char *name) {
	char endchar;
	switch(name[0]) {
		case '/': strcpyn(filename,name,MAXPATHLEN);return filename;
		case '~': {
			struct passwd *pas;
			if(name[1]=='/'||!name[1]) {
				uid_t uid=getuid();
				if((pas =getpwuid(uid))) {
					strcpy(filename,pas->pw_dir);
					strcat(filename,name+1);
					break;
					}
				else {
					}
				}
				else {
					char *sl=index(name,'/');
					if(sl) {
						strcpyn(filename,name+1,(sl-name-1));
						filename[(sl-name-1)]='\0';
						pas=getpwnam(filename);
						if(pas) {
							strcpy(filename,pas->pw_dir);
							strcat(filename,sl);
							break;
							}
						}
					else {
						if((pas=getpwnam(name+1))) {
							strcpy(filename,pas->pw_dir);
							break;
							}
						}	
				}
			};
		case '\\': if(name[0]=='\\'&&name[1]=='~')
				   name++;
		default: 
#ifdef __CYGWIN__
{
	char *ptr;
	return (GetFullPathName(name,MAXPATHLEN,filename,&ptr),filename);

}
}
#else
			if(getcwd(filename, MAXPATHLEN)) {
				int len=strlen(filename);
				filename[len]='/';
				strcpy(filename+len+1,name);
				return filename;
				}
			else {
				strcpy(filename,name);
				return filename;
				}
		};
#endif
	return filename;
	}
#endif
#endif
#endif

#ifdef __CYGWIN__
int samefilename(char *one,char *two) {
	return !strcasecmp(one,two);
	}
#else
int samefilename(char *one,char *two) {
	return !strcmp(one,two);
	}
#endif
#ifdef TRY
void main(void) {
	char name[MAXPATHLEN];
	char input[MAXPATHLEN];
	while(1) {
		gets(input);
		 expandfilename(name,input);
		 puts(name);
		 }
	 }
#endif
