首先看如下一段代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

void static_usage()
{
static int s = 0;

if(s == 0)
{
s = 1;
printf("s equals 0\n");
}
else
{
printf("s equals 1\n");
}
}

int main(void)
{
int i = 0;

for(i = 0; i < 5; ++i)
{
static_usage();
}

return 0;
}

实际打印结果:

1
2
3
4
5
s equals 0
s equals 1
s equals 1
s equals 1
s equals 1
1
2
3
4
5
s equals 0
s equals 0
s equals 0
s equals 0
s equals 0
阅读全文 »

1:/dev/null就是一个黑洞,不想要的东西重定向它即可

举例:find ./ -name “xyz” 2> /dev/null

解释:这样标准错误信息将被放入黑洞消失

2:/dev/zero有类似memset的功能,填充0×00(不是指ACII的’0′)

举例:dd if=/dev/zero of=xyz bs=1M count=10

解释:新建一个10M大小的空文件(填充的是0×00,可用hexdump命令查看)

背景:

安装ubuntu0804,第一个分区是swap分区(/dev/sda1),第二个分区是”boot”分区(dev/sda2),第三个分区是跟目录”/”分区(/dev/sda3)

如果”boot”分区和”/”分区在同一个分区,请根据自身实际情况进行修改

1:grub的三个主要引导文件

(1)/boot/grub/stage1与mbr一样也是512bytes大小,前446bytes也与mbr相同,

    即stage1文件是bootloader本地备份,但stage1文件446bytes之后64bytes的partition tables是无效的
    
    因为grub在安装mbr的时候会根据实际分区情况来填写mbr中的partition tables
    
    stage1文件的最后两个bytes与mbr一样也是”0x55aa”
阅读全文 »

我们从最简单的”hello world”开始,新建hello.c文件代码如下:

1
2
3
4
5
6
7
#include <stdio.h>

int main(void)
{
printf("Hello World\n");
return 0;
}

生产可执行文件的命令如下:

1
gcc hello.c -o hello

怎么样,很简单吧,但是到底做了些什么呢?分解动作如下:

第一步:预编译: .c文件 — 预处理后的.i文件

1
gcc -E hello.c -o hello.i

那么预编译做了哪些工作呢?

答:

a:将#define删除并展开所有宏

b:处理预编译指令#if #ifdef #else #endif以及#include

c:删除注释

d:添加调试标示(这部分占.i大部分内容)

阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from Tkinter import *
from tkFont import Font

def btnClick(ev = None):
foodl = ''
try:
foodl = eval(en.get())
except:
pass
if isinstance(foodl, (int, float, long)):
pass
else:
foodl = 'Error..'
lbl.config(text = foodl)

#主窗口
mainView = Tk()
mainView.title('YuanLin')

ft = Font(family = ('Verdana'), size = 18)
en = Entry(mainView, font = ft)
btn = Button(mainView, text = '计算(注意运算顺序)', command = btnClick, font = ft)
lbl = Label(text = '运算符: + - * / % **', font = ft)

enterEvent = lambda x: x.keycode == 13 and btnClick()
clickEvent = lambda x: lbl.config(text = '运算符: + - * / % **')

en.focus()
en.bind('<Key>', enterEvent)
en.bind('<Button-1>', clickEvent)

en.pack()
btn.pack()
lbl.pack()

mainView.mainloop()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdbool.h>

// bracket define & function
enum bracket_type
{
BRACKET_NONE,
BRACKET_LEFT,
BRACKET_RIGHT
};

enum bracket_type type(char * c)
{
enum bracket_type type = BRACKET_NONE;

if(*c == '(' || *c == '[')
{
type = BRACKET_LEFT;
}
else if(*c == ')' || *c == ']')
{
type = BRACKET_RIGHT;
}

return type;
}

bool match(char * c1, char * c2)
{
bool ret = false;

if(*c1 == '(' && *c2 == ')'
|| *c1 == ')' && *c2 == '('
|| *c1 == '[' && *c2 == ']'
|| *c1 == ']' && *c2 == '[')
{
ret = true;
}

return ret;
}

// stack define & function
#define STACK_SIZE 100

struct my_stack
{
char data[STACK_SIZE];
int top;
};

void init(struct my_stack * stack)
{
stack->top = -1;
}

bool push(struct my_stack * stack, char * c)
{
bool ret = false;

if(stack->top < STACK_SIZE)
{
stack->data[++stack->top] = *c;

ret = true;
}
else
{
ret = false;
}

return ret;
}

bool pop(struct my_stack * stack, char * c)
{
bool ret = false;

if(stack->top >= 0)
{
*c = stack->data[stack->top--];

ret = true;
}
else
{
ret = false;
}

return ret;
}

int size(struct my_stack * stack)
{
return stack->top + 1;
}

//brackets_match
bool brackets_match(char *str)
{
char c;
char * s = str;
bool ret = true;

struct my_stack stack;

init(&stack);

while(*s != '\0')
{
if(BRACKET_LEFT == type(s))
{
push(&stack, s);
}
else if(BRACKET_RIGHT == type(s))
{
if(pop(&stack, &c) == true)
{
if(match(&c, s) != true)
{
ret = false;
break;
}
}
else
{
ret = false;
break;
}
}

++s;
}

if(size(&stack) != 0)
{
ret = false;
}

return ret;
}

