title: stop a c program from launching another program
body: <p>I am doing a project for which I need to compile a C program , run it over a test data and then produce it's output. I use two bash scripts , first clearscript.sh which removes the temprory files from current directory( code.c, input, output ) and myscript.sh which compiles code.c , runs it over the input file and produces the output which I show in the text area in my GUI.</p>
  
  <p>Now my problem is that, how do I stop the user from doing things like executing system("rm *"), because that would do terrible things. One way is that I can search the string corresponding to system command and if found then I won't allow the program to run. But I feel that there must be a better solution to stop execution of a external program through a C program. </p>
  
  <p>Another problem is that when the program runs through a infinite loop the program stop responding. And I feel that there are many more problems which I will discover later. Is there any better approach to do what my program is doing.</p>
  
  <pre><code>    import java.awt.*;
      import javax.swing.*;
      import java.io.*;
      import java.awt.event.*;
  
  
  
      class test2 {
  
      static         JTextArea ta1,ta2,ta0;
  
      public static class myEvent implements ActionListener
  
        {
  
               public void actionPerformed(ActionEvent e)
  
                      { 
  
                              try{
  
                                      Process p = new ProcessBuilder("./cleanscript.sh").start();
                                      p.waitFor();
                                      }
                              catch(Exception e1)
                                      {}                                
  
                              try{
  
                                      BufferedWriter bw = new BufferedWriter(new FileWriter("code.c"));
                                      ta1.write(bw);
                                 }
                              catch (IOException ioe)
                              {        
                                      ioe.printStackTrace();
                              }
  
  
  
                              try{
                                      BufferedWriter bw = new BufferedWriter(new FileWriter("input"));
                                      ta0.write(bw);
                              }
                              catch(IOException ioe)
                              {
                                      ioe.printStackTrace();
                              }
  
  
  
  
  
                              try{
                              Process pb = new ProcessBuilder("./myscript.sh").start();
                              pb.waitFor();
                              }
                              catch(Exception t)
                                      {}
  
  
  
  
                              try {
  
                                      ta2.read(new FileReader(new File("output")),"fun");
                              }
                              catch (IOException ioe2)
                              {
                                      ioe2.printStackTrace();
                              }
  
  
  
                        }
      }
  
  
  
  
      public static void main(String args[]) {
  
      JFrame jf;
  
  
  
      jf = new JFrame();
  
  
      jf.setLocationByPlatform(true);
  
      ta0 = new JTextArea("Give Input to the program here,before running the program");
      ta1 = new JTextArea("Your C program here",0,100);
      ta2 = new JTextArea("Your output will be shown here");
  
      ta2.setEditable(false);
  
  
  
  
      JScrollPane jsp0,jsp1,jsp2;
  
  
      jsp0 = new JScrollPane(ta0);
      jsp1 = new JScrollPane(ta1);
      jsp2 = new JScrollPane(ta2);
  
  
  
  
      jf.getContentPane().setLayout(new GridLayout(4,1));
  
      jf.getContentPane().add(jsp1);
      jf.getContentPane().add(jsp0);
      jf.getContentPane().add(jsp2);
  
  
  
      JButton jb = new JButton("Run");
  
      jb.addActionListener(new myEvent());
  
  
      jf.getContentPane().add(jb);
  
  
  
      jf.setSize(1000,400);
      jf.setVisible(true);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      }}
  </code></pre>
  
  <p>code for cleanscript.sh</p>
  
  <pre><code>     rm code.c
       rm input
       rm output
  </code></pre>
  
  <p>code for myscript.sh</p>
  
  <pre><code>     cc code.c
       ./a.out &lt; input &gt; output
  </code></pre>