1

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?

1
  • Probably some undefined behavior. Compile with all warnings & debug info (gcc -Wall -Wextra -g). Then use the debugger (gdb) and probably its watchpoints. Commented Sep 7, 2015 at 19:25

2 Answers 2

1

There is no storage for the strings in cache.

Something like strdup would create storage, but you would need to free the memory later.

int lsh_cache_line(int counter,char *line, char *cache[10]){

 (cache[counter]) = strdup(line);
 printf("This is cache[%i]:%s\n", counter, cache[counter]);
 counter++;
 counter = counter % 10;
 return counter; 

}

There are 10 slots for strings, but no memory for the string values. You need to allocate some memory.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure what you mean I initialized cache in the main loop by char *cache[10] = {NULL}
0

I would bet that your lsh_read_line() function returns for you always the same buffer with different texts. You store then the pointer to this buffer into different cells in your array. You should copy the text to newly allocated string once you get it into your line variable and work later only with this new copy.

2 Comments

I added: char *tmp; strcpy(tmp, line); cache[counter] = tmp; to the cache function and got the same results
and what about char *tmp = malloc(strlen(line) * sizeof(char)); strcpy(tmp, line); cache[counter] = tmp;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.