sq_quote_argv and add_to_string rework with strbuf's.
[git/git.git] / rsh.c
CommitLineData
85023577 1#include "cache.h"
f336af17
PA
2#include "rsh.h"
3#include "quote.h"
6eb7ed54
DB
4
5#define COMMAND_SIZE 4096
6
86257aa3 7int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
6eb7ed54
DB
8 char *url, int rmt_argc, char **rmt_argv)
9{
10 char *host;
11 char *path;
12 int sv[2];
6eb7ed54 13 int i;
c9bc159d 14 pid_t pid;
7a33bcbe 15 struct strbuf cmd;
6eb7ed54
DB
16
17 if (!strcmp(url, "-")) {
18 *fd_in = 0;
19 *fd_out = 1;
20 return 0;
21 }
22
23 host = strstr(url, "//");
001d4a27
LT
24 if (host) {
25 host += 2;
26 path = strchr(host, '/');
27 } else {
28 host = url;
29 path = strchr(host, ':');
479346ad
SV
30 if (path)
31 *(path++) = '\0';
6eb7ed54 32 }
6eb7ed54
DB
33 if (!path) {
34 return error("Bad URL: %s", url);
35 }
7a33bcbe 36
25738089 37 /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */
7a33bcbe
PH
38 strbuf_init(&cmd, COMMAND_SIZE);
39 strbuf_addstr(&cmd, "env ");
40 strbuf_addstr(&cmd, GIT_DIR_ENVIRONMENT "=");
41 sq_quote_buf(&cmd, path);
42 strbuf_addch(&cmd, ' ');
43 sq_quote_buf(&cmd, remote_prog);
0de68d28 44
7a33bcbe
PH
45 for (i = 0 ; i < rmt_argc ; i++) {
46 strbuf_addch(&cmd, ' ');
47 sq_quote_buf(&cmd, rmt_argv[i]);
6eb7ed54 48 }
0de68d28 49
7a33bcbe 50 strbuf_addstr(&cmd, " -");
0de68d28 51
7a33bcbe 52 if (cmd.len >= COMMAND_SIZE)
0de68d28
PA
53 return error("Command line too long");
54
55 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
6eb7ed54 56 return error("Couldn't create socket");
0de68d28 57
c9bc159d
PD
58 pid = fork();
59 if (pid < 0)
60 return error("Couldn't fork");
61 if (!pid) {
c7c81b3a
JR
62 const char *ssh, *ssh_basename;
63 ssh = getenv("GIT_SSH");
64 if (!ssh) ssh = "ssh";
65 ssh_basename = strrchr(ssh, '/');
4852f723
MS
66 if (!ssh_basename)
67 ssh_basename = ssh;
68 else
69 ssh_basename++;
6eb7ed54
DB
70 close(sv[1]);
71 dup2(sv[0], 0);
72 dup2(sv[0], 1);
7a33bcbe 73 execlp(ssh, ssh_basename, host, cmd.buf, NULL);
6eb7ed54
DB
74 }
75 close(sv[0]);
76 *fd_in = sv[1];
77 *fd_out = sv[1];
78 return 0;
79}