Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
extern int errno;
void type_prompt(char *user, char *host);
void getspaceacp(char **to, char *token, int *count); // Speicher für ein token allokieren und es darin ablegen
int read_command(char **command, char ***parameters); //Liest commando in den Speicher ein
void show_command(char **command, char ***parameters, int arraycount);
void clean(char **command, char ***parameters, int arraycount);
void type_prompt(char *user,char *host)
{
printf("%s@%s:$",user,host);
}
// Speicher für ein token allokieren und es darin ablegen
void getspaceacp(char **to, char *token, int *count)
{
if ((to[*count] = malloc(strlen(token) + 1)) == NULL) {
printf("Fehler bei malloc(): %s\n", errno);
exit(1);
}
strcpy(to[*count], token);
}
int read_command(char **command, char ***parameters)
{
int i; // Für die Parameterliste
char buf[255];
char *cpoint = NULL, *token = NULL, *newline = NULL;
// Eingabe lesen
if ((fgets(buf, sizeof(buf), stdin)) == NULL) {
printf("Fehler beim Einlesen des Kommandos: %s\n", errno);
exit(1);
}
// Keine korrekte Eingabe?
if (strcmp(buf, "\n") == 0) {
printf("Keine korrekte Eingabe. Ende.\n");
exit(0);
}
// newline entfernen
if ((newline = strchr(buf, '\n')) != NULL)
*newline = '\0';
// Eingabe (Kommando) auslesen
token = strtok_r(buf, " ", &cpoint);
// Programm beenden?
if ((strcmp(token, "exit") == 0)) //exit ist der Standart Beenden Befehl für eine Shell
exit(0);
else {
i = 0;
// Kommando abspeichern
getspaceacp(command, token, &i); // &i hier nur "dummy"
// argv[0] in der Parameterliste muss der Programmname sein; hier setzen
*parameters = malloc(sizeof(char *)); // array hat jetzt 1 Element
getspaceacp(*parameters, token, &i);
i++;
// Parameter auslesen und abspeichern
while((token = strtok_r(NULL, " ", &cpoint)) != NULL) {
*parameters = realloc(*parameters, (i + 1) * sizeof(char *));
getspaceacp(*parameters, token, &i);
i++;
}
(*parameters)[i] = NULL;
}
return i; // Anzahl der array - Elemente
}
void show_command(char **command, char ***parameters, int arraycount)
{
int j;
printf("Befehl:\t\"%s\"\n", *command);
printf("Parameterliste: ");
for(j = 0; j < arraycount; j++)
printf("\n\t%d: \"%s\"", j, (*parameters)[j]);
printf("\n");
}
// Parameterliste durch Speicherfreigabe und "nullen" der Zeiger bereinigen
void clean(char **command, char ***parameters, int arraycount)
{
int i;
// Kommando
free(*command);
*command = NULL;
// Elemente des dynamischen arrays
for (i = arraycount - 1; i >= 0; i--) {
free((*parameters)[i]);
free(parameters[i]);
parameters = NULL;
}
// dynamisches array
}
int main(int argc, char *argv[],char *envp[])
{
int j;
char *command = NULL;
char **parameters = NULL; // dynamisch wachsendes array
int arraycount = 0; // Anzahl der array - Elemente
char *user = getenv("USER"), *host = getenv("HOST");
while(TRUE){
type_prompt(user,host);
arraycount = read_command(&command, ¶meters);
show_command(&command, ¶meters, arraycount);
if(fork() != 0)
{
waitpid(-1, NULL, 0);
//clean(&command, ¶meters, arraycount);
}
else{
execve(command, parameters, envp);
printf("Fehler bei exec(): %s\n", errno);
exit(1);
}
}
}
mfg gsus
Lesezeichen