I am currently using Kubuntu 8.04. Although I do not know any bash, I altered the bash shell (via internet guides) to append history instead of truncating the history file. After editing the shell, the default way to clear the history only clears the interactive history and not the actual history file (since it is no longer truncated upon closing the shell). I then wrote a c++ script to clear the contents of the history file (or an alternative file if one is presented as an argument). It works when I try to access the $HOME environmental variable using getenv("HOME"), but I receive a segmentation fault when I try to use getenv("HISTFILE") which is the current file that the bash shell stores its history to. I also wanted to clear the interactive history at the same time. The bash command is "history -c", but I do not know how to incorporate this. I tried system("history -c") but I received an error as "history" could not be found. I was wondering if there is a way to directly access the environment variable $HISTFILE and also call the shell command history -c. Here is my current c++ script
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "/mydocuments/Documents/c++/headers/stringcaps.h"
using namespace std;
int main (int argc, char * argv[])
{
string file, input = getenv("HOME"); //input is set to $HOME variable for use with file
if (argc == 1) //I would prefer to use $HISTFILE if possible
file = input + "/.bash_history"; //set file to default $HOME/.bash_history
else
file = argv[1]; //file becomes offered argument
ifstream file_check; //open file to check
file_check.open(file.c_str()); //if it exists
if (file_check.fail()) //if not, abort
cout << file << " does not exist\nNothing was cleared" << endl;
else //if file exists, continue
{
file_check.close(); //close test file before continuing
cout << "The contents of " << file
<< " will be cleared" << endl;
cout << "Action cannot be reversed\nContinue? [y/n] -> ";
getline(cin, input);
if (input == "")
input = "y";
lowercase(input); //function that changes a string to all lowercase letters found in stringcaps.h
if (input[0] == 'y')
{
ofstream hist_file;
hist_file.open(file.c_str(), ios::trunc); //open file and erase contents
hist_file.close();
/* Next line is currently removed until the problem is solved
system("history -c");*/
cout << file << " has been cleared" << endl;
}
else
cout << file << " not cleared" << endl;
}
return 0;
}
I would appreciate any help you can offer me or any advice on optimizing or improving the code.