#include <stdio.h> 

int home2(char**); 

int main(int argc, char ** argv, char ** envp){ 

  int i=0; 
  int child = 0; 
  int childpid = 0; 

  for(i = 0; i< 10000; i++){ 

   if(!(i%10))
     home(); 
   if(!(i%112))
     home2(envp); 
   if(!(i%321)){
     child = spoon();
     if(child)
       childpid = child; 
   }
   if(!(i%3522))
     if(childpid){
       info(childpid);
       childpid = 0; 
     }else{ 
       info(0); 
     }

   if(!(i%3522))
     sunn();
  }



} 

int home(){

  static int x = 1; 

  if(x){
    printf("HOME=%s\n", getenv("HOME")); 
    x = 0; 
  } 

}

int home2(char ** envp){

  static int x = 0; 
 
  if(!x){
    while(envp[x]){ 
      if(!strncmp(envp[x++],"HOME=",5)) 
        printf("%s\n",envp[x-1]); 
    }
    x = 1; 
  } 

}

int spoon(){

  static int x = 1; 
  char command[100]; 
  int childpid = 0; 

  sprintf(command, "/usr/bin/ps -eaf | grep "); 

  if(x){
    childpid = fork(); 
    if(childpid != 0){ 
      sprintf(command, "%s%d",command,childpid); 
      system(command); 
      
    }else{ 
      printf("This child will be defunct by the time the parent greps\n");
    }

    x = 0; 
    return(childpid); 
  } 

  return(0); 
}
int info(int childpid){

  if(childpid){
    printf("%s%d%s%d%s%s%s", 
	   "Notice in the defunct line of the ps output above the childs(",
	    childpid, 
	   ") parents pid is ",
	   childpid -1,
	   "\n\tthe sh ...  line is also owned by the parent and was\n",
	   "\texecuted after the child. The grep line is owned by\n",
	   "\tthe sh ...  from\n\n"); 
  } 

}
int sunn(){
  static int x = 1; 
  printf("Glory be to the sunn call number %d\n",x++); 

}











