Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Internal Commands • Internal commands are commands that the shell itself impleme

ID: 3795700 • Letter: I

Question

Internal Commands

• Internal commands are commands that the shell itself implement, as opposed to a separate program that is executed. Implement the commands listed below. Note that for these you need not fork a new process as they can be handled directly in the parent process.

• exit Exit the program. Does not matter how many arguments the user enters; they are all ignored.

• pwd Display the current working directory. Use getcwd() function. Run man getcwd for more. Ignore any extra arguments passed by the user.

• cd Change the current working directory. Use chdir() function. Pass chdir() the first argument the user enters (it will accept absolute and relative paths). Ignore any extra arguments passed by the user.

• If chdir() returns an error, display an error message.

• You do not need to support ~ for the home folder, or changing to the home folder with “cd”. • If the user presses enter on an empty line, do nothing (much like a normal Linux terminal).

• Plus change the prompt to include the current working directory. For example, if in the /home/ashriram folder, the prompt should be: /home/ashriram>

Creating a History Feature

• The next task is to modify your shell to provide a history feature that allows the user access up to the ten most recently entered commands. Start numbering the user's commands at 1 and increment for each command entered. These numbers will grow past 10. For example, if the user has entered 35 commands, then the most recent ten will be numbered 26 through 35. This history feature will be implementing using a few different techniques.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_CMD_LEN 128
#define HISTORY_COUNT 10

int user_history(char *hist[], int current)
{
   int i = current;
   int hist_num = 1;
   do {
           if (hist[i])
           {
               printf("%4d %s ", hist_num, hist[i]);
               hist_num++;
           }
           i = (i + 1) % HISTORY_COUNT;
       } while (i != current);
   return 0;
}

int clear_user_history(char *hist[])
{
   int i;
   for (i = 0; i < HISTORY_COUNT; i++)
   {
       free(hist[i]);
       hist[i] = NULL;
   }
   return 0;
}


int cd(char *pth)
{
char path[MAX_CMD_LEN];
strcpy(path,pth);
char cwd[MAX_CMD_LEN];
   getcwd(cwd,sizeof(cwd));
   strcat(cwd,"/");
   strcat(cwd,path);
   printf("cd location before chdir is : %s ", path);
   int ret = chdir(path);  
   if(ret!=0)
       printf("Error: Could not change to the directory ");
   else
       system("pwd");
return 0;
}

int hasPrefix(char const *p, char const *q)
{
int i = 0;
for(i = 0;q[i];i++)
{
if(p[i] != q[i])
return -1;
}
return 0;
}

int main()
{
   char cmd[MAX_CMD_LEN];
   char *hist[HISTORY_COUNT];
   char buf[1000];
   int i, current = 0;

   for (i = 0; i < HISTORY_COUNT; i++)
           hist[i] = NULL;
   char *tok;
   tok = strtok (cmd," ");
  
   while (1)
   {
       bzero(cmd, MAX_CMD_LEN);
       printf("user@shell# ");
       fgets(cmd, MAX_CMD_LEN, stdin);
      
       if (cmd[strlen(cmd) - 1] == ' ')
       {
           cmd[strlen(cmd) - 1] = '';
       }
      

       if(hasPrefix(cmd,"cd") == 0)
       {
           tok = strchr(cmd,' ');
           if(tok)
           {
char *tempTok = tok + 1;
tok = tempTok;
char *locationOfNewLine = strchr(tok, ' ');
if(locationOfNewLine)
               {
*locationOfNewLine = '';
}
               printf("cd location is : %s ", tok);
cd(tok);
}
           else
           {
               system("pwd");
           }
          
       }
       free(hist[current]);

       hist[current] = strdup(cmd);

       current = (current + 1) % HISTORY_COUNT;

       if (strcmp(cmd, "history") == 0)
               user_history(hist, current);
       else if (strcmp(cmd, "hc") == 0)
               clear_user_history(hist);
       else if (strcmp(cmd, "exit") == 0)
               exit(0);
       else if (strcmp(cmd,"pwd") == 0)
               getcwd(buf, 100);
   }
   clear_user_history(hist);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote