/*
 * Antonio Ospite 408/244
 * 
 * jlist.c: gestione di una lista di job
 */

#include "jlist.h"

/*
 * definizione delle funzioni 
 */

int
make_joblist (joblist * list)
{
  list->Head = NULL;
  list->Tail = list->Head;
  list->elements = 0;
  return 0;
}

int
add_job_to_list (joblist * list, pid_t pid, char *name, char *status)
{
  job_node *newjob;

  newjob = malloc (sizeof (job_node));

  if (newjob == NULL)
    return -1;

  newjob->pid = pid;
  strcpy (newjob->name, name);
  newjob->status = status;
  newjob->jnext = NULL;

  if (list->Head == NULL)
  {				/*
				 * inseriamo il primo elemento nella lista 
				 */
    list->Head = newjob;
    list->Tail = list->Head;
  }
  else
  {				/*
				 * inseriamo glia ltri nofi accodandoli alla
				 * lista 
				 */

    list->Tail->jnext = newjob;
    list->Tail = list->Tail->jnext;
  }

  list->elements++;

  return 0;
}

int
rm_job_from_list (joblist * list, pid_t pid)
{
  job_node *currentjob;
  job_node *previousjob;
  int found;

  currentjob = list->Head;
  previousjob = NULL;
  found = 0;

  /* 
   * se la lista e' vuota la funzione ritorna 0
   */
  if (list->elements == 0)
    return 0;

  if (currentjob->pid == pid)
    found = 1;


  while ((currentjob != NULL) && (found != 1))
  {
    previousjob = currentjob;
    currentjob = currentjob->jnext;
    if (currentjob != NULL)
      if (currentjob->pid == pid)
	found = 1;
  }

  /*
   * Se il job con identificativo "pid" non e' stato trovato 
   */
  if ((currentjob == NULL) || (found = 0))
    return -1;


  if (currentjob == list->Tail)
  {
    /*
     * cancellazione della coda della lista 
     */
    list->Tail = previousjob;
  }
  if (previousjob == NULL)
  {
    /*
     * cancellazione della testa della lista 
     */
    list->Head = currentjob->jnext;
    if (list->Tail == currentjob)
      list->Tail = list->Head;
  }
  else
  {
    /*
     * cancellazione di un nodo interno 
     */
    previousjob->jnext = currentjob->jnext;
  }

  /*
   * deallocazione della memoria 
   */
  free (currentjob);
  list->elements--;
  return 0;
}

int
change_job_status (joblist * list, pid_t pid, char *status)
{
  job_node *currentjob;
  int found;

  currentjob = list->Head;
  found = 0;

  if (list->elements == 0)	/*
				 * se la lista e' vuota la funzione ritorna 
				 */
    return 0;

  if (currentjob->pid == pid)
    found = 1;

  while ((currentjob != NULL) && (found != 1))
  {
    currentjob = currentjob->jnext;
    if (currentjob != NULL)
      if (currentjob->pid == pid)
	found = 1;
  }

  /*
   * Se il job con identificativo "pid" non e' stato trovato 
   */
  if ((currentjob == NULL) || (found = 0))
    return -1;

  else
    currentjob->status = status;

  return 0;
}

int
has_job_status (joblist * list, pid_t pid, char *status)
{
  job_node *currentjob;
  int found;

  currentjob = list->Head;
  found = 0;
  /*
   * Se la lista e' vuota la funzione ritorna -1
   */
  if (list->elements == 0)
    return -1;

  if (currentjob->pid == pid)
    found = 1;

  while ((currentjob != NULL) && (found != 1))
  {
    currentjob = currentjob->jnext;
    if (currentjob != NULL)
      if (currentjob->pid == pid)
	found = 1;
  }

  /*
   * Se il job con identificativo "pid" non e' stato trovato 
   */
  if ((currentjob == NULL) || (found = 0))
    return -1;

  else if ((strcmp (currentjob->status, status) == 0))
    return 0;

  else
    return -1;

}



void
printlist (joblist * list)
{
  job_node *currentjob;
  int jobs;

  currentjob = list->Head;
  jobs = 0;


  /*
   * stampa degli elementi della lista 
   */
  if (list->elements == 0)
    printf ("Non vi sono job in esecuzione");

  else
    while (currentjob != NULL)
    {
      printf ("%d - %d -- %s: %s\n", ++jobs, currentjob->pid,
	      currentjob->name, currentjob->status);

      currentjob = currentjob->jnext;	/*
					 * prossimo nodo 
					 */
    }

  printf (".\n");
  fflush (stdout);

  return;

}				/*

				 * fine di printlist() 
				 */


syntax highlighted by Code2HTML, v. 0.9.1