/* * Select the appropriate protocol for the URL scheme, and return a * write-only stream connected to the document referenced by the URL. */ fetchIO * fetchPut(struct url *URL, const char *flags) { if (strcasecmp(URL->scheme, SCHEME_FILE) == 0) return (fetchPutFile(URL, flags)); else if (strcasecmp(URL->scheme, SCHEME_FTP) == 0) return (fetchPutFTP(URL, flags)); else if (strcasecmp(URL->scheme, SCHEME_HTTP) == 0) return (fetchPutHTTP(URL, flags)); else if (strcasecmp(URL->scheme, SCHEME_HTTPS) == 0) return (fetchPutHTTP(URL, flags)); url_seterr(URL_BAD_SCHEME); return (NULL); }
int main(int argc, char **argv) { struct termios tios; tcflag_t saved; struct url *url; FILE *in, *out; const size_t bufsize = 1024; char buf[bufsize], *fullpath, *base; char *passwd = NULL, *p, *user = NULL, *u; size_t n; int c; if (argc != 3) usage(1); while ((c = getopt(argc, argv, "hp:u:")) != -1) switch (c) { case 'h': usage(0); break; case 'p': passwd = optarg; break; case 'u': user = optarg; break; default: usage(1); break; } argc -= optind; argv += optind; in = inputfile(argv[0]); fullpath = malloc(strlen(argv[0]) + strlen(argv[1]) + 2); if (fullpath == NULL) err(1, "malloc"); strcpy(fullpath, argv[1]); base = fullpath + strlen(argv[1]); *base++ = '/'; strcpy(base, argv[0]); fetchTimeout = 0; url = fetchParseURL(fullpath); if (url == NULL) errx(1, "parsing URL: %s", fetchLastErrString); if (user == NULL) { fprintf(stderr, "Username: "******"reading username"); u = (char *)url->user; strsep(&u, "\n\r"); } else { strlcpy(url->user, user, sizeof(url->user)); } if (passwd == NULL) { fprintf(stderr, "Password: "******"reading password"); tios.c_lflag = saved; tcsetattr(STDIN_FILENO, TCSAFLUSH | TCSASOFT, &tios); } else { if (fgets(url->pwd, sizeof(url->pwd), stdin) == NULL) err(1, "reading password"); } p = (char *)url->pwd; strsep(&p, "\n\r"); } else { strlcpy(url->pwd, passwd, sizeof(url->pwd)); } out = fetchPutFTP(url, ""); if (out == NULL) { printf("user: %s, pwd: %s\n", url->user, url->pwd); errx(1, "couldn't open a connection to %s: %s", fullpath, fetchLastErrString); } while ((n = fread(buf, 1, bufsize, in)) > 0) { if (fwrite(buf, n, 1, out) != 1) err(1, "writing to %s", fullpath); } fclose(in); fclose(out); fetchFreeURL(url); return (0); }