Programming/C
新的轉碼方法 , libiconv , BIG5 轉 UTF8
這段 code , 很好用, 就貼在這兒了, 下次可以直接剪下跟貼上
重點有兩個:
1. From encoding 是 CP950
2. libiconv 不要用 libc 的, 因為缺一個 function : iconvctl
int fnConvert(const char *from, const char *to, char* save, int savelen, char *src, int srclen) { iconv_t cd; char *inbuf = src; char *outbuf = save; size_t outbufsize = savelen; int status = 0; size_t savesize = 0; size_t inbufsize = srclen+1; char* inptr = inbuf; size_t insize = inbufsize; char* outptr = outbuf; size_t outsize = outbufsize; if ( ( cd = iconv_open(to, from) ) == (iconv_t)-1 ) { status = -1; goto done; } iconv(cd,NULL,NULL,NULL,NULL); if (inbufsize == 0) { status = -1; goto done; } while (insize > 0) { size_t res = iconv(cd, &inptr,&insize,&outptr,&outsize); if (outptr != outbuf) { int saved_errno = errno; int outsize = outptr - outbuf; strncpy(save+savesize, outbuf, outsize); errno = saved_errno; } if (res == (size_t)(-1)) { if (errno == EILSEQ) { int one = 1; iconvctl(cd,ICONV_SET_DISCARD_ILSEQ,&one); status = -3; } else if (errno == EINVAL) { if (inbufsize == 0) { status = -4; goto done; } else { break; } } else if (errno == E2BIG) { status = -5; goto done; } else { status = -6; goto done; } } } status = strlen(save); done: iconv_close(cd); return status; } EXAMPLE: int fnB2U( unsigned char *str ) { int srclen, destlen, status; unsigned char tmp[2048]; status = -1; if ( ( srclen = strlen( str ) ) == 0 ) return status; destlen = sizeof( unsigned char ) * ( srclen * 2 ) + 1; tmp[0] = ''; if ( ( status = fnConvert ( "CP950", "UTF-8", tmp, destlen, str, srclen) ) > 0 ) strcpy( str, tmp); else printf( "status = %dn"); return( status); }
get my ip address useing c / sample code
C CODE
/* FILENAME : my_local_ip.c */ #include #include #include #include #include #include #include #include int main(int argc, char **argv) { int s; struct sockaddr_in sin; int slen; int port; if( argc<3 ) { printf("Usage: %s \n", argv[0]); printf("\n\tsample %s 203.66.88.89 80\n",argv[0]); return -1; } port = atoi(argv[2]); s = socket(PF_INET, SOCK_STREAM, 0); memset(&sin, 0, sizeof(sin)); sin.sin_family = PF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = inet_addr(argv[1]); if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { printf("127.0.0.1"); return -1; } memset(&sin, 0, sizeof(sin)); slen = sizeof(sin); if(getsockname(s, (struct sockaddr *)&sin, &slen) < 0) { printf("127.0.0.1"); close(s); return -1; } printf("%s", inet_ntoa(sin.sin_addr)); close(s); return 0; }
可以依自己的需求來改這SHELL:
#!/bin/sh ADDR=`my_local_ip 168.95.1.1 53` FILENAME=${ADDR=}".txt" echo $FILENAME
解決 error while loading shared libraries 的方法
執行程式時, 若遇到
./update_item: error while loading shared libraries: libsqlrclientwrapper-0.40.so.1: cannot open shared object file: No such file or directory
像這樣的錯誤訊息 , 表示程式中所需的 so 檔(share library) 沒找到 , 要修改 /etc/ld.so.conf 加上這個目錄 , 改完要下 ldconfig –v 讓路徑生效
可以用 ldd 看該程式所使用到的 shared library 有那些, 及 lib 路徑
—–
以下是 gentoo 的標準解法 , 其他版本的 linux 則不太相同!!
cat /etc/ld.so.conf 這個檔是由 env-update 產生的 , 所以再繼續看一下 /etc/env.d 下 , 究竟有啥
# ld.so.conf autogenerated by env-update; make all changes to # contents of /etc/env.d directory /usr/local/lib /usr/i686-pc-linux-gnu/lib /usr/lib/gcc/i686-pc-linux-gnu/4.1.2 /usr/lib/gcc/i686-pc-linux-gnu/4.1.1 /usr/local/firstworks/lib
參考資料, 這是 compile sqlrelay 列出來的 information
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
———————————————————————-
/bin/sh ../../libtool –mode=finish /usr/local/firstworks/lib
PATH=”$PATH:/sbin” ldconfig -n /usr/local/firstworks/lib
———————————————————————-
Libraries have been installed in:
/usr/local/firstworks/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR’
flag during linking and do at least one of the following:
– add LIBDIR to the `LD_LIBRARY_PATH’ environment variable
during execution
– add LIBDIR to the `LD_RUN_PATH’ environment variable
during linking
– use the `-Wl,–rpath -Wl,LIBDIR’ linker flag
– have your system administrator add LIBDIR to `/etc/ld.so.conf’
在 /etc/env.d 下建立一個 90sqlrelay
內容是
LDPATH="/usr/local/firstworks/lib"
再run : env-update , 就看到 /etc/ld.so.conf 多了正確的 lib path 了
// ———————–
[12/6/13 下午4:37:54] RUTEN AA: 到 /usr/lib 找出原生的 .so 檔,應該會是 libsqlrclientwrapper-0.40.so.xxx.xxx
[12/6/13 下午4:38:13] RUTEN AA: ln -s libsqlrclientwrapper-0.40.so.xxx.xxx 到 loss 的 libsqlrclientwrapper-0.40.so.1.0.0
[12/6/13 下午4:38:17] RUTEN AA: 再 ldconfig
[12/6/13 下午4:40:53] RUTEN AA: 可以用 ldconfig -p 看是不是已經把 libsqlrclientwrapper-0.40.so.1 給 load 進來了