Question: Complete C code in section // your code here ONLY splitting the input string s into tokens array toks[]. You must use function strtok() or
Complete C code in section "// your code here" ONLY
-
splitting the input string s into tokens array toks[].
You must use function strtok() or strtok_r() for this. The man page for strtok() is scary, but using strtok() is not hard. To get the first token in string s, you call strtok(s, " "). The second argument means that s should be split into tokens based on space characters. To get the second token in s, you call strtok(NULL, " "). If will return NULL if there is no second token. To get the third token in s you call strtok(NULL, " ") again. In summary, you provide the string s in the first call but NULL in later calls, and keep calling until NULL is returned.
-
implementing the 'today' command
You must use Linux commands time() and localtime() for this. Try man 2 time, and man localtime. You will first call time() and then call localtime(). You must use system calls or C library functions to get the time, not user commands (look at the man page for 'man' if you don't know the difference).
#include
/* * A very simple shell that supports only commands 'exit', 'help', and 'today'. */
#define MAX_BUF 160 #define MAX_TOKS 100
int main() { int ch; char *pos; char s[MAX_BUF+2]; // 2 extra for the newline and ending '\0' static const char prompt[] = "msh> "; char *toks[MAX_TOKS];
// // YOUR CODE HERE (add declarations as needed) //
while (1) { // prompt for input if input from terminal if (isatty(fileno(stdin))) { printf(prompt); }
// read input char *status = fgets(s, MAX_BUF+2, stdin);
// exit if ^d entered if (status == NULL) { printf(" "); break; }
// input is too long if last character is not newline if ((pos = strchr(s, ' ')) == NULL) { printf("error: input too long "); // clear the input buffer while ((ch = getchar()) != ' ' && ch != EOF) ; continue; }
// remove trailing newline *pos = '\0';
// // YOUR CODE HERE // } exit(EXIT_SUCCESS); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
