-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
56 lines (42 loc) · 1.13 KB
/
parse.c
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "parse.h"
//
// This code is given for illustration purposes. You need not include or follow this
// strictly. Feel free to writer better or bug free code. This example code block does not
// worry about deallocating memory. You need to ensure memory is allocated and deallocated
// properly so that your shell works without leaking memory.
//
int getcmd(char *prompt, char *args[], char *stdin_string /*, int *background */) {
// memset(args, 0, 20);
int length, i = 0;
char *token, *loc;
char *line = NULL;
size_t linecap = 0;
printf("%s", prompt);
length = getline(&line, &linecap, stdin);
if (length <= 0) {
exit(-1);
}
strcpy(stdin_string, line);
// Check if background is specified..
// if ((loc = index(line, '&')) != NULL) {
// *background = 1;
// *loc = ' ';
// } else {
// *background = 0;
// }
while ((token = strsep(&line, " \t\n")) != NULL) {
for (int j = 0; j < strlen(token); j++) {
if (token[j] <= 32)
token[j] = '\0';
}
if (strlen(token) > 0) {
args[i++] = token;
}
}
args[i] = "\0";
return i;
}