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 3286152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:20:52+00:00 2026-05-17T20:20:52+00:00

This Program works fine with exception for one aspect of smarthistory(). I cannot figure

  • 0

This Program works fine with exception for one aspect of smarthistory(). I cannot figure out why when I enter a command number from the smarthistory array why it doesn’t execute. After entering the command to execute from the list nothing happens not even the printf statement right after. I am using the gcc compiler.

const int MAX_HISTORY=100;

const int MAX_COMMAND_LENGTH=64;

char history[100][64];
int historyCount = 0;
char smarthistory[100][64];
int smarthistoryCount=0;

void chopnl(char *s) { //strip '\n'
    s[strcspn(s, "\n")] = '\0';
}

void printHistory() {
    int i;
    for (i = 0; i < historyCount; i++)
        printf("%d | %s\n", i, history[i]);
}

void printSmartHistory() {
    int i;
    for (i = 0; i < smarthistoryCount; i++)
        printf("%d | %s\n", i, smarthistory[i]);
}

void isPartialMatch(char *commandQuery,char *history, int historyString)
{
    int lengthOfQuery=strlen(commandQuery);
    if(strncmp( history, commandQuery,lengthOfQuery)==0)
    {
        memcpy(smarthistory[smarthistoryCount++], history, strlen(history) + 1);
    }
    else
        return;

}

void smartHistory()
{
    char commandQuery[MAX_COMMAND_LENGTH];
    int commandNumber=0;
    int i=0;
    printHistory();
    printf("enter partial command:> ");
    fgets(commandQuery,MAX_COMMAND_LENGTH,stdin);
    chopnl(commandQuery);
    //printf("%d", strlen(commandQuery));

    for(i=0;i<=historyCount;i++)
    {
            isPartialMatch(commandQuery, history[i], i);
    }
    printf("SmartHistory Search Results\n");
    printSmartHistory();
    printf("enter a command number to execute:> ");
    scanf("%d", commandNumber);
    //chopnl(commandNumber);
    printf("command entered >");
    handleCommand(smarthistory[commandNumber]);
}
void placeInHistory(char *command) {
    // printf("command:> %s |stored in:> %d",command,historyCount );
    memcpy(history[historyCount++], command, strlen(command) + 1);
}

int main(int argc, char** argv) {
    char command[MAX_COMMAND_LENGTH];

    while (1) {
        printf("SHELL:>");
        fgets(command, MAX_COMMAND_LENGTH, stdin);
        chopnl(command);
        if (strcmpi(command, "exit") == 0)
            return EXIT_SUCCESS;
        placeInHistory(command);
        handleCommand(command);
    }
    return (EXIT_SUCCESS);
}

int handleCommand(char *command) {
    pid_t pid;
    int test=0;
    pid = fork();

    if (pid > 0) {
        wait(&test);
    } else if (pid == 0) {
        execCommand(command);
        exit(0);
    } else {
        printf("ERROR");
    }
}

int execCommand(char *command) {
    //system(command);    

    if (strcmpi(command, "history") == 0)
    {
        printHistory();
    }
    else if(strcmpi(command, "smarthistory") == 0)
    {
        smartHistory();
    }
    else if (strcmpi(command, "ls") == 0 || (strcmpi(command, "pwd") == 0)) {
        char *path[] = {"/bin/", NULL};
        strcat(path[0], command);
        execve(path[0], path, NULL);
    }else{system(command);}
}
  • 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-05-17T20:20:53+00:00Added an answer on May 17, 2026 at 8:20 pm

    Problem solved the problem was I needed to add an ‘&’ before commandNumber in the statement: scanf(“%d”, commandNumber); located in the smartHistory function. What strikes me as odd is I made another version of this where I moved the “history” and “smarthistory” into the handle command function instead of the execCommand function. When I did this the program prints the shell prompt 3 times…if anyone knows why, please let me know. However in the version above just adding the ‘&’ works great.

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

Sidebar

Related Questions

I am trying to understand how realloc works. This is my program. It's giving
I have written a program that uses qhttp to get a webpage. This works
This program stores pairs in a map, counting the number of times a word
I have an OpenGL program that works on all of my computers but one.
int main() { int i=3; (++i)++; printf(%d,i); } This programs works with g++ compiler
I'm trying to get this simple program to work on windows, but it crashes:
I've created this simple example program, but this doesn't work. it won't connect. any
This code below doesn't work correctly since my MFC program is in unicode circumstance.
Why doesn't this work? The program stops on: this.textBox1.Text = (New text); Thread demoThread;
I am building a C# program that unzips a file, and work on this

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.