exec() functions

Hello,
I am struggling with the following situation

void CriaFilho(char *prog)
{
pid_t pid, pidw;
int status;
char *argexe[]={"gcc", "-o", prog, "", (char *) 0};
argexe[3]=strcpy(prog, ".c");
printf("\nExemplo de aplicacao 01 das funcoes fork()+exec()\n");
pid= fork();
if ( pid==-1 ) {
perror("Erro na funcao fork()");
exit(1);
}

if ( pid ) {
/* pid>0, codigo para o processo pai */
printf("Codigo do Pai : PID=%5d PPID=%5d\n", \
(int) getpid(), (int) getppid());
printf("Codigo do Pai : Iniciado wait()\n");
pidw= wait(&status);
}
else {
/* pid=0, codigo para o processo filho */
printf("Codigo do Filho: Substituir imagem do processo " \
"pela do comando ls e executar!\n");
execvp(func, argexe);
printf("Se esta mensagem aparecer ocorreu um erro!");
}

printf("Codigo do Pai : Comando ls -al executado!\n");
}

The variable *prog will come from main as a list of different files to compile. As part of the exercise, the list will not have the ".c" end and therefore I need to add it. The problem is that the way I am doing it does not work. Can anyone help me out?
Thanks
1
2
char *argexe[]={"gcc", "-o", prog, "", (char *) 0};
argexe[3]=strcpy(prog, ".c");
This is an error. You've defined a constant string "" and copy ".c" onto that space, overwritting what happens to follow it in memory.

Why not this?
 
char *argexe[]={"gcc", "-o", prog, ".c", (char *) 0};
Last edited on
Thanks for your help.
In fact I wasn't using strcpy but strcat,because what I want is this:
*prog is prog01
I want to pass the name prog01.c to the compiler.
Thanks anyway. Can you still give a clue.
Pedro
Well, prepare the string before setting up the array.
1
2
3
4
5
6
static char s_prog[32]; // is this long enough?
strncpy(s_prog, prog, sizeof(s_prog));
strncat(s_prog, ".c", sizeof(s_prog));
s_prog[sizeof(s_prog) - 1] = 0;

char *argexe[] = { "gcc", "-o", s_prog, 0};
Thanks. I am going to give it a try and let you know.
Hi, it did help. If you don't mind I am going to ask for help again.
Now that I am able to compile to programs I need to run them using some input.

I am designing a function for it and the start looks like this:

void runprg (char *pr0g, char *test)

now assume *prg comes with "prog01" and *test comes with "2 2 2"

I need to get following array:

*argexe[] = { "./prog01", "./prog01", "2", "2", "2", "prog01.txt, 0};
in order to run exec() function.

To get "./prog01", I used:
static char s_prog[32];
strncpy(s_prog, prog, sizeof(s_prog));
strncat("./", s_prog, sizeof(s_prog));
s_prog[sizeof(s_prog) - 1] = 0;
but keep getting segmentation error. What I am doing wrong?

Also, what is the best way to get the 2's I need.

Again thank you for your help
Quoting http://pubs.opengroup.org/onlinepubs/007908799/xsh/exec.html

The arguments represented by arg0, ... are pointers to null-terminated character strings. These strings constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename that is associated with the process being started by one of the exec functions.

Is this what you're looking for?
Yes it is. Thank you very much for your help.
I have managed to build the ./prog01 string
I am now trying to get the 2' into the array. Do you have any suggestion for the best way?
There's an example of execvp here:
http://www.cplusplus.com/forum/unices/65833/#msg356116
Thanks a lot. I have the program up and running.
Topic archived. No new replies allowed.