在 SlideShare 看到一份專門介紹 Makefile 的簡報,寫得非常詳細,在這裡紀錄並分享給大家,有在寫 C/C++ 的朋友們必看阿。搞系統(Linux / FreeBSD)管理的工程師,也是必學工具之一。
Category: C/C++
[網站] 好站連結 (八) Android, javascript, CSS, PHP, Perl, FreeBSD, Linux
PHP
CSS
Html5
- A ROCK-SOLID DEFAULT FOR HTML5 AWESOME( Html5 模板)
- The Official Guide to HTML5 Boilerplate
- Create a Sticky Note Effect in 5 Easy Steps with CSS3 and HTML5
- Learning JavaScript and DOM with Console
- 淺談 JavaScript 編程語言的編碼規範
- Creating photo gallery using jQuery and VisualLightBox
- Importing multiple RSS feeds – using newsWidget (jQuery)
- Evolution of Script Loading
- Global eval. What are the options?
- uintptr_t(3) – Linux man page
- FTBFS (amd64/gcc-4.0): array type has incomplete element type
- 如何在 c 語言中取得 ubuntu 的 ip [論壇 – Ubuntu 程式設計]
- Filesystem Hierarchy Standard
- Kernel 2.6 核心與模組編譯
- 關於延時函數函數INIT_WORK – Linux/Unix社區 / 內核及驅動程序研究區
- Linux 驅動程式的中斷處理, #1: request_irq 基本觀念
- Driver porting: the workqueue interface. [LWN.net]
- Linux中的工作隊列 – Linux – 一蓑煙雨任平生
- disable_irq() disable_irq_nosync() enable_irq()_雅魯藏布江吧_貼吧
- Jollen 的 Android Booting 解析, #1: 整體開機流程
- Jollen 的 Android Booting 解析, #2: 關於開機的評估
- Jollen 的 Android Booting 解析, #3: 製作 Android Bootchart
- Build Android Bootchart
- android.git.kernel.org Git akm8976 Sensor
- Android 產品開發工作談:嵌入式系統技術講求整機開發
- Linux C , 請問以 system() 執行的指令如何取得 pid ???
- 如何簡單使用 ioctl
- 關於xloader和uboot的幾個初級問題
- inet_ntop(), inet_pton()
- inet_ntoa(), inet_aton(), inet_addr
- SIOCGARP to display the entire ARP table.
- 到底ntohl()與htonl()做了什麼?
- 在ubuntu8.10 下建立 ARM-Linux 交叉編譯環境
- TI Evm Board OMAP3 : How to Flash Linux System from U-boot
- 關於linux nand 驅動調試的一些注意點
2011 OSDC Day 1 筆記
Update: 補上 OSDC 紀錄影片 2011.06.26
今年很高興可以北上參加 OSDC 2011 (Open Source Developers Conference),由於之前都在南部唸書及工作,沒有機會北上參加聚會,現在人在新竹,終於有機會可以參加了,雖然早上六點就要起床趕電車了,不過到現場聽課感覺就是不同,也可以認識很多新朋友,底下來紀錄上課筆記
1.微軟與 jQuery 社群的親密接觸
講者: Eric Shangkuan (Microsoft) Slide: 微軟與 jQuery 社群的親密接觸 這是 OSDC 第一場演講,早上九點就開始了,雖然人不多,但是蠻多人還是為了講者而來,首先介紹什麼是 jQuery,以及 jQuery 一些基本用法,像是 CSS selector,如何在 Windows Visual Studio 上面開發 jQuery 及撰寫 plugin 整合進去 ASP.Net,最後介紹三個不錯用的 jQuery Plugin: Templeate, Datalink, Globalzation。- Templeate: 這搭配 Facebook api 可以直接做個人頁面,請參考這裡
- Globalzation: 前端多國語系實做
- Datalink: 可以快速處理 form,利用 object 跟 jQuery 搭配
[Linux] 嵌入式系統不可或缺的工具 – busybox 分析 ifconfig command

