2013年12月25日 星期三

printf 印出 IP address

最近 trace code(u-boot) 時發現,printf 現在可以印出 IP 及 MAC 之類的 address 型態。
%pI4:印出IPv4位址,變數型態為 unsigned long 的指標。
%pI6:印出IPv6位址,變數型態為 unsigned ling 的指標。
%pM:印出MAC位址,變數型態為 unsigned char 的指標。

這樣它就可以直接把 IP, MAC address 印出來。

EX:
typedef ulong IPaddr_t;
IPaddr_t NetOurSubnetMask;
printf("NetOurSubnetMask : %pI4\n", &NetOurSubnetMask);

unsigned char env_enetaddr[6];
printf("Address in environment is  %pM\n", env_enetaddr);

而變數裡面的值,IP的部份就是16進制的值直接連在一起。
例如:
192.168.10.100 => C0A80A64
192 = C0
168 = A8
 10 = 0A
100 = 64

MAC 的部份也是一樣是16進制的值,所以用 %s、%c會印不出來。
反而直接用%x一個一個去印是印得出來的。


這功能原本可能來自新版的 Linux Kernel 的 printk
參考:
https://www.kernel.org/doc/Documentation/printk-formats.txt

2013年12月20日 星期五

Linux 提示字元

前幾天失心瘋,玩起了 Git ,在網路上亂找一些關於 Git 的資料,像是一些參數設定,GUI工具之類的。因為現在的專案管理用的是 Git ,但它強大的功能也帶來了複雜的操作,雖然不見得什麼都用得到,但有得用,不學一學,用來耍酷一下就覺得很難過。

結果其中找了一項是,在 .bash_profile 裡加入:
PS1="[\[\033[1;32m\]\w\[\033[0m\]]\[\033[1;36m\]\$(git_branch)\[\033[0;33m\]\$(git_since_last_commit)\[\033[0m\]$ "

2013年12月17日 星期二

GUI Git Client

git 雖然功能強大,不過也因為功能太強大,branch 很容易會開很多。當 branch 一多,要使用 command line 來看各 branch 之類的差異、每個 branch 的進度之類的,就不是那麼的方便了。所以開始尋找有 GUI 的 git client。

2013年11月13日 星期三

vim "取代" 指令

以前在學校,提到 VI / VIM 時,老師總會說它是個強大的編輯器。其實當時只覺得它很難用(那時用的版本還是用H、J、K、L在移動游標),不知道它強大在哪?現在跟視窗化的編輯器比起來,我想是沒有強大到哪去啦,除了它可以不需要滑鼠。不過就 command line 下,它的確是具備了不少後來視窗界面下才被覺得習以為常的功能。

2013年11月7日 星期四

C 語言預設變數 備忘

這也是一個常常會混亂的東西@@,記憶力越來越差,不知道跟什麼東西都能 Google 有關?還是年紀有關?

GCC 中有事先定義了一些變數,讓我們可用,其中 Debug 時常用的就是 __FILE__,__LINE__跟我常常會弄混的 __func__。別人都是大寫,就它是小寫。其實本來是 __FUNCTION__,但後來 GNU 已經不建議使用(原因不明),所以改成了 __func__。

結果我就會搞不清到底是:
__func__,__function__,__FUNC__ 還是 __FUNCTION__。

實際上查了一下,除了這三個還有其它的,只是比較不常用。

1.  __BASE_FILE__
完整的原始檔案路徑

2.  __cplusplus
表示該檔案由 g++ 所編譯,當成 C++ 的檔案

3.  __DATE__
編譯的日期

4.  __TIME__
編譯的時間

5.  __FILE__  
原始檔名

6. __LINE__
所在行數

7. __VERSION__  
gcc 版本

8. __func__
為了避免混淆,我就不提另一個了

2013年11月4日 星期一

diff 參數備忘

diff 是我每次用都要查的指令@@,所以寫下來備忘一下。

diff -Naur path1 path2 > project.patch
-N    In  directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
-a    Treat  all  files as text and compare them line-by-line, even if they do not seem to be text.
-u    Use the unified output format.
-r    When comparing directories, recursively compare any subdirectories found.


如果要使用這個 patch 檔

patch -p# < project.patch
PS: "#" 表示要省略的路徑層數

man 的原文說明
-pnum or --strip=num
  Strip the smallest prefix containing num leading slashes from each file name found in the patch file.
  A sequence of one or more adjacent slashes is counted as a single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out the patch. For example, supposing the file name in the patch file was

/u/howard/src/blurfl/blurfl.c

setting -p0 gives the entire file name unmodified, -p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash, -p4 gives

blurfl/blurfl.c

and not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d option.