#include #include #include #include #include #define MAX_LINE

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

#include #include #include #include #include #define MAX_LINE

Post by answerhappygod »

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#define MAX_LINE 80
typedef struct {
char commandName[15];
int usageCounter;
}
history;
history hist[30];
int count = -1;
void usagedCommands()
{
printf("\nList of usaged command:\n");
int ss,i;
for(ss = 0; ss <= count; ss++)
{
for(i = 0; i< ((count+1) - ss -1); i++)
{
if(hist.usageCounter < hist[i+1].usageCounter)
{
history temp;
temp = hist;
hist = hist[i+1];
hist[i+1] = temp;
}
}
}
if(count <= 10)
{
for(i=0; i<=count; i++)
{
printf("%d.",(i+1));
printf("Command name :%s Usaged count: %d\n", hist.commandName,hist.usageCounter);
}
}
else
{
for(i=0; i<10; i++)
{
printf("%d.",(i+1));
printf("Command name :%s Usaged count: %d\n", hist.commandName,hist.usageCounter);
}
}
printf("\n");
}
int commands(char inputBuffer[], char *args[], int *flag)
{
int length,i,start,count2=0;
length = read(STDIN_FILENO,inputBuffer,MAX_LINE);
start = -1;
if(length == 0)
exit(0);
if(length < 0)
{
printf("Command not read.. \n");
exit(-1);
}
for(i=0;i<length;i++)
{
switch(inputBuffer)
{
case ' ':
case '\t':
if(start != -1)
{
args[count2]=&inputBuffer[start];
count2++;
}
inputBuffer='\0';
start = -1;
break;
case '\n':
if(start != -1)
{
args[count2] = &inputBuffer[start];
count2++;
}
inputBuffer='\0';
start = -1;
break;
default :
if(start == -1)
start = i;
if(inputBuffer[i] == '&')
{

*flag=1;
inputBuffer[i] ='\0';
}
}
}
args[count2] = NULL;
if(strcmp(args[0],"history") == 0)
{
if(count>-1)
usagedCommands();
else
printf("No Commands in the history..\n");
return -1;
}
else if(strcmp(args[0],"exit")==0)
{
exit(0);
}
else
{
count++;
int a = 0;
for(i=0;i<=count;i++)
{
if(strcmp(inputBuffer,hist[i].commandName) == 0)
{
a=1;
hist[i].usageCounter++;
count--;
break;
}
else
{
continue;
}
}
if(a==0)
{
strcpy(hist[count].commandName,inputBuffer);
hist[count].usageCounter=1;
}
}
}
int main(void)
{
char *args[MAX_LINE/2 + 1];
char ss[MAX_LINE];
int should_run = 1;
int i = 0;
int j;
while(should_run)
{
printf("OSH> ");
fflush(stdout);
scanf ("%[^\n]%*c", ss);
printf("input:%s\n",ss);
i = 0;
int j;
args[i] = strtok(ss," ");
while (args[i] != NULL) {
i++;
args[i] = strtok(NULL, " ");
}

if(strcmp(args[0], "exit") == 0)
break;
if(strcmp(args[i-1], "&") != 0)
{
pid_t pid;
pid = fork();
if(pid < 0)
{
fprintf(stderr,"FORK Failed\n");
return 1;
}
else if (pid == 0)
{
execvp(args[0],args);
for(int j=0;j<i;j++)
args[j] = NULL;
}
else
{
wait(NULL);
}
}
else
{
pid_t pid;
pid = fork();
if(pid < 0){
fprintf(stderr,"FORK Failed\n");
return 1;
}
else if (pid == 0){
args[i-1] = NULL;
execvp(args[0],args);
}
else
{
printf("\n\n");
}
}

}
return 0;
}


Please explain this code in words, the entire code
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply