Deleted Added
full compact
ftp.c (40939) ftp.c (40975)
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

--- 11 unchanged lines hidden (view full) ---

20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
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

--- 11 unchanged lines hidden (view full) ---

20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: ftp.c,v 1.5 1998/08/17 09:30:19 des Exp $
28 * $Id: ftp.c,v 1.6 1998/11/05 19:48:17 des Exp $
29 */
30
31/*
32 * Portions of this code were taken from or based on ftpio.c:
33 *
34 * ----------------------------------------------------------------------------
35 * "THE BEER-WARE LICENSE" (Revision 42):
36 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you

--- 29 unchanged lines hidden (view full) ---

66#include <stdarg.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71
72#include "fetch.h"
73#include "common.h"
29 */
30
31/*
32 * Portions of this code were taken from or based on ftpio.c:
33 *
34 * ----------------------------------------------------------------------------
35 * "THE BEER-WARE LICENSE" (Revision 42):
36 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you

--- 29 unchanged lines hidden (view full) ---

66#include <stdarg.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71
72#include "fetch.h"
73#include "common.h"
74#include "ftperr.c"
74#include "ftperr.inc"
75
76#define FTP_DEFAULT_TO_ANONYMOUS
77#define FTP_ANONYMOUS_USER "ftp"
78#define FTP_ANONYMOUS_PASSWORD "ftp"
79#define FTP_DEFAULT_PORT 21
80
81#define FTP_OPEN_DATA_CONNECTION 150
82#define FTP_OK 200
83#define FTP_PASSIVE_MODE 227
84#define FTP_LOGGED_IN 230
85#define FTP_FILE_ACTION_OK 250
86#define FTP_NEED_PASSWORD 331
87#define FTP_NEED_ACCOUNT 332
88
89#define ENDL "\r\n"
90
75
76#define FTP_DEFAULT_TO_ANONYMOUS
77#define FTP_ANONYMOUS_USER "ftp"
78#define FTP_ANONYMOUS_PASSWORD "ftp"
79#define FTP_DEFAULT_PORT 21
80
81#define FTP_OPEN_DATA_CONNECTION 150
82#define FTP_OK 200
83#define FTP_PASSIVE_MODE 227
84#define FTP_LOGGED_IN 230
85#define FTP_FILE_ACTION_OK 250
86#define FTP_NEED_PASSWORD 331
87#define FTP_NEED_ACCOUNT 332
88
89#define ENDL "\r\n"
90
91static url_t cached_host;
91static struct url cached_host;
92static FILE *cached_socket;
93
94static char *_ftp_last_reply;
95
96/*
97 * Get server response, check that first digit is a '2'
98 */
99static int
100_ftp_chkerr(FILE *s, int *e)
101{
102 char *line;
103 size_t len;
92static FILE *cached_socket;
93
94static char *_ftp_last_reply;
95
96/*
97 * Get server response, check that first digit is a '2'
98 */
99static int
100_ftp_chkerr(FILE *s, int *e)
101{
102 char *line;
103 size_t len;
104 int err;
104
105 if (e)
106 *e = 0;
107
108 do {
109 if (((line = fgetln(s, &len)) == NULL) || (len < 4)) {
110 _fetch_syserr();
111 return -1;

--- 5 unchanged lines hidden (view full) ---

117#ifndef NDEBUG
118 fprintf(stderr, "\033[1m<<< ");
119 fprintf(stderr, "%*.*s", (int)len, (int)len, line);
120 fprintf(stderr, "\033[m");
121#endif
122
123 if (!isdigit(line[1]) || !isdigit(line[1])
124 || !isdigit(line[2]) || (line[3] != ' ')) {
105
106 if (e)
107 *e = 0;
108
109 do {
110 if (((line = fgetln(s, &len)) == NULL) || (len < 4)) {
111 _fetch_syserr();
112 return -1;

--- 5 unchanged lines hidden (view full) ---

118#ifndef NDEBUG
119 fprintf(stderr, "\033[1m<<< ");
120 fprintf(stderr, "%*.*s", (int)len, (int)len, line);
121 fprintf(stderr, "\033[m");
122#endif
123
124 if (!isdigit(line[1]) || !isdigit(line[1])
125 || !isdigit(line[2]) || (line[3] != ' ')) {
125 _ftp_seterr(-1);
126 _ftp_seterr(0);
126 return -1;
127 }
128
127 return -1;
128 }
129
129 _ftp_seterr((line[0] - '0') * 100 + (line[1] - '0') * 10 + (line[2] - '0'));
130 err = (line[0] - '0') * 100 + (line[1] - '0') * 10 + (line[2] - '0');
131 _ftp_seterr(err);
130
131 if (e)
132
133 if (e)
132 *e = fetchLastErrCode;
134 *e = err;
133
134 return (line[0] == '2') - 1;
135}
136
137/*
138 * Send a command and check reply
139 */
140static int

--- 219 unchanged lines hidden (view full) ---

360 _ftp_cmd(f, "QUIT" ENDL);
361 fclose(f);
362}
363
364/*
365 * Check if we're already connected
366 */
367static int
135
136 return (line[0] == '2') - 1;
137}
138
139/*
140 * Send a command and check reply
141 */
142static int

--- 219 unchanged lines hidden (view full) ---

362 _ftp_cmd(f, "QUIT" ENDL);
363 fclose(f);
364}
365
366/*
367 * Check if we're already connected
368 */
369static int
368_ftp_isconnected(url_t *url)
370_ftp_isconnected(struct url *url)
369{
370 return (cached_socket
371 && (strcmp(url->host, cached_host.host) == 0)
372 && (strcmp(url->user, cached_host.user) == 0)
373 && (strcmp(url->pwd, cached_host.pwd) == 0)
374 && (url->port == cached_host.port));
375}
376
377/*
378 * FTP session
379 */
380static FILE *
371{
372 return (cached_socket
373 && (strcmp(url->host, cached_host.host) == 0)
374 && (strcmp(url->user, cached_host.user) == 0)
375 && (strcmp(url->pwd, cached_host.pwd) == 0)
376 && (url->port == cached_host.port));
377}
378
379/*
380 * FTP session
381 */
382static FILE *
381fetchXxxFTP(url_t *url, char *oper, char *mode, char *flags)
383fetchXxxFTP(struct url *url, char *oper, char *mode, char *flags)
382{
383 FILE *cf = NULL;
384 int e;
385
386 /* set default port */
387 if (!url->port)
388 url->port = FTP_DEFAULT_PORT;
389

--- 7 unchanged lines hidden (view full) ---

397 /* connect to server */
398 if (!cf) {
399 cf = _ftp_connect(url->host, url->port, url->user, url->pwd);
400 if (!cf)
401 return NULL;
402 if (cached_socket)
403 _ftp_disconnect(cached_socket);
404 cached_socket = cf;
384{
385 FILE *cf = NULL;
386 int e;
387
388 /* set default port */
389 if (!url->port)
390 url->port = FTP_DEFAULT_PORT;
391

--- 7 unchanged lines hidden (view full) ---

399 /* connect to server */
400 if (!cf) {
401 cf = _ftp_connect(url->host, url->port, url->user, url->pwd);
402 if (!cf)
403 return NULL;
404 if (cached_socket)
405 _ftp_disconnect(cached_socket);
406 cached_socket = cf;
405 memcpy(&cached_host, url, sizeof(url_t));
407 memcpy(&cached_host, url, sizeof(struct url));
406 }
407
408 /* initiate the transfer */
409 return _ftp_transfer(cf, oper, url->doc, mode, (flags && strchr(flags, 'p')));
410}
411
412/*
413 * Itsy bitsy teeny weenie
414 */
415FILE *
408 }
409
410 /* initiate the transfer */
411 return _ftp_transfer(cf, oper, url->doc, mode, (flags && strchr(flags, 'p')));
412}
413
414/*
415 * Itsy bitsy teeny weenie
416 */
417FILE *
416fetchGetFTP(url_t *url, char *flags)
418fetchGetFTP(struct url *url, char *flags)
417{
418 return fetchXxxFTP(url, "RETR", "r", flags);
419}
420
421FILE *
419{
420 return fetchXxxFTP(url, "RETR", "r", flags);
421}
422
423FILE *
422fetchPutFTP(url_t *url, char *flags)
424fetchPutFTP(struct url *url, char *flags)
423{
424 if (flags && strchr(flags, 'a'))
425 return fetchXxxFTP(url, "APPE", "w", flags);
426 else return fetchXxxFTP(url, "STOR", "w", flags);
427}
425{
426 if (flags && strchr(flags, 'a'))
427 return fetchXxxFTP(url, "APPE", "w", flags);
428 else return fetchXxxFTP(url, "STOR", "w", flags);
429}
430
431extern void warnx(char *fmt, ...);
432int
433fetchStatFTP(struct url *url, struct url_stat *us, char *flags)
434{
435 warnx("fetchStatFTP(): not implemented");
436 return -1;
437}
438