summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-05-21 12:28:02 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-05-21 12:28:02 +0200
commit6b47ff2690b473e86adf6ded723619b546047d7e (patch)
treed5f470c8e5af993247ab9219bd3880cc1c235c4e
parente29af1f554ee40a888689e134e12f8d4a3b24ea1 (diff)
downloadbetriebssysteme-6b47ff2690b473e86adf6ded723619b546047d7e.tar.gz
betriebssysteme-6b47ff2690b473e86adf6ded723619b546047d7e.zip
memory should be fixed? i think
-rw-r--r--02_exercise/shell.c55
1 files changed, 32 insertions, 23 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 5a8d48f..c960680 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -10,13 +10,14 @@
void print_prompt();
-int parse_line(char const *line, char **exe, char ***parts, size_t *part_count);
+int parse_line(char const *line, char ***parts, size_t *part_count);
// returns the return code of the executed program
int exec_command(const char *path, char *const argv[], unsigned timeout);
int main(int argc, char* argv[]) {
- while (true) {
+ bool done = false;
+ while (!done) {
char *line = NULL;
size_t cap = 0;
size_t length = 0;
@@ -29,35 +30,48 @@ int main(int argc, char* argv[]) {
line[length - 1] = '\0'; // cut the line feed
- char *exe = NULL;
char **arguments = NULL;
size_t argument_count;
- parse_line(line, &exe, &arguments, &argument_count);
+ parse_line(line, &arguments, &argument_count);
- if (strcmp(exe, "cd") == 0) {
- printf("Changing dirs \n");
- chdir("beispiele");
- } else if (strcmp(exe, "exit") == 0) {
- exit(0);
+ if (strcmp(arguments[0], "cd") == 0) {
+ if (arrayLen(arguments) != 3) {
+ fprintf(stderr, "usage: cd <path>");
+ goto clean;
+ }
+ int ret = chdir(arguments[1]);
+ if (ret)
+ printf("[%i] ", ret);
+ } else if (strcmp(arguments[0], "exit") == 0) {
+ done = true;
} else {
- exec_command(exe, arguments, 0);
+ int ret = exec_command(arguments[0], arguments, 0);
+ if (ret)
+ printf("[%i] ", ret);
}
+
+ clean:
free((void *)line);
- // no need to free exe, since it is part of arguments any way
+ while (arrayLen(arguments) > 0) {
+ char *tmp = arrayPop(arguments);
+ if (tmp)
+ arrayRelease(tmp);
+ }
arrayRelease(arguments);
}
}
-int parse_line(char const *line, char **exe, char ***parts, size_t *part_count) {
+int parse_line(char const *line, char ***parts, size_t *part_count) {
char *part;
char **local_parts;
- if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) {
+ arrayInit(part);
+ arrayInit(local_parts);
+/* if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) {
fprintf(stderr, "Failed to prepare new part array");
exit(-1);
- }
+ }*/
- bool found_exe = false;
char c;
int i = 0;
while (true) {
@@ -65,16 +79,11 @@ int parse_line(char const *line, char **exe, char ***parts, size_t *part_count)
if (c == ' ' || c == '\0') {
arrayPush(part) = '\0';
arrayPush(local_parts) = part;
- if (!found_exe) {
- // the first is the executable
- *exe = malloc(arrayLen(part) * sizeof(char));
- strcpy(*exe, part);
- found_exe = true;
- }
- arrayInit(part);
if (c == '\0') {
arrayPush(local_parts) = NULL;
break;
+ } else {
+ arrayInit(part);
}
} else {
arrayPush(part) = c;
@@ -110,7 +119,7 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) {
dup2(pipefd[1], 1); // includes close(1);
close(pipefd[1]);
execvp(path, argv);
- fprintf(stderr, "no program\n");
+ fprintf(stderr, "could not execute \"%s\"\n", path);
exit(-1);
}