FILE *fptemp = NULL;
PUBLIC void LYConvertToURL ARGS2(	char **,	AllocatedString,	int,		fixit){    char *old_string = *AllocatedString;    char *temp = NULL;    char *cp = NULL;#ifndef VMS    struct stat st;    FILE *fptemp = NULL;#endif /* !VMS */    if (!old_string || *old_string == '\0')	return;#if defined(DOSPATH) || defined(__EMX__)    {	 char *cp_url = *AllocatedString;	 for(; *cp_url != '\0'; cp_url++)		if(*cp_url == '\\') *cp_url = '/';	 cp_url--;	 if(*cp_url == ':')		 StrAllocCat(*AllocatedString,"/");    }#endif /* DOSPATH */    *AllocatedString = NULL;  /* so StrAllocCopy doesn't free it */    StrAllocCopy(*AllocatedString,"file://localhost");    if (*old_string != '/') {	char *fragment = NULL;#if defined(DOSPATH) || defined(__EMX__)	StrAllocCat(*AllocatedString,"/");#endif /* DOSPATH */#ifdef VMS	/*	 *  Not a SHELL pathspec.  Get the full VMS spec and convert it.	 */	char *cur_dir = NULL;	static char url_file[LY_MAXPATH], file_name[LY_MAXPATH], dir_name[LY_MAXPATH];	unsigned long context = 0;	$DESCRIPTOR(url_file_dsc, url_file);	$DESCRIPTOR(file_name_dsc, file_name);	if (*old_string == '~') {	    /*	     *	On VMS, we'll accept '~' on the command line as	     *	Home_Dir(), and assume the rest of the path, if	     *	any, has SHELL syntax.	     */	    StrAllocCat(*AllocatedString, HTVMS_wwwName((char *)Home_Dir()));	    if ((cp = strchr(old_string, '/')) != NULL) {		/*		 *  Append rest of path, if present, skipping "user" if		 *  "~user" was entered, simplifying, and eliminating		 *  any residual relative elements. - FM		 */		StrAllocCopy(temp, cp);		LYTrimRelFromAbsPath(temp);		StrAllocCat(*AllocatedString, temp);		FREE(temp);	    }	    goto have_VMS_URL;	} else {	    if ((fragment = strchr(old_string, '#')) != NULL)		*fragment = '\0';	    strcpy(url_file, old_string);	}	url_file_dsc.dsc$w_length = (short) strlen(url_file);	if (1&lib$find_file(&url_file_dsc, &file_name_dsc, &context,			    0, 0, 0, 0)) {	    /*	     *	We found the file.  Convert to a URL pathspec.	     */	    if ((cp = strchr(file_name, ';')) != NULL) {		*cp = '\0';	    }	    LYLowerCase(file_name);	    StrAllocCat(*AllocatedString, HTVMS_wwwName(file_name));	    if ((cp = strchr(old_string, ';')) != NULL) {		StrAllocCat(*AllocatedString, cp);	    }	    if (fragment != NULL) {		*fragment = '#';		StrAllocCat(*AllocatedString, fragment);		fragment = NULL;	    }	} else if ((NULL != getcwd(dir_name, sizeof(dir_name)-1, 0)) &&		   0 == chdir(old_string)) {	    /*	     * Probably a directory.  Try converting that.	     */	    StrAllocCopy(cur_dir, dir_name);	    if (fragment != NULL) {		*fragment = '#';	    }	    if (NULL != getcwd(dir_name, sizeof(dir_name)-1, 0)) {		/*		 * Yup, we got it!		 */		LYLowerCase(dir_name);		StrAllocCat(*AllocatedString, dir_name);		if (fragment != NULL) {		    StrAllocCat(*AllocatedString, fragment);		    fragment = NULL;		}	    } else {		/*		 *  Nope.  Assume it's an http URL with		 *  the "http://" defaulted, if we can't		 *  rule out a bad VMS path.		 */		fragment = NULL;		if (strchr(old_string, '[') ||		    ((cp = strchr(old_string, ':')) != NULL &&		     !isdigit((unsigned char)cp[1])) ||		    !LYExpandHostForURL((char **)&old_string,					URLDomainPrefixes,					URLDomainSuffixes)) {		    /*		     *	Probably a bad VMS path (but can't be		     *	sure).	Use original pathspec for the		     *	error message that will result.		     */		    strcpy(url_file, "/");		    strcat(url_file, old_string);		    CTRACE(tfp, "Can't find '%s'  Will assume it's a bad path.\n",				old_string);		    StrAllocCat(*AllocatedString, url_file);		} else {		    /*		     *	Assume a URL is wanted, so guess the		     *	scheme with "http://" as the default. - FM		     */		    if (!LYAddSchemeForURL((char **)&old_string, "http://")) {			StrAllocCopy(*AllocatedString, "http://");			StrAllocCat(*AllocatedString, old_string);		    } else {			StrAllocCopy(*AllocatedString, old_string);		    }		}	    }	} else {	    /*	     *	Nothing found.	Assume it's an http URL	     *	with the "http://" defaulted, if we can't	     *	rule out a bad VMS path.	     */	    if (fragment != NULL) {		*fragment = '#';		fragment = NULL;	    }	    if (strchr(old_string, '[') ||		((cp = strchr(old_string, ':')) != NULL &&		 !isdigit((unsigned char)cp[1])) ||		!LYExpandHostForURL((char **)&old_string,				    URLDomainPrefixes,				    URLDomainSuffixes)) {		/*		 *  Probably a bad VMS path (but can't be		 *  sure).  Use original pathspec for the		 *  error message that will result.		 */		strcpy(url_file, "/");		strcat(url_file, old_string);		CTRACE(tfp, "Can't find '%s'  Will assume it's a bad path.\n",			    old_string);		StrAllocCat(*AllocatedString, url_file);	    } else {		/*		 *  Assume a URL is wanted, so guess the		 *  scheme with "http://" as the default. - FM		 */		if (!LYAddSchemeForURL((char **)&old_string, "http://")) {		    StrAllocCopy(*AllocatedString, "http://");		    StrAllocCat(*AllocatedString, old_string);		} else {		    StrAllocCopy(*AllocatedString, old_string);		}	    }	}	lib$find_file_end(&context);	FREE(cur_dir);have_VMS_URL:	CTRACE(tfp, "Trying: '%s'\n", *AllocatedString);#else /* Unix: */#ifdef DOSPATH	if (strlen(old_string) == 1 && *old_string == '.') {	    /*	     *	They want .	     */	    char curdir[LY_MAXPATH];	    StrAllocCopy(temp, wwwName(Current_Dir(curdir)));	    StrAllocCat(*AllocatedString, temp);	    FREE(temp);	    CTRACE(tfp, "Converted '%s' to '%s'\n",			old_string, *AllocatedString);	} else#endif /* DOSPATH */	if (*old_string == '~') {	    /*	     *	On Unix, convert '~' to Home_Dir().	     */	    StrAllocCat(*AllocatedString, Home_Dir());	    if ((cp = strchr(old_string, '/')) != NULL) {		/*		 *  Append rest of path, if present, skipping "user" if		 *  "~user" was entered, simplifying, and eliminating		 *  any residual relative elements. - FM		 */		StrAllocCopy(temp, cp);		LYTrimRelFromAbsPath(temp);		StrAllocCat(*AllocatedString, temp);		FREE(temp);	    }	    CTRACE(tfp, "Converted '%s' to '%s'\n",			old_string, *AllocatedString);	} else {	    /*	     *	Create a full path to the current default directory.	     */	    char curdir[LY_MAXPATH];	    char *temp2 = NULL;	    BOOL is_local = FALSE;	    Current_Dir (curdir);	    /*	     *	Concatenate and simplify, trimming any	     *	residual relative elements. - FM	     */#if defined (DOSPATH) || defined (__EMX__)	    if (old_string[1] != ':' && old_string[1] != '|') {		StrAllocCopy(temp, wwwName(curdir));		LYAddHtmlSep(&temp);		LYstrncpy(curdir, temp, (sizeof(curdir) - 1));		StrAllocCat(temp, old_string);	    } else {		curdir[0] = '\0';		StrAllocCopy(temp, old_string);	    }#else	    StrAllocCopy(temp, curdir);	    StrAllocCat(temp, "/");	    StrAllocCat(temp, old_string);#endif /* DOSPATH */	    LYTrimRelFromAbsPath(temp);	    CTRACE(tfp, "Converted '%s' to '%s'\n", old_string, temp);	    if ((stat(temp, &st) > -1) ||		(fptemp = fopen(temp, "r")) != NULL) {		/*		 *  It is a subdirectory or file on the local system.		 */#if defined (DOSPATH) || defined (__EMX__)		/* Don't want to see DOS local paths like c: escaped  */		/* especially when we really have file://localhost/   */		/* at the beginning.  To avoid any confusion we allow */		/* escaping the path if URL specials % or # present.  */		if (strchr(temp, '#') == NULL &&			   strchr(temp, '%') == NULL)		StrAllocCopy(cp, temp);		else#endif /* DOSPATH */		cp = HTEscape(temp, URL_PATH);		StrAllocCat(*AllocatedString, cp);		FREE(cp);		CTRACE(tfp, "Converted '%s' to '%s'\n",			    old_string, *AllocatedString);		is_local = TRUE;	    } else {		char *cp2 = NULL;		StrAllocCopy(temp2, curdir);		LYAddPathSep(&temp2);		StrAllocCopy(cp, old_string);		if ((fragment = strchr(cp, '#')) != NULL)		    *fragment = '\0';	/* keep as pointer into cp string */		HTUnEscape(cp);   /* unescape given path without fragment */		StrAllocCat(temp2, cp);		/* append to current dir  */		StrAllocCopy(cp2, temp2);	/* keep a copy in cp2	  */		LYTrimRelFromAbsPath(temp2);		if (strcmp(temp2, temp) != 0 &&		    ((stat(temp2, &st) > -1) ||		     (fptemp = fopen(temp2, "r")) != NULL)) {		    /*		     *	It is a subdirectory or file on the local system		     *	with escaped characters and/or a fragment to be		     *	appended to the URL. - FM		     */		    FREE(temp);		    if (strcmp(cp2, temp2) == 0) {			/*			 *  LYTrimRelFromAbsPath did nothing, use			 *  old_string as given. - kw			 */			temp = HTEscape(curdir, URL_PATH);			LYAddHtmlSep(&temp);			StrAllocCat(temp, old_string);		    } else {			temp = HTEscape(temp2, URL_PATH);			if (fragment != NULL) {			    *fragment = '#';			    StrAllocCat(temp, fragment);			}		    }		    StrAllocCat(*AllocatedString, temp);		    CTRACE(tfp, "Converted '%s' to '%s'\n",				old_string, *AllocatedString);		    is_local = TRUE;		} else if (strchr(curdir, '#') != NULL ||			   strchr(curdir, '%') != NULL) {		    /*		     *	If PWD has some unusual characters, construct a		     *	filename in temp where those are escaped.  This		     *	is mostly to prevent this function from returning		     *	with some weird URL if the LYExpandHostForURL tests		     *	further down fail. - kw		     */		    FREE(temp);		    if (strcmp(cp2, temp2) == 0) {			/*			 *  LYTrimRelFromAbsPath did nothing, use			 *  old_string as given. - kw			 */			temp = HTEscape(curdir, URL_PATH);			LYAddHtmlSep(&temp);			StrAllocCat(temp, old_string);		    } else {			temp = HTEscape(temp2, URL_PATH);			if (fragment != NULL) {			    *fragment = '#';			    StrAllocCat(temp, fragment);			}		    }		}		FREE(cp);		FREE(cp2);	    }	    if (is_local == FALSE) {		/*		 *  It's not an accessible subdirectory or file on the		 *  local system, so assume it's a URL request and guess		 *  the scheme with "http://" as the default.		 */		CTRACE(tfp, "Can't stat() or fopen() '%s'\n",			    temp2 ? temp2 : temp);		if (LYExpandHostForURL((char **)&old_string,				       URLDomainPrefixes,				       URLDomainSuffixes)) {		    if (!LYAddSchemeForURL((char **)&old_string, "http://")) {			StrAllocCopy(*AllocatedString, "http://");			StrAllocCat(*AllocatedString, old_string);		    } else {			StrAllocCopy(*AllocatedString, old_string);		    }		} else if (fixit) {		  /* RW 1998Mar16  Restore AllocatedString to 'old_string' */		    StrAllocCopy(*AllocatedString, old_string);		}		CTRACE(tfp, "Trying: '%s'\n", *AllocatedString);	    }	    FREE(temp);	    FREE(temp2);	    if (fptemp) {		fclose(fptemp);		fptemp = NULL;	    }	}#endif /* VMS */    } else {	/*	 *  Path begins with a slash.  Simplify and use it.	 */	if (old_string[1] == '\0') {	    /*	     *	Request for root.  Respect it on Unix, but	     *	on VMS we treat that as a listing of the	     *	login directory. - FM	     */#ifdef VMS	    StrAllocCat(*AllocatedString, HTVMS_wwwName((char *)Home_Dir()));#else	    StrAllocCat(*AllocatedString, "/");	} else if ((stat(old_string, &st) > -1) ||		   (fptemp = fopen(old_string, "r")) != NULL) {	    /*	     *	It is an absolute directory or file	     *	on the local system. - KW	     */	    StrAllocCopy(temp, old_string);	    LYTrimRelFromAbsPath(temp);	    CTRACE(tfp, "Converted '%s' to '%s'\n", old_string, temp);	    cp = HTEscape(temp, URL_PATH);	    StrAllocCat(*AllocatedString, cp);	    FREE(cp);	    FREE(temp);	    if (fptemp) {		fclose(fptemp);		fptemp = NULL;	    }	    CTRACE(tfp, "Converted '%s' to '%s'\n",			old_string, *AllocatedString);#endif /* VMS */	} else if (old_string[1] == '~') {	    /*	     *	Has a Home_Dir() reference.  Handle it	     *	as if there weren't a lead slash. - FM	     */#ifdef VMS	    StrAllocCat(*AllocatedString, HTVMS_wwwName((char *)Home_Dir()));#else	    StrAllocCat(*AllocatedString, Home_Dir());#endif /* VMS */	    if ((cp = strchr((old_string + 1), '/')) != NULL) {		/*		 *  Append rest of path, if present, skipping "user" if		 *  "~user" was entered, simplifying, and eliminating		 *  any residual relative elements. - FM		 */		StrAllocCopy(temp, cp);		LYTrimRelFromAbsPath(temp);		StrAllocCat(*AllocatedString, temp);		FREE(temp);	    }	} else {	    /*	     *	Normal absolute path.  Simplify, trim any	     *	residual relative elements, and append it. - FM	     */	    StrAllocCopy(temp, old_string);	    LYTrimRelFromAbsPath(temp);	    StrAllocCat(*AllocatedString, temp);	    FREE(temp);	}	CTRACE(tfp, "Converted '%s' to '%s'\n",		    old_string, *AllocatedString);    }    FREE(old_string);    /* Pause so we can read the messages before invoking curses */    CTRACE_SLEEP(AlertSecs);}