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

[tyndur-devel] [PATCH] touch.c in c/shell/cmds plus Änderungen an shell.c und shell.h damit touch von der Shell auf aufgerufen werden kann



---
 src/modules/c/shell/cmds/touch.c |  113 ++++++++++++++++++++++++++++++++++++++
 src/modules/c/shell/shell.c      |    1 +
 src/modules/c/shell/shell.h      |    1 +
 3 files changed, 115 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/c/shell/cmds/touch.c

diff --git a/src/modules/c/shell/cmds/touch.c b/src/modules/c/shell/cmds/touch.c
new file mode 100644
index 0000000..a4462f3
--- /dev/null
+++ b/src/modules/c/shell/cmds/touch.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Alexander Hartmut Kluth.
+ *
+ * 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 <stdint.h>
+#include <string.h>
+#include <dir.h>
+#include <io.h>
+
+#include "types.h"
+#include "stdlib.h"
+#include "stdio.h"
+#include "unistd.h"
+#include <lost/config.h>
+
+
+void touch_display_usage(void);
+
+#ifdef CONFIG_SHELL_BUILTIN_ONLY
+    int shell_command_touch(int argc, char* argv[], const char* args)
+#else
+    int main(int argc, char* argv[])
+#endif
+{
+    if (argc < 2) {
+        touch_display_usage();
+        return -1;
+    }
+    
+    int ignore_existing_file = FALSE;
+	FILE* new_filehandle;
+    char* new_file;
+    
+    if (argc == 3) {
+    	if ((strcmp(argv[1], "-i")) == 0 || (strcmp(argv[1], "--ignore") == 0))  {
+    		ignore_existing_file = TRUE;
+    		new_file = argv[2];
+    	} else {
+    		touch_display_usage();
+    		return -1;
+    	}
+    } else {
+    	new_file = argv[1];
+    }
+
+	// Versuchen, die Datei zum Lesen zu öffnen...
+    new_filehandle = fopen(new_file, "r");
+    
+    if (new_filehandle != NULL) {
+    	// ...wenn die Datei existiert, prüfen, ob sie überschrieben werden soll
+    	if (ignore_existing_file) {
+    		fclose(new_filehandle);
+    		
+    		int result = io_remove_link(new_file);
+    
+    		if (result != 0) {
+        		fprintf(stderr, "Fehler beim Löschen der existierenden Datei!\n");
+        		return EXIT_FAILURE;
+    		}
+    		
+    		new_filehandle = fopen(new_file, "w");
+    		fclose(new_filehandle);
+    	} else {
+    		fprintf(stderr, "Fehler: Datei %s existiert bereits!\n", new_file);
+    		return EXIT_FAILURE;
+    	}
+    } else {
+    	fclose(new_filehandle);
+    	
+    	new_filehandle = fopen(new_file, "w");
+    	fclose(new_filehandle);
+    }
+    
+    return EXIT_SUCCESS;
+}
+
+
+void touch_display_usage()
+{
+    puts("\nAufruf: touch[-i|--ignore] Datei\n");
+}
+
diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c
index cb5de50..7c3b6be 100644
--- a/src/modules/c/shell/shell.c
+++ b/src/modules/c/shell/shell.c
@@ -99,6 +99,7 @@ shell_command_t shell_commands[] = {
     {"stat",        &shell_command_stat},
     {"sleep",       &shell_command_sleep},
     {"bench",       &shell_command_bench},
+    {"touch", 		&shell_command_touch},
 #endif
     {NULL,          NULL}
 };
diff --git a/src/modules/c/shell/shell.h b/src/modules/c/shell/shell.h
index 4a45786..6185590 100644
--- a/src/modules/c/shell/shell.h
+++ b/src/modules/c/shell/shell.h
@@ -83,6 +83,7 @@ int shell_command_clear(int argc, char* argv[], const char* args);
     int shell_command_stat(int argc, char* argv[], const char* args);
     int shell_command_sleep(int argc, char* argv[], const char* args);
     int shell_command_bench(int argc, char* argv[], const char* args);
+    int shell_command_touch(int argc, char* argv[], const char* args);
 #endif
 
 #endif
-- 
1.6.0.4