Пример #1
0
void cmd_cat(int argc, char **argv)
{
	int i;
	struct option longopts[] = {
		{"type", required_argument, 0, 't'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};
	int c;
	transfer_mode_t mode = tmAscii;

	optind = 0;
	while((c = getopt_long(argc, argv, "t:h", longopts, 0)) != EOF) {
		switch(c) {
		case 't':
			if(strncmp(optarg, "ascii", strlen(optarg)) == 0)
				mode = tmAscii;
			else if(strncmp(optarg, "binary", strlen(optarg)) == 0)
				mode = tmBinary;
			else {
				fprintf(stderr,
						_("Invalid option argument --type=%s\n"), optarg);
				return;
			}
			break;
		case 'h':
			fprintf(stderr, "Print file(s) on standard output.  Usage:\n"
					"  cat [options] <file>...\n"
					"Options:\n"
					"  -t, --type=TYPE    set transfer TYPE to ascii"
					" or binary\n"
					"  -h, --help         show this help\n");
			return;
		case '?':
			optind = -1;
			return;
		}
	}

	minargs(optind);
	need_connected();
	need_loggedin();

	for(i = optind; i < argc; i++) {
		listitem *gli;
		list *gl = rglob_create();
		stripslash(argv[i]);
		if(rglob_glob(gl, argv[i], true, false, 0) == -1)
			fprintf(stderr, _("%s: no matches found\n"), argv[i]);
		for(gli = gl->first; gli; gli=gli->next) {
			rfile *rf = (rfile *)gli->data;
			const char *fn = base_name_ptr(rf->path);
			if(strcmp(fn, ".") != 0 && strcmp(fn, "..") != 0) {
				ftp_receive(rf->path, stdout, mode, 0);
				fflush(stdout);
			}
		}
		rglob_destroy(gl);
	}
}
Пример #2
0
void cmd_cd(int argc, char **argv)
{
	char *e;
#if 0
	OPT_HELP("Change working directory.  Usage:\n"
			 "  cd [options] [directory]\n"
			 "Options:\n"
			 "  -h, --help    show this help\n"
			 "if [directory] is '-', cd changes to the previous working"
			 " directory\n"
			 "if omitted, changes to home directory\n");

	maxargs(optind);
#endif

	maxargs_nohelp(1);
	need_connected();
	need_loggedin();

	if(argc <= 1)
		e = ftp->homedir;
	else if(strcmp(argv[1], "-") == 0) {
		e = ftp->prevdir;
		if(e == 0) /* no previous directory */
			return;
	} else
		e = argv[1];
	e = tilde_expand_home(e, ftp->homedir);
	ftp_chdir(e);
	free(e);
}
Пример #3
0
void cmd_chmod(int argc, char **argv)
{
	int i;
	list *gl;
	listitem *li;

	OPT_HELP("Change permissions on remote file.  Usage:\n"
			 "  chmod [options] <mode> <file>\n"
			 "Options:\n"
			 "  -h, --help    show this help\n"
			 "<mode> is the permission mode, in octal (ex 644)\n");

	minargs(optind + 1);
	need_connected();
	need_loggedin();

	gl = rglob_create();
	for(i=optind+1; i<argc; i++) {
		stripslash(argv[i]);
		rglob_glob(gl, argv[i], true, true, NODOTDIRS);
	}

	for(li=gl->first; li; li=li->next) {
		if(ftp_chmod(((rfile *)li->data)->path, argv[optind]) != 0)
			printf("%s: %s\n", ((rfile *)li->data)->path, ftp_getreply(false));
	}
	rglob_destroy(gl);
}
Пример #4
0
void cmd_url(int argc, char **argv)
{
	int c;
	struct option longopts[] = {
		{"no-encoding", no_argument, 0, 'e'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};
	bool no_encoding = false;

	optind = 0;
	while((c = getopt_long(argc, argv, "eh", longopts, 0)) != EOF) {
		switch(c) {
		  case 'h':
        show_help(_("Print the current URL."), "url [options]",
            _("  -e, --no-encoding    don't encode URL as RFC1738 says\n"));
			return;
		  case 'e':
			no_encoding = true;
			break;
		  case '?':
			return;
		}
	}

	maxargs(optind - 1);
	need_connected();
	need_loggedin();

	printf("ftp://");

	if(no_encoding) {
		printf("%s@%s", ftp->url->username, ftp->url->hostname);
		if(ftp->url->port != 21)
			printf(":%d", ftp->url->port);
		printf("/%s\n", ftp->curdir);
		return;
	}

	if(!url_isanon(ftp->url)) {
		char *e = encode_url_username(ftp->url->username);
		printf("%s@", e);
		free(e);
	}
	printf("%s", ftp->url->hostname);
	if(ftp->url->port != 21)
		printf(":%u", ftp->url->port);
	if(strcmp(ftp->curdir, ftp->homedir) != 0) {
		char *d;
		char *e = ftp->curdir;
#if 0
		if(strncmp(ftp->curdir, ftp->homedir, strlen(ftp->homedir)) == 0)
			e = ftp->curdir + strlen(ftp->homedir) + 1;
#endif
		d = encode_url_directory(e);
		printf("/%s", d);
		free(d);
	}
	printf("\n");
}
Пример #5
0
void cmd_cdup(int argc, char **argv)
{
	OPT_HELP_NEW(_("Change to parent working directory."), "cdup [options]", NULL);

  maxargs(optind - 1);
	need_connected();
	need_loggedin();
	ftp_cdup();
}
Пример #6
0
void cmd_pwd(int argc, char **argv)
{
  OPT_HELP_NEW(_("Print the current working directory."), "pwd [options]", NULL);

	maxargs(optind - 1);
	need_connected();
	need_loggedin();

	ftp_pwd();
}
Пример #7
0
void cmd_cache(int argc, char **argv)
{
	int c;
	struct option longopts[] = {
		{"clear", no_argument, 0, 'c'},
		{"list", no_argument, 0, 'l'},
		{"touch", no_argument, 0, 't'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};
	bool touch = false;

	optind = 0;
	while((c = getopt_long(argc, argv, "clt::h", longopts, 0)) != EOF) {
		switch(c) {
		  case 'c':
			ftp_cache_clear();
			return;
		  case 'l':
			ftp_cache_list_contents();
			return;
		  case 't':
			  touch = true;
			  break;
		  case 'h':
			printf(_("Control the directory cache.  Usage:\n"
					 "  cache [option] [directories]\n"
					 "Options:\n"
					 "  -c, --clear        clear whole directory cache\n"
					 "  -l, --list         list contents of cache\n"
					 "  -t, --touch        remove directories from cache\n"
					 "                     if none given, remove current"
					 " directory\n"
					 "  -h, --help         show this help\n"));
			return;
		  case '?':
			return;
		}
	}

	need_connected();
	need_loggedin();

	if(touch) {
		if(optind < argc) {
			int i;
			for(i = optind; i < argc; i++)
				ftp_cache_flush_mark(argv[i]);
		} else
			ftp_cache_flush_mark(ftp->curdir);
	} else {
		minargs(1);
		maxargs(0);
	}
}
Пример #8
0
void cmd_cdup(int argc, char **argv)
{
	OPT_HELP("Change to parent working directory.  Usage:\n"
			 "  cdup [options]\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");
	maxargs(optind - 1);
	need_connected();
	need_loggedin();
	ftp_cdup();
}
Пример #9
0
void cmd_rmdir(int argc, char **argv)
{
	int i;
	OPT_HELP_NEW(_("Remove directory."), "rmdir [options]", NULL);
	minargs(optind);
	need_connected();
	need_loggedin();

	for(i=optind; i<argc; i++)
		ftp_rmdir(argv[i]);
}
Пример #10
0
void cmd_pwd(int argc, char **argv)
{
	OPT_HELP("Print the current working directory.  Usage:\n"
			 "  pwd [options]\n"
			 "Options:\n"
			 "  -h, --help     show this help\n");

	maxargs(optind - 1);
	need_connected();
	need_loggedin();

	ftp_pwd();
}
Пример #11
0
void cmd_mv(int argc, char **argv)
{
	OPT_HELP_NEW(_("Rename or move a file."), "mv [options] <src> <dest>", NULL);

	minargs(optind + 1);
	maxargs(optind + 1);
	need_connected();
	need_loggedin();

	ftp_set_tmp_verbosity(vbError);
	if(ftp_rename(argv[optind], argv[optind + 1]) == 0)
		printf("%s -> %s\n", argv[optind], argv[optind + 1]);
}
Пример #12
0
void cmd_idle(int argc, char **argv)
{
	OPT_HELP_NEW(_("Get or set idle timeout."), "idle [options] [timeout]",
	  _("Without the timeout option, print the current idle timeout\n"));
	maxargs(optind);
	need_connected();
	need_loggedin();

	if(argc - 1 == optind)
		ftp_idle(argv[optind]);
	else
		ftp_idle(0);
}
Пример #13
0
void cmd_rmdir(int argc, char **argv)
{
	int i;
	OPT_HELP("Remove directory.  Usage:\n"
			 "  rmdir [options]\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");
	minargs(optind);
	need_connected();
	need_loggedin();

	for(i=optind; i<argc; i++)
		ftp_rmdir(argv[i]);
}
Пример #14
0
void cmd_mv(int argc, char **argv)
{
	OPT_HELP("Rename or move a file.  Usage:\n"
			 "  mv [options] <src> <dest>\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");

	minargs(optind + 1);
	maxargs(optind + 1);
	need_connected();
	need_loggedin();

	ftp_set_tmp_verbosity(vbError);
	if(ftp_rename(argv[optind], argv[optind + 1]) == 0)
		printf("%s -> %s\n", argv[optind], argv[optind + 1]);
}
Пример #15
0
void cmd_nlist(int argc, char **argv)
{
	OPT_HELP_NEW(_("Simple file list."), "nlist [options] [file]", NULL);
	need_connected();
	need_loggedin();

	if(argc == optind + 1)
		ftp_list("NLST", 0, stdout);
	else {
		char *args;

		args = args_cat(argc, argv, optind);
		ftp_list("NLST", args, stdout);
		free(args);
	}
}
Пример #16
0
void cmd_idle(int argc, char **argv)
{
	OPT_HELP("Get or set idle timeout.  Usage:\n"
			 "  idle [options] [timeout]\n"
			 "Options:\n"
			 "  -h, --help    show this help\n"
			 "Without the timeout option, print the current idle timeout\n");
	maxargs(optind);
	need_connected();
	need_loggedin();

	if(argc - 1 == optind)
		ftp_idle(argv[optind]);
	else
		ftp_idle(0);
}
Пример #17
0
void cmd_filetime(int argc, char **argv)
{
	int i;

	OPT_HELP_NEW(_("Show modification time of remote file."), "filetime [options] <file>...", NULL);
	minargs(optind);
	need_connected();
	need_loggedin();

	for(i=optind;i<argc;i++) {
		time_t t = ftp_filetime(argv[i], true);
		if(t != (time_t) -1)
			printf("%s: %s", argv[i], ctime(&t));
		else
			printf("%s\n", ftp_getreply(false));
	}
}
Пример #18
0
void cmd_nlist(int argc, char **argv)
{
	OPT_HELP("Simple file list.  Usage:\n"
			 "  nlist [options] [file]\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");
	need_connected();
	need_loggedin();

	if(argc == optind + 1)
		ftp_list("NLST", 0, stdout);
	else {
		char *args;

		args = args_cat(argc, argv, optind);
		ftp_list("NLST", args, stdout);
		free(args);
	}
}
Пример #19
0
void cmd_cd(int argc, char **argv)
{
	char *e;

	maxargs_nohelp(1);
	need_connected();
	need_loggedin();

	if(argc <= 1)
		e = ftp->homedir;
	else if(strcmp(argv[1], "-") == 0) {
		e = ftp->prevdir;
		if(e == 0) /* no previous directory */
			return;
	} else
		e = argv[1];
	e = tilde_expand_home(e, ftp->homedir);
	ftp_chdir(e);
	free(e);
}
Пример #20
0
void cmd_filetime(int argc, char **argv)
{
	int i;

	OPT_HELP("Show modification time of remote file.  Usage:\n"
			 "  filetime [options] <file>...\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");

	minargs(optind);
	need_connected();
	need_loggedin();

	for(i=optind;i<argc;i++) {
		time_t t = ftp_filetime(argv[i]);
		if(t != (time_t) -1)
			printf("%s: %s", argv[i], ctime(&t));
		else
			printf("%s\n", ftp_getreply(false));
	}
}
Пример #21
0
void cmd_system(int argc, char **argv)
{
	if(argv) {
		OPT_HELP_NEW(_("Show type of remote system."), "system [options]\n", NULL);

		maxargs(optind - 1);
	}
	need_connected();
	need_loggedin();

#ifdef HAVE_LIBSSH
	if(ftp->session) {
		fprintf(stderr, "remote system: SSH (version %d)\n", ftp->ssh_version);
		return;
	}
#endif

	if(ftp_get_verbosity() != vbDebug)
		fprintf(stderr, _("remote system: "));
	ftp_set_tmp_verbosity(vbCommand);
	ftp_cmd("SYST");
}
Пример #22
0
void cmd_system(int argc, char **argv)
{
	if(argv) {
		OPT_HELP("Show type of remote system.  Usage:\n"
				 "  system [options]\n"
				 "Options:\n"
				 "  -h, --help    show this help\n");

		maxargs(optind - 1);
	}
	need_connected();
	need_loggedin();

	if(ftp->ssh_pid) {
		fprintf(stderr, "remote system: SSH (version %d)\n", ftp->ssh_version);
		return;
	}

	if(ftp_get_verbosity() != vbDebug)
		fprintf(stderr, _("remote system: "));
	ftp_set_tmp_verbosity(vbCommand);
	ftp_cmd("SYST");
}
Пример #23
0
/* store a local file on remote server */
void cmd_put(int argc, char **argv)
{
	int c, opt=PUT_VERBOSE;
	list *gl;
	char *put_output = 0;
	char *logfile = 0;
	pid_t pid;
#ifdef HAVE_REGEX
	int ret;
	char put_rx_errbuf[129];
#endif
	struct option longopts[] = {
		{"append", no_argument, 0, 'a'},
		{"delete-after", no_argument, 0, 'D'},
		{"dir-mask", required_argument, 0, '3'},
#ifdef HAVE_REGEX
		{"dir-rx-mask", required_argument, 0, '4'},
#endif
		{"skip-empty", no_argument, 0, 'e'},
		{"force", no_argument, 0, 'f'},
		{"nohup", no_argument, 0, 'H'},
		{"interactive", no_argument, 0, 'i'},
		{"logfile", required_argument, 0, 'L'},
		{"mask", required_argument, 0, 'm'},
#ifdef HAVE_REGEX
		{"rx-mask", required_argument, 0, 'M'},
#endif
		{"newer", no_argument, 0, 'n'},
		{"output", required_argument, 0, 'o'},
		{"preserve", no_argument, 0, 'p'},
		{"parents", no_argument, 0, 'P'},
		{"quiet", no_argument, 0, 'q'},
		{"recursive", no_argument, 0, 'r'},
		{"resume", no_argument, 0, 'R'},
		{"skip-existing", no_argument, 0, 's'},
		{"tagged", no_argument, 0, 't'},
		{"type", required_argument, 0, '1'},
		{"verbose", no_argument, 0, 'v'},
		{"unique", no_argument, 0, 'u'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0},
	};

	if(put_glob_mask) {
		free(put_glob_mask);
		put_glob_mask = 0;
	}
	if(put_dir_glob_mask) {
		free(put_dir_glob_mask);
		put_dir_glob_mask = 0;
	}
#ifdef HAVE_REGEX
	if(put_rx_mask_set) {
		regfree(&put_rx_mask);
		put_rx_mask_set = 0;
	}
	if(put_dir_rx_mask_set) {
		regfree(&put_dir_rx_mask);
		put_dir_rx_mask_set = 0;
	}
#endif

	put_skip_empty = false;

	optind = 0; /* force getopt() to re-initialize */
	while((c = getopt_long(argc, argv,
						   "aDefHiL:no:pPqrRstvum:M:", longopts, 0)) != EOF)
	{
		switch(c) {
		case 'i':
			opt |= PUT_INTERACTIVE;
			break;
		case 'f':
			opt |= PUT_FORCE;
			break;
		   case 'e':
			  opt |= PUT_SKIP_EMPTY;
			  put_skip_empty = true;
			  break;
		case '3': /* --dir-mask=GLOB */
			free(put_dir_glob_mask);
			put_dir_glob_mask = xstrdup(optarg);
			unquote(put_dir_glob_mask);
			break;
#ifdef HAVE_REGEX
		case '4': /* --dir-rx-mask=REGEXP */
			if(put_dir_rx_mask_set) {
				regfree(&put_dir_rx_mask);
				put_dir_rx_mask_set = false;
			}
			unquote(optarg);
			ret = regcomp(&put_dir_rx_mask, optarg, REG_EXTENDED);
			if(ret != 0) {
				regerror(ret, &put_dir_rx_mask, put_rx_errbuf, 128);
				ftp_err(_("Regexp '%s' failed: %s\n"), optarg, put_rx_errbuf);
				return;
			} else
				put_dir_rx_mask_set = true;
			break;
#endif
		case 'o':
			put_output = tilde_expand_home(optarg, ftp->homedir);
			path_collapse(put_output);
			stripslash(put_output);
			break;
		case 'H':
			opt |= PUT_NOHUP;
			break;
		case 'L':
			free(logfile);
			logfile = xstrdup(optarg);
			unquote(logfile);
			break;
		case 'm': /* --mask */
			free(put_glob_mask);
			put_glob_mask = xstrdup(optarg);
			break;
#ifdef HAVE_REGEX
		case 'M': /* --rx-mask */
			if(put_rx_mask_set) {
				regfree(&put_rx_mask);
				put_rx_mask_set = false;
			}

			ret = regcomp(&put_rx_mask, optarg, REG_EXTENDED);
			if(ret != 0) {
				regerror(ret, &put_rx_mask, put_rx_errbuf, 128);
				ftp_err(_("Regexp '%s' failed: %s\n"), optind, put_rx_errbuf);
				return;
			} else
				put_rx_mask_set = true;
			break;
#endif
		  case 'n':
			opt |= PUT_NEWER;
			break;
		  case 'v':
			opt |= PUT_VERBOSE;
			break;
		  case 'q':
			opt &= ~PUT_VERBOSE;
			break;
		  case 'a':
			opt |= PUT_APPEND;
			break;
		  case 'D':
			opt |= PUT_DELETE_AFTER;
			break;
		  case 'u':
			opt |= PUT_UNIQUE;
			if(!ftp->has_stou_command) {
				fprintf(stderr, _("Remote doesn't support the STOU"
								  " (store unique) command\n"));
				return;
			}
			break;
		  case 'r':
			opt |= PUT_RECURSIVE;
			break;
		  case 's':
			opt |= PUT_SKIP_EXISTING;
			break;
		  case 'R':
			opt |= PUT_RESUME;
			break;
		  case 't':
			opt |= PUT_TAGGED;
			break;
		  case '1':
			if(strncmp(optarg, "ascii", strlen(optarg)) == 0)
				opt |= PUT_ASCII;
			else if(strncmp(optarg, "binary", strlen(optarg)) == 0)
				opt |= PUT_BINARY;
			else {
				printf(_("Invalid option argument --type=%s\n"), optarg);
				return;
			}
			break;
		  case 'p':
			opt |= PUT_PRESERVE;
			break;
		  case 'P':
			opt |= PUT_PARENTS;
			break;
		  case 'h':
			print_put_syntax();;
			return;
		  case '?':
			return;
		}
	}
	if(optind>=argc && !test(opt, PUT_TAGGED)) {
/*		fprintf(stderr, _("missing argument, try 'put --help'"*/
/*						  " for more information\n"));*/
		minargs(optind);
		return;
	}

	if(test(opt, PUT_APPEND) && test(opt, PUT_SKIP_EXISTING)) {
		printf("Can't use --append and --skip-existing simultaneously\n");
		return;
	}

	need_connected();
	need_loggedin();

	gl = lglob_create();
	while(optind < argc) {
		char *f;

		f = tilde_expand_home(argv[optind], gvLocalHomeDir);
		stripslash(f);
		lglob_glob(gl, f, true, put_exclude_func);
		optind++;
	}

	if(list_numitem(gl) == 0) {
		if(!test(opt, PUT_TAGGED)) {
			list_free(gl);
			return;
		} else if(list_numitem(gvLocalTagList) == 0) {
			printf(_("no tagged files\n"));
			list_free(gl);
			return;
		}
	}

	free(ftp->last_mkpath);
	ftp->last_mkpath = 0;

	put_quit = false;
	put_batch = put_owbatch = put_delbatch = test(opt, PUT_FORCE);
	if(test(opt, PUT_FORCE))
		opt &= ~PUT_INTERACTIVE;

	if(put_output && !test(opt, PUT_RECURSIVE) && list_numitem(gl) +
	   (test(opt, PUT_TAGGED) ? list_numitem(gvLocalTagList) : 0) == 1)
		{
			opt |= PUT_OUTPUT_FILE;
		}

	gvInTransfer = true;
	gvInterrupted = false;

	if(test(opt, PUT_NOHUP)) {
		int r = 0;
		pid = fork();

		if(pid == 0) {
			r = transfer_init_nohup(logfile);
			if(r != 0)
				exit(0);
		}

		if(r != 0)
			return;

		if(pid == 0) { /* child process */
			transfer_begin_nohup(argc, argv);

			if(!test(opt, PUT_FORCE) && !test(opt, PUT_RESUME))
				opt |= PUT_UNIQUE;
			opt |= PUT_FORCE;

			putfiles(gl, opt, put_output);
			list_free(gl);
			if(test(opt, PUT_TAGGED)) {
				putfiles(gvLocalTagList, opt, put_output);
				list_clear(gvLocalTagList);
			}
			free(put_output);

			transfer_end_nohup();
		}
		if(pid == -1) {
			perror("fork()");
			return;
		}
		/* parent process */
		sleep(1);
		printf("%d\n", pid);
		input_save_history();
		gvars_destroy();
		reset_xterm_title();
		exit(0);
	}

	putfiles(gl, opt, put_output);
	list_free(gl);
	if(test(opt, PUT_TAGGED)) {
		putfiles(gvLocalTagList, opt, put_output);
		list_clear(gvLocalTagList);
	}
	free(put_output);
	gvInTransfer = false;
}
Пример #24
0
void cmd_bookmark(int argc, char **argv)
{
	struct option longopts[] = {
		{"save", optional_argument, 0, 's'},
		{"edit", no_argument, 0, 'e'},
		{"read", optional_argument, 0, 'r'},
		{"delete", no_argument, 0, 'd'},
		{"list", no_argument, 0, 'l'},
		{"noupdate", no_argument, 0, 'u'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};
	int action = BM_BOOKMARK;
	int c;
	char *bmfile = 0;

	optind = 0;
	while((c=getopt_long(argc, argv, "s::er::dluh", longopts, 0)) != EOF) {
		switch(c) {
			case 's':
				action = BM_SAVE;
				free(bmfile);
				bmfile = xstrdup(optarg);
				break;
			case 'e':
				action = BM_EDIT;
				break;
			case 'r':
				action = BM_READ;
				free(bmfile);
				bmfile = xstrdup(optarg);
				break;
			case 'd':
				action = BM_DELETE;
				break;
			case 'l':
				action = BM_LIST;
				break;
			case 'u':
				action = BM_TOGGLE_NOUPDATE;
				break;
			case 'h':
				print_bookmark_syntax();
				/* fall through */
			default:
				return;
		}
	}

	if(action == BM_SAVE) {
		bookmark_save(bmfile);
		if(bmfile)
			printf(_("bookmarks saved in %s\n"), bmfile);
		else
			printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory);
		return;
	}
	if(action == BM_READ) {
		char *tmp = 0;
		int ret;
		list_clear(gvBookmarks);
		if(bmfile)
			ret = parse_rc(bmfile, true);
		else {
			if (asprintf(&tmp, "%s/bookmarks", gvWorkingDirectory) == -1)
      {
        fprintf(stderr, _("Failed to allocate memory.\n"));
        return;
      }
			ret = parse_rc(tmp, false);
		}
		if(ret != -1)
			printf(_("bookmarks read from %s\n"), bmfile ? bmfile : tmp);
		free(tmp);
		return;
	}
	if(action == BM_EDIT) {
		invoke_shell("%s %s/bookmarks", gvEditor, gvWorkingDirectory);
		return;
	}
	if(action == BM_LIST) {
		listitem *li;

		printf(_("%zu bookmarks present in memory\n"),
			   list_numitem(gvBookmarks));
		for(li = gvBookmarks->first; li; li=li->next) {
			url_t *u = (url_t *)li->data;
			/* note: all info not printed */
			printf("%-20s", u->alias ? u->alias : u->hostname);
			printf("%s://%s@%s", u->protocol ? u->protocol : "ftp",
				   u->username, u->hostname);
			if(u->directory)
			{
				char* sp = shortpath(u->directory, 30, 0);
				printf("/%s", sp);
				free(sp);
			}
			putchar('\n');
		}
		return;
	}
	if(action == BM_DELETE) {
		int i;
		bool del_done = false;

		minargs(optind);

		for(i = optind; i < argc; i++) {
			listitem *li;

			while((li = list_search(gvBookmarks,
									(listsearchfunc)urlcmp_name,
									argv[i])) != 0)
			{
				url_t *u = (url_t *)li->data;
				printf(_("deleted bookmark %s\n"),
					   u->alias ? u->alias : u->hostname);
				list_delitem(gvBookmarks, li);
				del_done = true;
			}
		}

		if(del_done) {
			bookmark_save(0);
			printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory);
		}

		return;
	}
	if(action == BM_TOGGLE_NOUPDATE) {
		int i;
		bool toggle_done = false;

		if(argc == optind) {
			listitem *li;

			need_connected();
			need_loggedin();

			li = list_search(gvBookmarks,
							 (listsearchfunc)urlcmp_name,
							 ftp->url->alias);
			if(li) {
				url_t *u = (url_t *)li->data;
				u->noupdate = !u->noupdate;
				printf(_("%s: noupdate: %s\n"),
					   u->alias ? u->alias : u->hostname,
					   u->noupdate ? _("yes") : _("no"));
				toggle_done = true;
			}

			ftp->url->noupdate = !ftp->url->noupdate;
			if(!toggle_done)
				printf(_("%s: noupdate: %s\n"),
					   ftp->url->alias ? ftp->url->alias : ftp->url->hostname,
					   ftp->url->noupdate ? _("yes") : _("no"));
		}

		for(i = optind; i < argc; i++) {
			listitem *li;

			li = list_search(gvBookmarks,
							 (listsearchfunc)urlcmp_name,
							 argv[i]);
			if(li) {
				url_t *u = (url_t *)li->data;
				u->noupdate = !u->noupdate;
				printf(_("%s: noupdate: %s\n"),
					   u->alias ? u->alias : u->hostname,
					   u->noupdate ? _("yes") : _("no"));
				toggle_done = true;
			}
		}

		if(toggle_done) {
			bookmark_save(0);
			printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory);
		}

		return;
	}

	maxargs(1);

	need_connected();
	need_loggedin();

	create_bookmark(argc > 1 ? argv[1] : 0);
}
Пример #25
0
void cmd_get(int argc, char **argv)
{
    list *gl;
    int opt=GET_VERBOSE, c;
    char *logfile = 0;
    pid_t pid;
    struct group *grp;
    char *get_output = 0;
    int stat_thresh = gvStatsThreshold;
#ifdef HAVE_REGEX
    int ret;
    char get_rx_errbuf[129];
#endif
    struct option longopts[] = {
        {"append", no_argument, 0, 'a'},
        {"chmod", required_argument, 0, 'c'},
        {"chgrp", required_argument, 0, '2'},
        {"no-dereference", no_argument, 0, 'd'},
        {"delete-after", no_argument, 0, 'D'},
        {"dir-mask", required_argument, 0, '3'},
#ifdef HAVE_REGEX
        {"dir-rx-mask", required_argument, 0, '4'},
#endif
        {"interactive", no_argument, 0, 'i'},
        {"skip-empty", no_argument, 0, 'e'},
        {"force", no_argument, 0, 'f'},
        {"force-newer", no_argument, 0, 'F'},
        {"logfile", required_argument, 0, 'L'},
        {"mask", required_argument, 0, 'm'},
#ifdef HAVE_REGEX
        {"rx-mask", required_argument, 0, 'M'},
#endif
        {"newer", no_argument, 0, 'n'},
        {"nohup", no_argument, 0, 'H'},
        {"verbose", no_argument, 0, 'v'},
        {"preserve", no_argument, 0, 'p'},
        {"parents", no_argument, 0, 'P'},
        {"quiet", no_argument, 0, 'q'},
        {"recursive", no_argument, 0, 'r'},
        {"resume", no_argument, 0, 'R'},
        {"skip-existing", no_argument, 0, 's'},
        {"stats", optional_argument, 0, 'S'},
        {"tagged", no_argument, 0, 't'},
        {"type", required_argument, 0, '1'},
        {"unique", no_argument, 0, 'u'},
        {"output", required_argument, 0, 'o'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0},
    };

    if(cmod) {
        mode_free(cmod);
        cmod = 0;
    }

    if(get_glob_mask) {
        free(get_glob_mask);
        get_glob_mask = 0;
    }
    if(get_dir_glob_mask) {
        free(get_dir_glob_mask);
        get_dir_glob_mask = 0;
    }
#ifdef HAVE_REGEX
    if(get_rx_mask_set) {
        regfree(&get_rx_mask);
        get_rx_mask_set = 0;
    }
    if(get_dir_rx_mask_set) {
        regfree(&get_dir_rx_mask);
        get_dir_rx_mask_set = 0;
    }
#endif

    get_skip_empty = false;

    optind = 0; /* force getopt() to re-initialize */
    while((c=getopt_long(argc, argv, "abHc:dDeio:fFL:tnpPvqrRsuT:m:M:",
                         longopts, 0)) != EOF)
    {
        switch(c) {
          case 'a':
            opt |= GET_APPEND;
            break;
          case 'c':
            cmod = mode_compile(optarg, MODE_MASK_ALL);
            if(cmod == NULL) {
                fprintf(stderr, _("Invalid mode for --chmod: %s\n"), optarg);
                return;
            }
            opt |= GET_CHMOD;
            break;
        case '2': /* --chgrp */
            grp = getgrnam(optarg);
            if(grp == 0) {
                fprintf(stderr, _("%s is not a valid group name\n"), optarg);
                return;
            }
            {
                int i;
                for(i=0; grp->gr_mem && grp->gr_mem[i]; i++) {
                    if(strcmp(gvUsername, grp->gr_mem[i]) == 0)
                        break;
                }
                if(!grp->gr_mem[i]) {
                    fprintf(stderr,
                            _("you are not a member of group %s\n"), optarg);
                    return;
                }
            }
            group_change = grp->gr_gid;
            opt |= GET_CHGRP;
            break;
          case 'D':
            opt |= GET_DELETE_AFTER;
            break;
          case 'd':
            opt |= GET_NO_DEREFERENCE;
            break;
           case 'e':
              opt |= GET_SKIP_EMPTY;
              get_skip_empty = true;
              break;
        case '3': /* --dir-mask=GLOB */
            free(get_dir_glob_mask);
            get_dir_glob_mask = xstrdup(optarg);
            unquote(get_dir_glob_mask);
            break;
#ifdef HAVE_REGEX
        case '4': /* --dir-rx-mask=REGEXP */
            if(get_dir_rx_mask_set) {
                regfree(&get_dir_rx_mask);
                get_dir_rx_mask_set = false;
            }
            unquote(optarg);
            ret = regcomp(&get_dir_rx_mask, optarg, REG_EXTENDED);
            if(ret != 0) {
                regerror(ret, &get_dir_rx_mask, get_rx_errbuf, sizeof(get_rx_errbuf) - 1);
                ftp_err(_("Regexp '%s' failed: %s\n"), optarg, get_rx_errbuf);
                return;
            } else
                get_dir_rx_mask_set = true;
            break;
#endif
          case 'i':
            opt |= GET_INTERACTIVE;
            break;
          case 'f':
            opt |= GET_FORCE;
            break;
          case 'F':
            opt |= GET_FORCE_NEWER;
            break;
        case 'm': /* --mask */
            free(get_glob_mask);
            get_glob_mask = xstrdup(optarg);
            unquote(get_glob_mask);
            break;
#ifdef HAVE_REGEX
        case 'M': /* --rx-mask */
            if(get_rx_mask_set) {
                regfree(&get_rx_mask);
                get_rx_mask_set = false;
            }
            unquote(optarg);
            ret = regcomp(&get_rx_mask, optarg, REG_EXTENDED);
            if(ret != 0) {
                regerror(ret, &get_rx_mask, get_rx_errbuf, sizeof(get_rx_errbuf) - 1);
                ftp_err(_("Regexp '%s' failed: %s\n"), optarg, get_rx_errbuf);
                return;
            } else
                get_rx_mask_set = true;
            break;
#endif
          case 'o':
            get_output = tilde_expand_home(optarg, gvLocalHomeDir);
            /*stripslash(get_output);*/
            unquote(get_output);
            break;
          case 'v':
            opt |= GET_VERBOSE;
            break;
          case 'p':
            opt |= GET_PRESERVE;
            break;
          case 'P':
            opt |= GET_PARENTS;
            break;
          case 'H':
            opt |= GET_NOHUP;
            break;
          case 'q':
            opt &= ~GET_VERBOSE;
            break;
          case 'r':
            opt |= GET_RECURSIVE;
            break;
          case 's':
            opt |= GET_SKIP_EXISTING;
            break;
          case 'S':
            stat_thresh = optarg ? atoi(optarg) : 0;
            break;
          case 'R':
            opt |= GET_RESUME;
            break;
          case '1':
            if(strncmp(optarg, "ascii", strlen(optarg)) == 0)
                opt |= GET_ASCII;
            else if(strncmp(optarg, "binary", strlen(optarg)) == 0)
                opt |= GET_BINARY;
            else {
                printf(_("Invalid option argument --type=%s\n"), optarg);
                return;
            }
            break;
          case 'u':
            opt |= GET_UNIQUE;
            break;
          case 'L':
              free(logfile);
              logfile = xstrdup(optarg);
              unquote(logfile);
              break;
          case 't':
            opt |= GET_TAGGED;
            break;
          case 'n':
            opt |= GET_NEWER;
            break;
          case 'h':
            print_get_syntax();
            return;
          case '?':
            return;
        }
    }
    if(optind>=argc && !test(opt, GET_TAGGED)) {
        minargs(optind);
        return;
    }

    need_connected();
    need_loggedin();

    gl = rglob_create();
    while(optind < argc) {
        stripslash(argv[optind]);
        if(rglob_glob(gl, argv[optind], true, true, get_exclude_func) == -1)
            fprintf(stderr, _("%s: no matches found\n"), argv[optind]);
        optind++;
    }
    if(list_numitem(gl) == 0 && !test(opt, GET_TAGGED)) {
        rglob_destroy(gl);
        return;
    }
    if(test(opt, GET_TAGGED)
       && (!ftp->taglist || list_numitem(ftp->taglist)==0))
    {
        printf(_("no tagged files\n"));
        if(list_numitem(gl) == 0) {
            rglob_destroy(gl);
            return;
        }
    }

    get_quit = false;
    get_batch = get_owbatch = get_delbatch = test(opt, GET_FORCE);
    if(test(opt, GET_FORCE))
        opt &= ~GET_INTERACTIVE;

    if(get_output && !test(opt, GET_RECURSIVE) && list_numitem(gl) +
       (test(opt, GET_TAGGED) ? list_numitem(ftp->taglist) : 0) == 1)
        {
            /* if the argument to --output ends with a slash, we assume the
             * user wants the destination to be a directory
             */
            char *e = strrchr(get_output, 0);
            if(e && e[-1] != '/')
                opt |= GET_OUTPUT_FILE;
        }

    stats_reset(gvStatsTransfer);

    gvInTransfer = true;
    gvInterrupted = false;

    if(test(opt, GET_NOHUP)) {
        int r = 0;
        pid = fork();

        if(pid == 0) {
            r = transfer_init_nohup(logfile);
            if(r != 0)
                exit(0);
        }

        if(r != 0)
            return;

        if(pid == 0) { /* child process */
            transfer_begin_nohup(argc, argv);

            if(!test(opt, GET_FORCE) && !test(opt, GET_RESUME))
                opt |= GET_UNIQUE;
            opt |= GET_FORCE;

            if(list_numitem(gl))
                getfiles(gl, opt, get_output);
            rglob_destroy(gl);
            if(ftp->taglist && test(opt, GET_TAGGED))
                getfiles(ftp->taglist, opt, get_output);
            free(get_output);

            transfer_end_nohup();
        }
        if(pid == -1) {
            perror("fork()");
            return;
        }
        /* parent process */
        sleep(1);
        printf("%d\n", pid);
        input_save_history();
        gvars_destroy();
        reset_xterm_title();
        exit(0);
    }

    if(list_numitem(gl))
        getfiles(gl, opt, get_output);
    rglob_destroy(gl);
    if(ftp->taglist && test(opt, GET_TAGGED))
        getfiles(ftp->taglist, opt, get_output);
    free(get_output);
    mode_free(cmod);
    cmod = 0;
    gvInTransfer = false;

    stats_display(gvStatsTransfer, stat_thresh);
}
Пример #26
0
void cmd_fxp(int argc, char **argv)
{
	list *gl;
	listitem *fxp_tmp = 0;
	char *logfile = 0;
	char *fxp_output = 0;
#ifdef HAVE_REGEX
	int ret;
	char fxp_rx_errbuf[129];
#endif
	int c, opt = FXP_VERBOSE;
	struct option longopts[] = {
		{"append", no_argument, 0, 'a'},
		{"delete-after", no_argument, 0, 'D'},
		{"dir-mask", required_argument, 0, '3'},
#ifdef HAVE_REGEX
		{"dir-rx-mask", required_argument, 0, '4'},
#endif
		{"force", no_argument, 0, 'f'},
    {"force-newer", no_argument, 0, 'F'},
		{"nohup", no_argument, 0, 'H'},
		{"interactive", no_argument, 0, 'i'},
		{"logfile", required_argument, 0, 'L'},
		{"mask", required_argument, 0, 'm'},
#ifdef HAVE_REGEX
		{"rx-mask", required_argument, 0, 'M'},
#endif
		{"newer", no_argument, 0, 'n'},
		{"output", required_argument, 0, 'o'},
		{"preserve", no_argument, 0, 'p'},
		{"parents", no_argument, 0, 'P'},
		{"quiet", no_argument, 0, 'q'},
		{"recursive", no_argument, 0, 'r'},
		{"resume", no_argument, 0, 'R'},
		{"skip-existing", no_argument, 0, 's'},
		{"tagged", no_argument, 0, 't'},
		{"target", required_argument, 0, 'T'},
		{"type", required_argument, 0, '1'},
		{"unique", no_argument, 0, 'u'},
		{"verbose", no_argument, 0, 'v'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	if(fxp_glob_mask) {
		free(fxp_glob_mask);
		fxp_glob_mask = 0;
	}
	if(fxp_dir_glob_mask) {
		free(fxp_dir_glob_mask);
		fxp_dir_glob_mask = 0;
	}
#ifdef HAVE_REGEX
	if(fxp_rx_mask_set) {
		fxp_rx_mask_set = 0;
	}
	if(fxp_dir_rx_mask_set) {
		regfree(&fxp_dir_rx_mask);
		fxp_dir_rx_mask_set = 0;
	}
#endif

	if(list_numitem(gvFtpList) == 2) {
		fxp_tmp = gvFtpList->first;
		if(fxp_tmp->data == ftp)
			fxp_target = fxp_tmp->next->data;
		else
			fxp_target = fxp_tmp->data;
	} else
		fxp_target = 0;

	fxp_skip_empty = false;

	optind = 0; /* force getopt() to re-initialize */
	while((c=getopt_long(argc, argv, "aDefHiL:M:no:pPqrRstT:uvh",
						 longopts, 0)) != EOF)
		{
			switch(c) {
			case 'a': /* --append */
				opt |= FXP_APPEND;
				break;
			case 'D': /* --delete-after */
				opt |= FXP_DELETE_AFTER;
				break;
			case 'f': /* --force */
				opt |= FXP_FORCE;
				break;
      case 'F':
        opt |= FXP_FORCE_NEWER;
        break;
			   case 'e': /* --skip-empty */
				  opt |= FXP_SKIP_EMPTY;
				  fxp_skip_empty = true;
				  break;
			case '3': /* --dir-mask=GLOB */
				free(fxp_dir_glob_mask);
				fxp_dir_glob_mask = xstrdup(optarg);
				unquote(fxp_dir_glob_mask);
				break;
#ifdef HAVE_REGEX
			case '4': /* --dir-rx-mask=REGEXP */
				if(fxp_dir_rx_mask_set) {
					regfree(&fxp_dir_rx_mask);
					fxp_dir_rx_mask_set = false;
				}
				unquote(optarg);
				ret = regcomp(&fxp_dir_rx_mask, optarg, REG_EXTENDED);
				if(ret != 0) {
					regerror(ret, &fxp_dir_rx_mask, fxp_rx_errbuf, 128);
					ftp_err(_("Regexp '%s' failed: %s\n"),
							optarg, fxp_rx_errbuf);
					return;
				} else
					fxp_dir_rx_mask_set = true;
				break;
#endif
			case 'H': /* --nohup */
				opt |= FXP_NOHUP;
				break;
			case 'i': /* --interactive */
				opt |= FXP_INTERACTIVE;
				break;
			case 'L': /* --logfile=FILE */
				free(logfile);
				logfile = xstrdup(optarg);
				unquote(logfile);
				break;
			case 'm': /* --mask=GLOB */
				free(fxp_glob_mask);
				fxp_glob_mask = xstrdup(optarg);
				unquote(fxp_glob_mask);
				break;
#ifdef HAVE_REGEX
			case 'M': /* --rx-mask=REGEXP */
				if(fxp_rx_mask_set) {
					regfree(&fxp_rx_mask);
					fxp_rx_mask_set = false;
				}

				unquote(optarg);
				ret = regcomp(&fxp_rx_mask, optarg, REG_EXTENDED);
				if(ret != 0) {
					regerror(ret, &fxp_rx_mask, fxp_rx_errbuf, 128);
					ftp_err(_("Regexp '%s' failed: %s\n"),
							optarg, fxp_rx_errbuf);
					return;
				} else
					fxp_rx_mask_set = true;
				break;
#endif
			case 'n': /* --newer */
				opt |= FXP_NEWER;
				break;
			case 'o': /* --output=DIRECTORY */
				if(fxp_target == 0) {
					printf(_("FxP target not set, use --target=NAME"
							 " (as first option)\n"));
					return;
				}
				fxp_output = tilde_expand_home(optarg, fxp_target->homedir);
				stripslash(fxp_output);
				unquote(fxp_output);
				break;
			case 'p': /* --preserve */
				opt |= FXP_PRESERVE;
				break;
			case 'P': /* --parents */
				opt |= FXP_PARENTS;
				break;
			case 'q': /* --quiet */
				opt &= ~FXP_VERBOSE;
				break;
			case 'r': /* --recursive */
				opt |= FXP_RECURSIVE;
				break;
			case 'R': /* --resume */
				opt |= FXP_RESUME;
				break;
			case 's':
				opt |= FXP_SKIP_EXISTING;
				break;
			case 't': /* --tagged */
				opt |= FXP_TAGGED;
				break;
			case '1': /* --type=[ascii|binary] */
				if(strncmp(optarg, "ascii", strlen(optarg)) == 0)
					opt |= FXP_ASCII;
				else if(strncmp(optarg, "binary", strlen(optarg)) == 0)
					opt |= FXP_BINARY;
				else {
					printf(_("Invalid option argument --type=%s\n"), optarg);
					return;
				}
				break;
			case 'T': /* --target=HOST */
				fxp_tmp = ftplist_search(optarg);
				if(!fxp_tmp)
					return;
				fxp_target = (Ftp *)fxp_tmp->data;
				break;
			case 'u': /* --unique */
				opt |= FXP_UNIQUE;
				break;
			case 'v': /* --verbose */
				opt |= FXP_VERBOSE;
				break;
			case 'h': /* --help */
				print_fxp_syntax();
				return;
			case '?':
			default:
				return;
			}
		}

	if(optind >= argc && !test(opt, FXP_TAGGED)) {
		minargs(optind);
		return;
	}

	need_connected();
	need_loggedin();

	if(fxp_target == 0) {
		ftp_err(_("No target specified, try '%s --help'"
				  " for more information\n"), argv[0]);
		return;
	}

#ifdef HAVE_LIBSSH
	if(ftp->session || fxp_target->session) {
		ftp_err("FxP for SSH connections no implemented\n");
		return;
	}
#endif

	gl = rglob_create();
	while(optind < argc) {
		stripslash(argv[optind]);
		if(rglob_glob(gl, argv[optind], true, true, fxp_exclude_func) == -1)
			fprintf(stderr, _("%s: no matches found\n"), argv[optind]);
		optind++;
	}
	if(list_numitem(gl) == 0 && !test(opt, FXP_TAGGED)) {
		rglob_destroy(gl);
		return;
	}
	if(test(opt, FXP_TAGGED)
	   && (!ftp->taglist || list_numitem(ftp->taglist) == 0))
	{
		printf(_("no tagged files\n"));
		if(list_numitem(gl) == 0) {
			rglob_destroy(gl);
			return;
		}
	}

	fxp_quit = false;
	fxp_batch = fxp_owbatch = fxp_delbatch = test(opt, FXP_FORCE);
	if(test(opt, FXP_FORCE))
		opt &= ~FXP_INTERACTIVE;

	if(fxp_output && !test(opt, FXP_RECURSIVE) && list_numitem(gl) +
	   (test(opt, FXP_TAGGED) ? list_numitem(ftp->taglist) : 0) == 1)
		{
			opt |= FXP_OUTPUT_FILE;
		}

	gvInTransfer = true;
	gvInterrupted = false;

	if(test(opt, FXP_NOHUP)) {
		int r = 0;
		pid_t pid = fork();

		if(pid == 0) {
			r = transfer_init_nohup(logfile);
			if(r != 0)
				exit(0);
		}

		if(r != 0)
			return;

		if(pid == 0) { /* child process */
			transfer_begin_nohup(argc, argv);

			if(!test(opt, FXP_FORCE) && !test(opt, FXP_RESUME))
				opt |= FXP_UNIQUE;
			opt |= FXP_FORCE;

			if(list_numitem(gl))
				fxpfiles(gl, opt, fxp_output);
			rglob_destroy(gl);

			if(ftp->taglist && test(opt, FXP_TAGGED))
				fxpfiles(ftp->taglist, opt, fxp_output);

			free(fxp_output);

			transfer_end_nohup();
		}
		if(pid == -1) {
			perror("fork()");
			return;
		}
		/* parent process */
		sleep(1);
		printf("%d\n", pid);
		input_save_history();
		gvars_destroy();
		reset_xterm_title();
		exit(0);
	}

	if(list_numitem(gl))
		fxpfiles(gl, opt, fxp_output);
	rglob_destroy(gl);

	if(ftp->taglist && test(opt, FXP_TAGGED))
		fxpfiles(ftp->taglist, opt, fxp_output);

	free(fxp_output);
	gvInTransfer = false;
}