0

I am trying to write a shell program and I have an inputBuffer array that holds the command that was entered. I also have a historyBuffer array that will hold the past 10 commands that were entered. I have the global variable: char historyBuffer[10][MAX_LINE]; (where MAX_LINE == 80) and inside the main I have char inputBuffer[MAX_LINE]; Here is the whole main function:

int main(void){
    int flag;                  //equals 1 if a command is followed by '&'
    char *args[MAX_LINE/2+1];  //command line (of 80) must have <40 arguments
    int child,             //process id of the child process
    status;        //result from execvp system call
    char inputBuffer[MAX_LINE];//buffer to hold the command entered
    strcpy(historyBuffer, inputBuffer);

    signal(SIGINT, shellHandler); //called when ^C is pressed

    while(1){   //program terminates normally inside setup
        flag = 0;
        printf(" COMMAND->\n");
        setup(inputBuffer,args,&flag); //get next comman
        child = fork();   //creates a duplicate process
        switch(child){
            case -1:
                perror("Could not fork the process");
            break;  /* perror is a library routine that displays a system
                       error message, according to the value of the system
                       vaiable "errno" which will be set during a function
                       (like fork) that was unable to successfully 
                       complete its task */
            case 0: //here is the child process
                status = execvp(args[0], args);
                if(status !=0){
                    perror("Error in execvp");
                    exit(-2); //terminate this process with error code -2
                }
                break;
            default:
                if(flag==0) //handle parent, wait for child
                    while(child != wait((int *) 0));
        }//end switch
    }//end while
}//end main

The error is with the line strcpy(historyBuffer, inputBuffer);

I get the error message: expected 'char * __restirct__' but argument is of type 'char(*)[80]'

I'm not sure if it's an issue with the parameters in the strcpy function, if it's an issue with where I'm calling strcpy, or if it's an issue with the way I declared inputBuffer or historyBuffer? Or if it's a completely different issue that I'm oblivious to?

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
JCP
  • 3
  • 6
  • 1
    What are you trying to achive with this statement strcpy(historyBuffer, inputBuffer);? historyBuffer is a two-dimensional array. Moreover inputBuffer is not initialized. The code does not make sense. – Vlad from Moscow Sep 18 '17 at 20:09
  • 2
    `strcpy(historyBuffer, inputBuffer);` --> `strcpy(historyBuffer[n], inputBuffer);` where `n` is a number in range 0..9 – Support Ukraine Sep 18 '17 at 20:10
  • Better would be `strcpy(historyBuffer, inputBuffer);` --> `strncpy(historyBuffer[n], inputBuffer, MAX_LINE);` where n is a number in range 0..9 – fpmurphy Sep 19 '17 at 03:43

1 Answers1

0

The two arguments to strcpy should be pointers to char (i.e. "strings"). But historyBuffer is an array of pointers to char (i.e. a pointer to strings).

You would need to dereference historyBuffer when you pass it as an argument e.g. historyBuffer[0]. Also since your arrays are a fixed, known size, you should really be using strncpy which is less prone to overflows.

That will fix the error, but your code still doesn't make any sense. strcpy just performs a one time copy of memory, and you are calling it at the beginning of your code when inputBuffer has not been initialized with anything. So it is pointless.

If you actually want to store each inputBuffer in historyBuffer, you will need to call strcpy after each time it is read, and have some indices set up to indicate which elements of historyBuffer contain valid data and which history entry should be overwritten next.

Max
  • 21,123
  • 5
  • 49
  • 71