-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminishell.cpp
160 lines (130 loc) · 3.13 KB
/
minishell.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <string.h> // strtok()
#include <time.h> // time(), ...
#include <stdio.h> // printf(), ...
#include <unistd.h>
#include <sys/wait.h> // open()
#include <sys/types.h>
#include <signal.h> // signal()
#define MAXLINE 100
#define MOD "exit with CTR C"
using namespace std;
time_t startprogram;
time_t endprogram;
//Abbruch (Reagierung) bei "CTR + C"
void handler(int sig)
{
endprogram = time(NULL);
printf("Time vergangen : %ds\n", (endprogram - startprogram));
exit(0);
}
void sigchld_handler(int sighl)
{
while (waitpid(-1, NULL,WNOHANG) > 0);
}
//Funktion für Arbeitsverzeichnis
string aktuellesVerzeichnis()
{
char temp[256];
if (getcwd(temp,256)){
return string (temp);
}else
{
return string("");
}
}
int read_command(char* command, char* parameters[])
{
char befehl[256];
int i = 0;
char* tmp = NULL;
string prompt = "> ";
string directory = aktuellesVerzeichnis();
cout << directory.append(prompt);
cin.getline(befehl, 256); //
tmp = strtok(befehl, " ");
strcpy(command, tmp);
while(tmp != NULL)
{
parameters[i] = new char[256];
strcpy(parameters[i], tmp);
//hole nächsten token
tmp = strtok(NULL, " ");
//Weiter in dem parameters array
i++;
}
if((command[0] == 'c') && (command[1] == 'd'))
{
cout << chdir(parameters[1]) << endl;
//continue;
}
if(strcmp(parameters[i-1], "&") == 0)
{
parameters[i-1] = NULL;
return 0;
}
else
{
parameters[i] = NULL;
}
//Keine Parameter gesetzt
return 1;
return -1;
}
int main()
{
int childPid;
int status;
char command[256];
char* parameters[256];
int noParams;
startprogram = time(NULL);
if (signal(SIGCHLD, sigchld_handler))
{
perror("signal");
return 1;
}
signal(SIGINT, handler);
bool val = true;
while (val)
{
//Eingabe lesen
noParams = read_command(command, parameters);
//Kinderprozess erzeugen
childPid = fork();
if (childPid == -1)
{
cout << "can not fork!" << endl;
exit(1);
}
//Kindprozess
else if (childPid == 0)
{
int a = execvp(command, parameters);
//Überprüfen auf falsche eingabe
if(a != 0) // -1 bedeutet, dass es fehlgeschlagen ist
{
cout << command << ": Kommando nicht gefunden." << endl;
}
//Programm ende
exit(0);
}
//Vaterprozess
else
{
//Überprüfen auf Parameterübergabe
if (noParams == 1)
{
wait(&status); // Vaterprozess wartet mit sleep bis Kind fertig ist
}
else
{
cout << "[" << childPid << "]" << endl;
}
}
}
//Programm beenden
return 0;
}