2008年12月9日星期二

Snippet : string number increment(done with macro)

#include
#include
#include


#define strincr(s,l) ({int i = l - 1; for(;i>=0 && (s)[i] == '9';(s)[i--] = '0'); (i>= 0) && (++(s)[i]); s;})

char a[4] = "999";

int main(int argv, char **args){

char *s = strincr(a, strlen(a));

strincr(s, strlen(s));

printf("%s", s);
return 0;
}


Just for fun, that might not be faster than by the regular way of using 'atoi'

2008年7月21日星期一

To Change USB Disk Label in Linux

RenameUSBDrive - Community Ubuntu Documentation
There are 6 programs used to label a partition - the program used depends on the partition's filesystem type:

*

For FAT16 and FAT32 partitions, use mtools.
*

For NTFS partitions, use ntfsprogs.
*

For ext2 or ext3 partitions, use e2label.
*

For JFS partitions, use jfs_tune.
*

For ReiserFS (v3) partitions, use reiserfstune.
*

For XFS partitions, use xfs_admin

2008年3月8日星期六

performance camparison between php4 and php5

zz from sourceLib

The hardware used was a 2 CPU server, where each CPU was a Intel Xeon 2.8Ghz processor.

The machine contained 2Gig of RAM(DDR2 PC3200 ECC REG 400). The hard drives on the machine were 2-40Gig SATA.

The server was running Red Hat Enterprise Linux version 3.2 with the kernel version 2.4.21-27 and the x86_64 architecture.

The version of Apache running on the machine was 1.3.33.

Each version of PHP was built with the following configuration settings:

With mysql
With ftp enabled
With pcntl (process control) enabled
With memory limit enabled
With cgi disabled

The default php.ini was used as well.


Test PHP4 PHP5 Net
test_arithmetic 0.065 0.063 3.54%
test_array_operators 0.059 0.058 1.85%
test_bitwise 0.029 0.025 13.19%
test_casting 0.186 0.176 5.63%
test_chr_fixed 0.342 0.336 1.87%
test_chr_hardcoded 0.191 0.181 4.83%
test_chr_var 0.364 0.358 1.68%
test_comment_loop 0.079 0.070 11.50%
test_compare 0.054 0.049 9.98%
test_compare_false 0.008 0.008 0.00%
test_compare_invert 0.005 0.004 8.89%
test_compare_strict 0.014 0.012 18.88%
test_compare_unstrict 0.017 0.014 18.13%
test_constants 0.039 0.039 -2.34%
test_crc32 0.016 0.017 -4.43%
test_do_while 0.390 0.353 9.54%
test_do_while_break 0.056 0.048 14.70%
test_empty 0.004 0.004 13.95%
test_empty_loop 0.078 0.070 10.65%
test_ereg 1.000 1.017 -1.63%
test_foreach 1.240 1.120 9.68%
test_get_class 0.017 0.017 1.15%
test_global_scalar_assign 0.085 0.071 16.17%
test_global_string_assign 0.140 0.129 7.85%
test_if_constant 0.010 0.010 0.00%
test_increment 0.043 0.041 5.07%
test_is_array 0.009 0.008 5.88%
test_is_object 0.008 0.008 2.38%
test_is_type 0.029 0.027 8.16%
test_isset 0.005 0.004 18.37%
test_line 0.009 0.008 2.35%
test_local_array_assign 0.187 0.176 5.73%
test_local_boolean_assign 0.074 0.073 0.41%
test_local_float_assign 0.048 0.044 8.88%
test_local_hash_assign 0.040 0.038 5.28%
test_local_integer_assign 0.048 0.044 7.92%
test_local_object_assign 0.028 0.023 20.49%
test_local_scalar_assign 0.086 0.074 13.08%
test_local_string_assign 0.123 0.114 7.25%
test_md5 0.078 0.077 1.79%
test_microtime 0.075 0.087 -15.65%
test_mt_rand 0.011 0.011 0.00%
test_ord 1.447 1.416 2.17%
test_ordered_functions 0.629 0.602 4.18%
test_ordered_functions_references 0.603 0.576 4.56%
test_preg_match 0.050 0.065 -29.37%
test_rand 0.011 0.011 -0.94%
test_references 0.017 0.015 12.21%
test_sha1 0.094 0.099 -5.98%
test_string_append 0.091 0.083 8.88%
test_strlen 0.009 0.009 -2.22%
test_switch 0.190 0.191 -0.37%
test_time 0.023 0.023 -1.31%
test_unordered_functions 0.715 0.663 7.35%
test_variable_variables 0.038 0.034 10.90%
test_while 0.423 0.358 15.49%

