summaryrefslogtreecommitdiffstats
path: root/lib/libftpio/ftp_pkg.c
blob: d60596d303486af4677870b22363b89da1b77c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c)1995, 1996 Jordan Hubbard
 *
 * All rights reserved.
 *
 * This source code may be used, modified, copied, distributed, and
 * sold, in both source and binary form provided that the above
 * copyright and these terms are retained, verbatim, as the first
 * lines of this file.  Under no circumstances is the author
 * responsible for the proper functioning of the software nor does
 * the author assume any responsibility for damages incurred with
 * its use.
 *
 * $Id: ftp_pkg.c,v 1.1.1.1 1996/06/17 12:26:06 jkh Exp $
 *
 * TCL Interface code for functions provided by the ftp library.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <ftpio.h>
#include "ftp_pkg.h"

#ifndef TRUE
#define TRUE	(1)
#define FALSE	(0)
#endif

#define CHECK_ARGS(cnt, myname, str) \
if (argc <= (cnt)) { sprintf(interp->result, "usage: %s %s", myname, str); return TCL_ERROR; }

#define USAGE(myname, msg) \
{ fprintf(stderr, "%s: %s\n", myname, msg); return TCL_ERROR; }


/* Registration function */
int
Ftp_Init(Tcl_Interp *interp)
{
    Tcl_CreateCommand (interp, "ftp_login",	Ftp_login,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_chdir",	Ftp_chdir,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_getsize",	Ftp_getsize,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_get",	Ftp_get,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_put",	Ftp_put,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_binary",	Ftp_binary,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_passive",	Ftp_passive,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_get_url",	Ftp_get_url,		NULL, NULL);
    Tcl_CreateCommand (interp, "ftp_put_url",	Ftp_put_url,		NULL, NULL);
    return TCL_OK;
}

/*
 * ftp_login  host user passwd port
 *  -- returns new fileId
 * 
 */
int
Ftp_login(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;
    char *user, *pass;
    int port;

    CHECK_ARGS(1, argv[0], "host [user] [passwd] [port]");

    user = (argc > 2) ? argv[2] : "ftp";
    pass = (argc > 3) ? argv[3] : "setup@";
    port = (argc > 4) ? atoi(argv[4]) : 21;
    /* Debug("ftp_pkg: attempt login to host %s using %s/%s (port %d)", argv[1], user, pass, port); */
    fp = ftpLogin(argv[1], user, pass, port);
    if (fp) {
	/* Debug("ftp_pkg: logged successfully into host %s", argv[1]); */
	Tcl_EnterFile(interp, fp, TCL_FILE_READABLE | TCL_FILE_WRITABLE);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: login operation failed for host %s", argv[1]); */
    return TCL_ERROR;
}

/*
 * ftp_chdir  file-handle newdir
 *  -- returns status
 */
int
Ftp_chdir(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;

    CHECK_ARGS(2, argv[0], "fileId directory");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: attempt chdir to dir %s", argv[2]); */
    if (!ftpChdir(fp, argv[2])) {
	/* Debug("ftp_pkg: chdir successful"); */
	return TCL_OK;
    }
    /* Debug("ftp_pkg: chdir failed"); */
    return TCL_ERROR;
}

/*
 * ftp_getsize  file-handle filename
 *  -- returns size
 */
int
Ftp_getsize(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;
    int sz;

    CHECK_ARGS(2, argv[0], "fileId filename");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: attempt to get size of %s", argv[2]); */
    if ((sz = ftpGetSize(fp, argv[2])) >= 0) {
	/* Debug("ftp_pkg: getsize successful (%d)", sz); */
	sprintf(interp->result, "%d", sz);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: chdir failed"); */
    return TCL_ERROR;
}

/*
 * ftp_get  fileId filename
 *  -- returns new fileId for filename
 * 
 */
int
Ftp_get(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp, *fp2;

    CHECK_ARGS(2, argv[0], "fileId filename");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: attempt to get file %s", argv[2]); */
    fp2 = ftpGet(fp, argv[2]);
    if (fp2) {
	/* Debug("ftp_pkg: get operation successful for: %s", argv[2]); */
	Tcl_EnterFile(interp, fp2, TCL_FILE_READABLE);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: get operation failed for file %s", argv[2]); */
    return TCL_ERROR;
}

/*
 * ftp_put  fileId filename
 *  -- returns new fileId for filename
 * 
 */
int
Ftp_put(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp, *fp2;

    CHECK_ARGS(2, argv[0], "fileId filename");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: attempt to put file %s", argv[2]); */
    fp2 = ftpPut(fp, argv[2]);
    if (fp2) {
	/* Debug("ftp_pkg: put operation successful for: %s", argv[2]); */
	Tcl_EnterFile(interp, fp2, TCL_FILE_READABLE);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: put operation failed for file %s", argv[2]); */
    return TCL_ERROR;
}

/*
 * ftp_binary  fileId value
 *  -- Set binary mode to truth value for FTP session represented by fileId
 * 
 */
int
Ftp_binary(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;

    CHECK_ARGS(1, argv[0], "fileId");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: set binary mode"); */
    ftpBinary(fp);
    return TCL_OK;
}

/*
 * ftp_passive  fileId value
 *  -- Set passive mode to truth value for FTP session represented by fileId
 * 
 */
int
Ftp_passive(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;

    CHECK_ARGS(2, argv[0], "fileId bool");
    if (Tcl_GetOpenFile(interp, argv[1], TRUE, TRUE, &fp) != TCL_OK)
	return TCL_ERROR;
    /* Debug("ftp_pkg: set passive mode to %d", atoi(argv[2])); */
    ftpPassive(fp, atoi(argv[2]));
    return TCL_OK;
}

/*
 * ftp_get_url  URL user pass
 *  -- Return new fileId for open URL (using user and pass to log in)
 * 
 */
int
Ftp_get_url(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;
    char *user, *pass;

    CHECK_ARGS(1, argv[0], "URL [username] [password]");
    user = (argc > 2) ? argv[2] : "ftp";
    pass = (argc > 3) ? argv[3] : "setup@";
    /* Debug("ftp_pkg: attempt to get URL %s as %s/%s", argv[1], user, pass); */
    fp = ftpGetURL(argv[1], user, pass);
    if (fp) {
	/* Debug("ftp_pkg: get URL successful"); */
	Tcl_EnterFile(interp, fp, TCL_FILE_READABLE);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: get URL failed"); */
    return TCL_ERROR;
}

/*
 * ftp_put_url  URL user pass
 *  -- Return new fileId for open url (using user and pass to log in)
 * 
 */
int
Ftp_put_url(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
    FILE *fp;
    char *user, *pass;

    CHECK_ARGS(1, argv[0], "URL [username] [password]");
    user = (argc > 2) ? argv[2] : "ftp";
    pass = (argc > 3) ? argv[3] : "setup@";
    /* Debug("ftp_pkg: attempt to put URL %s as %s/%s", argv[1], user, pass); */
    fp = ftpPutURL(argv[1], user, pass);
    if (fp) {
	/* Debug("ftp_pkg: put URL successful"); */
	Tcl_EnterFile(interp, fp, TCL_FILE_READABLE);
	return TCL_OK;
    }
    /* Debug("ftp_pkg: put URL failed"); */
    return TCL_ERROR;
}
OpenPOWER on IntegriCloud