void cmd_status(int argc, char **argv) { OPT_HELP_NEW(_("Show status."), "status [options]", NULL); maxargs(optind - 1); if(ftp_connected()) printf(_("connected to '%s'\n"), host_getoname(ftp->host)); else puts(_("not connected")); if(ftp_loggedin()) printf(_("logged in as '%s'\n"), ftp->url->username); #ifdef HAVE_KERBEROS if(ftp_connected()) sec_status(); #endif if(list_numitem(gvFtpList) > 1 || ftp_connected()) printf(_("There are totally %u connections open\n"), list_numitem(gvFtpList)); }
int ftp_open_url(url_t *urlp, bool reset_vars) { bool use_proxy; int i; if(reset_vars) ftp_reset_vars(); /* don't assume server is in ascii mode initially even if RFC says so */ ftp->prev_type = '?'; #ifdef HAVE_POSIX_SIGSETJMP if(sigsetjmp(open_timeout_jmp, 1)) #else if(setjmp(open_timeout_jmp)) #endif { ftp_close(); ftp_err(_("Connection timeout after %u seconds\n"), ftp->open_timeout); return 1; } ftp_set_signal(SIGALRM, ftp_open_handler); alarm(ftp->open_timeout); use_proxy = (proxy_type(urlp) != 0); ftp_err(_("Looking up %s... "), use_proxy ? gvProxyUrl->hostname : urlp->hostname); /* Set the default port (22) for SSH if no port is specified. We * need to do this here, 'cause host_lookup() sets it to 21 * (default port for vanilla FTP) */ if(urlp->protocol) { if(strcmp(urlp->protocol, "sftp") == 0) url_setprotocol(urlp, "ssh"); if(strcmp(urlp->protocol, "ssh") == 0 && urlp->port == -1) urlp->port = 22; /* default SSH port */ } ftp->host = host_create(use_proxy ? gvProxyUrl : urlp); if(!host_lookup(ftp->host)) { herror(host_getname(ftp->host)); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } /* keep the value in urlp->port urlp->port = ntohs(ftp->host->port); and set it to 21 if it is -1 */ if(urlp->port == -1) { urlp->port = 21; } fprintf(stderr, "\r "); i = strlen(use_proxy ? gvProxyUrl->hostname : urlp->hostname); while(i--) fprintf(stderr, " "); fprintf(stderr, "\r"); ftp_trace("\n"); #ifdef HAVE_LIBSSH if(urlp->protocol && strcmp(urlp->protocol, "ssh") == 0) { int ret = ssh_open_url(urlp); alarm(0); return ret; } #endif if(urlp->protocol && strcmp(urlp->protocol, "ftp") != 0) { ftp_err(_("Sorry, don't know how to handle your '%s' protocol\n" "trying 'ftp' instead...\n"), urlp->protocol); url_setprotocol(urlp, 0); } if(use_proxy) { ftp_err(_("Connecting to proxy %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } else { ftp_err(_("Connecting to %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } ftp->ctrl = sock_create(); if (ftp->ctrl == 0) { ftp_err(_("Unable to create socket.\n")); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } if(!sock_connect_host(ftp->ctrl, ftp->host)) { alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } sock_lowdelay(ftp->ctrl); char* ip = host_getip(ftp->host); ftp_err(_("Connected to %s ([%s]:%d).\n"), host_getoname(ftp->host), ip, urlp->port); free(ip); /* read startup message from server */ ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); if(ftp->fullcode == 120) { ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); } alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); if(!sock_connected(ftp->ctrl)) { ftp_close(); return 1; } ftp->connected = (ftp->fullcode == 220); if(ftp->connected) { void (*tracefunq)(const char *fmt, ...); url_destroy(ftp->url); ftp->url = url_clone(urlp); tracefunq = (ftp->verbosity == vbDebug ? ftp_err : ftp_trace); char* remote_addr = printable_address(sock_remote_addr(ftp->ctrl)), *local_addr = printable_address(sock_local_addr(ftp->ctrl)); tracefunq("remote address: %s\n", remote_addr); tracefunq("local address: %s\n", local_addr); free(remote_addr); free(local_addr); return 0; } else { ftp_close(); return 1; } }
char *expand_prompt(const char *fmt) { unsigned maxlen; char *ins, *e; bool freeins; char *tmp; if(!fmt) return 0; char* prompt = (char *)xmalloc(strlen(fmt)+1); char* cp = prompt; while(fmt && *fmt) { if(*fmt == '%') { fmt++; ins = 0; freeins = false; maxlen = (unsigned)-1; if(isdigit((int)*fmt)) { maxlen = (unsigned)atoi(fmt); while(isdigit((int)*fmt)) fmt++; } switch(*fmt) { case 'c': if (asprintf(&ins, "%u", list_numitem(gvFtpList)) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); free(prompt); return NULL; } freeins = true; break; case 'C': if (asprintf(&ins, "%u", connection_number()) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); free(prompt); return NULL; } freeins = true; break; case 'u': /* username */ ins = ftp_loggedin() ? ftp->url->username : "******"; break; case 'h': /* remote hostname (as passed to cmd_open()) */ ins = ftp_connected() ? ftp->url->hostname : "?"; break; case 'H': /* %h up to first '.' */ if(!ftp_connected()) { ins = "?"; break; } e = strchr(ftp->url->hostname, '.'); if(e) { ins = xstrndup(ftp->url->hostname, e - ftp->url->hostname); freeins = true; } else ins = ftp->url->hostname; break; case 'm': /* remote machine name (as returned from gethostbynmame) */ ins = xstrdup(ftp_connected() ? host_getoname(ftp->host) : "?"); freeins = true; break; case 'M': /* %m up to first '.' */ if(!ftp_connected()) { ins = "?"; break; } e = strchr(host_getoname(ftp->host), '.'); if(e) { ins = xstrndup(host_getoname(ftp->host), e - host_getoname(ftp->host)); } else ins = xstrdup(host_getoname(ftp->host)); freeins = true; break; case 'n': /* remote ip number */ ins = xstrdup(ftp_connected() ? host_getoname(ftp->host) : "?"); freeins = true; break; case 'w': /* current remote directory */ if(!ftp_loggedin()) { ins = "?"; break; } ins = shortpath(ftp->curdir, maxlen, 0); freeins = true; break; case 'W': /* basename(%w) */ if(!ftp_loggedin()) { ins = "?"; break; } ins = (char *)base_name_ptr(ftp->curdir); break; case '~': /* %w but homedir replaced with '~' */ if(!ftp_loggedin()) { ins = "?"; break; } ins = shortpath(ftp->curdir, maxlen, ftp->homedir); freeins = true; break; case 'l': /* current local directory */ tmp = getcwd(NULL, 0); ins = shortpath(tmp, maxlen, 0); freeins = true; free(tmp); break; case 'L': /* basename(%l) */ tmp = getcwd(NULL, 0); ins = (char *)base_name_ptr(tmp); free(tmp); break; case '%': /* percent sign */ ins = "%"; break; case '#': /* local user == root ? '#' : '$' */ ins = getuid() == 0 ? "#" : "$"; break; case '{': /* begin non-printable character string */ #ifdef HAVE_LIBREADLINE ins = "\001\001"; /* \001 + RL_PROMPT_START_IGNORE */ #endif break; case '}': /* end non-printable character string */ #ifdef HAVE_LIBREADLINE ins = "\001\002"; /* \001 + RL_PROMPT_END_IGNORE */ #endif break; case 'e': /* escape (0x1B) */ ins = "\x1B"; break; default: /* illegal format specifier */ break; } if(ins) { const size_t len = strlen(prompt) + strlen(ins) + strlen(fmt+1) + 1; char* tmp = xmalloc(len); strlcpy(tmp, prompt, len); strlcat(tmp, ins, len); cp = tmp + strlen(prompt) + strlen(ins); free(prompt); prompt = tmp; if(freeins) free(ins); } } else *cp++ = *fmt; fmt++; } unquote_escapes(prompt); return prompt; }