diff --git a/half.c b/half.c new file mode 100644 index 0000000..ee1e373 --- /dev/null +++ b/half.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = n / 2; + printf("Halving, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} + diff --git a/square.c b/square.c new file mode 100644 index 0000000..f9f7146 --- /dev/null +++ b/square.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = n * n; + printf("Squaring, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} + diff --git a/twice.c b/twice.c new file mode 100644 index 0000000..1dca0cb --- /dev/null +++ b/twice.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +char* itoa(int val, int base){ + + static char buf[32] = {0}; + int i = 30; + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + return &buf[i+1]; + +} + +int main(int argc, char* argv[]) { + if (argc == 1) + exit(0); + + int n = atoi(argv[argc - 1]); + int res = 2 * n; + printf("Doubling, Current process id: %u, Current result: %d\n", getpid(), res); + + char* v[argc]; + for (int i = 1; i < (argc-1) ; i++) { + v[i-1] = argv[i]; + } + + v[argc-2] = itoa(res, 10); + v[argc-1] = NULL; + + execvp(v[0], v); +} +