LAMP starts

First it is ftp. That is always the way one deploy a site to server.

Download vsftp, ./configure and then make install.
Or install with yum or atp.
Edit /etc/vsftpd.conf(or maybe /etc/vsftpd/vsftpd.conf).
To enable anonymous access, add the 2 following line:

anon_root=/data
anon_upload_enable=YES

To add another user,

todo here...

Then we go with mysql 5.0(5.0.51a.)

Get mysql noninstall version, unpack it into /usr/local.
make a symbol link as /usr/local/mysql to /usr/local/mysql.real.path.
and execute the following command to create users and start mysql daemon:

shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /PATH/TO/MYSQL-VERSION-OS.tar.gz | tar xvf - shell> ln -s FULL-PATH-TO-MYSQL-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &

copy one of the support-files/my-*.cnf to /etc/my.cnf as the default mysql config.

To stop mysql : mysqladmin shutdown

If needed, use mysqladmin -u root -p shutdown


After mysql started, we need to add some account to it.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;

mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;

mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';

mysql> GRANT USAGE ON *.* TO 'dummy'@'localhost';

'all privileges' means this user can do anything.
*.* means all tables
'monty' is the user name, 'localhost' is the ip address from where this user can login.
'some_pass' is password.
'grant option' means this user can do grant operation.

To use old passward format for old mysql client :

mysql> SET PASSWORD FOR
-> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');

Alternative :

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
-> WHERE Host = 'some_host' AND User = 'some_user';
mysql> FLUSH PRIVILEGES; 
Or:
Start mysqld with the --old-passwords option.

Then we go with php5 (mysql 4.1 or later requires php 5).

download php source &

./configure --with-mysql & make install.



2008年2月28日星期四

Where's it in iphone

in iphone 1.1.3:
addressbook : /var/mobile/Library/AddressBook

2008年2月27日星期三

core dump

gdb exec_name core.pid

2008年2月24日星期日

dynamic lib in linux

/etc/ld.so.conf
dynamic libs config file


command : ldconfig
Each time after changing that file, run 'ldconfig' to flush config data.


command : ldd
Check which lib has been linked by an executable.
example : ldd a.out


libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00986000)
libc.so.6 => /lib/tls/libc.so.6 (0x0081c000)
/lib/ld-linux.so.2 (0x007fe000)


command : objdump
Print out many useful information about an executable file.
example : objdump -p a.out


a.out:     文件格式 elf32-i386

程序头:
    PHDR off    0x00000034 vaddr 0x08048034 paddr 0x08048034 align 2**2
         filesz 0x000000e0 memsz 0x000000e0 flags r-x
  INTERP off    0x00000114 vaddr 0x08048114 paddr 0x08048114 align 2**0
         filesz 0x00000013 memsz 0x00000013 flags r--
    LOAD off    0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12
    ....
动态节:
  NEEDED      libpthread.so.0
  NEEDED      libc.so.6
    ....

版本引用:
  required from libpthread.so.0:
    0x0d696910 0x00 04 GLIBC_2.0
    0x0d696911 0x00 03 GLIBC_2.1
  required from libc.so.6:
    0x0d696910 0x00 02 GLIBC_2.0

2008年2月21日星期四

the way ie and firefox rendering html

the html codes "<br \/>" will result different in IE(6) and firefox(2.0.0.12 currently)

IE treats \ as back-splash .. but firefox thinks '\' is a regular character.

The DOM tree generated by IE is what we expect. firefox generates automatically an end-tag for '<br \>' : the </br \>

So. you CAN'T just fetch innerHTML from node with such html codes in it and expect getting the same html string in both IE and firefox.

2008年2月18日星期一

continuation in javascript

What's continuation?
continuation这个词1直不知道怎么翻译最好。大概就是连续的,持续的意思。在编程的领域中,是指那些以同步的方式编写异步的程序。本来应该中断当前执行序列等待某些事件(如用户输入等)发生的程序,可能要为每次异步调用的返回设置1个相应结果的回调方法。而在continuation方式的编程中可以将这些异步处理完全屏蔽在底层。1次异步调用就如同调用1个普通函数1样,整个流程完全可以写在1个函数中(当然最好不要)。

