2012年3月27日 星期二

Javascript 只能輸入數值語法

<script>
function isNumberPressed(){
  var isValidKey = false;
  var intCode = window.event.keyCode;
  if (intCode >= 48 && intCode <= 57) isValidKey = true; //0-9
  if (intCode == 13) isValidKey = true; //13 enter
  if (intCode == 32) isValidKey = true; //32 spacebar
  window.event.returnValue = isValidKey;
}
</script>


<input type=text name=txt1 id=txt1 onkeypress="javascript:isNumberPressed();">

javascript select new Option 用法

var myselect=document.getElementById("channels");
myselect.options[myselect.length] = new Option("123", "1", "", (channel==36)?1:0);


new Option("標題","value",沒用""即可,selected)

2012年3月25日 星期日

document.form1.xxx1-n多物件呼叫

for(var i=0;i<=10;i++)
{
    var tmp=eval("document.form1.xxx"+i)
    tmp.disabled=flase;
}

2012年3月16日 星期五

svn 程式管理工具

apt-get install subversion


mkdir -p /home/svn/kin1
mkdir -p /home/svn/kin2


svnadmin create /home/svn/kin1
svnadmin create /home/svn/kin2 
mkdir /home/svn/conf 


cp /home/svn/kin1/conf/{authz,passwd} /home/svn/conf 
ll /home/svn/conf 
rm -rf /home/svn/kin{1,2}/conf/{authz,passwd}


vi /home/svn/kin1/conf/svnserve.conf  


[general] 
anon-access = none 
auth-access = write 
password-db = ../../conf/passwd 
authz-db = ../../conf/authz 
realm = kin1 #驗證實會顯示出此訊息



vi /home/svn/kin2/conf/svnserve.conf  


[general] 
anon-access = none 
auth-access = write 
password-db = ../../conf/passwd 
authz-db = ../../conf/authz 
realm = kin2 #驗證實會顯示出此訊息


vi /home/svn/conf/passwd 


[users] 
kin1 = 123456 
kin2 = 123456 
kin3 = 123456 


vi /home/svn/conf/authz  


[groups] 
adm = kin1 
user = kin2,kin3 
[kin1:/] 
@adm = rw 
kin2 =  rw
kin3 = r
[project2:/] 
@adm = rw

kin2 =  r
kin3 = rw


killall svnserve 
svnserve -d -r /home/svn


svn import kin1(目錄) svn://127.0.0.1/kin1/ -m "Version1" --username=kin1 --password=123456 #Import 程式碼目錄到kin1底下


svn co svn://127.0.0.1/kin1/ --username=kin1 --password=123456

2012年3月12日 星期一

linux iostat

這功能可以找出磁碟和CPU的使用狀況,安裝方式待下次測試.

2012年3月11日 星期日

rtorront 安裝過程

須先安裝兩各套件
    libtorrent rtorrent
安裝過程中也需要一些輔助軟體
    automake autoconf devscripts libtool libcppunit-dev
安裝步驟
    # wget http://xxxxxxxxxxxxxxxxx/libtorrent-x.xx.x.tar.gz
    # wget http://xxxxxxxxxxxxxxxxxxx/rtorrent-x.x.x.tar.gz
    # tar -zxvf libtorrent-x.x.x.tar.gz
    # tar -zxvf rtorrent-x.x.x.tar.gz
    # cd libtorrent-x.x.x
    # ./autogen.sh
    # ./configure
    # make
    # make install
    # cd ../rtorrent-x.x.x
    # ./autogen.sh
    # ./configure --with-xmlrpc-c
    # make
    # make install



安裝過程中需要先安裝XMLRPC-C


安裝完rtorrent並執行後若出現rtorrent: error while loading shared libraries: libtorrent.so.13: cannot open shared object file: No such file or directory訊息,可以試試看下面的處理方式:
    就是先修改/etc/ld.so.conf,增加include /usr/local/lib 这一行。再运行ldconfig即可.


安裝完後需在家目錄(cd ~ 即可)新增.rtorrent.rc內容如下:



