2013年2月7日 星期四

為你的Terminal Bash Prompt加上顏色

為了在落落長的Terminal畫面中快速看到前先下指令點,在Prompt上加上顏色是個不錯的方法,在.bashrc加入以下指令即可達到這樣的效果
export PS1='\[\033[01;35m\][\A]\[\033[00;32m\][\h \[\033[32m\]\W]\[\033[m\]\$ '

//PS:原先使用這種方式會有游標無法對齊的情況,將replace "\e[" to "\[\033[" 即可解決。
export PS1='\e[\[0;32m[\A][\h \W]$ \e[m'

Reference:how-do-i-fix-my-colour-bash-prompt-wrapping
Reference:http://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html

2012年2月9日 星期四

命名習慣

一般變數 全部小寫 名詞
struct 每個單字頭大寫
define,enum 全大寫
函式 第一個單字小寫 動詞
全域 開頭加上g_
const 開頭加上k_

gcc編譯選項

gcc編譯選項

-c 只做預處理(preprocessor)、組譯(Assembler)、還有編譯(Compiler),不做連結
-S 只做組譯(Assembler) 
-e 只做預處理(preprocessor) 
-o file 輸出名稱 
-v 編譯時顯示工具的詳細資訊 
-Wall 警告訊息 
-g debug用(gdb)

DropBear 建立設用者

Linux建立使用者
passwd: unknown uid 0 

echo "root:x:0:0:root:/root:/bin/sh" > /etc/passwd 
echo "root:::0:::::" > /etc/shadow 
echo "root:x:0:" > /etc/group 

echo "root:x:0:0:root:/root:/bin/sh" > passwd 
echo "root:::0:::::" > shadow 
echo "root:x:0:" > group 

第一次使用passwd改密碼 
adduser 

建立/etc/dropbear/ 
再來執行兩個指令 
/etc/dropbear/dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key 
/etc/dropbear/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key 

./dropbear

2011年12月16日 星期五

Linux指令筆記

samba mount
mount -t cifs //ip/path /localpath -o username=user,password=pass

2011年12月2日 星期五

[轉貼] mkimage使用詳解

uboot源代碼的tools/目錄下有mkimage工具,這個工具可以用來製作不壓縮或者壓縮的多種可啟動映像檔案。

mkimage在製作映像檔案的時候,是在原來的可執行映像檔案的前面加上一個0x40位元組的頭,記錄參數所指定的訊息,這樣uboot才能識別這個映像是針對哪個CPU體系架構的,哪個OS的,哪種類型,加載內存中的哪個位置,入口點在內存的那個位置以及映像名是什麼

root@Glym:/tftpboot# ./mkimage
Usage: ./mkimage -l image
-l ==> list image header information
./mkimage -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
-A ==> set architecture to 'arch'
-O ==> set operating system to 'os'
-T ==> set image type to 'type'
-C ==> set compression type 'comp'
-a ==> set load address to 'addr' (hex)
-e ==> set entry point to 'ep' (hex)
-n ==> set image name to 'name'
-d ==> use image data from 'datafile'
-x ==> set XIP (execute in place)
參數說明︰

-A 指定CPU的體系架構︰

取值 表示的體系架構
alpha Alpha 
arm A RM 
x86 Intel x86
ia64 IA64
mips MIPS
mips64 MIPS 64 Bit
ppc PowerPC
s390 IBM S390
sh SuperH
sparc SPARC
sparc64 SPARC 64 Bit
m68k MC68000

-O 指定作業系統類型,可以取以下值︰
openbsd、netbsd、freebsd、4_4bsd、linux、svr4、esix、solaris、irix、sco、dell、ncr、lynxos、vxworks、psos、qnx、u-boot、rtems、artos

-T 指定映像類型,可以取以下值︰
standalone、kernel、ramdisk、multi、firmware、script、filesystem

-C 指定映像壓縮模式,可以取以下值︰
none 不壓縮
gzip 用gzip的壓縮模式
bzip2 用bzip2的壓縮模式

-a 指定映像在內存中的加載位址,映像下載到內存中時,要按照用mkimage製作映像時,這個參數所指定的位址值來下載

-e 指定映像營運的入口點位址,這個位址就是-a參數指定的值加上0x40(因為前面有個mkimage添加的0x40個位元組的頭)

-n 指定映像名

-d 指定製作映像的源檔案
Reference from http://www.wretch.cc/blog/poemsoul/15441093

2011年11月14日 星期一

const的意義(const pointer, pointer to const, const pointer to a const, const function...)

#include
#include
using namespace std;
class A
{
private:
     int y;
public:
 A(){
  y=0;
 }
 int f1(int x) const // 此const代表此function不可以修改Data Member
 {
  x++;
  // y++; // Error! y為Data Member,因此不可以修改
  return y;
 }
 const int f2(int x) // 回傳值為const
 {
  x++;
  y++;
  return y;
 }
 int f3(int const x) // 參數為const
 {
  // x++; // Error! x為const,不可以修改
  y++;
  return y;
 }
 void f4(int const *x) // x is variable pointer to a constant integer,也可以寫成f4(const int *x)
 {
  // (*x)++; // Error! pointer x所指向的實體為const,因此不可以修改其值
  *x++; // 此行的意思是*(x++),先修改pointer x的值,在指向實體
 }
 void f5(int const &x) // x is variable reference to a constant integer
 {
  // x++; // Error! x就是一個const
 }
 void f6(int *const x) // x constant pointer to a variable integer
 {
  (*x)++;
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
 const char * f7() // 回傳一個variable pointer to a constant char
 {
  return "ABC";
 }
 void f8(const int * const x) // x is constant pointer to a const integer
 {
  // (*x)++; // Error! x所指向的實體為const,因此不可以修改其值
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
};
int main(){
 A a;
 int x=3;
 a.f1(x);
 a.f2(x);
 a.f3(x); // int x 會自動轉成const int x傳入
 a.f4(&x) ;
 a.f5(x) ;
 const char *s = a.f7(); // 必須使用一個const來接const
 // s[0]='0'; // Error! 無法對const 變數進行改變動作
 return 0;
}

Reference: http://csie-tw.blogspot.com/2010/03/c-constconst-pointer-pointer-to-const.html