I need to implement a prompt of commands (mere academic project). The
function presents errors in the call execve, as parameter. It follows
the code below. I´m using gcc... thanks.
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
void type_prompt(); //shows the prompt
void parser(char linha[] , char command[] , char **params ); // it
separates the command of the parameters
void read_command( char command[] , char params[][] ); // read the
command line
#define COMANDOSPERMITIDOS 9 //amount of command args
#define TAMMAX 100 //length of string allowed
int main (void) {
char command[TAMMAX] , params[COMANDOSPERMITIDOS][TAMMAX];
int pid , status;
while(1){
type_prompt();
read_command(command,params);
pid = fork();
if (pid < 0 ){
printf("Unable to fork");
continue;
}
if(pid != 0) {
waitpid(-1,&status,0);
}else {
execve(command , (char*)params ,0); // the error
appears here
}
}
getch();
exit(0);
return 0 ;
}
void type_prompt(){
printf("\n$ ");
}
void read_command( char command[] , char params[][] ){
char linha[TAMMAX];
gets(linha); //reading the command line from user
parser( linha , command , params );
}
void parser( char linha[] , char command[] , char **params ){
int i = 0 ;
int j = 0 ;
int k = 0 ;
for ( i = 0 ; i < TAMMAX ; i++ ){
if( linha[i] != '\0' ){
if ( linha[i] != ' ' ){ // isolating the command
command[i] = linha[i];
}else{
command[i] = '\0';
break;
}
} else break;
}
j = 0 ;
k = 0;
i = 0 ;
for( j = 0 ; j < TAMMAX ; j++ ){
if( linha[j] != '\0' ){ //repetir até o fim da string
if( linha[j] != ' ' ){ // isolating the params
params[i][k] = linha[j];
k++;
}
else{
params[i][k] = '\0';
k = 0 ;
i++;
}
} else{
params[i][k] = '\0';
k = 0 ;
i++;
break;
}
}
while (i < COMANDOSPERMITIDOS){
params[i][0] = '\0';
i++;
}
}