Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #include<sys/types.h>
- #include<sys/wait.h>
- #include<unistd.h>
- #include<errno.h>
- #define MAX_LINE 4096
- #define MAX_WORDS MAX_LINE/2
- /* a line can have at most MAX_LINE/2 words, why? */
- /* a word is at minimum 2 characters, so the most that can fit in a line is thus MAX/2? */
- void tokenize(char *line, char **words, int *nwords);
- /* break line into words separated by whitespace, placing them in the
- array words, and setting the count to nwords */
- int system(const char *command);
- int strcmp(const char *str1, const char *str2);
- int externalCommands(const char *command);
- int main()
- {
- char line[MAX_LINE], *words[MAX_WORDS], message[MAX_LINE];
- char linecopy[MAX_LINE];
- int stop=0,nwords=0;
- while(stop==0)
- {
- int i; /* for the for loops */
- printf("OSP CLI $ "); /*this provides the look of a terminal*/
- fgets (line, sizeof(line), stdin); /* fetches user input for a line */
- /* read a line of text here */
- if (strcmp(line, "")!=0) {
- strcpy(linecopy, line); /* copies the line to linecopy, since tokenizing the line changes it and I'm not sure if we need to keep the original line */
- tokenize(linecopy,words,&nwords); /*tokenizes the line, turning it from a long string into several smaller strings */
- printf("\n input: %s" ,line); /* prints the line, may be removed later if deemed unnecessary */
- } else {
- break;
- }
- for(i=0; i<nwords; i++) { /* this loop prints the words separately from one another (showing they have been token'd) and also shows the counter */
- printf("word %i = %s \n", i+1, words[i]);
- }
- /* do builtin functions here */
- printf("first word is %s \n", words[0]);
- int isExit;
- int isCd;
- isExit = strcmp(words[0], "exit");
- isCd = strcmp(words[0], "cd");
- if(isExit ==0) { /* If the user input reads as "exit" then sends a message saying so and terminate the program */
- printf("Exiting program now... \n");
- stop=1; /* setting stop to 1 breaks the while loop */
- break;
- }
- if(isCd==0) { /*This code is if the entered command is "cd", it changes the directory */
- int ret;
- printf("Changing Directory to %s \n", words[1]);
- ret = chdir(words[1]);
- switch (ret) {
- case 0:
- printf("Path successfully changed! \n");
- break;
- case -1:
- printf("Errors in changing directory! \n");
- break;
- }
- }
- /* do external functions with Fork and such */
- /* run an externalCommands(words) */
- if (isExit!=0 && isCd !=0) {
- externalCommands(words[0]);
- }
- }
- return 0;
- }
- /* this function works, it is up to you to work out why! */
- void tokenize(char *line, char **words, int *nwords)
- {
- *nwords=1;
- for(words[0]=strtok(line," \t\n");
- /*Below: if the number of words is below max, and the value at words[nwords] isn't null\n, tokenize the word then add 1 to nwords*/
- /*the function strtok breaks a line into words using a delimiter, */
- (*nwords<MAX_WORDS)&&(words[*nwords]=strtok(NULL, " \t\n"));
- *nwords=*nwords+1
- ); /* empty body */
- return;
- }
- int externalCommands(const char *command) {
- pid_t child;
- switch(child=fork())
- {
- case -1:
- perror("fork");
- exit(1);
- break;
- case 0:
- printf("I am the child my pid is: %d\n",getpid());
- exit(0);
- break;
- default:
- printf("I am the parent, my pid is %d," ,getpid());
- printf("and my childs is %d\n",child);
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement