All pastes #985212 Raw Edit

Something

public text v1 · immutable
·published 2008-04-14 21:11 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 FILENAME "index.data"
#define NOM_FICHIER "index.data"

/* Recherche de la ligne contenant le nom correspondant... */

static ligne entree;
static char buf[256];
static chained_list list;
struct svc_req request;

ligne * recherche_1_svc ( char ** nom, struct svc_req *request ) {
  FILE *f;
  int t;

  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';
  return ( &entree );
}

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

static int status_ajout;

int * ajout_1_svc ( ligne * a_ajouter, struct svc_req *request ) {
  FILE *f;
  ligne *test;

  status_ajout = 0;
  test = recherche_1_svc ( &(a_ajouter->nom), request  );
  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_svc ( char ** s, struct svc_req *request ) {
  printf ( "%s\n", *s ); fflush (stdout);
  return ((void *)1);
}

void free_chained_list(chained_list *liste)
{	if(liste !=NULL){
		chained_list *iterator;
		while(liste->next != NULL)
		{
			iterator = liste;
			liste = liste->next;
			free(iterator);
		}
	}
}

chained_list *listage_1_svc (void *rien2, struct svc_req *request) {
	chained_list *iterator;

	free_chained_list(&list);
	FILE *f;
	int tel;

	if ((f = fopen (FILENAME, "r")) == NULL) {perror ("Server : problem while opening file "); exit(0);}
	else {
		if( fscanf(f, "%s%d", buf, &tel) !=EOF) { 
		
		list.line.nom = malloc(sizeof(char)*strlen(buf));
		memcpy(list.line.nom, buf, strlen(buf)); 
		
		list.line.tel = tel; 
		
		iterator = &list;
		printf("%s\n" , iterator->line.nom);
		
		
		while( fscanf(f, "%s%d", buf, &tel) != EOF ) {
		iterator->next = malloc(sizeof(chained_list));
		iterator = iterator->next;
		iterator->line.nom = malloc(strlen(buf));
		memcpy(iterator->line.nom, buf, strlen(buf)); 
		iterator->line.tel = tel; 
		printf("%s\n" , iterator->line.nom);
		}
		
		}
	fclose(f);

	}
	return &list;	
}
void * stop_1_svc(char **mdp, struct svc_req *request)
{
	printf("Shutting down %s !\n", mdp[0]);
	if ( !strcmp(mdp[0] , "now" ) )
	{
		exit(0);
	}
	else return (void *)0;
}