lmmtolso.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. int main(void)
  8. {
  9. FILE *lmm; /* mmap-able file */
  10. FILE *lso; /* static object */
  11. struct stat fd_stat;
  12. int c;
  13. size_t i;
  14. if (!(lmm = fopen("locale.mmap", "r"))) {
  15. printf("cannot open locale.mmap!\n");
  16. return EXIT_FAILURE;
  17. }
  18. if (fstat(fileno(lmm), &fd_stat)) {
  19. printf("cannot stat locale.mmap!\n");
  20. fclose(lmm);
  21. return EXIT_FAILURE;
  22. }
  23. if (!(lso = fopen("locale_data.c", "w"))) {
  24. printf("cannot open locale_data.c!\n");
  25. fclose(lmm);
  26. return EXIT_FAILURE;
  27. }
  28. fprintf(lso,
  29. "#include <stddef.h>\n"
  30. "#include <stdint.h>\n"
  31. "#include \"lt_defines.h\"\n"
  32. "#include \"locale_mmap.h\"\n\n"
  33. "typedef union {\n"
  34. "\tunsigned char buf[%zu];\n"
  35. "\t__locale_mmap_t lmm;\n"
  36. "} locale_union_t;\n\n"
  37. "static const locale_union_t locale_union = { {",
  38. (size_t) fd_stat.st_size
  39. );
  40. i = 0;
  41. while ((c = getc(lmm)) != EOF) {
  42. if (!(i & 0x7)) {
  43. fprintf(lso, "\n\t");
  44. }
  45. fprintf(lso, "%#04x, ", c);
  46. ++i;
  47. }
  48. fprintf(lso,
  49. "\n} };\n\n"
  50. "const __locale_mmap_t *__locale_mmap = &locale_union.lmm;\n\n"
  51. );
  52. if (ferror(lmm)) {
  53. printf("error reading!\n");
  54. return EXIT_FAILURE;
  55. }
  56. if (ferror(lso) || fclose(lso)) {
  57. printf("error writing!\n");
  58. return EXIT_FAILURE;
  59. }
  60. fclose(lmm);
  61. return EXIT_SUCCESS;
  62. }