assembler.c (1237B)
1 #include <assert.h> 2 #include <limits.h> 3 #include <stdio.h> 4 5 static int inclure(FILE *); 6 static int lire_chemin(FILE *, char *); 7 static int copier_fichier(char *); 8 9 char *nom_prog; 10 11 int 12 main(int argc, char **argv) 13 { 14 (void)argc; 15 16 if ((nom_prog = argv[0]) == NULL || nom_prog[0] == '\0') 17 nom_prog = "assembler"; 18 19 return inclure(stdin); 20 } 21 22 23 static int 24 inclure(FILE *f) 25 { 26 int res; 27 char chemin[PATH_MAX+1]; 28 29 assert(f != NULL); 30 31 printf("(pqt\n"); 32 while ((res = lire_chemin(f, chemin)) > 0) { 33 printf("(fch (@:nom %s)\n", chemin); 34 if (copier_fichier(chemin) < 0) 35 return -1; 36 printf(")\n"); 37 } 38 printf(")\n"); 39 40 return res; 41 } 42 43 static int 44 lire_chemin(FILE *f, char *chemin) 45 { 46 int c; 47 long n; 48 49 assert(f != NULL); 50 assert(chemin != NULL); 51 52 for (n = 0; (c = getc(f)) != '\n' && c != EOF; n++) { 53 if (n == PATH_MAX) { 54 fprintf(stderr, "%s: chemin trop long\n", nom_prog); 55 return -1; 56 } 57 *chemin++ = c; 58 } 59 if (c == EOF) 60 return 0; 61 *chemin = '\0'; 62 63 return 1; 64 } 65 66 static int 67 copier_fichier(char *chemin) { 68 int c; 69 FILE *f; 70 71 if ((f = fopen(chemin, "r")) == NULL) { 72 fprintf(stderr, "%s: impossible d'ouvrir %s\n", 73 nom_prog, chemin); 74 return -1; 75 } 76 77 while ((c = getc(f)) != EOF) 78 putc(c, stdout); 79 80 return 0; 81 }