Blog Archive
Monday, January 26, 2009
Find Files in Linux
In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.
find / -name 'mypage.htm'
In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.
find -name 'file*'
In the above example the system would search for any file beginning with file in the current directory and any subdirectory.
find -name '*' -size +1000k
In the above example the system would search for any file that is larger then 1000k.
( expression )
True if expression is true.
! expression
Negation of a primary; the unary NOT operator.
expression [-a] expression
Conjunction of primaries; the AND operator is implied by the juxtaposition of two primaries or made explicit by the optional -a operator. The second expression shall not be evaluated if the first expression is false.
expression -o expression
Alternation of primaries; the OR operator. The second expression shall not be evaluated if the first expression is true.
find \( -name 'hello*' -a -name '*.sh' \)
find \( -name '*.sh' -o -name '*.txt' \)
對找到檔案做處理: -exec
有時我們會把找到的特定檔做特別的處理 ,像是刪除和移動就可以用 exec來處理
請看範例。請注意指令的後面要加 \; 來做結束
而此範例是把找到的檔案cp 到user的家目錄下的txt目錄裡面,在這裡的重點是 {}就是找到檔案的代號, 而exec 後面就是要操作的命令
find /tmp/ -type f -name "*.txt" -exec cp {} ~/txt \;
My First Makefile
# Makefile Test
include FileList
CC = gcc
CFG = release
OBJS = $(addsuffix .o, $(basename $(SOURCE)))
EXE = hello
# release
CFLAGS_RELEASE = -O2
DFLAGS_RELEASE = LINUX OS_POSIX
# debug
CFLAGS_DEBUG =
DFLAGS_DEBUG = _DEBUG LINUX OS_POSIX
# switch CFG
ifeq ($(CFG), release)
CFLAGS = $(CFLAGS_RELEASE)
DFLAGS = $(addprefix -D, $(DFLAGS_RELEASE))
else
CFLAGS = $(CFLAGS_DEBUG)
DFLAGS = $(addprefix -D, $(DFLAGS_DEBUG))
endif
# make rules
all: $(EXE)
$(EXE): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) $(DFLAGS) -c $<
clean:
rm -f $(EXE) $(OBJS)
[filelist.sh]
#!/bin/sh
FileList=`find -name '*.c'`
echo "SOURCE =" '\'
for i in $FileList; do
echo $i '\'
done
echo
Tuesday, December 30, 2008
Unicode and RichEditCtrl
Unicode and RichEditCtrl
Unicode Strings in MFC
The easiest way to deal with Unicode strings is to use CStringW class. As it is, CStringW can edit Unicode strings. Another option is to use char* equivalent wide-byte type in MFC called LPWSTR.
Writing and reading Unicode strings to and from controls in MFC is not exactly as straight forward. GetDlgItemText and SetDlgItemText only take single byte character strings. This page explains how to send messages to set and retrieve Unicode strings to the controls without the use of those functions.
Take a look at the following functions. Those functions read and write Unicode strings from and to MFC controls (RichEditCtrl). Sending messages requires several steps. First is to specify the type of the message. The type of the message, the codepage, and other optional settings are set to a structure called GETTEXTEX and SETTEXTEX. Then calculate and prepare space to store the results.
LPWSTR GetUnicodeString(UINT id)
{
CRichEditCtrl* edit=(CRichEditCtrl*)GetDlgItem(id);
int nLength = edit->GetTextLengthEx(GTL_DEFAULT,1200);
LPWSTR lpszWChar = new WCHAR[nLength+1];
GETTEXTEX getTextEx;
getTextEx.cb=(nLength+1)*sizeof(WCHAR);
getTextEx.codepage=1200;
getTextEx.flags=GT_DEFAULT;
getTextEx.lpDefaultChar=NULL;
getTextEx.lpUsedDefChar=NULL;
edit->SendMessage(EM_GETTEXTEX, (WPARAM)&getTextEx, (LPARAM)lpszWChar);
return lpszWChar;
}
void SetUnicodeString(UINT id, LPWSTR str)
{
SETTEXTEX setTextEx;
setTextEx.codepage=1200;
setTextEx.flags=ST_DEFAULT;
CRichEditCtrl *caption=(CRichEditCtrl*)GetDlgItem(id);
if(caption!=NULL)
caption->SendMessage(EM_SETTEXTEX, (WPARAM)&setTextEx, (LPARAM)str);
}
The usage of GetUnicodeString above is shown below. Specify the ID of the RichEditCtrl control.
CStringW str=GetUnicodeString(IDC_RICHEDITCTRL);
Likewise, the usage of SetUnicodeString above is shown below. Specify the ID of the RichEditCtrl control, and the Unicode string to be set to the control.
SetUnicodeString(IDC_RICHEDITCTRL,str);
Friday, December 19, 2008
Visual Studio 2005 SP1
Visual Studio® 2005 Team Suite SP1
Visual C++ 2005 SP1 Redistributable Package (x86)
Visual Studio 2005 Service Pack 1 GDIPLUS.DLL Security Update
Integrate VS 2005 & SP1:
[HowTo]整合Visual Studio 2005與Service Pack 1
Monday, December 15, 2008
C Restrict Pointers
One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself, "doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through a particular pointer. However, it's still possible to change the variable through a different pointer. For example:
void f (const int* pci, int *pi;); // is *pci immutable?
{
(*pi)+=1; // not necessarily: n is incremented by 1
*pi = (*pci) + 2; // n is incremented by 2
}
int n;
f( &n, &n);
In this example, both pci and pi point to the same variable, n. You can't change n's value through pci but you can change it using pi. Therefore, the compiler isn't allowed to optimize memory access for *pci by preloading n's value. In this example, the compiler indeed shouldn't preload n because its value changes three times during the execution of f(). However, there are situations in which a variable is accessed only through a single pointer. For example:
FILE *fopen(const char * filename, const char * mode);
The name of the file and its open mode are accessed through unique pointers in fopen(). Therefore, it's possible to preload the values to which the pointers are bound. Indeed, the C99 standard revised the prototype of the function fopen() to the following:
/* new declaration of fopen() in
FILE *fopen(const char * restrict filename,
const char * restrict mode);
Similar changes were applied to the entire standard C library: printf(), strcpy() and many other functions now take restrict pointers:
int printf(const char * restrict format, ...);
char *strcpy(char * restrict s1, const char * restrict s2);
C++ doesn't support restrict yet. However, since many C++ compilers are also C compilers, it's likely that this feature will be added to most C++ compilers too.
Danny Kalev
Friday, December 05, 2008
WinXP FAT32 與 NTFS 之間的轉換
保留資料
1) > convert D:/FS:NTFS /V
2) Partition Magic
不保留資料
> format D:/FS:NTFS
[NTFS -> FAT32]
保留資料
Partition Magic
不保留資料
> format D:/FS:FAT32
Wednesday, December 03, 2008
POSIX Threads Programming
Books:
- Programming with POSIX Threads
- Multithreading Programming Techniques
- Getting Started With POSIX Threads
Saturday, July 05, 2008
Set Environment Variables for VC
VC6:
$(VS_Path)\VC98\Bin\vcvars32.bat
VC7 (2003) / VC8 (2005) / VC9 (2008):
$(VS_Path)\Common7\Tools\vsvars32.bat
Sunday, March 02, 2008
精通 vi - Chap 3 快速移動位置
往下捲動一個螢幕 ^F
往上捲動一個螢幕 ^B
往下捲動半個螢幕 ^D
往上捲動半個螢幕 ^U
往下捲動一行 ^E
往上捲動一行 ^Y
將本行移到螢幕頂端 z [Enter]
將本行移到螢幕中間 z.
將本行移到螢幕最後 z-
移到螢幕頂端 H
移到螢幕中間 M
移到螢幕最後 L
移到下一行首 + 或 [Enter]
移到上一行首 -
移到句子開頭 (
移到下一句子開頭 )
移到段落開頭 {
移到下一段落開頭 }
到第 n 行 nG
到最後一行 G
2. 搜尋
向下找 /pattern
向上找 ?pattern
同一方向重複找 n
相反方向重複找 N
重複上一搜尋命令,方向相同 ;
重複上一搜尋命令,方向相反 ,
3. 其它
估計目前所在位置的百分比 ^G
精通 vi - Chap 2 簡單的文字編輯
文字物件 更改 刪除 複製
一個單字 cw dw yw
兩個單字,不含標點 2cW 2dW 2yW
往回三個單字 3cb 3db 3yb
一整行 cc dd yy
到一行末 c$ d$ y$
到一行首 c0 d0 y0
單一字元 r x y1
五個字元 5s 5x 5y1
貼上 p 或 P
2. 游標移動
左下上右 h, j, k, l
到下一行首 +
到上一行首 -
到單字結尾 e
往後一個單字 w
往前一個單字 b
到本行末 $
到本行首 0
3. 建立與處理文字
在游標所在位置插入文字 i
在行首插入文字 I
在游標所在位置附加文字 a
在行尾附加文字 A
在游標下一行開啟新行 o
在游標上一行開啟新行 O
刪除一行並代換文字 S
用新文字覆蓋現存的文字 R
合併此行與下一行 J
切換大小寫 ~
重複上一動作 .
還原上一動作 u
還原一整行的編輯 U
精通 vi - Chap 1 vi 文字編輯器
> vi [filename]
儲存
:w
:w [new filename] (另存新檔)
:w! (強迫寫入)
:w! [filename] (強迫寫入或蓋掉另一檔案)
結束並儲存
ZZ
消除編輯結果,回到原檔案
:e!
結束但不儲存
:q!
Thursday, February 21, 2008
INIT: Id "co" respawning too fast: disabled for 5 minutes
ADSL on Linux
2) Setup the rpm of PPPOE if it's necessary.
3) Setup configuration.
> adsl-setup
4) To start ADSL:
> /sbin/ifup ppp0
5) To stop ADSL:
> /sbin/ifdown ppp0
Saturday, August 11, 2007
CVS Server How-To
http://greenisland.csie.nctu.edu.tw/wp/2005/06/16/26/
1. Install CVS
2. vi /etc/services
should have these two lines:
cvspserver 2401/tcp # cvs client/server operations
cvspserver 2401/udp # cvs client/server operations
3. Add group and user
> groupadd cvs
> adduser cvsroot
> passwd cvsroot
4. Change permission of /home/cvsroot
> chown cvsroot.cvs /home/cvsroot
> chmod 755 /home/cvsroot
5. xinetd settings
> cd /etc/xinetd.d
> vi cvspserver
service cvspserver
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/bin/cvs
server_args = --allow-root=/home/cvsroot pserver
log_on_failure += USERID
}
6. Initialize CVS repository
> cvs -d /home/cvsroot init
7. Restart xinetd
> /etc/init.d/xinetd restart
8. That's all. Now we can login to CVS.
> cvs -d :pserver:cvsroot@localhost:/home/cvsroot login
Saturday, May 19, 2007
Tuesday, March 06, 2007
Saturday, January 27, 2007
BASH Quick Guide
A quick guide to writing scripts using the bash shell
A simple shell script
A shell script is little more than a list of commands that are run in sequence. Conventionally, a shellscript should start with a line such as the following:#!/bin/bash
THis indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly. A simple example
Here's a very simple example of a shell script. It just runs a few simple commands#!/bin/bash
echo "hello, $USER. I wish to list some files of yours"
echo "listing files in the current directory, $PWD"
ls # list files
Firstly, notice the comment on line 4. In a bash script, anything following a pound sign # (besides the shell name on the first line) is treated as a comment. ie the shell ignores it. It is there for the benifit of people reading the script.
$USER and $PWD are variables. These are standard variables defined by the bash shell itself, they needn't be defined in the script. Note that the variables are expanded when the variable name is inside double quotes. Expanded is a very appropriate word: the shell basically sees the string $USER and replaces it with the variable's value then executes the command.
We continue the discussion on variables below ...
Variables
Any programming language needs variables. You define a variable as follows:X="hello"
and refer to it as follows: $X
More specifically, $X is used to denote the value of the variable X. Some things to take note of regarding semantics: - bash gets unhappy if you leave a space on either side of the = sign. For example, the following gives an error message:
X = hello
- while I have quotes in my example, they are not always necessary. where you need quotes is when your variable names include spaces. For example,
X=hello world # error
X="hello world" # OK
foo=bar
is considered a command. The problem with foo = bar
is the shell sees the word foo
seperated by spaces and interprets it as a command. Likewise, the problem with the command X=hello world
is that the shell interprets X=hello
as a command, and the word "world" does not make any sense (since the assignment command doesn't take arguments). Single Quotes versus double quotes
Basically, variable names are exapnded within double quotes, but not single quotes. If you do not need to refer to variables, single quotes are good to use as the results are more predictable.
An example
The output looks like this (assuming your username is elflord)#!/bin/bash
echo -n '$USER=' # -n option stops echo from breaking the line
echo "$USER"
echo "\$USER=$USER" # this does the same thing as the first two lines
so the double quotes still have a work around. Double quotes are more flexible, but less predictable. Given the choice between single quotes and double quotes, use single quotes.$USER=elflord
$USER=elflord
Using Quotes to enclose your variables
Sometimes, it is a good idea to protect variable names in double quotes. This is usually the most important if your variables value either (a) contains spaces or (b) is the empty string. An example is as follows:
This script will give the following output:#!/bin/bash
X=""
if [ -n $X ]; then # -n tests to see if the argument is non empty
echo "the variable X is not the empty string"
fi
the variable X is not the empty string
Why ? because the shell expands $X to the empty string. The expression [ -n ] returns true (since it is not provided with an argument). A better script would have been: In this example, the expression expands to [ -n "" ] which returns false, since the string enclosed in inverted commas is clearly empty.#!/bin/bash
X=""
if [ -n "$X" ]; then # -n tests to see if the argument is non empty
echo "the variable X is not the empty string"
fi
Variable Expansion in action
Just to convince you that the shell really does "expand" variables in the sense I mentioned before, here is an example:This looks a little enigmatic. What happens with the last line is that it actually executes the command#!/bin/bash
LS="ls"
LS_FLAGS="-al"
$LS $LS_FLAGS $HOME
ls -al /home/elflord
(assuming that /home/elflord is your home directory). That is, the shell simply replaces the variables with their values, and then executes the command. Using Braces to Protect Your Variables
OK. Here's a potential problem situation. Suppose you want to echo the value of the variable X, followed immediately by the letters "abc". Question: how do you do this ? Let's have a try :THis gives no output. What went wrong ? The answer is that the shell thought that we were asking for the variable Xabc, which is uninitialised. The way to deal with this is to put braces around X to seperate it from the other characters. The following gives the desired result:#!/bin/bash
X=ABC
echo "$Xabc"
#!/bin/bash
X=ABC
echo "${X}abc"
Conditionals, if/then/elif
Sometimes, it's necessary to check for certain conditions. Does a string have 0 length ? does the file "foo" exist, and is it a symbolic link , or a real file ? Firstly, we use the if command to run a test. The syntax is as follows:Sometimes, you may wish to specify an alternate action when the condition fails. Here's how it's done.if condition
then
statement1
statement2
..........
fi
alternatively, it is possible to test for another condition if the first "if" fails. Note that any number of elifs can be added.if condition
then
statement1
statement2
..........
else
statement3
fi
if condition1
then
statement1
statement2
..........
elif condition2
then
statement3
statement4
........
elif condition3
then
statement5
statement6
........
fi
The statements inside the block between if/elif
and the next elif
or fi
are executed if the corresponding condition is true. Actually, any command can go in place of the conditions, and the block will be executed if and only if the command returns an exit status of 0 (in other words, if the command exits "succesfully" ). However, in the course of this document, we will be only interested in using "test" or "[ ]" to evaluate conditions.
The Test Command and Operators
The command used in conditionals nearly all the time is the test command. Test returns true or false (more accurately, exits with 0 or non zero status) depending respectively on whether the test is passed or failed. It works like this:test operand1 operator operand2
for some tests, there need be only one operand (operand2) The test command is typically abbreviated in this form: [ operand1 operator operand2 ]
To bring this discussion back down to earth, we give a few examples: #!/bin/bash
X=3
Y=4
empty_string=""
if [ $X -lt $Y ] # is $X less than $Y ?
then
echo "\$X=${X}, which is greater than \$Y=${Y}"
fi
if [ -n "$empty_string" ]; then
echo "empty string is non_empty"
fi
if [ -e "${HOME}/.fvwmrc" ]; then # test to see if ~/.fvwmrc exists
echo "you have a .fvwmrc file"
if [ -L "${HOME}/.fvwmrc" ]; then # is it a symlink ?
echo "it's a symbolic link
elif [ -f "${HOME}/.fvwmrc" ]; then # is it a regular file ?
echo "it's a regular file"
fi
else
echo "you have no .fvwmrc file"
fi
Some pitfalls to be wary of
The test command needs to be in the form "operand1
gives exactly the "wrong" output (ie it echos "hello", since it sees an operand but no operator.)if [ 1=2 ]; then
echo "hello"
fi
Another potential trap comes from not protecting variables in quotes. We have already given an example as to why you must wrap anything you wish to use for a -n
test with quotes. However, there are a lot of good reasons for using quotes all the time, or almost all of the time. Failing to do this when you have variables expanded inside tests can result in very wierd bugs. Here's an example: For example,
This will give misleading output since the shell expands our expression to#!/bin/bash
X="-n"
Y=""
if [ $X = $Y ] ; then
echo "X=Y"
fi
[ -n = ]
and the string "=" has non zero length. A brief summary of test operators
Here's a quick list of test operators. It's by no means comprehensive, but its likely to be all you'll need to remember (if you need anything else, you can always check the bash manpage ... )operator | produces true if... | number of operands |
-n | operand non zero length | 1 |
-z | operand has zero length | 1 |
-d | there exists a directory whose name is operand | 1 |
-f | there exists a file whose name is operand | 1 |
-eq | the operands are integers and they are equal | 2 |
-neq | the opposite of -eq | 2 |
= | the operands are equal (as strings) | 2 |
!= | opposite of = | 2 |
-lt | operand1 is strictly less than operand2 (both operands should be integers) | 2 |
-gt | operand1 is strictly greater than operand2 (both operands should be integers) | 2 |
-ge | operand1 is greater than or equal to operand2 (both operands should be integers) | 2 |
-le | operand1 is less than or equal to operand2 (both operands should be integers) | 2 |
Loops
Loops are constructions that enable one to reiterate a procedure or perform the same procedure on several different items. There are the following kinds of loops available in bash- for loops
- while loops
For loops
The syntax for the for loops is best demonstrated by example.THe for loop iterates the loop over the space seperated items. Note that if some of the items have embedded spaces, you need to protect them with quotes. Here's an example:#!/bin/bash
for X in red green blue
do
echo $X
done
Can you guess what would happen if we left out the quotes in the for statement ? This indicates that variable names should be protected with quotes unless you are pretty sure that they do not contain any spaces.#!/bin/bash
colour1="red"
colour2="light blue"
colour3="dark green"
for X in "$colour1" $colour2" $colour3"
do
echo $X
done
Globbing in for loops
The shell expands a string containing a * to all filenames that "match". A filename matches if and only if it is identical to the match string after replacing the stars * with arbitrary strings. For example, the character "*" by itself expands to a space seperated list of all files in the working directory (excluding those that start with a dot "." ) So
echo *
lists all the files and directories in the current directory. echo *.jpg
lists all the jpeg files. echo ${HOME}/public_html/*.jpg
lists all jpeg files in your public_html directory. As it happens, this turns out to be very useful for performing operations on the files in a directory, especially used in conjunction with a for loop. For example:
#!/bin/bash
for X in *.html
do
grep -L '' "$X"
done
While Loops
While loops iterate "while" a given condition is true. An example of this:
#!/bin/bash
X=0
while [ $X -le 20 ]
do
echo $X
X=$((X+1))
done
This raises a natural question: why doesn't bash allow the C like for loops
for (X=1,X<10;> As it happens, this is discouraged for a reason: bash is an interpreted language, and a rather slow one for that matter. For this reason, heavy iteration is discouraged.Command Substitution
Command Substitution is a very handy feature of the bash shell. It enables you to take the output of a command and treat it as though it was written on the command line. For example, if you want to set the variable X to the output of a command, the way you do this is via command substitution.
There are two means of command substitution: brace expansion and backtick expansion.
Brace expansion workls as follows:
$(commands)
expands to the output of commands This permits nesting, so commands can include brace expansionsBacktick expansion expands
`commands`
to the output of commandsAn example is given;:
#!/bin/bash
files="$(ls )"
web_files=`ls public_html`
echo $files
echo $web_files
X=`expr 3 \* 2 + 4` # expr evaluate arithmatic expressions. man expr for details.
echo $XNote that even though the output of ls contains newlines, the variables do not. Bash variables can not contain newline characters (which is a pain in the butt. But that's life) Anyway, the advantage of the $() substitution method is almost self evident: it is very easy to nest. It is supported by most of the bourne shell varients (the POSIX shell or better is OK). However, the backtick substitution is slightly more readable, and is supported by even the most basic shells (any #!/bin/sh version is just fine)