summaryrefslogtreecommitdiffstats
path: root/02_exercise/shell.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/shell.c')
-rw-r--r--02_exercise/shell.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 41378f4..933d162 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -1,3 +1,4 @@
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -31,6 +32,10 @@ int main(void) {
exit(-1);
}
+ if (strspn(line, " \n\t") == strlen(line)) {
+ continue;
+ }
+
line[length - 1] = '\0'; // cut the line feed
char **arguments = NULL;
@@ -82,11 +87,11 @@ int parse_line(char const *line, char ***parts, size_t *part_count) {
return -1;
}
- char c;
- int i = 0;
- while (true) {
- c = line[i++];
+ for (size_t i = 0; i < strlen(line) + 1; ++i) {
+ char c = line[i];
if (c == ' ' || c == '\0') {
+ if (arrayLen(part) == 0)
+ continue;
arrayPush(part) = '\0';
arrayPush(local_parts) = part;
if (c == '\0') {
@@ -120,6 +125,7 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) {
close(pipefd[1]);
execvp(path, argv);
fprintf(stderr, "could not execute \"%s\"\n", path);
+ fflush(stderr);
exit(-1);
}