// Despertador_f.c
//
// Modulo auxiliar para Despertador_m.c
//
// Contiene la funcion esperar_tiempo implementada con la llamada al sistema nanosleep
// La estrucuta timespec se utiliza para indicar
// el intervalo de tiempo a dormir con una precision de nanosegundos.
// Para mas informacion sobre nanosleep, ver man nanosleep.
//
//    struct timespec {
//        time_t tv_sec;        /* seconds */
//        long   tv_nsec;       /* nanoseconds */
//    };
//
////////////////////////////////////////////////////////////////////////

#include <unistd.h>
#include <time.h>

///////////////////////////////////////////
// espera dormido seg segundos
//
int  esperar_tiempo( unsigned int seg )
{
   struct timespec TiempoSol ,TiempoRestante;

   if (seg <= 0)
                  return 0;

   else {
                  TiempoSol.tv_sec = seg ;
                  TiempoSol.tv_nsec = 0 ;

                  while (nanosleep(&TiempoSol, &TiempoRestante)== -1)
                  {
                                 TiempoSol.tv_sec = TiempoRestante.tv_sec;
                                 TiempoSol.tv_nsec = TiempoRestante.tv_nsec;
                  }
                  return(seg);
   }
}