#include <stdio.h> #include <sys/types.h> #include "bonus.h"
void parse(char *line, char **argv) { while (line != '\0') { / if not the end of line ....... */ while (*line == ' ' || *line == '\t' || *line == '\n') line++ = '\0'; / replace white spaces with 0 */ argv++ = line; / save the argument position */ while (*line != '\0' && *line != ' ' && *line != '\t' && line != '\n') line++; / skip the argument until ... */ } argv = '\0'; / mark the end of argument list */ }
void execute(char **argv) { pid_t pid; int status;
if ((pid = fork()) < 0) {
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) {
if (execvp(*argv, argv) < 0) {
printf("\n");
exit(1);
}
}
else {
while (wait(&status) != pid)
;
}
}
#define Buff_Len 1024
void main(void)
{
char line[Buff_Len];
char *argv[100];
char path = "/bin/";
char progpath[20];
int i,n;
int arr[100]={0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,983};
while (1) {
printf("some fuctions include\n");
printf("quit, hello, FIB ,fibonacci\n");
printf("ls, help, cd, \n");
printf("uab_sh >> ");
gets(line);
printf("\n");
parse(line, argv);
if (strcmp(argv[0], "quit") == 0) //Exit from the OS
{ exit(0);}
else if (strcmp(argv[0], "hello") == 0)
{printf("Hello_World\n");
execute(argv);
}
else if (strcmp(argv[0], "FIB") == 0)
{printf("The first 10 values is: ");
for (i=0;i<10;i++){printf("%d, ",arr[i]);}
printf("\n");
execute(argv);
}
else if (strcmp(argv[0], "fibonacci") == 0 )
{printf("Key in the amount of numbers to display :\n");
scanf("%d",&n);
printf("The first %d values is: ",n);
for (i=0;i<n;i++){printf("%d ,",arr[i]);}
execute(argv);}
else if (strcmp(argv[0], "ls") == 0)
{
execvp(argv[1],argv[0]);
execute(argv);
}
else if (strcmp(argv[0], "help") == 0)
{execute(argv);}
else if (strcmp(argv[0], "cd") == 0)
{path = ".";}
}
}