]> git.webhop.me Git - lcd4linux.git/commitdiff
- plugin_file.c: add fileexist function
authorsvenhoefer <svenhoefer@svenhoefer.com>
Tue, 3 May 2016 09:44:27 +0000 (11:44 +0200)
committersvenhoefer <svenhoefer@svenhoefer.com>
Tue, 3 May 2016 10:27:37 +0000 (12:27 +0200)
plugin_file.c

index 8d6e548e9c8a246601a0db827d64b93f2cf4b541..b94bc7df763cd2dc0607652b4cc5cb98ad0c6247 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
 /* these should always be included */
 #include "debug.h"
@@ -86,6 +87,24 @@ static void my_readline(RESULT * result, RESULT * arg1, RESULT * arg2)
     SetResult(&result, R_STRING, &value);
 }
 
+/* function 'exist' */
+/* takes one argument, file name */
+/* returns 1 if found, 0 if not */
+
+static void my_exist(RESULT * result, RESULT * arg1)
+{
+       char *value;
+       value = strdup("0");
+
+       /* int access() return 0 if permitted, -1 otherwise */
+       if ( access(R2S(arg1), F_OK) == 0 ) {
+               value = strdup("1");
+       }
+
+       /* store result */
+       SetResult(&result, R_STRING, value);
+}
+
 /* plugin initialization */
 /* MUST NOT be declared 'static'! */
 int plugin_init_file(void)
@@ -95,6 +114,7 @@ int plugin_init_file(void)
     /* the second parameter is the number of arguments */
     /* -1 stands for variable argument list */
     AddFunction("file::readline", 2, my_readline);
+    AddFunction("file::exist", 1, my_exist);
 
     return 0;
 }