int main(void)
{
int i = 0;
char * str[3] = {
"([()([])])", // match
"([()([(])])", // unmatch
"([()([])]))" // unmatch
};

for(i = 0; i < 3; ++i)
{
printf("%s\n", brackets_match(str[i]) == true ? "match" : "unmatch");
}

return 0;
}

问题描述

ARM编译环境的头文件time.h中,有如下的时间转换接口:

1
extern time_t mktime(struct tm * /*timeptr*/);

如果,传入的实参tm不合法的话,mktime会返回-1

但是,实际的代码却没有返回fasle

1
2
3
4
if(-1 == mktime(&result))
{
return FALSE; // not return false
}

问题分析

原来,此时time_t的类型定义是 typedef unsigned int time_t;(不同编译环境定义各不相同)

所以,导致获取到的实际返回值是一个极大的正值

1
(signed int)-1 -> 补码 0xFFFFFFFF -> (unsigned int)4294967295
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// type define
struct node
{
void * data;
struct node * next;
};

struct slist
{
int size;
struct node * head;
};

typedef void (* slist_print_cb) (void * data);

// declaration
struct slist * slist_create(void);
bool slist_insert(struct slist * list, void * data, int pos);
bool slist_remove(struct slist * list, int pos);
int slist_size(struct slist * list);
void * slist_data(struct slist * list, int pos);
struct node * slist_find(struct slist * list, int pos);

static void slist_print_int(void * data);

// implement
struct slist * slist_create(void)
{
struct slist * list = NULL;

list = (struct slist *) malloc(sizeof(struct slist));
memset(list, 0, sizeof(struct slist));

list->size = 0;
list->head = NULL;

return list;
}

bool slist_insert(struct slist * list, void * data, int pos)
{
struct node * prev = NULL;
struct node * insert = NULL;

insert = (struct node *) malloc(sizeof(struct node));
memset(insert, 0, sizeof(struct node));

insert->data = data;
insert->next = NULL;

prev = slist_find(list, pos - 1);
if(prev)
{
insert->next = prev->next;
prev->next = insert;
}
else
{
list->head = insert;
}

++list->size;

return true;
}

bool slist_remove(struct slist * list, int pos)
{
struct node * prev = NULL;
struct node * remove = NULL;

prev = slist_find(list, pos - 1);
remove = slist_find(list, pos);

if(prev && remove)
{
if(remove == list->head)
{
list->head = remove->next;
}
else
{
prev->next = remove->next;
}

free(remove);

--list->size;
}

return true;
}

int slist_size(struct slist * list)
{
return list->size;
}

void * slist_data(struct slist * list, int pos)
{
struct node * found = NULL;

found = slist_find(list, pos);

if(found)
{
return found->data;
}
else
{
return NULL;
}
}

struct node * slist_find(struct slist * list, int pos)
{
int i = 0;
struct node * found = NULL;

found = list->head;

for(i = pos; i > 0; --i)
{
if(found)
{
found = found->next;
}
else
{
break;
}
}

return found;
}

void slist_print(void * data, slist_print_cb cb)
{
if(cb)
{
cb(data);
}
}

static void slist_print_int(void * data)
{
printf("%d\n", (int) data);
}

int main(void)
{
struct slist * list = NULL;

list = slist_create();

slist_insert(list, (void *)1, 0);
slist_insert(list, (void *)2, 0);

slist_print(slist_data(list, 0), slist_print_int); // 1
slist_print(slist_data(list, 1), slist_print_int); // 2

slist_remove(list, 0);

slist_print(slist_data(list, 0), slist_print_int); // 2

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below. If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed. It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
syntax on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
" au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
" \| exe "normal g'\"" | endif
"endif

" Uncomment the following to have Vim load indentation rules according to the
" detected filetype. Per default Debian Vim only load filetype specific
" plugins.
"if has("autocmd")
" filetype indent on
"endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes) in terminals

" Source a global configuration file if available
" XXX Deprecated, please move your changes here in /etc/vim/vimrc
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif

" added by yuanlin begin
set nu
set autoindent
set cindent
set noswapfile
set autochdir

""""""""""""""""""""""""""""""
" enable backspace key
""""""""""""""""""""""""""""""
set nocompatible
set backspace=indent,eol,start

""""""""""""""""""""""""""""""
" Tag list (ctags)
""""""""""""""""""""""""""""""
let Tlist_Ctags_Cmd = '/usr/bin/ctags'

let Tlist_Show_One_File = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_Use_Right_Window = 1
let Tlist_Auto_Open = 1

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has("cscope")
set csprg=/usr/bin/cscope
set csto=1
set cst
set nocsverb
" add any database in current directory
"if filereadable("cscope.out")
" cs add cscope.out
"endif
set csverb
endif

nmap <C-g> :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-f> :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-c> :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-d> :cs find d <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>i :cs find i <C-R>=expand("<cfile>")<CR>$<CR>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" add for coding
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let &termencoding=&encoding
set fileencodings=utf-8,gbk

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" shortcuts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <C-z> u
nmap <C-y> <C-r>
nmap <C-n> : tabn <CR>
nmap <C-p> : tabp <CR>
nmap <C-e> : tabe
nmap <C-o> : TlistToggle <CR>
nmap <C-h> : set hls <CR>
nmap <S-h> : set nohls <CR>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" NerdCommenter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"\cc comment
"\cu uncomment
"\c<space> comment/uncomment
"\ca switch sytle

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" colors
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
color desert

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" copy paste
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set paste

" added by yuanlin end
0%