Commit e9b24539 authored by desiredeveloper's avatar desiredeveloper

shell till part 3

parents
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
extern char **environ;
void funcExit(){
exit(0);
}
void funcEnv(){
int i;
for (i = 0; environ[i] != NULL; i++)
printf("%s\n",environ[i]);
}
void funcClear(){
printf("\e[1;1H\e[2J");
}
void funcDir(char **tokens){
char a[150]="ls -la ";
system(strcat(a,tokens[1]));
}
void funcPwd(){
char *temp = (char *)malloc(sizeof(char)*150);
getcwd(temp,150);
printf("%s:$ ",temp);
free(temp);
}
void funcCd(char *fpath){
char *temp = (char *)malloc(sizeof(char)*150);
getcwd(temp,150);
if(fpath && chdir(fpath)!=0){
printf("ERROR: Directory doesn't exist!\n");
return;
}
printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n",getenv("OLDPWD"),getenv("PWD"));
if(fpath){
setenv("OLDPWD",temp,1);
getcwd(temp,150);
setenv("PWD",temp,1);
}
else
setenv("OLDPWD",temp,1);
printf("AFTER:\nOLDPWD=%s\nPWD=%s\n",getenv("OLDPWD"),getenv("PWD"));
free(temp);
}
void exBackground(char **tokens,int n){
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0,forkcalls=0;
for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&")!=0 ){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
}
arglist[j]=NULL;
forkcalls++;
char *temp = (char *)malloc(sizeof(char)*150);
switch (fork()) {
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
}
free(temp);
}
j=0;
}
for(int i=0;arglist[i]!=NULL;i++)
free(arglist[i]);
free(arglist);
}
void exSerial(char **tokens,int n){
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0;
for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&&")!=0 ){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
}
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
switch (fork()) {
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
}
free(temp);
}
wait(NULL);
j=0;
}
for(int i=0;arglist[i]!=NULL;i++)
free(arglist[i]);
free(arglist);
}
void exParallel(char **tokens,int n){
char **arglist = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
int j=0,forkcalls=0;
for(int i=0;i<n;i++){
while(tokens[i]!=NULL && strcmp(tokens[i],"&&&")!=0 ){
arglist[j] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(arglist[j++],tokens[i++]);
}
forkcalls++;
arglist[j]=NULL;
char *temp = (char *)malloc(sizeof(char)*150);
switch (fork()) {
case -1:
break;
case 0:
strcpy(temp,"/bin/");
strcat(temp,arglist[0]);
if(execv(temp,arglist)==-1){
strcpy(temp,"/usr/bin/");
strcat(temp,arglist[0]);
execv(temp,arglist);
}
free(temp);
}
j=0;
}
for(int i=0;arglist[i]!=NULL;i++)
free(arglist[i]);
free(arglist);
}
/* Splits the string by space and returns the array of tokens
*/
char **tokenize(char *line)
{
char **tokens = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
char *token = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
int i, tokenIndex = 0, tokenNo = 0;
for(i =0; i < strlen(line); i++){
char readChar = line[i];
if (readChar == ' ' || readChar == '\n' || readChar == '\t'){
token[tokenIndex] = '\0';
if (tokenIndex != 0){
tokens[tokenNo] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(tokens[tokenNo++], token);
tokenIndex = 0;
}
} else {
token[tokenIndex++] = readChar;
}
}
free(token);
tokens[tokenNo] = NULL ;
return tokens;
}
int main(int argc, char* argv[]) {
char line[MAX_INPUT_SIZE];
char **tokens;
int i;
clock_t t1;
FILE* fp;
if(argc == 2) {
fp = fopen(argv[1],"r");
if(fp < 0) {
printf("File doesn't exists.");
return -1;
}
}
while(1) {
int parallel=0,serial=0,background=0,internal=0,cdir=0;
/* BEGIN: TAKING INPUT */
bzero(line, sizeof(line));
if(argc == 2) { // batch mode
if(fgets(line, sizeof(line), fp) == NULL) { // file reading finished
break;
}
line[strlen(line) - 1] = '\0';
} else { // interactive mode
funcPwd();
// waitpid(-1,NULL,WNOHANG);
scanf("%[^\n]", line);
getchar();
}
if(!strlen(line))
continue;
// printf("Command entered: %s\n", line);
/* END: TAKING INPUT */
line[strlen(line)] = '\n'; //terminate with new line
tokens = tokenize(line);
//do whatever you want with the commands, here we just print them
for(i=0;tokens[i]!=NULL;i++){
if(!parallel)
parallel=strcmp(tokens[i],"&&&")==0;
if(!serial)
serial=strcmp(tokens[i],"&&")==0;
if(!background)
background=strcmp(tokens[i],"&")==0;
// printf("found token %s\n", tokens[i]);
}
t1 = clock();
if(parallel)
exParallel(tokens,i);
else if(serial)
exSerial(tokens,i);
else if(background)
exBackground(tokens,i);
else {
if(!strcmp(tokens[0],"dir")){
funcDir(tokens);
}
else if(!strcmp(tokens[0],"clear")){
funcClear();
}
else if(!strcmp(tokens[0],"quit")){
funcExit();
}
else if(!strcmp(tokens[0],"env")){
funcEnv();
}
else if(!strcmp(tokens[0],"cd")){
funcCd(tokens[1]);
}
else{
system(line);
}
}
t1 = clock() - t1;
printf("%f sec\n",(double)t1/CLOCKS_PER_SEC);
// Freeing the allocated memory
for(i=0;tokens[i]!=NULL;i++){
free(tokens[i]);
}
free(tokens);
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment