5 #include <sys/socket.h>
9 #define COMMAND_SIZE 4096
12 * Write a shell-quoted version of a string into a buffer, and
13 * return bytes that ought to be output excluding final null.
15 static int shell_quote(char *buf, int nmax, const char *str)
21 while ( (ch = *str++) ) {
23 if ( strchr(" !\"#$%&\'()*;<=>?[\\]^`{|}", ch) )
48 * Append a string to a string buffer, with or without quoting. Return true
49 * if the buffer overflowed.
51 static int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
59 oc = shell_quote(p, size, str);
62 memcpy(p, str, (oc >= size) ? size-1 : oc);
76 int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
77 char *url, int rmt_argc, char **rmt_argv)
82 char command[COMMAND_SIZE];
88 if (!strcmp(url, "-")) {
94 host = strstr(url, "//");
97 path = strchr(host, '/');
100 path = strchr(host, ':');
105 return error("Bad URL: %s", url);
107 /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */
108 sizen = COMMAND_SIZE;
111 of |= add_to_string(&posn, &sizen, "env ", 0);
112 of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT, 0);
113 of |= add_to_string(&posn, &sizen, "=", 0);
114 of |= add_to_string(&posn, &sizen, path, 1);
115 of |= add_to_string(&posn, &sizen, " ", 0);
116 of |= add_to_string(&posn, &sizen, remote_prog, 1);
118 for ( i = 0 ; i < rmt_argc ; i++ ) {
119 of |= add_to_string(&posn, &sizen, " ", 0);
120 of |= add_to_string(&posn, &sizen, rmt_argv[i], 1);
123 of |= add_to_string(&posn, &sizen, " -", 0);
126 return error("Command line too long");
128 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
129 return error("Couldn't create socket");
132 const char *ssh, *ssh_basename;
133 ssh = getenv("GIT_SSH");
134 if (!ssh) ssh = "ssh";
135 ssh_basename = strrchr(ssh, '/');
143 execlp(ssh, ssh_basename, host, command, NULL);