Hi, i didn't know whether to put this under C or Shell scripting so i chose this one.

Well I am doing a piece of shell scripting and have written some code. and basically when i run it no prompt shows up so i cannot type commands like help or quit.

I run and write it in Unix / pico and i get warnings (which i heard we're okay) and no errors.

#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
	char buf[1024];
	char *args[64];

	for(;;)
	{
	/*
	 *Prompt for and read a command.
	 */
	printf("Command:");
	
	if(gets(buf) == NULL)
	{
	printf("\n");
	exit(0);
	}


	/*
	 * Split the String into Arguments.
	 */
	parse(buf,args);

	/*
	 * Execute the command.
	 */
	execute(args);
     	} // endof for loop
}
 
void help()
{
printf(“**********myshell usage/help options *************/n”);
printf(“help – prints the list of commands supported by the shell/n”);
}


/* parse -- Splits the command in buf into individual arguments */
parse(buf,args)
char *buf;
char **args;
{
        while(*buf != NULL)
	{
	   while((*buf == ' ') || (*buf == '\t'))
		*buf++ = NULL;
	
	/* Save the Argument. */
	  *args++ = buf;

	/* Skip over the Argument. */
	while((*buf != NULL) && (*buf != ' ' ) && (*buf != '\t'))
	  buf++;
	}

	*args= NULL;
}	

/* execute -- spawns a child process and execute the program */
execute(args)
char **args;
{
	int pid,status;
	
	/* Get a child process */
	
	if ((pid= fork())<0)
	{
	perror("fork");
	exit(1);

	}

 	if (pid==0)
	{
                             if(strcmp(*args,"help")==0)
                             {
                             help();/*call the help function*/
                             }
	   else
                            {
                             execvp(*args,args);
	        }	
        	perror(*args);
	exit(1);

	execvp(*args,args);
	perror(*args);
	exit(1);
        }						
		
 /* The parent executes the wait.*/

	while(wait(&status) !=pid)
	/* empty */;
}

thats about it, i am sorry if thats alot of code or is unclear.


Basically i just need help figuring out why it's not creating a prompt

Thanks,

Recommended Answers

All 4 Replies

Maybe you should check out those 'warnings' the compiler generates them for a reason..You should never ignore a warning unless you have a valid reason.

Also never and I mean never use gets()

This will print your prompt...and compile without warnings or errors...your code had a few errors.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
	char buf[1024];

	for(;;)
	{
	/*
	 *Prompt for and read a command.
	 */
	printf("Command:");
	
	fgets(buf, 1024, stdin);
	fprintf(stdout, "buf->%s\n", buf);
	}
	exit(0);
}

here's a blurb from my man help file about gets()

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

commented: Damn straight! +19

one of the warnings did say that get was dangerour and should not be used. (that strikes me as stupid, why allow it to be used if they dont want it to be used haha :P)
My tutor helped me with that part anyway.

So i adjusted my code of those errors you spoke of.

Heres the warning i get: (it's reduced but still no prompt)

myshell.c: In function 'parse':         // haha i accidentally missed the p on this post, it's fine in the code :P
myshell.c:47: warning: comparison between integer and pointer
myshell.c:50: warning: assignment makes integer from pointer without a cast
myshell.c:54: warning: comparison between pointer and integer
parse(buf,args)
char *buf;
char **args;
{

This style of C code was made obsolete 20+ YEARS ago.

Write something like this instead. void parse ( char *buf, char **args ) > I run and write it in Unix / pico and i get warnings (which i heard we're okay) and no errors.
And yet it failed to work....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.