My code, which gets called when you type "recent":
char* runRecent() {
FILE *ffp;
ffp = fopen("bash.txt","r");
char line[MAXLINE];
int fileItCount = 0;
for (int i = 0 ; i < numLinesinFile ; i++) {
fgets(line, sizeof(line), ffp);
if (fileCounter - 2 < 0){
printf("No recent commands exist.\n");
exit(EXIT_FAILURE);
}
if (i == (numLinesinFile - 2) && i >= 0) {
printf("Previous command: %s\n",line);
char * lineRet = strdup(line);
printf("line ret: %s\n",lineRet); //output: correct, something along the lines of "ls" or "ls -a"
printf("line ret: %d\n",&lineRet); //output: 1528174960
return lineRet;
}
}
}
The lineRet then gets passed to a function:
start(runRecent());
Where the function declaration looks like:
void start(char inputBuf[]){
printf("input is %s\n",inputBuf); //prints the command "recent" instead of the previous command that was executed
}
Why is this returning the current command instead of the previous one?
freethe result ofrunRecent.