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
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
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?
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.
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?