All pastes #985191 Raw Edit

index_local.c

public text v1 · immutable
·published 2008-04-14 20:56 UTC
rendered paste body
/* 

   Gestion d'un index telephonique 
   (pour etre server RPC avec rpcgen)

   fichier donnee: lignes ascii de la forme "nom telephone(entier)" 

*/

#include <stdio.h>
#include <errno.h>

#include "index.h"

#define NOM_FICHIER "index.data"

/* Recherche de la ligne contenant le nom correspondant... */
#define TAILLECHAR 256
static ligne entree;
static char buf[TAILLECHAR];

ligne  * recherche_1_1_svc(char ** nom, struct svc_req * c) {
  FILE *f;
  int t;
  printf("test\n");
  entree.nom = (char *)buf; entree.tel = 0;
  if ((f = fopen (NOM_FICHIER, "r")) == NULL) {perror ("svc: fopen ");}
  else {
    while ( fscanf(f,"%s%d", buf, &t ) != EOF ) {
      if ( !strcmp( *nom, buf) ) {
	entree.tel = t;
	fclose ( f );
	return ( &entree );
      }
    }
    fclose ( f );
  } /* entry not found: return empty string */
  entree.nom[0] = '\0';
  printf("test\n");
  return ( &entree );
}

/* Ajout d'une ligne si pas deja presente... */

static int status_ajout;

int * ajout_1_1_svc(ligne * a_ajouter, struct svc_req *c) {
  FILE *f;
  ligne *test;
  printf("test\n");
  status_ajout = 0;
  test = recherche_1_1_svc ( &(a_ajouter->nom), c );
  if ( ! (test->tel) ) {
    f = fopen ( NOM_FICHIER, "a" );
    fprintf (f, "%s %d\n", a_ajouter->nom, a_ajouter->tel);
    fclose(f);
    status_ajout = 1;
  }
  return ( &status_ajout );
}

/* Affiche une chaine de caractere... */

void * affiche_1_1 ( char ** s ) {
  printf ( "%s\n", *s ); fflush (stdout);
  return ((void *)1);
}


/* ============================================================ */
liste_ligne * listing_1_1_svc(void *a, struct svc_req *c)
/* ============================================================ */
{
FILE *f;
int t;

liste_ligne lst_lig;
liste_ligne *lst_tmp;

printf("Test1\n");
if ((f = fopen (NOM_FICHIER, "r")) == NULL) {perror ("svc: fopen ");}
else {
	//Si la premiere ligne n'est pas vide, on la recupere
	if ( fscanf(f,"%s%d", buf, &t ) != EOF ){
		lst_lig.lig.nom = malloc(TAILLECHAR);
		strcpy(lst_lig.lig.nom,buf);
		lst_lig.lig.tel = t;

		printf("Nom : %s, tel : %d\n",lst_lig.lig.nom, lst_lig.lig.tel);
		lst_tmp=lst_lig.ptr_lig;
	}
	//On recupere la suite si il y en a une
	while ( fscanf(f,"%s%d", buf, &t ) != EOF ) {
		lst_tmp= malloc(sizeof(liste_ligne));
		lst_tmp->lig.nom = malloc(TAILLECHAR);
		strcpy(lst_tmp->lig.nom,buf);
		lst_tmp->lig.tel = t;

		printf("Nom : %s, tel : %d\n",lst_tmp->lig.nom, lst_tmp->lig.tel);
		lst_tmp = lst_tmp->ptr_lig;
      
		}
		fclose ( f );
	}
		

printf("Test2, lst_lig = %p\n", lst_lig);

return &lst_lig;
}/* listing_1_1_svc */