]> bicyclesonthemoon.info Git - botm/runcwd/commitdiff
Tool is ready.
authorb <rowerynaksiezycu@gmail.com>
Fri, 18 Dec 2020 23:27:04 +0000 (00:27 +0100)
committerb <rowerynaksiezycu@gmail.com>
Fri, 18 Dec 2020 23:27:04 +0000 (00:27 +0100)
makefile [new file with mode: 0644]
runcwd.c [new file with mode: 0644]
runcwdi.c [new file with mode: 0644]
runcwdit.c [new file with mode: 0644]
test.c [new file with mode: 0644]

diff --git a/makefile b/makefile
new file mode 100644 (file)
index 0000000..f2433c6
--- /dev/null
+++ b/makefile
@@ -0,0 +1,13 @@
+CC=/usr/bin/gcc
+CF=-g -Wall
+
+all: runcwd runcwdi test
+
+runcwd: runcwd.c
+       $(CC) $(CF) -o runcwd runcwd.c
+
+runcwdi: runcwdi.c
+       $(CC) $(CF) -o runcwdi runcwdi.c
+
+test: test.c
+       $(CC) $(CF) -o test test.c
diff --git a/runcwd.c b/runcwd.c
new file mode 100644 (file)
index 0000000..118f6a5
--- /dev/null
+++ b/runcwd.c
@@ -0,0 +1,27 @@
+//Niezła wymota
+//This generic wrapper allows to run a program with specified CWD. First argument is the CWD, second is the program to run, rest are the parameters passed to that program
+
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[], char *envp[])
+{
+       int r;
+       
+       if (argc<2) {
+               fprintf(stderr,"%s\n","CWD missing.");
+               return 1;
+       }
+       else if (argc <3) {
+               fprintf(stderr,"%s\n","Command missing.");
+               return 1;
+       }
+       
+       if (chdir(argv[1]))
+               fprintf(stderr,"Chdir to %s failed.\n",argv[1]);
+       
+       r=execve(argv[2],argv+2,envp);
+       
+       fprintf(stderr,"Failed to run %s.\n",argv[2]);
+       return r;
+}
diff --git a/runcwdi.c b/runcwdi.c
new file mode 100644 (file)
index 0000000..3f304b2
--- /dev/null
+++ b/runcwdi.c
@@ -0,0 +1,35 @@
+//Niezła wymota
+//This generic wrapper allows to run a program with specified CWD. First argument is the CWD (must end with "/", anything after it is ignored), second is the program to run, rest are the parameters passed to that program
+
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[], char *envp[])
+{
+       int r;
+       
+       if (argc<2) {
+               fprintf(stderr,"%s\n","CWD missing.");
+               return 1;
+       }
+       else if (argc <3) {
+               fprintf(stderr,"%s\n","Command missing.");
+               return 1;
+       }
+       
+       for (r=strlen(argv[1])-1; r>=0; --r)
+       {
+               if (argv[1][r] == '/')
+                       break;
+               argv[1][r]=0;
+       }
+       
+       if (chdir(argv[1]))
+               fprintf(stderr,"Chdir to %s failed.\n",argv[1]);
+       
+       r=execve(argv[2],argv+2,envp);
+       
+       fprintf(stderr,"Failed to run %s.\n",argv[2]);
+       return r;
+}
diff --git a/runcwdit.c b/runcwdit.c
new file mode 100644 (file)
index 0000000..98742ba
--- /dev/null
@@ -0,0 +1,46 @@
+//Niezła wymota
+//This generic wrapper allows to run a program with specified CWD. First argument is the CWD (must end with "/", anything after it is ignored), second is the program to run, rest are the parameters passed to that program
+
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[], char *envp[])
+{
+       int r;
+       FILE* file;
+       
+       if (argc<2) {
+               fprintf(stderr,"%s\n","CWD missing.");
+               return 1;
+       }
+       else if (argc <3) {
+               fprintf(stderr,"%s\n","Command missing.");
+               return 1;
+       }
+       
+       for (r=strlen(argv[1])-1; r>=0; --r)
+       {
+               if (argv[1][r] == '/')
+                       break;
+               argv[1][r]=0;
+       }
+       
+       if (chdir(argv[1]))
+               fprintf(stderr,"Chdir to %s failed.\n",argv[1]);
+       
+       file=fopen("/home/b/pro/runcwd/cwda.txt","a");
+       if (file != NULL) {
+               for (r=0; r<argc; ++r)
+               {
+                       fprintf (file,"%s%s",r?" ":"",argv[r]);
+               }
+               fputs ("\n", file);
+               fclose (file);
+       }       
+       
+       //r=execve(argv[2],argv+2,envp);
+       
+       //fprintf(stderr,"Failed to run %s.\n",argv[2]);
+       //return r;
+}
diff --git a/test.c b/test.c
new file mode 100644 (file)
index 0000000..5e7d5fd
--- /dev/null
+++ b/test.c
@@ -0,0 +1,23 @@
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <stdio.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main (int argc, char **argv){
+       FILE* file;
+       int i;
+       file=fopen("/home/b/pro/runcwd/cwda.txt","w");
+       if (file != NULL) {
+               fprintf(file,"%s\n",get_current_dir_name());
+               for (i=0; i<argc; ++i) {
+                       fprintf(file,"%s\n",argv[i]);
+               }
+               fclose (file);
+       }
+       printf("%s\n",get_current_dir_name());
+       for (i=0; i<argc; ++i) {
+               printf("%s\n",argv[i]);
+       }
+}