FTP: Difference between revisions

Content added Content deleted
(initial submission of php 7.x solution to this problem)
(Added Wren)
Line 1,530: Line 1,530:
}
}
</lang>
</lang>

=={{header|Wren}}==
{{trans|C}}
{{libheader|ftplib}}
An embedded program so we can ask the C host to communicate with ''ftplib'' for us.
<lang ecmascript>/* ftp.wren */

var FTPLIB_CONNMODE = 1
var FTPLIB_PASSIVE = 1
var FTPLIB_ASCII = 65 // 'A'

foreign class Ftp {
foreign static init()

construct connect(host) {}

foreign login(user, pass)

foreign options(opt, val)

foreign chdir(path)

foreign dir(outputFile, path)

foreign get(output, path, mode)

foreign quit()
}

Ftp.init()
var ftp = Ftp.connect("ftp.easynet.fr")
ftp.login("anonymous", "ftptest@example.com")
ftp.options(FTPLIB_CONNMODE, FTPLIB_PASSIVE)
ftp.chdir("/debian/")
ftp.dir("", ".")
ftp.get("ftp.README", "README", FTPLIB_ASCII)
ftp.quit()</lang>
<br>
We now embed this in the following C program, compile and run it.
<lang c>#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <ftplib.h>
#include "wren.h"

/* C <=> Wren interface functions */

void C_init(WrenVM* vm) {
FtpInit();
}

void C_ftpAllocate(WrenVM* vm) {
netbuf *nbuf;
const char *host = wrenGetSlotString(vm, 1);
FtpConnect(host, &nbuf);
netbuf** pnbuf = (netbuf**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(netbuf*));
*pnbuf = nbuf;
}

void C_login(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
const char *user = wrenGetSlotString(vm, 1);
const char *pass = wrenGetSlotString(vm, 2);
FtpLogin(user, pass, nbuf);
}

void C_options(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
int opt = (int)wrenGetSlotDouble(vm, 1);
long val = (long)wrenGetSlotDouble(vm, 2);
FtpOptions(opt, val, nbuf);
}

void C_chdir(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
const char *path = wrenGetSlotString(vm, 1);
FtpChdir(path, nbuf);
}

void C_dir(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
const char *outputFile = wrenGetSlotString(vm, 1);
if (strlen(outputFile) == 0) outputFile = NULL;
const char *path = wrenGetSlotString(vm, 2);
FtpDir(outputFile, path, nbuf);
}

void C_get(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
const char *output = wrenGetSlotString(vm, 1);
if (strlen(output) == 0) output = NULL;
const char *path = wrenGetSlotString(vm, 2);
char mode = (char)wrenGetSlotDouble(vm, 3);
FtpGet(output, path, mode, nbuf);
}

void C_quit(WrenVM* vm) {
netbuf* nbuf = *(netbuf**)wrenGetSlotForeign(vm, 0);
FtpQuit(nbuf);
}

WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Ftp") == 0) {
methods.allocate = C_ftpAllocate;
}
}
return methods;
}

WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Ftp") == 0) {
if ( isStatic && strcmp(signature, "init()") == 0) return C_init;
if (!isStatic && strcmp(signature, "login(_,_)") == 0) return C_login;
if (!isStatic && strcmp(signature, "options(_,_)") == 0) return C_options;
if (!isStatic && strcmp(signature, "chdir(_)") == 0) return C_chdir;
if (!isStatic && strcmp(signature, "dir(_,_)") == 0) return C_dir;
if (!isStatic && strcmp(signature, "get(_,_,_)") == 0) return C_get;
if (!isStatic && strcmp(signature, "quit()") == 0) return C_quit;
}
}
return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}

void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}

char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}

int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "ftp.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>

{{out}}
Sample output:
<pre>
drwxr-xr-x 25 1002 1002 4096 Aug 15 06:55 dists
drwxr-xr-x 4 1002 1002 4096 Sep 27 07:52 doc
-rw-r--r-- 1 1002 1002 254738 Sep 27 08:21 extrafiles
drwxr-xr-x 3 1002 1002 4096 Sep 27 08:15 indices
-rw-r--r-- 1 1002 1002 14538903 Sep 27 08:15 ls-lR.gz
drwxr-xr-x 5 1002 1002 4096 Dec 19 2000 pool
drwxr-xr-x 4 1002 1002 4096 Nov 17 2008 project
-rw-r--r-- 1 1002 1002 1320 Aug 14 09:28 README
-rw-r--r-- 1 1002 1002 1290 Jun 26 2010 README.CD-manufacture
-rw-r--r-- 1 1002 1002 3203 Aug 14 09:27 README.html
-rw-r--r-- 1 1002 1002 291 Mar 4 2017 README.mirrors.html
-rw-r--r-- 1 1002 1002 86 Mar 4 2017 README.mirrors.txt
drwxr-xr-x 3 1002 1002 4096 Oct 10 2012 tools
drwxr-xr-x 26 1002 1002 4096 Aug 14 13:55 zzz-dists
</pre>


=={{header|zkl}}==
=={{header|zkl}}==