/* include files */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

/* a simple define for convenience */
#define TRUE 1

int main()
{
  /* C declaration before any function call */
  int pid,i=0;
  char tab[256], *s;

  /* the main loop of our shell */
  while (TRUE) {
    /* the printf to print strings and numbers */
    printf("prompt %d> ",i);
    /* "flush" the standard output */
    fflush(stdout);

    /* read the command line */
    s = gets(tab);
	     
    if (s == NULL) { /* <Control+D> pressed */
      fprintf(stderr, "Bye bye\n");
      exit(0);
    }

    /* one process running */
    pid = fork();
    /* 2 processes running */
    printf("I'm running\n");

    switch (pid) /* where are we ? */
      {
      case 0: /* in the child! */
	printf("In the child\n");
	/* exec command */
	execlp(tab,tab,NULL);
	/* executed only if the exec command fails */
	perror("exec");
	exit(0);
	break;
      case -1: /* in... the parent: fork has failed no wait!*/
	perror("fork");
	break;
      default: /* in the parent, the fork worked! */
	printf("In the parent.. wait\n");
	wait(0);
	i++; /* for the next command */
      }
  }
  /* end of the loop */

  return 0;
}

