/* vonkoch.c */ /* compilation avec : */ /* cc -W -Wall vonkoch.c -o vonkoch -lm */ #include #include /* Pour sqrt */ /* Pour ecrire votre code C, aller a la fin du fichier... vers la ligne 194 */ /****************************************************************************/ /************* Pour tracer des vecteurs *************************************/ /****************************************************************************/ /***/#include /* gnuplot.c */ /***/#include /***/#include /* Pour exit, fork */ /***/#include /* Pour EXIT_FAILURE */ /***/#include /* Pour errno */ /***/#include /* Pour strcmp */ /***/#define PipeEtFork /***/ /* Si PipeEtFork non defini : */ /***/ /* lancement avec : vonkoch | gnuplot -persist */ /***/typedef struct { double x,y; } PointGnuplot; /***/static int stderrDejaUtilise = 0; /***/FILE *fdGnuplot; /***/FILE* openGnuplot(char *fileName) /***/{ /***/ FILE *fd; /***/ if (fileName!=NULL && strcmp(fileName,"")!=0) { /***/ fd=fopen(fileName,"w"); /***/ if (fd==NULL) printf("Attention:openGnuplot, probleme avec fopen\n"); /***/ return fd; /***/ } /***/#ifdef PipeEtFork /***/ { /***/ int tube[2]; /***/ int ok; /***/ pid_t fils; /***/ ok = pipe(tube); /***/ if (ok==-1) { /***/ printf("Attention:openGnuplot, probleme avec pipe\n"); /***/ exit(EXIT_FAILURE); /***/ } /***/ do { /***/ fils=fork(); /***/ } while ((fils==-1) && (errno==EAGAIN)); /***/ if (fils==-1) { /***/ printf("Attention:openGnuplot, probleme avec fork\n"); /***/ exit(EXIT_FAILURE); /***/ } /***/ if (fils==0) { /* Le fils */ /***/ dup2(tube[0],STDIN_FILENO); /* tube[0] => Entree standard */ /***/ close(tube[0]); /* Il est donc maintenant possible de fermer tube[0] */ /***/ close(tube[1]); /* ESSENTIEL : pour la fin du tube */ /***/ execlp("gnuplot","gnuplot","-noraise","-persist",NULL); /***/ printf("Attention:initGnuplot, probleme avec execlp\n"); /***/ system("/home2/commun_depinfo/enseignants/rodin/bin/INSTALL_gnuplot"); /***/ execlp("gnuplot","gnuplot","-noraise","-persist",NULL); /***/ printf("Attention:initGnuplot, probleme avec execlp (BIS !!!)\n"); /***/ exit(EXIT_FAILURE); /***/ } /***/ /* Le processus principal continue */ /***/ close(tube[0]); /***/#if 1 /***/ fd = fdopen(tube[1],"a"); /***/ if (fd==NULL) { /***/ perror("fdopen"); /***/ printf("Attention:openGnuplot, probleme avec fdopen\n"); /***/ close(tube[1]); /***/ exit(EXIT_FAILURE); /***/ } /***/ /* Et on ne fait pas : close(tube[1]); !!! */ /***/#else /***/ /* Si on veut vraiment travailler avec stderr !*/ /***/ dup2(tube[1],STDERR_FILENO); /* stderr <- tube[1] */ /***/ close(tube[1]); /***/ fd = stderr; /***/#endif /***/ } /***/#else /* Avec un lancement : $pg $* | gnuplot -persist */ /***/ { /***/ Ici, On inverse stdout et stderr de facon a envoyer /***/ stderr dans le pipe | /***/ => Mis a part dans les fonctions decrites ici, /***/ il ne faut pas utiliser stderr /***/ int temp=dup(STDOUT_FILENO); /* temp <- stdout */ /***/ close(STDOUT_FILENO); /***/ dup2(STDERR_FILENO,STDOUT_FILENO); /* stdout <- stderr */ /***/ close(STDERR_FILENO); /***/ dup2(temp,STDERR_FILENO); /* stderr <- temp */ /***/ close(temp); /***/ fd = stderr; /* Et l'ecriture se fera sur stderr ! */ /***/ } /***/#endif /***/ if (fd==stderr) /***/ { /***/ if (stderrDejaUtilise) /***/ { /***/ printf("\n"); /***/ printf("*********************************************************\n"); /***/ printf("* Attention:openGnuplot, stderr utilise plusieus fois ! *\n"); /***/ printf("*********************************************************\n"); /***/ } /***/ else { stderrDejaUtilise=1; /***/ printf("\n"); /***/ printf("************************************************\n"); /***/ printf("* stderr ne doit pas etre utilise dans le code *\n"); /***/ printf("************************************************\n"); /***/ printf("\n"); /***/ } /***/ } /***/ return fd; /***/} /***/void closeGnuplot(FILE* flot) /***/{ /***/ fclose(flot); /***/} /***/void setAutoscaleGnuplot(FILE* flot) /***/{ /***/ fprintf(flot,"set autoscale\n"); /***/ fflush(flot); /***/} /***/void setRange(FILE* flot, /***/ double xmin, double xmax, double ymin, double ymax) /***/{ /***/ fprintf(flot,"set xrange [%f:%f]\n",xmin,xmax); /***/ fprintf(flot,"set yrange [%f:%f]\n",ymin,ymax); /***/ fflush(flot); /***/} /***/void beginPointsToGnuplot(FILE* flot, /***/ char *style) /* "lines" , "linespoint" , "points" */ /***/{ /***/ fprintf(flot,"plot '-' with %s\n",style); /***/ fflush(flot); /***/} /***/void pointsToGnuplot(FILE* flot, PointGnuplot tabPoint[], int nbPoints) /***/{ /***/ int i=0; /***/ for(i=0;i