I am building a shell history function to take in a string and add it to an array of strings as the program runs.
My problem is that whenever I update the array with a new line (string) the previous element in cache gets filled with my CWD (current working directory), but I need to to keep the previous string I set it to.
This is my main loop that gets the string and attempts to store the history with the cache function:
//prints out the cwd; then loops to take in the line, split it up into arguments, and attempt to execute it
//while lsh_execute returns 0, then frees up the allocated space
void lsh_loop(void)
{
char *line; //pointer to a char (the beg. of an array of chars)
char *cache[10] = {NULL}; //history array
char **args; //pointer to a pointer of a char...
int status, counter = 0, i, j;
do {
printf("%s>", getcwd(0,0)); //print cwd
line = lsh_read_line(); //call read line
counter = lsh_cache_line(counter,line, cache);
printf("This is counter:%i\n", counter);
for(i=0; i<10; i++){
printf("This is cache[%i]:%s\n", i, cache[i]);
}
args = lsh_split_line(line); //split line
status = lsh_execute(args); //execute the split args
free(line); //free memory
free(args);
} while (status); //continue as long as execute returns 1
}
In this function, I am copying the input string line to the array of strings:
int lsh_cache_line(int counter,char *line, char *cache[10]){
(cache[counter]) = line;
printf("This is cache[%i]:%s\n", counter, cache[counter]);
counter++;
counter = counter % 10;
return counter;
}
This is the output of my program:
paul@paul-VirtualBox:~/Desktop$ gcc shell.c
paul@paul-VirtualBox:~/Desktop$ ./a.out
/home/paul/Desktop>HI
This is cache[0]:HI
This is counter:1
This is cache[0]:HI
This is cache[1]:(null)
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>this is my problem
This is cache[1]:this is my problem
This is counter:2
This is cache[0]:/home/paul/Desktop
This is cache[1]:this is my problem
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>it overwrites my previous string with the cwd
This is cache[2]:it overwrites my previous string with the cwd
This is counter:3
This is cache[0]:/home/paul/Desktop
This is cache[1]:/home/paul/Desktop
This is cache[2]:it overwrites my previous string with the cwd
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>^C
paul@paul-VirtualBox:~/Desktop$
I have experimented with different ways of declaring and initializing the array of strings but this way seems to work the best.
What am I doing wrong?
gcc -Wall -Wextra -g). Then use the debugger (gdb) and probably its watchpoints.