#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gprintf.h>

/* EXO 2.2 */

typedef struct data{
  GMainLoop * mloop;
  guint       nb_sec;
}Data;

gboolean fct_rappel(gpointer p){
  static int cmp = 0;
  cmp++;
  g_print("%d - ", cmp);
  if(cmp == ((Data*)p)->nb_sec){
    g_main_loop_quit(((Data*)p)->mloop);
    return FALSE;
  }

  return TRUE;
}


int main(int argc, char* argv[]){
  g_printf(" === EXO 2.2  === \n");

  g_printf("Saisir le nombre de secondes : \n");
  GIOChannel *channel = g_io_channel_unix_new (STDIN_FILENO);
  gchar *str;
  g_io_channel_read_line (channel, &str, NULL, NULL, NULL);
  gint64 val = g_ascii_strtoll(str, NULL, 10);


  
  GMainLoop *loop = g_main_loop_new(NULL, FALSE);
  Data pdata;
  pdata.mloop  = loop;
  pdata.nb_sec = val ; 
  

  g_timeout_add(1000, fct_rappel, &pdata);

  g_main_loop_run (loop);
  g_main_loop_unref(loop);
  g_print(" Temps ecoule \n");
}
