2011年12月31日 星期六
[心得] 電子發票的碎碎念: 紙本發票x電子發票x載具
2011年12月19日 星期一
[C/C++] 檢查資料夾是否存在?
檢查指定是否存在, 若不存在則建立資料夾並新增一個檔案於該目錄中.
使用API名稱: stat, mkdir
Reference:
1. stat wiki
2. stat的作用
#include <stdio.h>
#include <sys/stat.h> //include for stat
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
struct stat buf;
do
{
if(stat("data/",&buf) == 0)
{
printf("data/ folder found \n");
}
else
{
printf("can not find data/ folder \n");
if(mkdir("data/", S_IRWXU) == 0)
{
printf("create data/ folder sucess \n");
}
else
{
printf("create data/ folder fail \n");
}
break;
}
}while(0);
FILE *fd = fopen("data/data.txt", "w");
if(fd > 0)
{
fprintf(fd, "%s \n", "Data in files");
fclose(fd);
}
else
{
printf("data/data.txt open fail \n");
}
return 0;
}
2011年12月10日 星期六
[Linux] route & /proc/net/route
在Linux環境下, 通常可用 route 指令查詢目前的routing資訊.
jason@jason-laptop:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 * 255.255.255.0 U 2 0 0 wlan0
10.110.215.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
default 10.110.215.254
jason@jason-laptop:~$
假若route程式未被編入"bin"目錄, 亦可直接自/proc/net/route取得相關資訊
jason@jason-laptop:~$ cat /proc/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
wlan0
eth0 00D76E
eth0 0000FEA9 00000000 0001 0 0 1000 0000FFFF 00 0
eth0 00000000 FED76E
此欄位中
1) IP address用16進制表示.
如
2) Flag使用數字表示, 其含義可參照 net-tools
範例中,
1 = 1 = U
3 = 2*1 +1 = UG
(Ref.0)
Exploring the /proc/net/ Directory
(Ref.1) route --help 摘錄
route flag:
U (route is up)
H (target is a host)
G (use gateway)
R (reinstate route for dynamic routing)
D (dynamically installed by daemon or redirect)
M (modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (reject route)
(Ref.2) net-support.h 摘錄
/* Keep this in sync with /usr/src/linux/include/linux/route.h */
#define RTF_UP 0x0001 /* route usable "U" */
#define RTF_GATEWAY 0x0002 /* destination is a gateway "G" */
#define RTF_HOST 0x0004 /* host entry (net otherwise) "H" */
#define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
#define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
#define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
#define RTF_MTU 0x0040 /* specific MTU for this route */
#ifndef RTF_MSS
#define RTF_MSS RTF_MTU /* Compatibility :-( */
#endif
#define RTF_WINDOW 0x0080 /* per route window clamping */
#define RTF_IRTT 0x0100 /* Initial round trip time */
#define RTF_REJECT 0x0200 /* Reject route */