Friday, January 19, 2007

Wretch Album Fetcher

期末考唸書唸累了,於是寫了這個抓無名小站相簿的 bash script。只要把相簿第一頁網址輸入就可以自動把整個相簿抓下來。雖然我是沒有抓相簿的習慣,練習一下 scripting 也不錯,還有 wget 的用法。還有請不要把 SLEEP 設太小,以免增加主機負荷。

#!/bin/sh
# Usage:
#   wretch-get 'http://www.wre....'
# Please don't set too short sleeping interval.

UserAgent="\"\""
URL="http://www.wretch.cc/album/"
ID="DisplayImage"
WGET="wget -U $UserAgent -q"
SLEEP="sleep 1"

tmp="/tmp/wretch-get.$$"
str=""
next=""
pic=""
ref=""
count=0

$WGET -U $UserAgent $@ -O $tmp
str=`grep $ID $tmp`
next=`echo $str | sed -e "s/.*href='\([a-zA-Z0-9\.\/\?=&:]*\)'.*/\1/"`
pic=`echo $str | sed -e "s/.*src='\([a-zA-Z0-9\.\/\?&=:]*\)'.*/\1/"`
ref="$@"

while [ -n "$str" ]; do
    $WGET --referer $ref $pic
    # settings for the next iteration
    ref="$URL$next"
    $WGET $URL$next -O $tmp
    str=`grep $ID $tmp`
    next=`echo $str |\
        sed -e "s/.*href='\([a-zA-Z0-9\.\/\?=&:]*\)'.*/\1/"`
    pic=`echo $str |\
        sed -e "s/.*src='\([a-zA-Z0-9\.\/\?&=:]*\)'.*/\1/"`

    # counting and sleep
    count=`expr $count + 1`
    $SLEEP
done

rm $tmp

echo "$count pics fetched. Some pics may have two sizes."

Saturday, January 13, 2007

簡單的 bash build script

這個 script 用 test 比對檔案的版本,如果有更新就重新編譯。為什麼不用 Makefile?因為我比較習慣 bash……有時間會認真學 make 的語法啦……

#!/bin/sh
# A simple building script

# target file names separated with " "
targets="foo bar"

for i in $targets; do
# EDIT the matching conditions and prefix/suffix
    if [ $i.c -nt $i ]; then
# EDIT following commands while matching
        echo "[C] $i"
        gcc -g $i.c NanoTime.o -o $i -lpthread
    fi
done

Thursday, January 11, 2007

Pthreads Semaphore

senaphore.h 提供的 semaphore 實作。

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

void *runner( void* );
void critical_sleep( int );

sem_t mutex;

int main(){
 pthread_t t1, t2;
 int r1, r2;

 sem_init( &mutex, 0, 1 );

 r1 = 1;
 r2 = 2;
 pthread_create( &t1, NULL, runner, &r1 );
 pthread_create( &t2, NULL, runner, &r2 );
 pthread_join( t1, NULL );
 pthread_join( t2, NULL );
 return 0;
}

void * runner( void * p ){
 int *id = (int*) p;
 printf( "thread %d: started\n", *id );
 critical_sleep( *id );
 printf( "thread %d: terminated\n", *id );
}

void critical_sleep( int id ){
 printf( "thread %d: entering critical_sleep()\n", id );
 sem_wait( &mutex );
 printf( "thread %d: entered critical_sleep()\n", id );
 printf( "thread %d: 1 sec sleeping\n", id );
 sleep( 1 );
 printf( "thread %d: leaving critical_sleep()\n", id );
 sem_post( &mutex );
 printf( "thread %d: left critical_sleep()\n", id );
}

參考資料

nanosleep in POSIX

nanosleep 的用法,兩個參數都是 timespec:第一個是暫停的時間,第二個是暫停期間被中斷時用來記錄剩下的時間。

#include <time.h>
...
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000;
nanosleep( &ts, NULL );
...

Thursday, January 04, 2007

Firefox 魅力不再?

這篇翻譯的原文在 Mozilla Links 上,我半夜不睡覺發神經翻的,有很多地方搞錯了, Bob 幫我修正以後發表在 Mozilla Links 正體中文版上,非常感謝他。下面是我原本的翻譯,我只能說相形見絀。

這是電腦世界雜誌的 Preston Gralla 最近在他的部落格上,對於近來 Firefox 的電視廣告提出的質疑。我在此發表對於這篇文章的評論。

「開放原始碼軟體難道就只是大手筆的電視廣告嗎?」

不是,不過讓更多人知道你的產品是。在電腦世界雜誌工作的您,對此應該更清楚。

「做為開放原始碼軟體, Firefox 靠的是優秀的人才,無私貢獻他們的時間和心血。如果有多餘的錢可以亂花,或許可以付給這些貢獻者補償他們花的時間。他們用這些錢可以做出的貢獻比電視多。」

我不認為有多的錢,而且廣告不會白費。難道 VeriSign 放在你文章前面的橫幅廣告也是因為他們錢太多嗎?這叫作投資:更多使用者帶來更多內建的搜尋插件,更多的收入,就有更多程式設計師帶來更好的產品。

Mozilla 的任務是「為網際網路捍衛選擇和創新」,所以必需讓好的產品為人所知。

所以你的觀點如果能明確提供到底投資多少,會更有說服力。當然這有可能太多,而且也有些爭議。不然這就像所有橫幅廣告和廣告信一樣,每天都有。這些錢當然花在疫苗或振災上會更好,希望你能明白。

當然我同意這些電視廣告還不夠酷不夠有魅力,相較於口耳相傳的方式。

各位看倌您怎麼想呢?