"""
fuzz harvester. note: expects
fuz input to have 'fun' in the filename
"""
import os
import sys
import time
import signal

global pid

def timeout(a,b):
  os.kill(pid, 9)

def savefile(args):
  for a in args:
    if 'fun' in a:
      try:
        os.stat("keep")
      except OSError:
        os.system("mkdir keep")
      t = time.time()
      print "[+] Kept good file\n"
      os.system("mv %s keep/%f_%s"%(a, t, a))
      break

signal.alarm(2) #change if needed
signal.signal(signal.SIGALRM, timeout) 

pid = os.fork()
if pid == 0:
  os.execv(sys.argv[1], sys.argv[1:])
else:
  try:
    npid, status = os.waitpid(pid, 0)
  except OSError:
    exit(0)
  #uncomment the next line for os x
  # sig = (status>>8)&0xff
  sig = status & 0xff
  if sig != 0:
    savefile(sys.argv)  
    print "Sig %d, keeping file\n"%sig
  print "child exited with",sig
