summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2020-05-13 13:01:48 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2020-05-13 13:01:48 +0300
commit5345c4c9f6311fa5dc27565a3b26bc5f91d92d7f (patch)
tree33f8494dc28c9e117cd40a0c1b7b133835450dfe /main.c
parent76076f53d567580240037137ba734c56a38d8698 (diff)
fixed few possible bugsiconv_file_converter
Diffstat (limited to 'main.c')
-rw-r--r--main.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..b56e82b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,126 @@
+// Copyright © 2010-2012 sss
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#include <unistd.h>
+#include <stdio.h>
+#include <iconv.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+
+int main(int argc, char **argv)
+{
+ int opt = -1;
+ char ifile[256], ofile[256], fcharset[32], tcharset[32];
+ memset(ifile, 0, sizeof(ifile));
+ memset(ofile, 0, sizeof(ofile));
+ memset(fcharset, 0, sizeof(fcharset));
+ memset(tcharset, 0, sizeof(tcharset));
+
+ ifile[0] = 0;
+ ofile[0] = 0;
+ fcharset[0] = 0;
+ tcharset[0] = 0;
+ while((opt = getopt(argc, argv, "i:o:f:t:")) != -1)
+ {
+ switch(opt)
+ {
+ case 'i':
+ strncpy(ifile, optarg, sizeof(ifile) - 1);
+ break;
+ case 'o':
+ strncpy(ofile, optarg, sizeof(ofile) - 1);
+ break;
+ case 'f':
+ strncpy(fcharset, optarg, sizeof(fcharset) - 1);
+ break;
+ case 't':
+ strncpy(tcharset, optarg, sizeof(tcharset) - 1);
+ break;
+ default:
+ printf("usage: %s -ioft\n\t-i input file\n\t-o output file\n\t-f from charset\n\t-t to charset\n", argv[0]);
+ }
+ }
+ iconv_t id = iconv_open(tcharset, fcharset);
+ if((long)id == -1)
+ {
+ printf("failed to create iconv descriptor with error: %s\n", strerror(errno));
+ return 1;
+ }
+ FILE *in = fopen(ifile, "r");
+ if(!in)
+ {
+ printf("failed to open input file with error: %s\n", strerror(errno));
+ return 1;
+ }
+ FILE *out = fopen(ofile, "w");
+ if(!out)
+ {
+ printf("failed to open input file with error: %s\n", strerror(errno));
+ return 1;
+ }
+ while(!feof(in))
+ {
+ char *buf, *outbuf, *op, *ip;
+
+ buf = (char*)malloc(4096);
+ if (!buf)
+ {
+ printf("failed to allocate memory for \"buf\"\n");
+ return 1;
+ }
+ outbuf = (char*)malloc(4096);
+ if (!outbuf)
+ {
+ printf("failed to allocate memory for \"outbuf\"");
+ free(buf);
+ return 1;
+ }
+ op = outbuf;
+ ip = buf;
+ if(!fgets(buf, 4096, in))
+ {
+ if (buf)
+ free(buf);
+ if (outbuf)
+ free(outbuf);
+ break;
+ }
+ size_t len = strlen(buf), outlen = 4096;
+ int enc_len = iconv(id, &ip, &len, &op, &outlen);
+ if(enc_len == -1)
+ {
+ printf("failed to convert buffer with error: %s\n", strerror(errno));
+ free(buf);
+ free(outbuf);
+ return 1;
+ }
+ enc_len = 4096 - outlen;
+ if((int)fwrite(outbuf, 1, enc_len, out) != enc_len)
+ {
+ printf("failed to write data to file with error: %s\n", strerror(errno));
+ free(buf);
+ free(outbuf);
+ return 1;
+ }
+ free(buf);
+ free(outbuf);
+ }
+ return 0;
+}
+