ftp.c (525be862e61b9cdcda070734bb4208e6dced891a) | ftp.c (38c7e4a631ce968b02e4a08944aabad9b57844e8) |
---|---|
1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 46 unchanged lines hidden (view full) --- 55 * 56 */ 57 58#include <sys/param.h> 59#include <sys/socket.h> 60#include <netinet/in.h> 61 62#include <ctype.h> | 1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 46 unchanged lines hidden (view full) --- 55 * 56 */ 57 58#include <sys/param.h> 59#include <sys/socket.h> 60#include <netinet/in.h> 61 62#include <ctype.h> |
63#include <err.h> |
|
63#include <errno.h> 64#include <fcntl.h> 65#include <netdb.h> 66#include <stdarg.h> 67#include <stdio.h> 68#include <stdlib.h> 69#include <string.h> 70#include <time.h> --- 90 unchanged lines hidden (view full) --- 161 162 return last_code; 163} 164 165/* 166 * Send a command and check reply 167 */ 168static int | 64#include <errno.h> 65#include <fcntl.h> 66#include <netdb.h> 67#include <stdarg.h> 68#include <stdio.h> 69#include <stdlib.h> 70#include <string.h> 71#include <time.h> --- 90 unchanged lines hidden (view full) --- 162 163 return last_code; 164} 165 166/* 167 * Send a command and check reply 168 */ 169static int |
169_ftp_cmd(int cd, char *fmt, ...) | 170_ftp_cmd(int cd, const char *fmt, ...) |
170{ 171 va_list ap; 172 size_t len; 173 char *msg; 174 int r; 175 176 va_start(ap, fmt); 177 len = vasprintf(&msg, fmt, ap); --- 14 unchanged lines hidden (view full) --- 192 } 193 194 return _ftp_chkerr(cd); 195} 196 197/* 198 * Return a pointer to the filename part of a path 199 */ | 171{ 172 va_list ap; 173 size_t len; 174 char *msg; 175 int r; 176 177 va_start(ap, fmt); 178 len = vasprintf(&msg, fmt, ap); --- 14 unchanged lines hidden (view full) --- 193 } 194 195 return _ftp_chkerr(cd); 196} 197 198/* 199 * Return a pointer to the filename part of a path 200 */ |
200static char * 201_ftp_filename(char *file) | 201static const char * 202_ftp_filename(const char *file) |
202{ 203 char *s; 204 205 if ((s = strrchr(file, '/')) == NULL) 206 return file; 207 else 208 return s + 1; 209} 210 211/* 212 * Change working directory to the directory that contains the 213 * specified file. 214 */ 215static int | 203{ 204 char *s; 205 206 if ((s = strrchr(file, '/')) == NULL) 207 return file; 208 else 209 return s + 1; 210} 211 212/* 213 * Change working directory to the directory that contains the 214 * specified file. 215 */ 216static int |
216_ftp_cwd(int cd, char *file) | 217_ftp_cwd(int cd, const char *file) |
217{ 218 char *s; 219 int e; 220 221 if ((s = strrchr(file, '/')) == NULL || s == file) { 222 e = _ftp_cmd(cd, "CWD /"); 223 } else { 224 e = _ftp_cmd(cd, "CWD %.*s", s - file, file); --- 4 unchanged lines hidden (view full) --- 229 } 230 return 0; 231} 232 233/* 234 * Request and parse file stats 235 */ 236static int | 218{ 219 char *s; 220 int e; 221 222 if ((s = strrchr(file, '/')) == NULL || s == file) { 223 e = _ftp_cmd(cd, "CWD /"); 224 } else { 225 e = _ftp_cmd(cd, "CWD %.*s", s - file, file); --- 4 unchanged lines hidden (view full) --- 230 } 231 return 0; 232} 233 234/* 235 * Request and parse file stats 236 */ 237static int |
237_ftp_stat(int cd, char *file, struct url_stat *us) | 238_ftp_stat(int cd, const char *file, struct url_stat *us) |
238{ | 239{ |
239 char *ln, *s; | 240 char *ln; 241 const char *s; |
240 struct tm tm; 241 time_t t; 242 int e; 243 244 us->size = -1; 245 us->atime = us->mtime = 0; 246 247 if ((s = strrchr(file, '/')) == NULL) --- 194 unchanged lines hidden (view full) --- 442 free(io); 443 return f; 444} 445 446/* 447 * Transfer file 448 */ 449static FILE * | 242 struct tm tm; 243 time_t t; 244 int e; 245 246 us->size = -1; 247 us->atime = us->mtime = 0; 248 249 if ((s = strrchr(file, '/')) == NULL) --- 194 unchanged lines hidden (view full) --- 444 free(io); 445 return f; 446} 447 448/* 449 * Transfer file 450 */ 451static FILE * |
450_ftp_transfer(int cd, char *oper, char *file, 451 int mode, off_t offset, char *flags) | 452_ftp_transfer(int cd, const char *oper, const char *file, 453 int mode, off_t offset, const char *flags) |
452{ 453 struct sockaddr_storage sin; 454 struct sockaddr_in6 *sin6; 455 struct sockaddr_in *sin4; 456 int low, pasv, verbose; 457 int e, sd = -1; 458 socklen_t l; 459 char *s; --- 251 unchanged lines hidden (view full) --- 711 close(sd); 712 return NULL; 713} 714 715/* 716 * Log on to FTP server 717 */ 718static int | 454{ 455 struct sockaddr_storage sin; 456 struct sockaddr_in6 *sin6; 457 struct sockaddr_in *sin4; 458 int low, pasv, verbose; 459 int e, sd = -1; 460 socklen_t l; 461 char *s; --- 251 unchanged lines hidden (view full) --- 713 close(sd); 714 return NULL; 715} 716 717/* 718 * Log on to FTP server 719 */ 720static int |
719_ftp_connect(struct url *url, struct url *purl, char *flags) | 721_ftp_connect(struct url *url, struct url *purl, const char *flags) |
720{ 721 int cd, e, direct, verbose; 722#ifdef INET6 723 int af = AF_UNSPEC; 724#else 725 int af = AF_INET; 726#endif 727 const char *logname; | 722{ 723 int cd, e, direct, verbose; 724#ifdef INET6 725 int af = AF_UNSPEC; 726#else 727 int af = AF_INET; 728#endif 729 const char *logname; |
728 char *user, *pwd; | 730 const char *user; 731 const char *pwd; |
729 char localhost[MAXHOSTNAMELEN]; 730 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1]; 731 732 direct = CHECK_FLAG('d'); 733 verbose = CHECK_FLAG('v'); 734 if (CHECK_FLAG('4')) 735 af = AF_INET; 736 else if (CHECK_FLAG('6')) --- 100 unchanged lines hidden (view full) --- 837 && (strcmp(url->pwd, cached_host.pwd) == 0) 838 && (url->port == cached_host.port)); 839} 840 841/* 842 * Check the cache, reconnect if no luck 843 */ 844static int | 732 char localhost[MAXHOSTNAMELEN]; 733 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1]; 734 735 direct = CHECK_FLAG('d'); 736 verbose = CHECK_FLAG('v'); 737 if (CHECK_FLAG('4')) 738 af = AF_INET; 739 else if (CHECK_FLAG('6')) --- 100 unchanged lines hidden (view full) --- 840 && (strcmp(url->pwd, cached_host.pwd) == 0) 841 && (url->port == cached_host.port)); 842} 843 844/* 845 * Check the cache, reconnect if no luck 846 */ 847static int |
845_ftp_cached_connect(struct url *url, struct url *purl, char *flags) | 848_ftp_cached_connect(struct url *url, struct url *purl, const char *flags) |
846{ 847 int e, cd; 848 849 cd = -1; 850 851 /* set default port */ 852 if (!url->port) 853 url->port = _fetch_default_port(url->scheme); --- 42 unchanged lines hidden (view full) --- 896 } 897 return NULL; 898} 899 900/* 901 * Get and stat file 902 */ 903FILE * | 849{ 850 int e, cd; 851 852 cd = -1; 853 854 /* set default port */ 855 if (!url->port) 856 url->port = _fetch_default_port(url->scheme); --- 42 unchanged lines hidden (view full) --- 899 } 900 return NULL; 901} 902 903/* 904 * Get and stat file 905 */ 906FILE * |
904fetchXGetFTP(struct url *url, struct url_stat *us, char *flags) | 907fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags) |
905{ 906 struct url *purl; 907 int cd; 908 909 /* get the proxy URL, and check if we should use HTTP instead */ 910 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 911 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 912 return _http_request(url, "GET", us, purl, flags); --- 21 unchanged lines hidden (view full) --- 934 /* initiate the transfer */ 935 return _ftp_transfer(cd, "RETR", url->doc, O_RDONLY, url->offset, flags); 936} 937 938/* 939 * Get file 940 */ 941FILE * | 908{ 909 struct url *purl; 910 int cd; 911 912 /* get the proxy URL, and check if we should use HTTP instead */ 913 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 914 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 915 return _http_request(url, "GET", us, purl, flags); --- 21 unchanged lines hidden (view full) --- 937 /* initiate the transfer */ 938 return _ftp_transfer(cd, "RETR", url->doc, O_RDONLY, url->offset, flags); 939} 940 941/* 942 * Get file 943 */ 944FILE * |
942fetchGetFTP(struct url *url, char *flags) | 945fetchGetFTP(struct url *url, const char *flags) |
943{ 944 return fetchXGetFTP(url, NULL, flags); 945} 946 947/* 948 * Put file 949 */ 950FILE * | 946{ 947 return fetchXGetFTP(url, NULL, flags); 948} 949 950/* 951 * Put file 952 */ 953FILE * |
951fetchPutFTP(struct url *url, char *flags) | 954fetchPutFTP(struct url *url, const char *flags) |
952{ 953 struct url *purl; 954 int cd; 955 956 /* get the proxy URL, and check if we should use HTTP instead */ 957 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 958 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 959 /* XXX HTTP PUT is not implemented, so try without the proxy */ --- 17 unchanged lines hidden (view full) --- 977 return _ftp_transfer(cd, CHECK_FLAG('a') ? "APPE" : "STOR", 978 url->doc, O_WRONLY, url->offset, flags); 979} 980 981/* 982 * Get file stats 983 */ 984int | 955{ 956 struct url *purl; 957 int cd; 958 959 /* get the proxy URL, and check if we should use HTTP instead */ 960 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 961 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) 962 /* XXX HTTP PUT is not implemented, so try without the proxy */ --- 17 unchanged lines hidden (view full) --- 980 return _ftp_transfer(cd, CHECK_FLAG('a') ? "APPE" : "STOR", 981 url->doc, O_WRONLY, url->offset, flags); 982} 983 984/* 985 * Get file stats 986 */ 987int |
985fetchStatFTP(struct url *url, struct url_stat *us, char *flags) | 988fetchStatFTP(struct url *url, struct url_stat *us, const char *flags) |
986{ 987 struct url *purl; 988 int cd; 989 990 /* get the proxy URL, and check if we should use HTTP instead */ 991 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 992 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) { 993 FILE *f; --- 20 unchanged lines hidden (view full) --- 1014 1015 /* stat file */ 1016 return _ftp_stat(cd, url->doc, us); 1017} 1018 1019/* 1020 * List a directory 1021 */ | 989{ 990 struct url *purl; 991 int cd; 992 993 /* get the proxy URL, and check if we should use HTTP instead */ 994 if (!CHECK_FLAG('d') && (purl = _ftp_get_proxy()) != NULL) { 995 if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) { 996 FILE *f; --- 20 unchanged lines hidden (view full) --- 1017 1018 /* stat file */ 1019 return _ftp_stat(cd, url->doc, us); 1020} 1021 1022/* 1023 * List a directory 1024 */ |
1022extern void warnx(char *, ...); | |
1023struct url_ent * | 1025struct url_ent * |
1024fetchListFTP(struct url *url, char *flags) | 1026fetchListFTP(struct url *url, const char *flags) |
1025{ 1026 warnx("fetchListFTP(): not implemented"); 1027 return NULL; 1028} | 1027{ 1028 warnx("fetchListFTP(): not implemented"); 1029 return NULL; 1030} |