一.关于ISSPACE:
原型:extern int isspace(int c);
用法:#include
功能:判断字符c是否为空白符
说明:当c为空白符时,返回非零值,否则返回零。
空白符指空格、水平制表、垂直制表、换页、回车和换行符。
#include
#include
int main()
{
char str[]="123c @# FD\ts\n";
int i;
for(i=0; str[i]!=0;i++)
if(isspace(str[i]))
printf("str[%d] is a white-space character: %d\n", i, str[i]);
return 0;
}
isspace()函数对"\n"标识符也做为空格处理
二.关于UMASK(设置建立新文件时的权限遮罩)
相关函数 |
creat,open
|
表头文件 |
#include #include
|
定义函数 |
mode_t umask(mode_t mask);
|
函数说明 |
umask()会将系统umask值设成参数mask&0777后的值,然后将先前的umask值返回。在使用open()建立新文件时,该参数mode并非真正建立文件的权限,而是(mode&~umask)的权限值。例如,在建立文件时指定文件权限为0666,通常umask值默认为022,则该文件的真正权限则为0666&~022=0644,也就是rw-r--r--返回值此调用不会有错误值返回。返回值为原先系统的umask值。
#include #include #include #define DEFAULT_UMASK 0077 int main() { mode_t old_umask; old_umask = umask((mode_t)DEFAULT_UMASK); printf("Set umask to %04o\n",old_umask); return 0; } |
结果: Set umask to 0022
三.读链表结构
1.这是一个结构定义,这个成员结构都有一个list和字符串值
struct str_list
{
struct list list;
const char *str;
};
2.将str_list类型传入到list_iterate_items()中,这里的sll为主链表的头,我们就是从sll这个链表上寻找我们需要的节点
int str_list_match_item(struct list *sll, const char *str)
{
struct str_list *sl;
list_iterate_items(sl, sll)
if (!strcmp(str, sl->str))
return 1;
return 0;
}
3.list为一个struct list结构,这里的v就是从主链表中循环找到的那些节点
#define list_iterate_items(v, head) list_iterate_items_gen(v, (head), list)
4.这里的v实际上是str_list结构类型的,head为主链表
这里的list_struct_base()函数是从主链表头开始找节点,依次返回str_list结构类型的节点
这个for循环的意思就是,从头节点的下一个节点开始,如果找到的节点不等于头节点,说明没有到链表尾,那么就依次移动到下一个节点,直到循环完整个链表,最后又再次回到头为止
#define list_iterate_items_gen(v, head, field) \
for( v = list_struct_base((head)->n, typeof(*v), field); \
&v->field != (head); \
v = list_struct_base(v->field.n, typeof(*v), field))
5.这里的参数v实际上主链表上的节点,t是一种struct str_list结构类型,head为一个空的list结构成员
把头节点申明为0位置,然后用v节点减去头节点,从而找到v节点的偏移,并成功找到v节点
#define list_struct_base(v, t, head) \
((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->head))
四.结构
#include
#include
#include
struct dm_hash_node {
struct dm_hash_node *next;
void *data;
unsigned keylen;
char key[0];
};
struct dm_hash_table {
unsigned num_nodes;
unsigned num_slots;
struct dm_hash_node **slots;
};
struct dm_hash_table *dm_hash_create(unsigned size_hint)
#include
#include
#include
struct dm_hash_node {
struct dm_hash_node *next;
void *data;
unsigned keylen;
char key[0];
};
struct dm_hash_table {
unsigned num_nodes;
unsigned num_slots;
//该句只占一个指针的大小,如果用struct dm_hash_node slots,则占用dm_hash_node整个结构的大小
//如果是struct dm_hash_node *slots,*和**都只表示在这个结构中占用一个指针标识符而已
struct dm_hash_node **slots;
};
struct dm_hash_table *dm_hash_create(unsigned size_hint)
{
size_t len;
unsigned new_size = 16u;
struct dm_hash_table *hc = malloc(sizeof(*hc));
if (!hc)
{
return 0;
}
memset(hc, 0, sizeof(*hc));
while (new_size < size_hint)
new_size = new_size << 1;
hc->num_slots = new_size;
printf("new_size is %u\n",new_size);
len = sizeof(*(hc->slots)) * new_size;//这里的sizeof大小为指针大小
printf("len is %d\n",len);
if (!(hc->slots = malloc(len)))
{
printf("!(hc->slots = malloc(len))) error \n");
}
memset(hc->slots, 0, len);
return hc;
return 0;
}
int main()
{
struct dm_hash_table *names;
names = dm_hash_create(128);
printf("struct dm_hash_node is %d\n",sizeof(struct dm_hash_node *));
return 0;
}
输出:
new_size is 128
len is 512
struct dm_hash_node is 4
五.杂
#include
#include
#include
#define PATH_MAX 4096
static char *_consume(char *buffer, int (*fn) (int))
{
while (*buffer && fn(*buffer))
buffer++;
return buffer;
}
static int _isword(int c)
{
return !isspace(c);
}
int split_words(char *buffer, unsigned max, char **argv)
{
unsigned arg;
for (arg = 0; arg < max; arg++)
{
buffer = _consume(buffer, isspace);/* 去除buffer中的空格 */
if (!*buffer)
break;
argv[arg] = buffer;
buffer = _consume(buffer, _isword);
if (*buffer)
{
*buffer = '\0';
buffer++;
}
}
return arg;
}
int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...)
{
int n;
va_list ap;
va_start(ap, format);
n = vsnprintf(buf, bufsize, format, ap);
va_end(ap);
if (n < 0 || (n > bufsize - 1))
return -1;
return n;
}
int main()
{
int r = 0;
char proc_mounts[PATH_MAX];
char *split[4], buffer[PATH_MAX + 16];
char path[PATH_MAX];
int len = sizeof(path);
FILE * fp;
char *proc="/proc";
lvm_snprintf(proc_mounts, sizeof(proc_mounts),"%s/mounts", proc);
printf("proc_mounts is %s\n",proc_mounts);
fp = fopen(proc_mounts, "r");
while (fgets(buffer, sizeof(buffer), fp))
{
printf("buffer is %s----\n",buffer);
if (split_words(buffer, 4, split) == 4 && !strcmp(split[2], "sysfs"))
{
printf("split buffer is %s----\n",buffer);
if (lvm_snprintf(path, len, "%s/%s", split[1],"block") >= 0)
{
printf("path is %s----\n",path);
r = 1;
}
break;
}
}
return 0;
}
输出:
proc_mounts is /proc/mounts
buffer is rootfs / rootfs rw 0 0
----
buffer is /dev/root / ext3 rw,data=ordered 0 0
----
buffer is /dev /dev tmpfs rw 0 0
----
buffer is /proc /proc proc rw 0 0
----
buffer is /sys /sys sysfs rw 0 0
----
split buffer is /sys----
path is /sys/block----
-----------------------------------------
fgets(在文件中读取一字符串)
六.关于commands
**********a.c**************************************************
#include
#include
static void _register_command(const char *name, const char * fn,
const char *desc,...)
{
int nargs = 0, i;
va_list ap;
va_start(ap,desc);
printf("%s\n",va_arg(ap, char *));
//while (va_arg(ap, int) >= 0)
// nargs++;
va_end(ap);
printf("%d hello\n",nargs);
}
static void _register_commands()
{
#define xx(a, b, c...) _register_command(# a, a, b, ## c);
#include "commands.h"
#undef xx
}
int main()
{
_register_commands();
printf("successe \n");
return 0;
}
***********commands.h**********************************************
xx("aaaaa","bbbbbbbb","cccccccccc\n\n""dddddddddddddd")
七.关于arg
*************b.c***************************************************
#include
#include
struct arg {
const char short_arg;
int x;
const char *long_arg;
int (*fn) ();
unsigned count;
char *value;
int32_t i_value;
uint32_t ui_value;
int64_t i64_value;
uint64_t ui64_value;
void *ptr;
};
struct arg the_args[2 + 1] = {
#define arg(a, b, c, d) {b, 0, "--" c, d, 0, NULL, 0, 0, INT64_C(0), UINT64_C(0), NULL},
#include "args.h"
#undef arg
};
static void create_toolcontext(struct arg *the_args)
{
printf("success\n");
}
int main()
{
int i;
create_toolcontext(&the_args[0]);
printf("%c \n",the_args[0].short_arg);
printf("%s \n",the_args[0].long_arg);
printf("%d",trustcache_ARG);
return 0;
}
*********args.h*************************************************
arg(trustcache_ARG, 'm', "trustcache", NULL)
arg(ARG_COUNT, '-', "", NULL)
八.scandir读取特定的目录数据
相关函数:opendir, readdir, alphasort
表头文件:#include
定义函数:int scandir(const char *dir, struct dirent **namelist, nt (*select) (const struct dirent *), nt (*compar) (const struct dirent **, const struct dirent**));
函数说明:scandir()会扫描参数dir指定的目录文件,经由参数select指定的函数来挑选目录结构至参数namelist数组中,最后再调用参数compar指定的函数来排序namelist数组中的目录数据。每次从目录文件中读取一个目录结构后便将此结构传给参数select所指的函数, select函数若不想要将此目录结构复制到namelist数组就返回0, 若select为空指针则代表选择所有的目录结构。scandir()会调用qsort()来排序数据,参数compar则为qsort()的参数,若是要排列目录名称字母则可使用alphasort(). 结构dirent定义请参考readdir()
返回值 :成功则返回复制到namelist数组中的数据结构数目,有错误发生则返回-1
错误代码:ENOMEM 核心内存不足
/* print files in current directory in reverse order */
#include
main(){
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while(n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}