HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Un*x terminal history wiper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wiperterminalhistory

Problem

Using various answers from Stack Overflow and Ask Ubuntu, I've hacked together this simple C program that wipes terminal history. It works but I'm relatively new to C so I'm not sure if everything is correct.

#include 

int main(void) {
  // check if the program is being executed in a terminal
  if (isatty(0)) {
    // if the program is being executed in a 
    // terminal just start the wiper
    system("cat /dev/null > ~/.bash_history;history -c;history -w;rm -r ~/.bash_history;exit;");
  } else {
    // if the program is being executed as a stand-alone
    // open a new terminal window and then start the wiper
    system("gnome-terminal -x sh -c 'cat /dev/null > ~/.bash_history;history -c;history -w;rm -r ~/.bash_history;exit;'");
  }
  // stop the program
  return 0;
}

Solution

That's a lot of shell commands to do very little.

You could replace all those shell commands to get the equivalent effect with just this:

rm ~/.bash_history; HISTFILE=


Let me explain step by step why this is equivalent:

  • cat /dev/null > ~/.bash_history - truncate the file. Exactly the same as the shorter > ~/.bash_history. This is pointless, because later you delete the file anyway.



  • history -c - clear the history. Pointless because later you delete the file anyway



  • history -w - write the content of the history to the file. Pointless because later you delete the file anyway



  • rm -r ~/.bash_history - delete the file, recursively. The recursive flag is pointless, as the history file is never a directory



  • exit - This is pointless, as the system function will exit automatically anyway



All this can be reduced to what I gave above, by benefiting from the fact that if HISTFILE is unset, history is not saved anywhere.

Btw, I don't understand why you need a gnome terminal at all. I think you can simply remove that special treatment.

Lastly, using system is generally not recommended, it should be avoided as much as possible. If you must use it for some reason, you should call commands by their absolute paths, otherwise the program will be vulnerable to path injection attacks.

Code Snippets

rm ~/.bash_history; HISTFILE=

Context

StackExchange Code Review Q#120299, answer score: 3

Revisions (0)

No revisions yet.