Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2005, 1:04 PM   #1
ar1
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 ar1 is on a distinguished road
C program strange error

Hi, I wrote the following code which creates a simple shell (like bash). It works fine, can take a command along with arguments and run it or give error if it doesn't exist. However, the strange problem is that it doesn;t execute anything the first time "enter" is hit!! But after that it works perfectly.

I couldn't find the bug, any help would be great. Thanks
---------------------------------------------------------------------------------------------
#include <string.h>
#include <errno.h>

#define TRUE 1

void type_prompt();
void *read_command(char *argv[]);
int n = 0;

int main(int argc, char *argv[], char *envp[])
{
char *command;
int status;
argv[0] = NULL;

while (TRUE) /* repeat forever */
{
type_prompt(); /* display prompt on screen */
read_command(argv); /* read input from terminal */

if (fork() != 0) /* fork off child process */
{
waitpid(-1, &status, 0); /* wait for child to exit */
}
else
{
if (execve(argv[0], argv, envp) == -1) /* execute command */
printf("Ouch, can't execute: %s\n", strerror(errno));
}
}
return 0;
}

void type_prompt()
{
printf("[arajgarh]$ ");
}

void *read_command(char *argv[])
{
n = 0;
char temp[256];

gets(temp);
argv[n++] = strtok (temp," ");

while (argv[n-1] != NULL)
argv[n++] = strtok (NULL, " ");
}
ar1 is offline   Reply With Quote
Old Feb 9th, 2005, 1:14 PM   #2
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
The first enter is from the read_command function of the parent.
Monster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC