You need to sign in or sign up before continuing.
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 @@
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 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()
{
......@@ -18,9 +30,13 @@ void funcExit()
void funcEnv()
{
int i;
for (i = 0; environ[i] != NULL; i++)
printf("%s\n", environ[i]);
// int i;
// for (i = 0; environ[i] != NULL; 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()
......@@ -31,7 +47,10 @@ void funcClear()
void funcDir(char **tokens)
{
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()
......@@ -50,7 +69,7 @@ void funcCd(char *fpath)
if (fpath && chdir(fpath) != 0)
{
printf("ERROR: Directory doesn't exist!\n");
printf("ERROR:Directory doesn't exist!\n");
return;
}
printf("BEFORE:\nOLDPWD=%s\nPWD=%s\n", getenv("OLDPWD"), getenv("PWD"));
......@@ -86,12 +105,20 @@ void ioRedirect(char **tokens, int n)
case -1:
break;
case 0:
if (!strcmp(tokens[n - 2], ">>"))
fp = freopen(tokens[n - 1], "a", stdout);
else if (!strcmp(tokens[n - 2], ">"))
fp = freopen(tokens[n - 1], "w", stdout);
else
fp = freopen(tokens[n - 1], "r", stdin);
if (!strcmp(tokens[n - 2], ">>")){
fp = freopen(tokens[n - 1], "a", stdout);
}
else if (!strcmp(tokens[n - 2], ">")){
fp = freopen(tokens[n - 1], "w", stdout);
}
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/");
strcat(temp, arglist[0]);
if (execv(temp, arglist) == -1)
......@@ -120,7 +147,7 @@ void handler(int sig)
pid_t pid;
pid = wait(NULL);
printf("\nBackground process [%d] finished.\n", pid);
printf("\nMyShell:Background process [%d] finished.\n", pid);
signal(SIGCHLD, NULL);
getcwd(temp, 150);
sprintf(buffer, "%s:$ ", temp);
......@@ -323,7 +350,7 @@ int main(int argc, char *argv[])
fp = fopen(argv[1], "r");
if (fp < 0)
{
printf("File doesn't exists.");
printf("ERROR:File doesn't exist");
return -1;
}
}
......@@ -336,11 +363,8 @@ int main(int argc, char *argv[])
bzero(line, sizeof(line));
if (argc == 2)
{ // batch mode
if (fgets(line, sizeof(line), fp) == NULL)
{ // file reading finished
if(fscanf(fp,"%[^\n]\n",line)==EOF)
break;
}
line[strlen(line) - 1] = '\0';
}
else
{ // interactive mode
......@@ -369,7 +393,7 @@ int main(int argc, char *argv[])
if (!io)
io = strcmp(tokens[i], ">>") == 0 || strcmp(tokens[i], ">") == 0 || strcmp(tokens[i], "<") == 0;
}
t1 = clock();
if (parallel)
exParallel(tokens, i);
else if (serial)
......@@ -380,34 +404,43 @@ int main(int argc, char *argv[])
ioRedirect(tokens, i);
else
{
t1 = clock();
if (!strcmp(tokens[0], "dir") && i == 2)
{
funcDir(tokens);
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
}
else if (!strcmp(tokens[0], "clear"))
{
funcClear();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
}
else if (!strcmp(tokens[0], "quit"))
{
funcExit();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
}
else if (!strcmp(tokens[0], "env"))
{
funcEnv();
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
}
else if (!strcmp(tokens[0], "cd"))
{
funcCd(tokens[1]);
t1 = clock() - t1;
printf("%f sec\n", (double)t1 / CLOCKS_PER_SEC);
}
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++)
......
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