Sample
举个例子,假设现在有1个登录流程需要实现:首先渲染登录界面当用户输入并提交时,调用另外1段程序,检查用户输入,处理登录的结果。代码可能会是这样:

function startLogin(){
renderLogin();
}

function checkLogin(String username, String pwd){
if (isOk(username, pwd)){
renderHome();
} else {
renderError();
}
}
然后在适当的时候checkLogin将被调用,处理login的检查和转向。如果用continuation的方式写,代码就会明了的多了:
//rewrite in continuation way
function login(){
renderLogin();
loginData = getLogin();
if (isOk(loginData.username, loginData.pwd)){
renderHome();
} else {
renderError();
}
}
当程序执行到getLogin方法时,由于这是1个长时间的过程,程序将被continuation的实现框架中断,等待输入。
当输入返回时,再从中断处继续执行,将结果返回给loginData。在整个执行过程中程序员看到的只是1段连续执行的代码,所有需要异步的操作,由continuation的实现框架去处理。

Why
上面的例子很简单,只是展示continuation风格的编码方式,对于复杂的项目,其中1个流程中可能涉及到数10个表单,对几10种不同的输入进行处理。如果用异步的方式编写代码,越来越多的函数(或类)散步在项目的目录树中用于处理各种可能输入的,代码的检查和修改也就越来越困难。

continuation就是用来解决这些问题的,当项目中需要很多异步调用完成时,使用continuation的方式编写代码,逻辑和流程会变得非常清晰。

Where
continuation可以用在任何需要解决异步调用问题的场合,在server端和client端都会用到。
  • javaFlow是1个控制java代码执行流程的包,可以将代码执行的某个位置和上下文记录下来,中断当前运行,执行其他操作,当条件适合的时候,再回复到中断的位置继续执行。
  • JAUJava实现的continuation
在server端continuation经常用来等待用户输入。在client端,continuation用来等待服务器端响应。
ajax就像它的名字,通过javascript实现的异步调用。RIA盛行,client端的异步调用也多起来了,多到受不了之前,可以试试continuation的方式来写RIA。

Narrative
Narrative对javascript的持续异步执行的实现,因为javascript实际上是个脚本语言,没有java那么强大,narrative实现异步的方式实际上是重写了所有需要异步执行的代码(几乎就是所有的function了),narrative中用到了1个mozilla下的javascript解析器的项目。
重写之后的javascript代码,为函数中每个必要的步骤设置1个标志(1个数字),以便中断后从中断点继续执行。
重写后,javascript函数中所有的局部变量会被替换成某个和函数相关的对象的属性,也是为了中断后不丢失程序的上下文。
重写后的函数可能在调用异步过程的时候被中断,中断时函数会真正的退出,当异步调用返回结果(或达到其他某个条件时),函数重新被调用,但是会直接跳到上次中断处,这是通过重写时设置的数字标识实现的。函数中用到的局部变量,也从会某个对象中重新取得。

为了支持异步调用,narrative的源代码(重写前的代码)中对需要异步执行的函数调用提供了1个新的语法:
someFunc->(arg1, arg2...)
表示这个函数要以异步的方式执行,其中可能会中断。这也是narrative中的javascript代码唯一与普通javascript不同的地方。

begin sed at a sight

Sed is an Editor..
The normal way to use 'sed' is:
sed 'reg_pattern/commands' filename

sed processes each line in a file as a unit.
sed finds out each line matched the given regular expression, & deal with it by these 'commands'. commands could be a single command such as D, or sequence of commands quoted by '{}'.

the most used format of sed : "sed 's/pattern/replace/' file" is an abbreviation, 's' is a command. searching pattern reg is absent, so that 's' command applies to each line.
the full format is : sed '/.*/{s/pattern/replace/g}' file

there are some usually used examples:

# join all lines
sed ':x;/./{N;s/[\n\r]//g;bx}'

#remove single-line comment
sed 's/\/\*.*\*\///g'

#remove multi-line comment
sed '/^[ \t]*\/\*/, /.*\*\//d'

#remove dos-line-breaker in unix file
sed 's/\cm//g'