Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:28:36+00:00 2026-06-15T19:28:36+00:00

The code below is supposed to work as a shell. It has the previous

  • 0

The code below is supposed to work as a shell. It has the previous and next option, a history like feature, the exit and the execute command.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUFFER_SIZE 256
#define HISTORY_LENGTH 128

int executeCommand(char*cmd)
{
    return(!strcmp(cmd,"e\n"));
}

int exitCommand(char*cmd)
{
    return (!strcmp(cmd,"exit\n"));
}

int previousCommand(char*cmd)
{
    return (!strcmp(cmd,"p\n"));
}

int nextCommand(char*cmd)
{
    return (!strcmp(cmd,"n\n"));
}

void execute(char *line)
{
    line[strlen(line)-1]='\0';
    char **arguments;
    char* temp;
    int i=0;
    arguments=(char**)malloc(sizeof(char)*10);
    temp=strtok(line," ");
    arguments[i]=malloc(strlen(temp)*sizeof(char));
    if(arguments[i]!=NULL)
    {
        strcpy(arguments[i],temp);
        i++;
    }
    else
    {
        printf("Out of memory");
    }
    while(temp!=NULL)
    {
        temp=strtok(NULL," ");
        if(temp==NULL){
            arguments[i]=NULL;
        }
        else{
            arguments[i]=malloc(strlen(temp)*sizeof(char));
            if(arguments[i]!=NULL)
            {
                strcpy(arguments[i],temp);
                i++;
            }
        }
    }
    printf("%s  ",arguments[0]);
    printf("%s  ",arguments[1]);
    printf("%s  ",arguments[2]);
    execvp(arguments[0],arguments);
}

int main(int argc, char*argV[]) {
    int i;
    char *cmd=(char*)malloc(sizeof(char)*BUFFER_SIZE);
    char **history=NULL;
    int historylength=0;
    int currentCommand=0;
    history=(char**)malloc(sizeof(char)*BUFFER_SIZE);
    do{
        fgets(cmd,BUFFER_SIZE-1,stdin);
        if(exitCommand(cmd))
            break;
        else
            if(previousCommand(cmd))
            {
                if(currentCommand>0)
                    printf("%s",history[--currentCommand]);
                else if(currentCommand==0)
                {
                    currentCommand=historylength;
                    printf("%s",history[--currentCommand]);
                }
            }
            else
                if(nextCommand(cmd))
                {
                    if(currentCommand<historylength)
                        printf("%s",history[currentCommand++]);
                }
                else
                    if(executeCommand(cmd))
                    {
                        execute(history[--currentCommand]);
                    }
                    else
                    {
                        history[historylength]=malloc(strlen(cmd)*sizeof(char));
                        if(history[historylength]!=NULL)
                        {
                            strcpy(history[historylength],cmd);
                            currentCommand=++historylength;
                        }
                        else
                        {
                            printf("Out of memory");
                            break;
                        }
                    }

    } while(1);

    free(cmd);

    for(i=0;i<historylength;i++)
        free(history[i]);
    free(history);

    return 0;
}

I want to make this work for the function cat. I type in the e cat main.c and I expect it to execute the cat command but it’s not doing anything, what am I doing wrong here? I’m not a pro at this so I appreciate all the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:28:37+00:00Added an answer on June 15, 2026 at 7:28 pm

    This is incorrect:

    arguments=(char**)malloc(sizeof(char)*10);
    

    as arguments is a char**, so the code needs to allocate sizeof(char*). Change to:

    arguments = malloc(10 * sizeof(*arguments));
    

    Same mistake for history also. Additionally, see Do I cast the result of malloc?

    There is one char less than required being allocated for the char* as strcpy() writes a terminating null character. Change:

    arguments[i]=malloc(strlen(temp)*sizeof(char));
    

    to:

    arguments[i] = malloc(strlen(temp) + 1);
    

    sizeof(char) is guaranteed to be 1 and can be omitted from the size calculation.

    Prevent i from exceeding the bounds of the memory allocated for arguments. As the code currently stands there is no protection to prevent i from exceeding 9 (0 to 9 are the valid values for i as arguments was allocated 10 elements).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below doesn't work on any browser. It's supposed to show an alert box.
My code below is supposed to display each question ($_POST[questionText]). For each question, it
Every time I run the code below it is supposed to come up with
I suppose button should has Close title in the code below, but it has
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
Apple says in the Safari HTML reference that the below code is supposed to
The code below is supposed to get the new url as root while the
I have this great pyodbc lib. I try the code below, it supposed to
I'm having a (probably super simple) issue. The code below is supposed to _POST
Can anybody spot what is wrong with the code below. It is supposed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.