From 71af5b61eb8a5081801e2d6abc500410603b811e Mon Sep 17 00:00:00 2001 From: b Date: Sat, 19 Dec 2020 00:27:04 +0100 Subject: [PATCH] Tool is ready. --- makefile | 13 +++++++++++++ runcwd.c | 27 +++++++++++++++++++++++++++ runcwdi.c | 35 +++++++++++++++++++++++++++++++++++ runcwdit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test.c | 23 +++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 makefile create mode 100644 runcwd.c create mode 100644 runcwdi.c create mode 100644 runcwdit.c create mode 100644 test.c diff --git a/makefile b/makefile new file mode 100644 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 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 +#include + +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 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 +#include +#include + +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 index 0000000..98742ba --- /dev/null +++ b/runcwdit.c @@ -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 +#include +#include + +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 +#include + +#include +#include + +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