OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.1 KiB

  1. /*
  2. * --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. *
  5. * Filename: package/.../musl/iconv.c
  6. * Copyright (C) 2020 The OpenSDE Project
  7. *
  8. * More information can be found in the files COPYING and README.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License. A copy of the
  13. * GNU General Public License can be found in the file COPYING.
  14. * --- SDE-COPYRIGHT-NOTE-END ---
  15. */
  16. /*
  17. * iconv.c
  18. * Implementation of SUSv4 XCU iconv utility
  19. * Copyright © 2011 Rich Felker
  20. * Licensed under the terms of the GNU General Public License, v2 or later
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <iconv.h>
  25. #include <locale.h>
  26. #include <langinfo.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. int main(int argc, char **argv)
  31. {
  32. const char *from=0, *to=0;
  33. int b;
  34. iconv_t cd;
  35. char buf[BUFSIZ];
  36. char outbuf[BUFSIZ*4];
  37. char *in, *out;
  38. size_t inb;
  39. size_t l;
  40. size_t unitsize=0;
  41. int err=0;
  42. FILE *f;
  43. while ((b = getopt(argc, argv, "f:t:csl")) != EOF) switch(b) {
  44. case 'l':
  45. puts("UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF32-LE, UCS-2BE, UCS-2LE, WCHAR_T,\n"
  46. "US_ASCII, ISO8859-1, ISO8859-2, ISO8859-3, ISO8859-4, ISO8859-5,\n"
  47. "ISO8859-6, ISO8859-7, ...");
  48. exit(0);
  49. case 'c': case 's': break;
  50. case 'f': from=optarg; break;
  51. case 't': to=optarg; break;
  52. default: exit(1);
  53. }
  54. if (!from || !to) {
  55. setlocale(LC_CTYPE, "");
  56. if (!to) to = nl_langinfo(CODESET);
  57. if (!from) from = nl_langinfo(CODESET);
  58. }
  59. cd = iconv_open(to, from);
  60. if (cd == (iconv_t)-1) {
  61. if (iconv_open(to, "WCHAR_T") == (iconv_t)-1)
  62. fprintf(stderr, "iconv: destination charset %s: ", to);
  63. else
  64. fprintf(stderr, "iconv: source charset %s: ", from);
  65. perror("");
  66. exit(1);
  67. }
  68. if (optind == argc) argv[argc++] = "-";
  69. for (; optind < argc; optind++) {
  70. if (argv[optind][0]=='-' && !argv[optind][1]) {
  71. f = stdin;
  72. argv[optind] = "(stdin)";
  73. } else if (!(f = fopen(argv[optind], "rb"))) {
  74. fprintf(stderr, "iconv: %s: ", argv[optind]);
  75. perror("");
  76. err = 1;
  77. continue;
  78. }
  79. inb = 0;
  80. for (;;) {
  81. in = buf;
  82. out = outbuf;
  83. l = fread(buf+inb, 1, sizeof(buf)-inb, f);
  84. inb += l;
  85. if (!inb) break;
  86. if (iconv(cd, &in, &inb, &out, (size_t [1]){sizeof outbuf})==-1
  87. && errno == EILSEQ) {
  88. if (!unitsize) {
  89. wchar_t wc='0';
  90. char dummy[4], *dummyp=dummy;
  91. iconv_t cd2 = iconv_open(from, "WCHAR_T");
  92. if (cd == (iconv_t)-1) {
  93. unitsize = 1;
  94. } else {
  95. iconv(cd2,
  96. (char *[1]){(char *)&wc},
  97. (size_t[1]){1},
  98. &dummyp, (size_t[1]){4});
  99. unitsize = dummyp-dummy;
  100. if (!unitsize) unitsize=1;
  101. }
  102. }
  103. inb-=unitsize;
  104. in+=unitsize;
  105. }
  106. if (inb && !l && errno==EINVAL) break;
  107. if (out>outbuf && !fwrite(outbuf, out-outbuf, 1, stdout)) {
  108. perror("iconv: write error");
  109. exit(1);
  110. }
  111. if (inb) memmove(buf, in, inb);
  112. }
  113. if (ferror(f)) {
  114. fprintf(stderr, "iconv: %s: ", argv[optind]);
  115. perror("");
  116. err = 1;
  117. }
  118. }
  119. return err;
  120. }