/*  Fexec -- Forks and Execs a program

    Written by Clinton Ebadi
    Website: (http://unknownlamer.org/) 
    Email: <clinton @at@ unknownlamer .dot. org>

    I. Clinton Ebadi, hereby disclaim all copyright and release this
    program into the public domain.
*/

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main (int argc, char *argv[]) 
{
  int i;
  pid_t pid_of_child;
  if (argc == 1) 
    {
      fprintf (stderr, "Usage: %s program arg1 arg2 ...argN\n", argv[0]);
      fprintf (stderr, "Where program is the program to execute "
	       "and arg1 ... argN are\narguments to that program\n");
      fprintf (stderr, 
	       "%s is Copyright (c) 2002 Clinton Ebadi"
	       "<unknown_lamer@unknownlamer.org>\n",
	       argv[0]);
      fprintf (stderr, "%s is licensed under the GPL Version 2. "
	       "See COPYING fo Details\n",argv[0]);
      return 2; /* 2 == no execution took place ? */
    }

  /*
  for (i=0;i<argc;i++)
    fprintf (stderr,"argv[%d] = %s\n", i, argv[i]);
  */
  
  for (i=0;i<=argc-2;i++)
    argv[i] = argv[i+1];
  
  argv[argc-1] = NULL;
  /*
  fprintf (stderr,"After shift\n");
  for (i=0;i<argc-1;i++)
    fprintf (stderr,"argv[%d] = %s\n", i, argv[i]);
  */
  pid_of_child = fork();

  if (pid_of_child == 0) 
    {
      execvp (argv[0], argv);
      fprintf (stderr, argv[0]); /* ... */
      perror (" failed to execute because");
      exit (1);
    }  
  /*  fprintf (stderr,"Exiting fexec...\n\n"); */

  return 0;
}

