Back to index

im-sdk  12.3.91
csconv.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999 Sun Microsystems, Inc.
00003  * Copyright (c) 1999 Nihon Sun Microsystems K.K.
00004  * All rights reserved.
00005  */
00006 
00007 /*
00008  * "$Id: csconv.c,v 1.1.1.1 2000/10/29 16:50:58 himi Exp $"
00009  */
00010 
00011 #pragma ident "@(#)csconv.c 1.1    99/04/27 SMI"
00012 
00013 
00014 #include <stdio.h>
00015 #include <stddef.h>
00016 #include <stdlib.h>
00017 #include <unistd.h>
00018 #include <locale.h>
00019 #include <fcntl.h>
00020 #include <sys/mman.h>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 
00024 #include "csconv.h"
00025 
00026 
00027 #define DEFAULT_BUF_SIZE (1024 * 1024 * 8)
00028 
00029 
00030 char * ME;
00031 
00032 
00033 void
00034 usage(int exit_value)
00035 {
00036        fprintf(stderr,
00037               "%s [-l locale] -f fromcode -t tocode [-o outfile] [file ...]\n",
00038               ME);
00039 
00040        exit(exit_value);
00041 }
00042 
00043 
00044 int
00045 main(int argc, char ** argv)
00046 {
00047        int           c;
00048        char *        locale;
00049        char *        tocode;
00050        char *        fromcode;
00051        csconv_t      cd;
00052        char *        ip;
00053        size_t        ileft;
00054        char *        op;
00055        size_t        oleft;
00056        char *        inbuf;
00057        size_t        inbytesleft;
00058        char *        outbuf;
00059        size_t        outbytesleft;
00060        int           len;
00061        int           fd;
00062        int           fd_out;
00063        char *        file_out;
00064        struct stat   st;
00065        char *        addr;
00066 
00067        extern char * optarg;
00068        extern int    optind;
00069 
00070 
00071        setlocale(LC_ALL, "");
00072 
00073        locale = NULL;
00074        tocode = NULL;
00075        fromcode = NULL;
00076        file_out = NULL;
00077 
00078        if (0 == argc) {
00079               fprintf(stderr, "0 == argc\n");
00080               exit(1);
00081        }
00082 
00083        if (NULL == (ME = strrchr(*(argv + 0), '/'))) {
00084               ME = *(argv + 0);
00085        }
00086 
00087        while (EOF != (c = getopt(argc, argv, "f:t:l:o:"))) {
00088               switch (c) {
00089               case 'l':
00090                      if (NULL != locale) {
00091                             usage(1);
00092                      }
00093                      locale = optarg;
00094                      break;
00095               case 't':
00096                      if (NULL != tocode) {
00097                             usage(1);
00098                      }
00099                      tocode = optarg;
00100                      break;
00101               case 'f':
00102                      if (NULL != fromcode) {
00103                             usage(1);
00104                      }
00105                      fromcode = optarg;
00106                      break;
00107               case 'o':
00108                      if (NULL != file_out) {
00109                             usage(1);
00110                      }
00111                      file_out = optarg;
00112                      break;
00113               default:
00114                      break;
00115               }
00116        }
00117 
00118        if ((NULL == fromcode) || (NULL == tocode)) {
00119               usage(1);
00120        }
00121 
00122        if (NULL == locale) {
00123               cd = csconv_open(tocode, fromcode);
00124               if ((csconv_t)(-1) == cd) {
00125                      perror("csconv_open");
00126                      exit(1);
00127               }
00128        } else {
00129               cd = csconv_open_locale(locale, tocode, fromcode);
00130               if ((csconv_t)(-1) == cd) {
00131                      perror("csconv_open_locale");
00132                      exit(1);
00133               }
00134        }
00135 
00136        if (NULL == file_out) {
00137               fd_out = 1;
00138        } else {
00139               fd_out = open(file_out, O_CREAT|O_RDWR|O_TRUNC, 0666);
00140               if ((-1) == fd_out) {
00141                      perror("open");
00142                      fprintf(stderr, "%s: cannot optn %s to write\n",
00143                             ME, file_out);
00144                      exit(1);
00145               }
00146        }
00147 
00148        if (optind == argc) {
00149               inbuf = malloc(DEFAULT_BUF_SIZE);
00150               if (NULL == inbuf) {
00151                      perror("malloc");
00152                      exit(1);
00153               }
00154               inbytesleft = DEFAULT_BUF_SIZE;
00155 
00156               outbuf = malloc(DEFAULT_BUF_SIZE);
00157               if (NULL == outbuf) {
00158                      perror("malloc");
00159                      exit(1);
00160               }
00161               outbytesleft = DEFAULT_BUF_SIZE;
00162 
00163               ip = inbuf;
00164               ileft = inbytesleft;
00165               while(0 < (len = read(0, ip, ileft))) {
00166                      ip += len;
00167                      ileft -= len;
00168               }
00169 
00170               inbytesleft = (inbytesleft - ileft);
00171 
00172               ip = inbuf;
00173               ileft = inbytesleft;
00174               op = outbuf;
00175               oleft = outbytesleft;
00176 
00177               csconv(cd, (const char **)(&ip), &ileft, &op, &oleft);
00178 
00179               write(fd_out, outbuf, outbytesleft - oleft);
00180 
00181               free(inbuf);
00182               free(outbuf);
00183        }
00184 
00185        for ( ; optind < argc; optind++) {
00186               fd = open(*(argv + optind), O_RDONLY, 0);
00187               if ((-1) == fd) {
00188                      perror("open");
00189                      fprintf(stderr, "%s: failed to open %s\n",
00190                             ME, *(argv + optind));
00191                      continue;
00192               }
00193               if ((-1) == fstat(fd, &st)) {
00194                      perror("fstat");
00195                      fprintf(stderr, "%s: failed to stat %s\n",
00196                             ME, *(argv + optind));
00197                      close(fd);
00198                      continue;
00199               }
00200               addr = mmap(0, st.st_size,
00201                          PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
00202               if (MAP_FAILED == addr) {
00203                      perror("mmap");
00204                      fprintf(stderr, "%s: failed to mmap %s\n",
00205                             ME, *(argv + optind));
00206                      close(fd);
00207                      continue;
00208               }
00209               close(fd);
00210 
00211               inbuf = addr;
00212               inbytesleft = st.st_size;
00213               if (DEFAULT_BUF_SIZE < (st.st_size * 2)) {
00214                      outbytesleft = (st.st_size * 2);
00215               } else {
00216                      outbytesleft = DEFAULT_BUF_SIZE;
00217               }
00218               if (NULL == (outbuf = malloc(outbytesleft))) {
00219                      perror("malloc");
00220                      exit(1);
00221               }
00222 
00223               ip = NULL;
00224               ileft = 0;
00225               op = NULL;
00226               oleft = 0;
00227 
00228               csconv(cd, (const char **)(&ip), &ileft, &op, &oleft);
00229 
00230               ip = inbuf;
00231               ileft = inbytesleft;
00232               op = outbuf;
00233               oleft = outbytesleft;
00234 
00235               csconv(cd, (const char **)(&ip), &ileft, &op, &oleft);
00236 
00237               munmap(addr, st.st_size);
00238 
00239               write(fd_out, outbuf, outbytesleft - oleft);
00240 
00241               free(outbuf);
00242        }
00243 
00244        csconv_close(cd);
00245 
00246        if(NULL != file_out) {
00247               close(fd_out);
00248        }
00249 
00250        return 0;
00251 }