Fondamentalmente ho creato una shell usando i comandi POSIX standard, voglio essere in grado di implementare anche Piping. In questo momento gestisce i comandi correttamente e può eseguire l'elaborazione in background con &. Ma ho bisogno di essere in grado di usare pipe e >> pure. Ad esempio qualcosa del genere: cat file1 file2 >> file3 cat file1 file2 | altro altro file1 | grep stuffImplementazione di pipe in una shell C (Unix)
Ecco il codice che ho attualmente. Voglio anche EVITARE le chiamate "SYSTEM". So che ho bisogno di usare dup2, ma il modo in cui ho fatto il mio codice è un po 'strano, quindi spero che qualcuno possa dirmi se è possibile implementare pipe in questo codice? Grazie! So che è usato dup2, ma anche im def. confuso su come implementare >> come BENE come |
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void Execute(char* command[],bool BG)
{
//Int Status is Used Purely for the waitpid, fork() is set up like normal.
int status;
pid_t pid = fork();
switch(pid)
{
case 0:
execvp(command[0], command);
if(execvp(command[0], command) == -1)
{
cout << "Command Not Found" << endl;
exit(0);
}
default:
if(BG == 0)
{
waitpid(pid, &status, 0);
//Debug cout << "DEBUG:Child Finished" << endl;
}
}
}
bool ParseArg(char* prompt, char* command[], char Readin[],bool BG)
{
fprintf(stderr, "myshell>");
cin.getline(Readin,50);
prompt = strtok(Readin, " ");
int i = 0;
while(prompt != NULL)
{
command[i] = prompt;
if(strcmp(command[i], "&") == 0){
//Debug cout << "& found";
command[i] = NULL;
return true;
}
//Debug cout << command[i] << " ";
i++;
prompt = strtok(NULL, " ");
}
return false;
}
void Clean(char* command[])
{
//Clean Array
for(int a=0; a < 50; a++)
{
command[a] = NULL;
}
}
int main()
{
char* prompt;
char* command[50];
char Readin[50];
bool BG = false;
while(command[0] != NULL)
{
Clean(command);
BG = ParseArg(prompt, command, Readin, BG);
if(strcmp(command[0], "exit") == 0 || strcmp(command[0], "quit") == 0)
{
break;
}
else
{
Execute(command,BG);
}
}
return 1;
}
Perché stavi cercando di evitare le chiamate di sistema? Portabilità? È possibile attaccare il più possibile le chiamate di sistema specificate da POSIX. Inoltre, la shell è uno strano mix di C e C++. – nategoose