fh = fopen_or_warn(_PATH_PROCNET_DEV, "r"); if (!fh) { return if_readconf(); }看一下 /proc/net/dev 內容
Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo: 104 1 0 0 0 0 0 0 104 1 0 0 0 0 0 0 eth0:21798505 51360 0 0 0 0 0 0 7693686 46844 0 0 0 0 0 0Continue reading “[Linux] 嵌入式系統不可或缺的工具 – busybox 分析 ifconfig command”
[C/C++] 判斷檔案是否存在 file_exists
在 PHP 函式裡面,有直接 file_exists 可以使用,相當方便:
在 C 裡面該如何實做?有兩種方式如下:
1. 直接開檔
bool file_exists(const char * filename) { if (FILE * file = fopen(filename, "r")) { fclose(file); return true; } return false; }C++ 寫法
std::fstream foo; foo.open("bar"); if(foo.is_open() == true) std::cout << "Exist"; else std::cout << "Doesn't Exist";[/code]2. 讀取檔案狀態
#includeint file_exists (char * fileName) { struct stat buf; int i = stat ( fileName, &buf ); /* File found */ if ( i == 0 ) { return 1; } return 0; }
[C/C++] 將字串轉成 16 進位
最近在碰嵌入式系統遇到一個還蠻常見的問題,我要將16進位的字串(例如 AAC2) test 轉成16進位的 unsigned int,讓我可以進行 & | not 一些二進位運算,底下是轉換程式,大家參考看看
int power(int x,int n)
{
int i;
int num = 1;
for(i=1;i<=n;i++)
num*=x;
return num;
}
int transfer_string_to_hex(unsigned char *str_name)
{
char string[]="0123456789ABCDEF";
int number[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i = 0;
int j = 0;
int str_number = 0;
for(i=0; ipow 這個函式可以使用,所以自己寫了 power 來取代,我用在偵測網路線是否有插上:
int detect_wan_wire()
{
FILE *fp = NULL;
unsigned char *pch;
unsigned char buf[128] = {0};
unsigned int mask = 0x0004;
int retry = 3;
int res = 0;
int i = 0;
unsigned int a;
unsigned int b;
sprintf(buf, "mii_mgr -g -p 7 -r 1 > %s", WAN_FILE);
system(buf);
char * pEnd;
long int li1, li2, li3, li4;
for (i=0; iAND, OR, XOR, NOT 與16 進制
[C/C++] cstring (string.h) 函式:strcat, strncat, strcmp, strncmp
串接函式 strcat
strcat 此函式用來連接兩字串合併成單一字串,直接看底下範例:
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
output:
these strings are concatenated.
看一下 strcat 原始碼:
char *
strcat(char * __restrict s, const char * __restrict append)
{
char *save = s;
for (; *s; ++s);
while ((*s++ = *append++));
return(save);
}
設定指標 save 成 source,再將 s 指標指向最後,接下來根據 append 字串一個一個往後串接,直到碰到 \0 終止 while 迴圈,最後在將指標 *save 回傳即可。
比較函式 strcmp
strcmp 用來比較兩字串是否相同,相同回傳 0,不相同則回傳兩字串 ASCII 相減的值。底下範例:
/* strcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}
來看看 strcmp 原始碼
#include <string.h>
/*
* Compare strings.
*/
int
strcmp(s1, s2)
const char *s1, *s2;
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}
大家可以看到,比對字串會依序比對,直到 s1 最後字元 \0 則會回傳 0 代表比對成功,如果中途有字元比對不同,則會將兩個字元相減回傳。
串接函式 strncat
strncat 用來串接指定多少字元,底下範例:
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
printf("%s\n", str1);
return 0;
}
strncat 原始碼
#include <string.h>
/*
* Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes
* are written at dst (at most n+1 bytes being appended). Return dst.
*/
char *
strncat(char * __restrict dst, const char * __restrict src, size_t n)
{
if (n != 0) {
char *d = dst;
const char *s = src;
while (*d != 0)
d++;
do {
if ((*d = *s++) == 0)
break;
d++;
} while (--n != 0);
*d = 0;
}
return (dst);
}
一樣是先將 dst 指標指向最後一個字元+1,再根據需要串接的大小來決定 dst 最後的指標。
比較函式 strncmp
直接看範例,比較字串前兩個字元是否相同,如果相同則印出
/* strncmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
puts ("Looking for R2 astromech droids...");
for (n=0 ; n<3 ; n++)
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
return 0;
}
strncmp 原始碼
#include <string.h>
int
strncmp(s1, s2, n)
const char *s1, *s2;
size_t n;
{
if (n == 0)
return (0);
do {
if (*s1 != *s2++)
return (*(const unsigned char *)s1 -
*(const unsigned char *)(s2 - 1));
if (*s1++ == 0)
break;
} while (--n != 0);
return (0);
}
最後參數傳入 0 則會直接回傳 0,依序比對,直到 n =0 的時候跳出比較迴圈,然後回傳 0,代表比對成功。
[C/C++] cstring (string.h) 搜尋函式:strstr, strchr
strstr
這次介紹 C 語言常用 string 函式:strstr,主要是針對兩個輸入參數做比對,Parameters 1 是輸入字串,Parameters 2 是找尋字串,strstr 會先將頭一次比對成功的 pointer 回傳,也就是如果要找尋 appleboyappleboy 字串中的 boy,函式會回傳第一次比對成功的 boy pointer,而並非回傳最後一個比對到的,底下是一個參考範例:/* strstr example */ #include看一下 Kernel 原始檔案,strstr 函式:#include int main () { char str[] ="This is a simple string"; char * pch; /* 找尋 simple 字串 */ pch = strstr (str,"simple"); /* 將 simple 換成 sample */ strncpy (pch,"sample",6); puts (str); return 0; }
/* * Find the first occurrence of find in s. */ char * strstr(s, find) const char *s, *find; { char c, sc; size_t len; if ((c = *find++) != 0) { len = strlen(find); do { do { if ((sc = *s++) == 0) return (NULL); } while (sc != c); } while (strncmp(s, find, len) != 0); s--; } return ((char *)s); }先將 sc 指定為內容字串,c 指定為找尋字串,利用兩個迴圈開始一一筆對,利用 strncmp 比對字串,在將比對成功的 pointer 回傳,但是回傳之前需要在將 s– 回到前一個指標,這樣就ok了。
strchr
這字串用來找尋第一次比對成功單一字母符號,也是一樣回傳該指標,底下是範例:/* strchr example */ #include上面範例很容易,那底下來看看完整 strchr 程式碼:#include int main () { char str[] = "This is a sample string"; char * pch; printf ("Looking for the 's' character in \"%s\"...\n",str); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d\n",pch-str+1); pch=strchr(pch+1,'s'); } return 0; }
char * strchr (const char *p, int ch) { char c; c = ch; for (;; ++p) { if (*p == c) return ((char *)p); if (*p == '\0') return (NULL); } /* NOTREACHED */ }就只能針對單一字母符號做搜尋,跟 strstr 不同的是 strstr 是針對多個字母符號搜尋,很好區別吧 ^^。