470,571 Members | 2,299 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

implementation of a shell

Hi,

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.... 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++;
}
}

Oct 17 '06 #1
4 6598

glmlima wrote:
Hi,

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.... thanks.
....
execve(command , (char*)params ,0); // the error
appears here
execve() is part of Unix, not part of C. If you want people in this
group to help (hint: they may well not have Unix manuals to hand), you
could at least give us the prototype of execve() from the relevant
header file and/or an extract from the manuals. You could also tell us
what the error message was.

Style hint - use "/*...*/" for comments rather than "//" , as they are
less of a nuisance to cut and paste
when/if they wrap.

As I have a suitable machine and compiler immediately to hand, I did
some playing.

My (gcc) compiler complained as follows:-

smallShell.c: In function `main':
smallShell.c:28: warning: passing arg 2 of `execve' from incompatible
pointer type
smallShell.c: In function `read_command':
smallShell.c:45: warning: passing arg 3 of `parser' from incompatible
pointer type

The manual page for execve says its prototype is :-

int execve(const char *filename, char *const argv [], char
*const envp[]);

Is "(char*)params" of type "char * []" ? No. Why did you cast?

The second warning relates to this code:-
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 ){

So the 3rd argument to parser is "char **", but you're passing char
[][] which is not the same thing.

Oct 17 '06 #2
I have that to simulate "argv []" of the main function like
int main (int argc, to char *argv [])...
I know that I must use pointer, but I don´t know how to...
I need help...

Oct 19 '06 #3
glmlima wrote:
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
use <for system headers.
int main (void) {
char command[TAMMAX] , params[COMANDOSPERMITIDOS][TAMMAX];
execve(command , (char*)params ,0); // the error
appears here
the 2nd argument must be an array of pointers, not a 2d array of chars.
Also the last point must be NULL.

Tom

Oct 19 '06 #4
"glmlima" <ma*************@gmail.comwrites:
I have that to simulate "argv []" of the main function like
int main (int argc, to char *argv [])...
I know that I must use pointer, but I don´t know how to...
I'm not sure what you mean, but one thing you need to know is that C
does not have array parameters. In a parameter declaration (and
*only* in a parameter declaration), "char *argv[]" really means
"char **argv". This isn't a type conversion, it's a transformation
that takes place during compilation.

So argv is a pointer to pointer to char. It points to the first
element of an array of pointers to char (the array is allocated and
initialized by the calling environment). Each element of that array,
other than the last, points to the first character of a string.

You'll probably find section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, a useful read.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Bowling | last post: by
1 post views Thread by kumar | last post: by
8 posts views Thread by Siemel Naran | last post: by
1 post views Thread by Mitan | last post: by
5 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by semedao | last post: by
21 posts views Thread by Tom Gur | last post: by
9 posts views Thread by vippstar | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.