Commit b4cea012 authored by desiredeveloper's avatar desiredeveloper

final changes

parent f4d2735d
No preview for this file type
echo hi
echo hi >> file
pwd
echo hi >> file
pwd
\ No newline at end of file
...@@ -4,12 +4,24 @@ ...@@ -4,12 +4,24 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#define MAX_INPUT_SIZE 1024 #define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64 #define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64 #define MAX_NUM_TOKENS 64
extern char **environ; // extern char **environ;
int fileExists(char *path){
return access( path, F_OK ) != -1 ? 1:0;
}
int folderExists(const char *path)
{
struct stat stats;
stat(path, &stats);
return S_ISDIR(stats.st_mode)? 1:0;
}
void funcExit() void funcExit()
{ {
...@@ -18,9 +30,13 @@ void funcExit() ...@@ -18,9 +30,13 @@ void funcExit()
void funcEnv() void funcEnv()
{ {
int i; // int i;
for (i = 0; environ[i] != NULL; i++) // for (i = 0; environ[i] != NULL; i++)
printf("%s\n", environ[i]); // printf("%s\n", environ[i]);
setenv("COURSE", "CS-744", 1);
setenv("ASSIGNMENT", "ASSIGNMENT_1", 1);
printf("COURSE=%s\nASSIGNMENT=%s\nPWD=%s\n",getenv("COURSE"),getenv("ASSIGNMENT"),getenv("PWD"));
} }
void funcClear() void funcClear()
...@@ -31,7 +47,10 @@ void funcClear() ...@@ -31,7 +47,10 @@ void funcClear()
void funcDir(char **tokens) void funcDir(char **tokens)
{ {
char a[150] = "ls -la "; char a[150] = "ls -la ";
system(strcat(a, tokens[1])); if(folderExists(tokens[1]) || fileExists(tokens[1]))
system(strcat(a, tokens[1]));
else
printf("ERROR:Directory does not exist\n");
} }
void funcPwd() void funcPwd()
...@@ -50,7 +69,7 @@ void funcCd(char *fpath) ...@@ -50,7 +69,7 @@ void funcCd(char *fpath)
if (fpath && chdir(fpath) != 0) if (fpath && chdir(fpath) != 0)
{ {
printf("ERROR: Directory doesn't exist!\n"); printf("ERROR:Directory doesn't exist!\n");
return; return;
} }
printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n", getenv("OLDPWD"), getenv("PWD")); printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n", getenv("OLDPWD"), getenv("PWD"));
...@@ -86,12 +105,20 @@ void ioRedirect(char **tokens, int n) ...@@ -86,12 +105,20 @@ void ioRedirect(char **tokens, int n)
case -1: case -1:
break; break;
case 0: case 0:
if (!strcmp(tokens[n - 2], ">>")) if (!strcmp(tokens[n - 2], ">>")){
fp = freopen(tokens[n - 1], "a", stdout); fp = freopen(tokens[n - 1], "a", stdout);
else if (!strcmp(tokens[n - 2], ">")) }
fp = freopen(tokens[n - 1], "w", stdout); else if (!strcmp(tokens[n - 2], ">")){
else fp = freopen(tokens[n - 1], "w", stdout);
fp = freopen(tokens[n - 1], "r", stdin); }
else{
if( fileExists (tokens[n-1])) {
fp = freopen(tokens[n - 1], "r", stdin);
} else {
printf("ERROR:File does not exist\n");
exit(0);
}
}
strcpy(temp, "/bin/"); strcpy(temp, "/bin/");
strcat(temp, arglist[0]); strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1) if (execv(temp, arglist) == -1)
...@@ -120,7 +147,7 @@ void handler(int sig) ...@@ -120,7 +147,7 @@ void handler(int sig)
pid_t pid; pid_t pid;
pid = wait(NULL); pid = wait(NULL);
printf("\nBackground process [%d] finished.\n", pid); printf("\nMyShell:Background process [%d] finished.\n", pid);
signal(SIGCHLD, NULL); signal(SIGCHLD, NULL);
getcwd(temp, 150); getcwd(temp, 150);
sprintf(buffer, "%s:$ ", temp); sprintf(buffer, "%s:$ ", temp);
...@@ -323,7 +350,7 @@ int main(int argc, char *argv[]) ...@@ -323,7 +350,7 @@ int main(int argc, char *argv[])
fp = fopen(argv[1], "r"); fp = fopen(argv[1], "r");
if (fp < 0) if (fp < 0)
{ {
printf("File doesn't exists."); printf("ERROR:File doesn't exist");
return -1; return -1;
} }
} }
...@@ -336,11 +363,8 @@ int main(int argc, char *argv[]) ...@@ -336,11 +363,8 @@ int main(int argc, char *argv[])
bzero(line, sizeof(line)); bzero(line, sizeof(line));
if (argc == 2) if (argc == 2)
{ // batch mode { // batch mode
if (fgets(line, sizeof(line), fp) == NULL) if(fscanf(fp,"%[^\n]\n",line)==EOF)
{ // file reading finished
break; break;
}
line[strlen(line) - 1] = '\0';
} }
else else
{ // interactive mode { // interactive mode
...@@ -369,7 +393,7 @@ int main(int argc, char *argv[]) ...@@ -369,7 +393,7 @@ int main(int argc, char *argv[])
if (!io) if (!io)
io = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0; io = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0;
} }
t1 = clock();
if (parallel) if (parallel)
exParallel(tokens, i); exParallel(tokens, i);
else if (serial) else if (serial)
...@@ -380,34 +404,43 @@ int main(int argc, char *argv[]) ...@@ -380,34 +404,43 @@ int main(int argc, char *argv[])
ioRedirect(tokens, i); ioRedirect(tokens, i);
else else
{ {
t1 = clock();
if (!strcmp(tokens[0], "dir") && i == 2) if (!strcmp(tokens[0], "dir") && i == 2)
{ {
funcDir(tokens); funcDir(tokens);
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
} }
else if (!strcmp(tokens[0], "clear")) else if (!strcmp(tokens[0], "clear"))
{ {
funcClear(); funcClear();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
} }
else if (!strcmp(tokens[0], "quit")) else if (!strcmp(tokens[0], "quit"))
{ {
funcExit(); funcExit();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
} }
else if (!strcmp(tokens[0], "env")) else if (!strcmp(tokens[0], "env"))
{ {
funcEnv(); funcEnv();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
} }
else if (!strcmp(tokens[0], "cd")) else if (!strcmp(tokens[0], "cd"))
{ {
funcCd(tokens[1]); funcCd(tokens[1]);
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
} }
else else
{ {
system(line); system(line);
} }
}
t1 = clock() - t1; }
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
// Freeing the allocated memory // Freeing the allocated memory
for (i = 0; tokens[i] != NULL; i++) for (i = 0; tokens[i] != NULL; i++)
......
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