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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:46:33+00:00 2026-05-27T03:46:33+00:00

I have been trying to program a UNIX style shell command prompt in C.

  • 0

I have been trying to program a UNIX style shell command prompt in C. Within this program I need it to keep track of the commands that have already been used, so the user can recall the last command by entering ‘r’. I made a globally initialized array to hold strings. Whenever the array of characters a user entered needs to be saved, I add it to the global array. I have tried memcpy, simply copying each value using a loop, and just copying the pointer. None of these have been working. I am not super familiar with C and I am sure it is a pointer problem.

Whenever I copy the pointer of inputBuffer to my global array string (it does get copied), however upon leaving the setup function this pointer disappears? I am not exactly sure what I am doing wrong.

Test:

(1)user input –> ls

string[0] = ls

(2)user input –> r

inputBuffer = ls

string[recent] = ls

incorrectly does…

inputBuffer = r

string[recent] = r

(I have included the relevant parts of the code.)

 #define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
    #define SAVED_BUFFER 100


    char *string[SAVED_BUFFER];
    int p = 0;
    int recent = -1;
    int stringSize = 0;

    void setup(char inputBuffer[], char *args[],int *background)
    {
        int length, /* # of characters in the command line *

        /* read what the user enters on the command line */
        length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  

        start = -1;
        if (length == 0)
            exit(0);            /* ^d was entered, end of user command stream */
        if (length < 0){
            perror("error reading the command");
            exit(-1);           /* terminate with error code of -1 */
        }

        if (inputBuffer[0] == 'r' && inputBuffer[1] == '\n' && stringSize > 0) {
            int k;  

            memcpy(inputBuffer, string[recent], strlen(string[recent]) + 1);
            printf("%s",inputBuffer);
            printf("%s",string[recent]);

        }
        else {  

            string[p] = inputBuffer;
            printf("%s", string[0]);

            stringSize++;
            recent++; // one behind strings current array location, to get history
            p++; // current string array spot
        }

    }

    int main(void)
    {
        char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
        int background;             /* equals 1 if a command is followed by '&' */
        char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */


        while (1) {            /* Program terminates normally inside setup */
        background = 0;
        printf("COMMAND2->");

            fflush(0);

        setup(inputBuffer, args, &background);       /* get next 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-27T03:46:33+00:00Added an answer on May 27, 2026 at 3:46 am

    When you “save the input buffer” you actually only store a pointer to the inputBuffer array:

    string[p] = inputBuffer;
    

    The actual data is not copied, you just store a pointer to the global input buffer. When the next input replaces the old content of inputBuffer, you will see the new content even if you access it through string[recent].

    The calls to memcpy don’t actually do anything, since the passed input and output buffer all refer to the same memory.

    To actually store a copy of the data you have to allocate new memory to store the copy. Since you are dealing with strings, this is most easily done with strdup(), which duplicates a string:

    string[p] = strdup(inputBuffer);
    

    Later, once you are done with such a copy and don’t need it anymore you have to free the memory used by the copy:

    free(string[p]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to execute this program on my MinGW, through Code::Blocks: #include
I have been trying to solve this program: Program to draw a color cube
I have been trying to understand of this following C-program: #include <stdio.h> int arr[]
I have been trying to get this program to work but so far having
I have been trying to compile the program on this website and got most
I have been trying to create a Ruby program that will be running online
I have been trying to create a simple program with Python which uses OpenCV
I have been trying to do a fill using the open source Srecord Program.
I've been trying to optimize a numeric program of mine, and have run into
I have been trying to get around this error for a day now and

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.