[tyndur-devel] [PATCH] FTP-Client
Paul Lange ·
*ftp: Kommando 'help' hinzugefügt und ein paar Änderungen übernommen Singed-off-by: Paul Lange <matheeguru at gmx.de> --- src/modules/c/ftp/Makefile.all | 7 + src/modules/c/ftp/command.c | 219 ++++++++++++++++++++++++++++++++++ src/modules/c/ftp/command.h | 68 +++++++++++ src/modules/c/ftp/ftp.c | 252 ++++++++++++++++++++++++++++++++++++++++ src/modules/c/ftp/ftp.h | 45 +++++++ src/modules/c/ftp/help.c | 52 ++++++++ src/modules/c/ftp/main.c | 167 ++++++++++++++++++++++++++ 7 files changed, 810 insertions(+), 0 deletions(-) create mode 100644 src/modules/c/ftp/Makefile.all create mode 100644 src/modules/c/ftp/command.c create mode 100644 src/modules/c/ftp/command.h create mode 100644 src/modules/c/ftp/ftp.c create mode 100644 src/modules/c/ftp/ftp.h create mode 100644 src/modules/c/ftp/help.c create mode 100644 src/modules/c/ftp/main.c diff --git a/src/modules/c/ftp/Makefile.all b/src/modules/c/ftp/Makefile.all new file mode 100644 index 0000000..fb31509 --- /dev/null +++ b/src/modules/c/ftp/Makefile.all @@ -0,0 +1,7 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/apps/ftp" +$LOST_TOOLS_LD -oftp -Ttext=0x40000000 *.o --start-group $2 --end-group + +mv ftp $1/apps/ diff --git a/src/modules/c/ftp/command.c b/src/modules/c/ftp/command.c new file mode 100644 index 0000000..361793d --- /dev/null +++ b/src/modules/c/ftp/command.c @@ -0,0 +1,219 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <types.h> +#include <ctype.h> +#include <libgen.h> +#include <sys/stat.h> +#include <readline/readline.h> +#include "ftp.h" + +#define BLOCK_128K 131072 + +extern char* host; +extern FILE* handle; + +void ftp_account(void) +{ + char* input, *prompt, *c; + + // falls keine Verbindung besteht, abbrechen + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + asprintf(&prompt, "\033[1;37mBenutzer (%s): \033[0m", host); + input = readline(prompt); + free(prompt); + + // sorgt dafür, dass die Leerzeichen entfernt werden + for (; isspace(*input); input++); + for (c = input; !isspace(*c) && *c; c++); + *c = '\0'; + + fprintf(handle, "USER %s\r\n", input); + fflush(handle); + response(); + + free(input); + input = readline("\033[1;37mPasswort: \033[0m"); + + for (; isspace(*input); input++); + for (c = input; !isspace(*c) && *c; c++); + *c = '\0'; + + fprintf(handle, "PASS %s\r\n", input); + fflush(handle); + response(); + free(input); +} + +void ftp_ls(void) +{ + FILE* data_handle; + char buffer[BLOCK_128K]; + + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + data_handle = ftp_data_connect(); + request("LIST"); + + while (!feof(data_handle)) { + fwrite(buffer, 1, fread(buffer, 1, BLOCK_128K, data_handle),stdout); + } + + // das verbraucht noch viel Zeit, da tyndur lange braucht für das fclose() + fclose(data_handle); + response(); + puts("\033[1;37mDatenverbindung beendet\033[0m"); +} + +void ftp_get(char* pathname) +{ + FILE* data_handle; + FILE* file_handle; + char* filename; + char buffer[BLOCK_128K]; + + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + filename = basename(pathname); + file_handle = fopen(filename, "w+"); + data_handle = ftp_data_connect(); + + fprintf(handle, "RETR %s\r\n", pathname); + fflush(handle); + + // falls 550 zurückgegeben wird, ist die Datei nicht vorhanden + response(); + + while (!feof(data_handle)) { + fwrite(buffer, 1, fread(buffer, 1, BLOCK_128K, data_handle),file_handle); + } + + fclose(file_handle); + fclose(data_handle); + response(); + puts("\033[1;37mDatenverbindung beendet\033[0m"); +} + +void ftp_put(char* pathname) +{ + FILE* data_handle; + FILE* file_handle; + char* filename; + char buffer[BLOCK_128K]; + size_t count; + + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + filename = basename(pathname); + file_handle = fopen(pathname, "r"); + data_handle = ftp_data_connect(); + + fprintf(handle, "STOR %s\r\n", filename); + fflush(handle); + response(); + + while (!feof(file_handle)) { + count = fread(buffer, 1, BLOCK_128K, file_handle); + fwrite(buffer, 1, count, data_handle); + } + + fclose(file_handle); + fclose(data_handle); + response(); + puts("\033[1;37mDatenverbindung beendet\033[0m"); +} + +void ftp_mkdir(const char* pathname) +{ + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + fprintf(handle, "MKD %s\r\n", pathname); + fflush(handle); + response(); +} + +void ftp_rmdir(const char* pathname) +{ + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + fprintf(handle, "RMD %s\r\n", pathname); + fflush(handle); + response(); +} + +void ftp_cd(const char* pathname) +{ + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + fprintf(handle, "CWD %s\r\n", pathname); + fflush(handle); + response(); +} + +void ftp_cdup(void) +{ + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + fprintf(handle, "%s\r\n", "CDUP"); + fflush(handle); + response(); +} diff --git a/src/modules/c/ftp/command.h b/src/modules/c/ftp/command.h new file mode 100644 index 0000000..a5a27e9 --- /dev/null +++ b/src/modules/c/ftp/command.h @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef COMMAND_H +#define COMMAND_H + +// listet die Dateien aus dem aktuellen Verzeichniss des FTP-Servers auf +void ftp_ls(void); + +// auf dem FTP-Server einloggen +void ftp_account(void); + +// erzeugt ein Verzeichniss auf dem FTP-Server +// @param Pfadname+Dateiname +void ftp_mkdir(const char*); + +// loescht ein Verzeichniss auf dem FTP-Server +// @param Pfadname+Dateiname +void ftp_rmdir(const char*); + +// wechselt das Verzeichniss auf dem FTP-Server +// @param Pfadname +void ftp_cd(const char*); + +// zeigt das Hauptverzeichniss auf dem FTP-Server an +void ftp_cdup(void); + +// uebertraegt eine Datei auf den FTP-Server +// @param Pfadname+Dateiname +void ftp_get(const char*); + +// speichert eine Datei vom FTP-Server +// @param Pfadname+Dateiname +void ftp_put(const char*); + +#endif diff --git a/src/modules/c/ftp/ftp.c b/src/modules/c/ftp/ftp.c new file mode 100644 index 0000000..6650565 --- /dev/null +++ b/src/modules/c/ftp/ftp.c @@ -0,0 +1,252 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCZLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <types.h> +#include <ctype.h> +#include <string.h> +#include <readline/readline.h> + +#define COMMAND_PORT 21 +#define LINE_LENGTH 80 + +extern char* host; +extern FILE* handle; + +/** + * sendet einen Befehl an den Server und gibt den Request aus + * + * @param command enthaelt den Befehl für den Server + */ +void request(const char* command) +{ + char buffer; + char response[LINE_LENGTH]; + bool line_end = TRUE; + int i = 0; + + fprintf(handle, "%s\r\n", command); + fflush(handle); + + while (line_end) { + while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) { + if (buffer != EOF) { + response[i++] = buffer; + } + } + response[i] = '\0'; + printf("%s\n", response); + line_end = FALSE; + + if ((i > 3) && (response[3] == '-')) { + line_end = TRUE; + i = 0; + } + } +} + + +/** + * gibt den Request string auf dem Bildschirm aus + */ +void response(void) +{ + char buffer; + char response[LINE_LENGTH]; + bool line_end = TRUE; + int i = 0; + + while (line_end) { + while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) { + if (buffer != EOF) { + response[i++] = buffer; + } + } + response[i] = '\0'; + printf("%s\n", response); + line_end = FALSE; + + if ((i > 3) && (response[3] == '-')) { + line_end = TRUE; + i = 0; + } + } +} + + +/** + * Stellt eine Verbindung zum Server her + */ +void ftp_connect(void) +{ + char* host_path; + char* prompt; + char* input; + char* c; + + if (handle) return; + puts("\033[1;37mVerbinde...\033[0m"); + asprintf(&host_path, "tcpip:/%s:%d", host, COMMAND_PORT); + handle = fopen(host_path, "r+"); + free(host_path); + + if (!handle) { + puts("\033[1;37mFehler: Host nicht erreichbar!\033[0m"); + handle = NULL; + return; + } + printf("\033[1;37mVerbunden mit %s, warte auf Antwort...\033[0m\n", host); + response(); + + asprintf(&prompt, "\033[1;37mBenutzer (%s): \033[0m", host); + input = readline(prompt); + free(prompt); + + // sorgt dafür, dass die Leerzeichen entfernt werden + for (; isspace(*input); input++); + for (c = input; !isspace(*c) && *c; c++); + *c = '\0'; + + fprintf(handle, "USER %s\r\n", input); + fflush(handle); + response(); + + input = readline("\033[1;37mPasswort: \033[0m"); + + for (; isspace(*input); input++); + for (c = input; !isspace(*c) && *c; c++); + *c = '\0'; + + fprintf(handle, "PASS %s\r\n", input); + fflush(handle); + response(); + + request("TYPE A"); +} + + +/** + * Schliest die Verbindung zum Server + */ +void ftp_disconnect(void) +{ + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung...\033[0m"); + return; + } + + request("QUIT"); + fclose(handle); + handle = NULL; + puts("\033[1;37mVerbindung getrennt...\033[0m"); +} + + +/** + * Öffnet eine passive Datenverbindung zum Server + * + * @return Handle zum Datenfluss + */ +FILE* ftp_data_connect(void) +{ + char buffer; + char* c, *host_path; + char response[LINE_LENGTH], portI[4], portII[4]; + bool line_end = TRUE; + unsigned int port, port_low; + int i = 0; + FILE* data_handle = NULL; + + puts("\033[1;37mDatenverbindung wird geöffnet...\033[0m"); + fprintf(handle, "%s\r\n", "PASV"); + fflush(handle); + + // hier wird statt response() aufzurufen, der Inhalt dieser Funktion + // wiedergegeben, da der Inhalt des response noch gebraucht wird + while (line_end) { + while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) { + if (buffer != EOF) { + response[i++] = buffer; + } + } + response[i] = '\0'; + printf("%s\n", response); + line_end = FALSE; + + if ((i > 3) && (response[3] == '-')) { + line_end = TRUE; + i = 0; + } + } + // bis zum zur Klammer Parsen + for (c = response; *c != '(' || *c == '\n'; c++); + + // hier wird der Port rausgeparst, der dann noch in Zahlenwerte umgewandelt + // werden sowie noch zusammengerechnet werden muss, da der Port so vom + // Server verschickt wird '(127, 0, 0, 1, p1,p2)'. Vorne die IP dahinter + // der Port, der so zusammengerechnet werden muss p1*256 + p2! + i = 0; + while (i < 4) { + for (; *(c++) != ',' || *c == '\n';); + i++; + } + + i = 0; + while (*c != ',' || *c == '\n') { + portI[i] = *c; + i++; + c++; + } + portI[i++] = '\0'; + c++; + i = 0; + while (*c != ')' || *c == '\n') { + portII[i] = *c; + i++; + c++; + } + portII[i++] = '\0'; + port = atoi(portI); + port *= 256; + port_low = atoi(portII); + port += port_low; + + asprintf(&host_path, "tcpip:/%s:%d", host, port); + data_handle = fopen(host_path, "r+"); + free(host_path); + + return data_handle; +} diff --git a/src/modules/c/ftp/ftp.h b/src/modules/c/ftp/ftp.h new file mode 100644 index 0000000..efb797e --- /dev/null +++ b/src/modules/c/ftp/ftp.h @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FTP_H +#define FTP_H + +extern void ftp_connect(void); +extern FILE* ftp_data_connect(void); +extern void ftp_disconnect(void); +extern void request(const char*); +extern void response(void); + +#endif diff --git a/src/modules/c/ftp/help.c b/src/modules/c/ftp/help.c new file mode 100644 index 0000000..3150c88 --- /dev/null +++ b/src/modules/c/ftp/help.c @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +char* help = +"FTP-Client Befehlsliste:\n\n" +"account - einloggen mit Benutzername und Passwort\n" +"ascii - ascii Mode einschalten\n" +"binary - binary Mode einschalten\n" +"cd - [Pfad] wechseln des Verzeichnisses auf dem Server\n" +"cdup - zeigt das Stammverzeichniss auf dem Server an\n" +"clear - löscht den Inhalt des Terminals\n" +"close - schließt die Verbindung zum Server\n" +"exit - beendet den FTP-Client\n" +"get - [Pfad+Dateiname] holt eine Datei vom Server\n" +"help - zeigt diese Hilfe an\n" +"mkdir - [Pfad+Ordnername] erstellt einen Ordner auf dem Server\n" +"open - [ftp.name.net] öffnet eine Verbindung zu einem Server\n" +"put - [Pfad+Dateiname] speichert eine Datei auf dem Server\n" +"quit - schließt die Verbindung zum Server und beendet den FTP-Client\n" +"rmdir - [Pfad+Ordnername] löscht einen Ordner auf dem Server\n"; diff --git a/src/modules/c/ftp/main.c b/src/modules/c/ftp/main.c new file mode 100644 index 0000000..48e4ac6 --- /dev/null +++ b/src/modules/c/ftp/main.c @@ -0,0 +1,167 @@ +/** + * Copyright (c) 2009 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Paul Lange. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the tyndur Project + * and its contributors. + * 4. Neither the name of the tyndur Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <readline/readline.h> +#include <types.h> +#include "command.h" +#include "ftp.h" + +char* host; +FILE* handle; + +extern char* help; + +int main(int argc, char* argv[]) +{ + bool prgm_end = FALSE; + char* input, *buffer; + + if (argc == 2) { + host = argv[1]; + ftp_connect(); + } + + // Bildet den Ablauf des kompletten Programmes + do { + input = readline("\033[1;37mFTP> \033[0m"); + // durchlaeuft den String bis zum ersten Zeichen + for (; isspace(*input); input++); + + if (!strncmp(input, "exit", 4)) { + prgm_end = TRUE; + } else if (!strncmp(input, "close", 5)) { + ftp_disconnect(); + } else if (!strncmp(input, "quit", 4)) { + ftp_disconnect(); + prgm_end = TRUE; + } else if (!strncmp(input, "help", 4)) { + printf(help); + } else if (!strncmp(input, "open", 4)) { + // parst den host aus dem string + input += 4; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + host = input; + ftp_connect(); + } + } else if (!strncmp(input, "account", 7)) { + ftp_account(); + } else if (!strncmp(input, "mkdir", 5)) { + input += 5; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + ftp_mkdir(input); + } + } else if (!strncmp(input, "rmdir", 5)) { + input += 5; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + ftp_rmdir(input); + } + } else if (!strncmp(input, "cdup", 4)) { + ftp_cdup(); + } else if (!strncmp(input, "ascii", 5)) { + // falls keine Verbindung besteht, abbrechen + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung..." + "\033[0m"); + continue; + } + request("TYPE A"); + } else if (!strncmp(input, "binary", 6)) { + if (handle == NULL) { + puts("\033[1;37mes besteht keine Serververbindung..." + "\033[0m"); + continue; + } + request("TYPE I"); + } else if (!strncmp(input, "clear", 5)) { + puts("\33[2J\33[H"); + } else if (!strncmp(input, "get", 3)) { + input += 3; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + ftp_get(input); + } + } else if (!strncmp(input, "put", 3)) { + input += 3; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + ftp_put(input); + } + } else if (!strncmp(input, "cd", 2)) { + input += 2; + for (; isspace(*input); input++); + if (*input != '\0') { + for (buffer = input; !isspace(*buffer) && *buffer; + buffer++); + *buffer = '\0'; + + ftp_cd(input); + } + } else if (!strncmp(input, "ls", 2)) { + ftp_ls(); + } else { + printf(help); + } + + free(input); + } while (!prgm_end); + return 0; +} -- 1.6.0.4