c - Using "execv" or "execvp" to morph a program to isPrime? -
i have program called 'prime.c' below:
#include "stdio.h" #include "stdlib.h" #include <unistd.h> int isprime(int number); void morph(char *number); int main(int argc, char *argv[]) { int i; int number; if (argc < 2) { printf("returning 2"); return(2); } else number = atoi(argv[1]); if (isprime(number)) { exit(1); } exit(0); } /* purpose: check if input number prime number input: number - number checked return: 0 - if number not prime number 1 - if number prime number */ int isprime(int number) { int i; for(i = 2; i*i < number; i++) { usleep(100); if (number % == 0) { printf("%d not prime number!",number); return(0); } } printf("%d prime number!",number); return(1); } void morph(char *number) { execvp(isprime.exe,number); }
which takes in number , checks see if it's prime number or not. create executable called 'isprime.exe' did using 'gcc -o isprime.exe prime.c -lm' , worked perfectly. next step i'm stuck, create function morph(char *number) takes in string number , morphs program isprime program using either execv or execvp. i've started create morph function can't work, appreciated!
if @ the reference see e.g. execvp
function takes string first argument, , argv
style array second argument.
the first argument path (relative current process current working directory, or absolute path) executable want run. second argument become argv
argument in new program.
so lets isprime.exe
in same directory current program, call this:
char *primeargv[] = { "isprime.exe", // first argument name of program number, // actual first argument null, // array must `null` terminated }; execvp("isprime.exe", primeargv);
Comments
Post a Comment