I am writing a simple shell. I have posted my code here. This code works with the simple comments. But my problem now is everytime after I entered a commend, I have to hit enter key twice in order to get the output. Here is an example of my output
?-> ls
?->
a.out getchar.cpp
So my question is how to delete the second ?-> I would be so appreciate for your help. Thank you
//Shell
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <signal.h>
#include <string.h>
using namespace std;
#define ARGLENGTH 100
int execute(char *command[]);
int main()
{
char *command[1000000];
int numargs = 0;
char buffer[ARGLENGTH];
while(1) {
printf("?-> ", numargs);
if( fgets(buffer, ARGLENGTH, stdin) && *buffer != '\n' ) {
buffer[strlen(buffer)-1] = '\0';
command[numargs] = (char *)malloc(strlen(buffer)+1);
strcpy(command[numargs++], buffer);
}
else{
if( numargs > 0 ){
command[numargs] = NULL;
execute(command);
numargs = 0;
}
}
if (command[0][0] == 'e'){
break;}
}
return 0;
}
int execute(char *command[])
{
int pid,exitstatus;
pid = fork();
if( pid==-1 ){
perror("fork failed");
exit(1);
}
else if( pid==0 ){
execvp(command[0], command);
perror("execution failed");
exit(1);
}
else {
while (wait(&exitstatus) != pid);
}
}
Aucun commentaire:
Enregistrer un commentaire