min_peers = 20
max_peers = 100
min_peers_seed = 5
max_peers_seed = 10
max_uploads = 10
download_rate = 0
upload_rate = 20
directory = /home/kin/filedownload/downloading
session = /home/kin/filedownload/session
port_range = 6881-6881
port_random = no
check_hash = yes
use_udp_trackers = yes
encryption = allow_incoming,enable_retry,prefer_plaintext
peer_exchange = yes
system.method.set_key = event.download.finished,move_complete,"execute=mv,-u,$d.get_base_path=,/home/kin/filedownload/downloads/;d.set_directory=/home/kin/filedownload/downloads/"
schedule = watch_directory,10,10,load_start=/home/kin/filedownload/torrent/*.torrent
schedule = tied_directory,10,10,start_tied=
schedule = untied_directory,10,10,close_untied=
system.method.set_key = event.download.inserted,delete_torrents,"d.delete_tied="
schedule = throttle_1,01:00:00,24:00:00,download_rate=50
schedule = throttle_2,15:00:00,24:00:00,upload_rate=20
encoding_list = UTF-8
dht = auto
dht_port = 6881
execute_nothrow=rm,/tmp/bt_scgi.socket
schedule = chmod,0,0,"execute=chmod,777,/tmp/bt_scgi.socket"
scgi_local = /tmp/bt_scgi.socket
# Enable the default ratio group.
ratio.enable=
# Change the limits, the defaults should be sufficient.
ratio.min.set=200
ratio.max.set=500
ratio.upload.set=5120M
# Changing the command triggered when the ratio is reached.
system.method.set = group.seeding.ratio.command, d.close=, d.erase=
network.send_buffer.size.set = 4M
network.receive_buffer.size.set = 16M

2012年3月9日 星期五

Jquery下拉伸縮選單

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery Menu</title>
<script type="text/javascript" src="http://www.webdm.cn/themes/script/jquery.js"></script>
<script type="text/javascript">


$(document).ready(function(){
   $("#wenzhang>dd>dl>dd").hide();
   $("#wenzhang>dd>dl>dt").removeClass("open");
   $.each($("#wenzhang>dd>dl>dt"), function(){
   $(this).click(function(){
   $("#wenzhang>dd>dl>dd").not($(this).next()).slideUp();
   $(this).next().slideToggle(300);
   });
   //$(this).toggle(function(){$(this).addClass("open");},function(){$(this).removeClass("open");});
   });
});
</script>
<style type="text/css">
dl,dd,dt,ul,li{ margin:0; padding:0; list-style:none;}
#wenzhang{ width:200px; text-align:center; font-size:12px;border-left:1px solid #dedede;border-right:1px solid #dedede; border-bottom:1px solid #dedede;}
#wenzhang  dd dl dt{ border-top:1px solid #dedede; background:#f2f2f2; height:25px; line-height:25px; font-weight:bold;}
#wenzhang ul li{border-top:1px solid #efefef; line-height:25px; background:#f9f9f9;}
body {
margin-left: 0px;
margin-top: 0px;
}
</style>
</head>
<body>
<dl id="wenzhang">
<dd><dl><dt>AAAAAAAA</dt></dl></dd>
<dd>
    <dl>
        <dt >1111111111</dt>
            <dd>
              <ul>
                <li>1111111111</li>
                <li>1111111111</li>
                <li>1111111111</li>
                <li>1111111111</li>
                <li>1111111111</li>
                <li>1111111111</li>
              </ul>
            </dd>
</dl>
</dd>
    
    <dd>
    <dl>
        <dt>2222222222</dt>
            <dd>
              <ul>
                <li>2222222222</li>
                <li>2222222222</li>
                <li>2222222222</li>
                <li>2222222222</li>
              </ul>
            </dd>
</dl>
    </dd>
    
    <dd>
        <dl>
        <dt>3333333333</dt>
          <dd>
              <ul>
                <li>3333333333</li>
              </ul>
            </dd>
        </dl>
   </dd>
</dl>
</body>
</html> 

2012年3月8日 星期四

在Linux kernel module 建立proc

轉自:http://kaijie-chang.blogspot.com/2010/12/linux-kernel-module-proc.html

在Linux kernel module 建立proc

建立 Proc的方法很簡單,以下是個建立,讀寫的範例
//include kernel module一些基本的header
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>

//建立proc_dir struct
static struct proc_dir_entry *dir;
static struct proc_dir_entry *item;
//先implement 對proc讀寫的function
int item_read(char *page, char **start, off_t off, int count, int *eof, void *data )
{
    return sprintf(page,"hello");
}
ssize_t itemt_write( struct file *filp, const char __user *buff, unsigned long len, void *data )
{
char *buffer;
copy_from_user(buffer,buff,len);
printk(KERN_INFO"%s",buffer);
return len;
}
//將讀寫的function註冊到proc_dir struct
int itemt_init(struct proc_dir_entry *entry)
{
entry->read_proc = item_read;
entry->write_proc = item_write;
entry->owner = THIS_MODULE;
return 0;
}
//將proc註冊到系統上
int proc_init(void)
{
entry_dir = proc_mkdir("dir", &proc_root);//把proc目錄建立到/proc 根目錄下
entry_item = create_proc_entry ("item", 0644, entry_dir);//再把item的proc建立在dir下
itemt_init(entry_item);//把proc callback initial好
}
void proc_exit(void)
{
remove_proc_entry("item", entry_item);
remove_proc_entry("dir", &proc_root);

判斷檔案是否存在

使用函數 
    int access(const char *filename, int amode);
apmode為0時是檢查檔案的存在性


access可以檢查檔案其他屬性:
06 檢查讀寫權限
04 檢查讀權限
02 檢查寫權限
01 檢查執行權限
00 檢查檔案存在


#include<stdio.h>
main()
{
    printf("access=%d\n",access("/tmp/file",0));
    //if 0 檔案存在
    //if -1檔案不存在
} 

pthread使用說明

轉自:http://www.cnblogs.com/xianghang123/archive/2011/08/11/2134927.html


linux下C/C++,多線程pthread 


 ·線程創建 


  函數原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg); 


  返回值:若是成功建立線程返回0,否則返回錯誤的編號。 


  形式參數:pthread_t *restrict tidp要創建的線程的線程id指針;const pthread_attr_t *restrict attr創建線程時的線程屬性;void* (start_rtn)(void)返回值是void類型的指針函數;void *restrict arg start_rtn的形參。 


  ·線程掛起:該函數的作用使得當前線程掛起,等待另一個線程返回才繼續執行。也就是說當程序運行到這個地方時,程序會先停止,然後等線程id為thread的這個線程返回,然後程序才會斷續執行。 


  函數原型:int pthread_join( pthread_t thread, void **value_ptr); 


  參數說明如下:thread等待退出線程的線程號;value_ptr退出線程的返回值。 


  ·線程退出 


  函數原型:void pthread_exit(void *rval_ptr); 


  ·獲取當前線程id 


  函數原型:pthread_t pthread_self(void); 


  ·互斥鎖 


  創建pthread_mutex_init;銷毀pthread_mutex_destroy;加鎖pthread_mutex_lock;解鎖pthread_mutex_unlock。 


  ·條件鎖 


  創建pthread_cond_init;銷毀pthread_cond_destroy;觸發pthread_cond_signal;廣播pthread_cond_broadcast S;等待pthread_cond_wait。  


  ·正確處理Linux平台下的線程結束問題 


  在Linux平台下,當處理線程結束時需要注意的一個問題就是如何讓一個線程善始善終,讓其所占資源得到正確釋放。在Linux平台默認情況下,雖然各個線程之間是相互獨立的,一個線程的終止不會去通知或影響其他的線程。但是已經終止的線程的資源並不會隨著線程的終止而得到釋放,我們需要調用pthread_join() 來獲得另一個線程的終止狀態並且釋放該線程所占的資源。








【用C】


/* example.c*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++){
sleep(1);
printf("This is a pthread.\n");}
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++){
printf("This is the main process.\n");
sleep(1);
}
pthread_join(id,NULL);
return (0);
}





編譯命令 gcc -o example example.c -lpthread


注:【1】pthread 庫不是 Linux 系統默認的庫,連接時需要使用靜態庫 libpthread.a,所以在使用pthread_create()創建線程,以及調用 pthread_atfork()函數建立fork處理程序時,需要鏈接該庫。


      【2】還要添加頭文件pthread.h


      【3】因為C庫自帶sleep,所以可以不添加unistd.h頭文件





【用C++】


/* example.cpp*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *thread(void *ptr)
{
int i;
for(i=0;i<3;i++){
sleep(1);
printf("This is a pthread.\n");}
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++){
printf("This is the main process.\n");
sleep(1);
}
pthread_join(id,NULL);
return (0);
}





編譯鏈接命令 g++ -o example2 example.c -lpthread





【注】 標亮部分,注意C++開發時與C的區別,解釋如下:


我們注意在Posix定義建立線程函數的原型:
extern int pthread_create (pthread_t *__restrict __threadp,
                           __const pthread_attr_t *__restrict __attr,
                           void *(*__start_routine) (void *),
                           void *__restrict __arg) __THROW;
這個call中的第三個參數是載入一個函數,這個函數有一個參數可以傳入,返回一個 通用指針。
我們再來看看原來函數中是怎樣調用這一原型的,基本上就是類似一下的調用方式:
(void *) thread 或是 (void *) &thread


這個表達式的含義:取一個指向函數main_thread的指針,然後將其轉換為一個通用指針。
這就是說顯然上述兩個事情並非同一個事情,故而正確的調用方式是
ret=pthread_create(&id,NULL,thread,NULL);
處理函數的定義如下:
void *thread(void *ptr)
值得注意的是在gcc編譯時不會出錯,但是用g++就會有問題:invalid conversion from `void*' to `void*(*)(void*),究其原因就是C語言編譯器允許隱含性的將一個通用指針轉換為任意類型的指針,而C++不允許。

2012年3月2日 星期五

輸出u32/u64格式

d, lx, ld, lu, 這幾各都是輸出32位元
hd, hx, hu, 這幾各都是輸出16位元
hhd, hhx, hhu, 這幾各都是輸出8位元
lld, ll, llu, llx, 這幾各都是輸出65位元

printf( "%llu ",.....)
%llu   是64位元無符號
%llx   是64位元16進制

2012年3月1日 星期四

Get DHCP Assignment Default Gateway

Shell Code:
    route -n | grep 'UG[ \t]' | awk '{print $2}'


Shell Code:
    route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]\+[ \t]\+0\.0\.0\.0[ \t]\+[^ \t]*G[^ \t]*[ \t]' | awk '{print $2}'

Fedora無法開機救援

開機進入GRUB啟動後按 "e",接著在移動到kernel選項在按"e"
進入編輯模式,並輸入"single",跳出後按"b"重新啟動即可進入
編輯模式.

2012年2月29日 星期三

基金配息換算

(金額/淨值)/美金=(單位數*配息)*美金=實際月配息

2012年2月22日 星期三

Javascript控制iframe的url適用IE,google,FF

<script>
var iframe;
function set_status() {
iframe = document.getElementById("iframe");
iframe.onload = iframe.onreadystatechange = iframeload; 
iframe.src = "http://tw.yahoo.com";
}


function iframeload() {
if (!iframe.readyState || iframe.readyState == "complete") {
alert("Local iframe is now loaded.");
}
}
</script>
<iframe id="iframe" name="iframe">

2012年2月20日 星期一

編譯簡單的Kernel modules

轉自於 http://telesky.pixnet.net/blog/post/6969608-cross-compile-the-linux-device-driver-example%3A-hello-module

1. mkdir hello_module   建立一個資料夾來開發
2. 在 /hello_module 內建立 hello.c 與 Makefile
    hello.c :
                       #include <linux/init.h>
                       #include <linux/module.h>
                       MODULE_LICENSE("Dual BSD/GPL");
                       static int hello_init(void)
                       {
                            printk(KERN_INFO "Hello, world\n");
                            return 0;
                        }

                       static void hello_exit(void)
                       {
                            printk(KERN_INFO "Goodbye, cruel world\n");
                        }

                        module_init(hello_init);
                        module_exit(hello_exit);

    Makefile :
                      KERNELDIR := /home/xxx/android/kernel/             
                      PWD :=$(shell pwd)
                      ARCH=arm                    
                      CROSS_COMPILE=/home/xxx/android/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-  
                      CC=$(CROSS_COMPILE)gcc
                      LD=$(CROSS_COMPILE)ld

                      obj-m := hello.o

                      modules:
                              $(MAKE) -C $(KERNELDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules  
                       clean:
                               rm *.o *.ko *.mod.c *.markers *.order *.symvers
                      #============================================================
                      KERNELDIR: 指名你的kernel source, 通常會在 /usr/src/ 裡面找到,
                                          輸入uname –r 也可以看到你目前的 kernel 版本
                     -C 後面代表你的kernel source 目錄所在地
                      M= 後面代表你的 module(hello.c) 目錄所在地,
                           $(shell pwd) 就是目前所在目錄
                      $(MAKE) 跟  rm 的前面似乎不能用空白鍵, 改用<TAB>吧!

3. Prepare your kernel
     我就是這一關卡很久, 如果你的kernel沒有先設置好的話,
     會有一堆問題而且很難根據 Error Message 去 debug
     首先, 進入到你的 kernel source 目錄下 (我的是 /home/xxx/android/kernel )
     # vim Makefile
     找到 ARCH 跟CROSS_COMPILE 的位置
     把原本的
                  ARCH                    ?= $(SUBARCH)
                  CROSS_COMPILE  ?=
     改成
                  ARCH           := arm
                  CROSS_COMPILE  := /home/xxx/android/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-
     然後輸入 make mrproper 先清掉一些垃圾
     輸入 make oldconfig , 會出現一堆東西給你選, 都按 Enter 應付它就對了
     輸入 make modules_prepare , 這一步是關鍵, 如果沒有 Error,
     就可以到 step 4 準備領回你的 hello.ko 囉!
4. make your module
    進入 /hello_module , 輸入 make
    若沒有Error , 應該會看到以下畫面, 也會看到一堆東西生出來了, 當然也有最重要的 hello.ko !!
    make -C /home/xxx/android/kernel/ ARCH=arm CROSS_COMPILE=/home/xxx/android/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi- M=/home/xxx/android/hello_module modules
    make[1]: Entering directory `/home/xxx/android/kernel'

          WARNING: Symbol version dump /home/xxx/android/kernel/Module.symvers
           is missing; modules will have no dependencies and modversions.

    CC [M]  /home/xxx/android/hello_module/hello.o
    Building modules, stage 2.
    MODPOST 1 modules
    CC      /home/xxx/android/hello_module/hello.mod.o
    LD [M]  /home/xxx/android/hello_module/hello.ko
    make[1]: Leaving directory `/home/xxx/android/kernel'
5. insmod/rmmod (掛載/卸載)
    insmod hello.ko 或 rmmod hello.ko   來看你編寫的 driver 給你什麼訊息
    通常按下去什麼都沒看到, 要到/var/log/messages 或輸入 dmesg 去看

     May 11 10:32:49 localhost kernel: Hello, world
     May 11 10:37:25 localhost kernel: Goodbye, cruel world
     (ps: 不過因為我操作的工作站環境的 kernel 不是 android 的,
            且預設架構是 x86 不是 ARM,
            所以在工作站上會出現
            insmod: error inserting 'hello.ko': -1 Invalid module format 的訊息
            把 hello.ko 放到我的 android 環境中的 /sdcard 後 ,
            輸入 insmod/rmmod 就可以看到訊息囉!
            # insmod hello.ko
               Hello, world
            # rmmod hello.ko
               Goodbye, cruel world
             )