CTRACE(tfp, "HTFTP: Treating as NetPresenz (MACOS) server.\n");
CTRACE((tfp, "HTFTP: Treating as NetPresenz (MACOS) server.\n"));
PRIVATE int get_connection ARGS2(	CONST char *,		arg,	HTParentAnchor *,	anchor){    int status;    char * command = 0;    connection * con;    char * username = NULL;    char * password = NULL;    static BOOLEAN firstuse = TRUE;    if (firstuse) {	/*	**  Set up freeing at exit. - FM	*/#ifdef LY_FIND_LEAKS	atexit(free_FTPGlobals);#endif	firstuse = FALSE;    }    if (control) {	/*	**  Reuse this object - KW, DW & FM	*/	if (control->socket != -1) {	    NETCLOSE(control->socket);	}	con = control;	con->addr = 0;	con->binary = FALSE;    } else {	/*	**  Allocate and init control struct.	*/	con = (connection *)calloc(1, sizeof(connection));	if (con == NULL)	    outofmem(__FILE__, "get_connection");    }    con->socket = -1;    if (!arg) return -1;		/* Bad if no name specified	*/    if (!*arg) return -1;		/* Bad if name had zero length	*//* Get node name:*/    {	char *p1 = HTParse(arg, "", PARSE_HOST);	char *p2 = strrchr(p1, '@');	/* user? */	char * pw = NULL;	if (p2 != NULL) {	    username = p1;	    *p2 = '\0';			/* terminate */	    p1 = p2+1;			/* point to host */	    pw = strchr(username, ':');	    if (pw != NULL) {		*pw++ = '\0';		password = HTUnEscape(pw);	    }	    if (*username)		HTUnEscape(username);	    /*	     *	If the password doesn't exist then we are going to have	     *	to ask the user for it.  The only problem is that we	     *	don't want to ask for it every time, so we will store	     *	away in a primitive fashion.	     */	    if (!password) {		char *tmp = NULL;		HTSprintf0(&tmp, "%s@%s", username, p1);		/*		 *  If the user@host is not equal to the last time through		 *  or user_entered_password has no data then we need		 *  to ask the user for the password.		 */		if (!last_username_and_host ||		    strcmp(tmp, last_username_and_host) ||		    !user_entered_password) {		    StrAllocCopy(last_username_and_host, tmp);		    HTSprintf0(&tmp, gettext("Enter password for user %s@%s:"),				     username, p1);		    FREE(user_entered_password);		    user_entered_password = HTPromptPassword(tmp);		} /* else we already know the password */		password = user_entered_password;		FREE(tmp);	    }	}	if (!username)	    FREE(p1);    } /* scope of p1 */    status = HTDoConnect (arg, "FTP", IPPORT_FTP, (int *)&con->socket);    if (status < 0) {	if (status == HT_INTERRUPTED) {	    CTRACE (tfp, "HTFTP: Interrupted on connect\n");	} else {	    CTRACE(tfp, "HTFTP: Unable to connect to remote host for `%s'.\n",			arg);	}	if (status == HT_INTERRUPTED) {	    _HTProgress (CONNECTION_INTERRUPTED);	    status = HT_NOT_LOADED;	} else {	    HTAlert(gettext("Unable to connect to FTP host."));	}	if (con->socket != -1)	{	  NETCLOSE(con->socket);	}	FREE(username);	if (control == con)	    control = NULL;	FREE(con);	return status;			/* Bad return */    }    CTRACE(tfp, "FTP connected, socket %d  control %ld\n",		con->socket, (long)con);    control = con;		/* Current control connection */    /* Initialise buffering for control connection */    HTInitInput(control->socket);    init_help_message_cache();	/* Clear the login message buffer. *//*	Now we log in		Look up username, prompt for pw.*/    status = response((char *)0);	/* Get greeting */    if (status == HT_INTERRUPTED) {	CTRACE (tfp, "HTFTP: Interrupted at beginning of login.\n");	_HTProgress (CONNECTION_INTERRUPTED);	NETCLOSE(control->socket);	control->socket = -1;	return HT_INTERRUPTED;    }    server_type = GENERIC_SERVER;	/* reset */    if (status == 2) {		/* Send username */	char *cp;		/* look at greeting text */	/* don't gettext() this -- incoming text: */	if (strlen(response_text) > 4) {	    if ((cp = strstr(response_text, " awaits your command")) ||		(cp = strstr(response_text, " ready."))) {		*cp = '\0';	    }	    cp = response_text + 4;	    if (!strncasecomp(cp, "NetPresenz", 10))		server_type = NETPRESENZ_SERVER;	} else {	    cp = response_text;	}	StrAllocCopy(anchor->server, cp);	status = send_cmd_2("USER", (username && *username)			    ? username			    : "anonymous");	if (status == HT_INTERRUPTED) {	    CTRACE (tfp, "HTFTP: Interrupted while sending username.\n");	    _HTProgress (CONNECTION_INTERRUPTED);	    NETCLOSE(control->socket);	    control->socket = -1;	    return HT_INTERRUPTED;	}    }    if (status == 3) {		/* Send password */	if (password) {	    /*	     * We have non-zero length password, so send it. - FM	     */	    HTSprintf0(&command, "PASS %s%c%c", password, CR, LF);	} else {	    /*	     * Create and send a mail address as the password. - FM	     */	    char *user = NULL;	    CONST char *host = NULL;	    char * cp;	    if (personal_mail_address && *personal_mail_address) {		/*		 * We have a non-zero length personal		 * mail address, so use that. - FM		 */		StrAllocCopy(user, personal_mail_address);		if ((cp=strchr(user, '@')) != NULL) {		    *cp++ = '\0';		    host = cp;		} else {		    host = HTHostName();		}	    } else {		/*		 * Use an environment variable and the host global. - FM		 */		if ((cp=getenv("USER")) != NULL)		    StrAllocCopy(user, cp);		else		    StrAllocCopy(user, "WWWuser");		host = HTHostName();	    }	    /*	     * If host is not fully qualified, suppress it	     * as ftp.uu.net prefers a blank to a bad name	     */	    if (!(host) || strchr(host, '.') == NULL)		host = "";	    HTSprintf0(&command, "PASS %s@%s%c%c", user, host, CR, LF);	    FREE(user);	}	status = response(command);	FREE(command);	if (status == HT_INTERRUPTED) {	    CTRACE (tfp,		       "HTFTP: Interrupted while sending password.\n");	    _HTProgress (CONNECTION_INTERRUPTED);	    NETCLOSE(control->socket);	    control->socket = -1;	    return HT_INTERRUPTED;	}    }    FREE(username);    if (status == 3) {	status = send_cmd_1("ACCT noaccount");	if (status == HT_INTERRUPTED) {	    CTRACE (tfp, "HTFTP: Interrupted while sending password.\n");	    _HTProgress (CONNECTION_INTERRUPTED);	    NETCLOSE(control->socket);	    control->socket = -1;	    return HT_INTERRUPTED;	}    }    if (status != 2) {	CTRACE(tfp, "HTFTP: Login fail: %s", response_text);	/* if (control->socket > 0) close_connection(control->socket); */	return -1;		/* Bad return */    }    CTRACE(tfp, "HTFTP: Logged in.\n");    /** Check for host type **/    if (server_type != NETPRESENZ_SERVER)	server_type = GENERIC_SERVER;	/* reset */    use_list = FALSE;			/* reset */    if ((status=response("SYST\r\n")) == 2) {	/* we got a line -- what kind of server are we talking to? */	if (strncmp(response_text+4,		    "UNIX Type: L8 MAC-OS MachTen", 28) == 0) {	    server_type = MACHTEN_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as MachTen server.\n");	} else if (strstr(response_text+4, "UNIX") != NULL ||		   strstr(response_text+4, "Unix") != NULL) {	    server_type = UNIX_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as Unix server.\n");	} else if (strstr(response_text+4, "MSDOS") != NULL) {	    server_type = MSDOS_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as MSDOS (Unix emulation) server.\n");	} else if (strncmp(response_text+4, "VMS", 3) == 0) {	    server_type = VMS_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as VMS server.\n");	} else if ((strncmp(response_text+4, "VM/CMS", 6) == 0) ||		   (strncmp(response_text+4, "VM ", 3) == 0)) {	    server_type = CMS_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as CMS server.\n");	} else if (strncmp(response_text+4, "DCTS", 4) == 0) {	    server_type = DCTS_SERVER;	    CTRACE(tfp, "HTFTP: Treating as DCTS server.\n");	} else if (strstr(response_text+4, "MAC-OS TCP/Connect II") != NULL) {	    server_type = TCPC_SERVER;	    CTRACE(tfp, "HTFTP: Looks like a TCPC server.\n");	    get_ftp_pwd(&server_type, &use_list);	    unsure_type = TRUE;	} else if (server_type == NETPRESENZ_SERVER) { /* already set above */	    use_list = TRUE;	    set_mac_binary(server_type);	    CTRACE(tfp, "HTFTP: Treating as NetPresenz (MACOS) server.\n");	} else if (strncmp(response_text+4, "MACOS Peter's Server", 20) == 0) {	    server_type = PETER_LEWIS_SERVER;	    use_list = TRUE;	    set_mac_binary(server_type);	    CTRACE(tfp, "HTFTP: Treating as Peter Lewis (MACOS) server.\n");	} else if (strncmp(response_text+4, "Windows_NT", 10) == 0) {	    server_type = WINDOWS_NT_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as Window_NT server.\n");	} else if (strncmp(response_text+4, "MS Windows", 10) == 0) {	    server_type = MS_WINDOWS_SERVER;	    use_list = TRUE;	    CTRACE(tfp, "HTFTP: Treating as MS Windows server.\n");	} else if (strncmp(response_text+4,			   "MACOS AppleShare IP FTP Server", 30) == 0) {	    server_type = APPLESHARE_SERVER;	    use_list = TRUE;	    set_mac_binary(server_type);	    CTRACE(tfp, "HTFTP: Treating as AppleShare server.\n");	} else	{	    server_type = GENERIC_SERVER;	    CTRACE(tfp, "HTFTP: Ugh!  A Generic server.\n");	    get_ftp_pwd(&server_type, &use_list);	    unsure_type = TRUE;	 }    } else {	/* SYST fails :(  try to get the type from the PWD command */	 get_ftp_pwd(&server_type, &use_list);    }/*  Now we inform the server of the port number we will listen on*/#ifdef NOTREPEAT_PORT    {	int status = response(port_command);	if (status != 2) {	    if (control->socket)		close_connection(control->socket);	    return -status;		/* Bad return */	}	CTRACE(tfp, "HTFTP: Port defined.\n");    }#endif /* NOTREPEAT_PORT */    return con->socket;			/* Good return */}