+/**
+ * Start a client for either type of remote connection. Work out
+ * whether the arguments request a remote shell or rsyncd connection,
+ * and call the appropriate connection function, then run_client.
+ *
+ * Calls either start_socket_client (for sockets) or do_cmd and
+ * client_run (for ssh).
+ **/
+static int start_client(int argc, char *argv[])
+{
+ char *p, *shell_machine = NULL, *shell_user = NULL;
+ char **remote_argv;
+ int remote_argc;
+ int f_in, f_out;
+ int ret;
+ pid_t pid;
+
+ /* Don't clobber argv[] so that ps(1) can still show the right
+ * command line. */
+ if ((ret = copy_argv(argv)) != 0)
+ return ret;
+
+ if (!read_batch) { /* for read_batch, NO source is specified */
+ char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
+ if (path) { /* source is remote */
+ char *dummy_host;
+ int dummy_port = 0;
+ *argv = path;
+ remote_argv = argv;
+ remote_argc = argc;
+ argv += argc - 1;
+ if (argc == 1 || **argv == ':')
+ argc = 0; /* no dest arg */
+ else if (check_for_hostspec(*argv, &dummy_host, &dummy_port)) {
+ rprintf(FERROR,
+ "The source and destination cannot both be remote.\n");
+ exit_cleanup(RERR_SYNTAX);
+ } else {
+ remote_argc--; /* don't count dest */
+ argc = 1;
+ }
+ if (filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ am_sender = 0;
+ if (rsync_port)
+ daemon_over_rsh = shell_cmd ? 1 : -1;
+ } else { /* source is local, check dest arg */
+ am_sender = 1;
+
+ if (argc > 1) {
+ p = argv[--argc];
+ remote_argv = argv + argc;
+ } else {
+ static char *dotarg[1] = { "." };
+ p = dotarg[0];
+ remote_argv = dotarg;
+ list_only |= 1;
+ xfer_dirs |= 1;
+ }
+ remote_argc = 1;
+
+ path = check_for_hostspec(p, &shell_machine, &rsync_port);
+ if (path && filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (!path) { /* no hostspec found, so src & dest are local */
+ local_server = 1;
+ if (filesfrom_host) {
+ rprintf(FERROR,
+ "--files-from cannot be remote when the transfer is local\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ shell_machine = NULL;
+ } else { /* hostspec was found, so dest is remote */
+ argv[argc] = path;
+ if (rsync_port)
+ daemon_over_rsh = shell_cmd ? 1 : -1;
+ }