/* remplit le périphérique avec le superbloc, la table des inoeuds et des blocs libres chaînés */
void fill_device(char * pgname, SGF_t *mon_SGF)
{
	/* allocation (par écriture) des blocs sur le périphérique de mémoire de masse */
    int i;
    int erreur;
    char *bloc;			/* bloc à allouer dynamiquement en fonction de la taille d'un bloc */

    assert( mon_SGF != NULL && mon_SGF->superbloc != NULL && mon_SGF->table_inoeuds != NULL );		/* DEBUG*/

    if ( mon_SGF->superbloc->taille_bloc < sizeof(superbloc_t) )
    {
	fprintf(stderr,"%s: taille d'un bloc %d o < %ld o (taille min)\n", pgname, mon_SGF->superbloc->taille_bloc, (long int)sizeof(superbloc_t));
	exit (EXIT_TAILLE_BLOC);
    }

							/* écrit le superbloc en premier, dans le bloc 0 */
    if ((erreur = ecrire_superbloc (mon_SGF)) > 0)
    {
	affiche_erreur( erreur, pgname);
	exit (erreur);
    }
							/* écrit la table des inoeuds dans les blocs suivants */
    if ((erreur = ecrire_table_inoeuds (mon_SGF)) > 0)
    {
	affiche_erreur( erreur, pgname);
	exit (erreur);
    }

    bloc = malloc(mon_SGF->superbloc->taille_bloc);
    if (bloc == NULL)
    {
	fprintf(stderr,"%s: pb allocation mémoire\n", pgname);
	exit (EXIT_MEM_ALLOC);
    }

							/* avance jusqu'au premier bloc libre */
    lseek(mon_SGF->device_num, mon_SGF->superbloc->premier_bloc_libre * mon_SGF->superbloc->taille_bloc, SEEK_SET);
    memset (bloc, 0, mon_SGF->superbloc->taille_bloc);
    for (i=mon_SGF->superbloc->premier_bloc_libre; i<mon_SGF->superbloc->taille_du_SF-1; i++)
    {
	*(int *)bloc = i+1;				/* chainage des blocs libre, numéro du bloc libre suivant */
	if (write(mon_SGF->device_num, bloc, mon_SGF->superbloc->taille_bloc) < mon_SGF->superbloc->taille_bloc)
	{
	    fprintf(stderr,"%s: ", pgname);
	    perror (DEVICE_NAME);
	    exit (EXIT_WRITE_PB);
	}
    }
    *(int *)bloc = NULL_BLOC;				/* chainage des blocs libres, fin de chaine */
    if (write(mon_SGF->device_num, bloc, mon_SGF->superbloc->taille_bloc) < mon_SGF->superbloc->taille_bloc)
    {
	fprintf(stderr,"%s: ", pgname);
	perror (DEVICE_NAME);
	exit (EXIT_WRITE_PB);
    }
}
void ajoute_inoeud_racine(char * pgname, SGF_t *mon_SGF)
{
    int		num_inoeud_racine;	/* inoeud racine du répertoire racine à créer en premier */
    dir_element_t element_racine[2];	/* élément contenu par le répertoire racine */
    int erreur;

    assert( mon_SGF != NULL && mon_SGF->superbloc != NULL && mon_SGF->table_inoeuds != NULL );		/* DEBUG*/

							/* créé le premier inoeud avec le répertoire racine "/" */
    num_inoeud_racine = creer_inoeud ( INOEUD_REPERTOIRE, mon_SGF);
    if (num_inoeud_racine <0) 
    {
	fprintf(stderr,"%s: SF plein, plus d'inoeud disponible !\n",pgname );
	exit (EXIT_FS_FULL);
    }

    element_racine[0].elt_inoeud = num_inoeud_racine;		/* le répertoire racine, pointe vers lui-même avec '.' */
    strcpy(element_racine[0].elt_name, ".");
    element_racine[1].elt_inoeud = NO_INOEUD;			/* et c'est tout, la liste s'arrête là */

    erreur=ecrire_donnees_dans_inoeud ( (char *) element_racine, sizeof(element_racine), num_inoeud_racine, mon_SGF);
    if ( erreur )
    {
	affiche_erreur( erreur, pgname);
	exit (erreur);
    }
}
int main (void)
{
	char ligne[LG_LIGNE];
	wordexp_t       mots;
	int           erreur;
	pid_t            pid;

	while (1) {
		fprintf(stdout, "-> ");
		if (fgets(ligne, LG_LIGNE, stdin) == NULL)
			break;
		if (strlen(ligne) == 0)
			continue;
		if (ligne[strlen(ligne) - 1] == '\n')
			ligne[strlen(ligne) - 1] = '\0';
		if ((erreur = wordexp(ligne, & mots, WRDE_SHOWERR)) != 0) {
			affiche_erreur(erreur);
			goto fin_boucle;
		}
		if (mots.we_wordc == 0)
			goto fin_boucle;
		if (strcmp(mots.we_wordv[0], "set") == 0) {
			if (mots.we_wordc != 3) {
				fprintf(stderr, "syntaxe : set variable valeur\n");
				goto fin_boucle;
			}
			if (setenv(mots.we_wordv[1], mots.we_wordv[2], 1) < 0)
				perror("");
			goto fin_boucle;
		}
		if ((pid = fork()) < 0) {
			perror("fork");
			goto fin_boucle;
		}
		if (pid == 0) {
			execvp(mots.we_wordv[0], mots.we_wordv);
			perror(mots.we_wordv [0]);
			exit(EXIT_FAILURE);
		} else {
			wait(NULL);
		}
		fin_boucle :
			wordfree(& mots);
	}
	fprintf(stdout, "\n");
	return EXIT_SUCCESS;
}
int main(int argc, char * argv[])
{
    SGF_t * mon_SGF;		/* ma structure de SGF */
    int inoeud_rep;
	char* chemin;
    dir_element_t * liste_rep = NULL;
    int i;
	int list=0;

    if ( argc != 2 && argc != 3)
    {
		fprintf(stderr,"Usage: %s [-l] dir\n", argv[0]);
		exit (EXIT_PARAM);
    }
	if(argc == 3) {
		if(strcmp(argv[1], "-l") != 0) {
			fprintf(stderr,"Usage: %s [-l] dir\n", argv[0]);
			exit (EXIT_PARAM);
		}
		chemin = argv[2];
		list = 1;
	} else {
		chemin = argv[1];
	}

    if (chemin[0]!='/')
    {
	fprintf(stderr,"%s: erreur de designation \"%s\" doit commencer par '/' (chemin absolu).\n", argv[0], argv[1]);
	exit (EXIT_PARAM);
    }

    mon_SGF = ouvrir_SGF();
    if (mon_SGF == NULL )
    {
	fprintf(stderr,"%s: ", argv[0]);
	perror (DEVICE_NAME);
	exit (EXIT_DEVICE);
    }

    inoeud_rep=inoeud_designation_element(chemin, 0, mon_SGF);

    if (inoeud_rep == NO_INOEUD)
    {
	fprintf(stderr,"%s: chemin \"%s\" inexistant !\n", argv[0], chemin);
	exit (EXIT_PARAM);
    }

    if ((liste_rep =lire_liste_rep_dans_inoeud ( inoeud_rep, mon_SGF)) == NULL)
    {
	affiche_erreur( EXIT_DEVICE , argv[0]);
	exit (EXIT_DEVICE);
    }

	afficherInoeud(inoeud_rep, chemin, (list) ? mon_SGF->table_inoeuds[inoeud_rep].taille : -1);
    i=0;
    while( liste_rep[i].elt_inoeud != NO_INOEUD)
    {
		afficherInoeud(liste_rep[i].elt_inoeud, liste_rep[i].elt_name, (list) ? mon_SGF->table_inoeuds[liste_rep[i].elt_inoeud].taille : -1);
		i++;
    }

    mon_SGF=fermer_SGF(mon_SGF);

    exit (0);
}