toybox/toys/posix/iconv.c
<<
>>
Prefs
   1/* iconv.c - Convert character encoding
   2 *
   3 * Copyright 2014 Felix Janda <felix.janda@posteo.de>
   4 *
   5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/iconv.html
   6 *
   7 * Deviations from posix: no idea how to implement -l
   8
   9USE_ICONV(NEWTOY(iconv, "cst:f:", TOYFLAG_USR|TOYFLAG_BIN))
  10
  11config ICONV
  12  bool "iconv"
  13  default y
  14  depends on TOYBOX_ICONV
  15  help
  16    usage: iconv [-f FROM] [-t TO] [FILE...]
  17
  18    Convert character encoding of files.
  19
  20    -c  Omit invalid chars
  21    -f  Convert from (default utf8)
  22    -t  Convert to   (default utf8)
  23*/
  24
  25#define FOR_iconv
  26#include "toys.h"
  27#include <iconv.h>
  28
  29GLOBALS(
  30  char *f, *t;
  31
  32  void *ic;
  33)
  34
  35static void do_iconv(int fd, char *name)
  36{
  37  char *outstart = toybuf+2048;
  38  size_t outlen, inlen = 0;
  39  int readlen = 1;
  40
  41  for (;;) {
  42    char *in = toybuf, *out = outstart;
  43
  44    if (readlen && 0>(readlen = read(fd, in+inlen, 2048-inlen))) {
  45      perror_msg("read '%s'", name);
  46      return;
  47    }
  48    inlen += readlen;
  49    if (!inlen) break;
  50
  51    outlen = 2048;
  52    iconv(TT.ic, &in, &inlen, &out, &outlen);
  53    if (in == toybuf) {
  54      // Skip first byte of illegal sequence to avoid endless loops
  55      if (toys.optflags & FLAG_c) in++;
  56      else *(out++) = *(in++);
  57      inlen--;
  58    }
  59    if (out != outstart) xwrite(1, outstart, out-outstart);
  60    memmove(toybuf, in, inlen);
  61  }
  62}
  63
  64void iconv_main(void)
  65{
  66  if (!TT.t) TT.t = "utf8";
  67  if (!TT.f) TT.f = "utf8";
  68
  69  if ((iconv_t)-1 == (TT.ic = iconv_open(TT.t, TT.f)))
  70    perror_exit("%s/%s", TT.t, TT.f);
  71  loopfiles(toys.optargs, do_iconv);
  72  if (CFG_TOYBOX_FREE) iconv_close(TT.ic);
  73}
  74