[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lost] [Patch] [3/3] Shell: cp



Mit den beiden neuen Funktionen kann die Shell dann ein bißchen kopieren lernen. ;-)
Index: cmds/cp.c
===================================================================
--- cmds/cp.c	(Revision 0)
+++ cmds/cp.c	(Revision 0)
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Kevin Wolf.
+ *
+ * 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 LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST 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 <stdint.h>
+#include <dir.h>
+#include <io.h>
+
+#include "types.h"
+#include "stdlib.h"
+#include "stdio.h"
+#include "unistd.h"
+#include "config.h"
+
+void cp_display_usage();
+
+#ifdef CONFIG_SHELL_BUILTIN_ONLY
+    int shell_command_cp(int argc, char* argv[], const char* args)
+#else
+    #define _USE_START_
+    #include "init.h"
+    int main(int argc, char* argv[])
+#endif
+{
+    if (argc != 3) {
+        cp_display_usage();
+        return -1;
+    }
+    
+    char* src_path = argv[1];
+    char* dst_path = argv[2];
+
+    FILE* src = fopen(src_path, "r");
+    if (src == NULL) {
+        return EXIT_FAILURE;        
+    }
+
+    FILE* dst;
+    if (is_directory(dst_path)) {
+        char* filename = io_split_filename(src_path);
+        size_t length = strlen(filename) + strlen(dst_path) + 1;
+
+        char dst_file_path[length + 1];
+        strcpy(dst_file_path, dst_path);
+        dst_file_path[strlen(dst_file_path) + 1] = '\0';
+        dst_file_path[strlen(dst_file_path)] = '/';
+        strcat(dst_file_path, filename);
+        dst_file_path[length] = '\0';
+
+        dst = fopen(dst_file_path, "w");
+    } else {
+        dst = fopen(dst_path, "w");
+    }
+
+    if (dst == NULL) {
+        fclose(src);
+        return EXIT_FAILURE;        
+    }
+
+    uint8_t buffer[1024];
+        
+    while (!feof(src)) { 
+        size_t length = fread(buffer, 1024, 1, src);
+        if (length < 0) {
+            fprintf(stderr, "Fehler beim Kopieren: %d\n", length);
+            fclose(src);
+            fclose(dst);
+            return EXIT_FAILURE;
+        }
+        
+        fwrite(buffer, length, 1, dst);
+    }
+
+    fclose(src);
+    fclose(dst);
+
+    return EXIT_SUCCESS;
+}
+
+void cp_display_usage()
+{
+    puts("\nAufruf: cp <Quelle> <Ziel>");
+}
+
Index: shell.c
===================================================================
--- shell.c	(Revision 608)
+++ shell.c	(Arbeitskopie)
@@ -89,7 +89,8 @@
     {"pstree",      &shell_command_pstree},
     {"pwd",         &shell_command_pwd},
     {"symlink",     &shell_command_symlink},
-    {"dbg_st",      &shell_command_dbg_st}
+    {"dbg_st",      &shell_command_dbg_st},
+    {"cp",          &shell_command_cp},
 #endif
 };
 
Index: shell.h
===================================================================
--- shell.h	(Revision 608)
+++ shell.h	(Arbeitskopie)
@@ -61,6 +61,7 @@
     int shell_command_pwd(int argc, char* argv[], const char* args);
     int shell_command_symlink(int argc, char* argv[], const char* args);
     int shell_command_dbg_st(int argc, char* argv[], const char* args);
+    int shell_command_cp(int argc, char* argv[], const char* args);
 #endif
 
 #endif