工作总结(一):Linux C

时间:2023-02-10 22:55:32

这三个月以来一直忙着赶进度,没有停下来记录一些东西,很多很好的东西往往只能零零散散地记在草稿本上, 这样偶尔想起来自己都找不到,所以现在抽空总结下来。

这些天做了三件事,其一是在Linux下开发了对接service的IPTV client lib on PC,之所以是on PC,因为我写的这段程序只是为了以后移植到一个运行RTOS的机顶盒上面;其二是基于PayPal做的支付系统,其三是一个监控服务器和用户状态的简单后台管理页面,这两个都是用cakephp + bootstrap做的,并没有涉及到数据库,与数据库交互是使用了已经写好的API。这篇记录C的内容。

一、打印指针值:

int a = ;
int *p = &a;
printf("%p", p);

二、

PHPStorm: PHP IDE

PyCharm: PYTHON IDE

Brackets : 强大免费的开源跨平台Web前端开发工具IDE

coolshell.com : 酷壳网

三、

问题:VMWare Workstation虚拟机出现故障,非正常关机时,无法再次打开。

解决:删除虚拟磁盘目录下的*.lck文件(可能需要重启)。

四、字符串的正确使用:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(int argc, char *argv[])

{

    char str1[] = "abcdefghijklmnopqrstuvwxyz";//编译器将在末尾自动添加“\0”,但是需要保证,字符数组中有空余的位置

    char *str2 = NULL;

    str2 = (char *)malloc( + );

    memset(str2, ,  + );

    memcpy(str2, str1, );

    printf("str1 length is %ld, size is %ld\n", strlen(str1), sizeof(str1));

    printf("str2 length is %ld, size is %ld\n", strlen(str2), sizeof(str2));

    int i = ;

    for(i = ; i < ; i++)

        printf("%02x ", str1[i]);

    printf("\n");

    for(i = ; i < ; i++)

        printf(" %c ", str1[i]);

    printf("\n");

    if(NULL != str2) free(str2);

    return ;

}

输出:

hubery@hubery-VirtualBox:~/CP/HW$ ./shellInput
str1 length is , size is
str2 length is , size is
6a 6b 6c 6d 6e 6f 7a
a b c d e f g h i j k l m n o p q r s t u v w x y z

尽量不要使用strlen、strcpy、strcat这些需要通过结束符“\0”来判断字符串长度的函数,因为一旦字符串没有正常的结束符,你将得到意想不到的结果。

五、在子函数中malloc变量(声明时字符串大小不可知):

#include <stdlib.h>
#include <stdio.h> int malloc_inner(char **p)
{
*p = (char *)malloc();
return ;
} int main()
{
char *str = null;
malloc_inner(&str);
free(str);
return ;
}

六、命令行参数:

//shellInput.c
#include <stdio.h>
#include <stdlib.h> int main(int argc, char *argv[])
{
printf("argc = %d, argv[0] = %s, argv[1] = %s\n", argc, argv[], argv[]);
return ;
} //output
#./shellInput hello
argc = , argv[] = ./shellInput, argv[] = hello

七、使用CGDB调试一个程序:

objdump,cgdb,dmesg -c.

编译参数: -g -rdynamic

调试: cgdb ./a.out

加断点: b func_name

步进: n

步入: s

打印: p

GDB/CGDB 调试时打印完整内容:set print element 0

查看dumped的位置: bt/where

使down一个程序: kill -s SIGSEGV pid

八、C读写文件:

snprintf(filename, sizeof(filename), "%s.sig", arv[]);
//write
if((f = fopen(filename, "wb+")) == NULL)
{
ret = ;
printf("...");
//...
}
if(fwrite(buf, , olen ,f) != olen)
{
printf("...");
//...
}
fclose(f); //read
f = fopen(filename,"rb");
i = fread(buf, , sizeof(buf), f);
fclose(f);

九、结构体

typedef struct {
slist_node node;
VdsChannel channel;
CHAR *type;
CHAR *video_id;
CHAR *url;//需要url时,先查询链表,若没有再作请求
}VdsChanInfo; struct timerr
{
time_t time_begin;
unsigned int time_long;
unsigned int time_refresh;
char user_id[];
char token[];
};
struct timerr timer ;//用法1 //结构体数组
struct
{
UINT32 isValid;//1 for true, 0 for false
UINT32 decode_type;//0 for all, 1 for sha1+rsa, 2 for aes+rsa
CHAR *request_encrypt_key;
CHAR *response_encrypt_key; }keyPairs[KEY_PAIR_SUM]; //构造枚举类型
typedef enum {
VDS_READY,
VDS_AUTH_OK,
VDS_GET_KEY_OK,
VDS_GET_CHAN_INIT_OK,
VDS_GET_CHAN_URL_OK
}VdsStatus; typedef struct { BOOL running;
BOOL exit;
VdsStatus status; VdsIptvParam param;
pthread_t thread_id;
pthread_mutex_t lock;
pthread_mutex_t debug_lock;
UINT32 chan_num;
slist channel_list; }VdsIptvMgr; VdsIptvMgr * get_mgr(void);//用法2

十、链表使用

 //singly_linked_list.h
#ifndef __SINGLY_LINKED_LIST_H__
#define __SINGLY_LINKED_LIST_H__
#include "porting.h" #ifdef __cplusplus
extern "C" {
#endif typedef struct _slist_node {
struct _slist_node *next;
} slist_node; typedef struct {
UINT32 count;
slist_node *head;
slist_node *tail;
} slist; static __inline__ void slist_add_head(slist *list, slist_node *node) {
if (list->count == ) {
list->head = node;
list->tail = node;
}
else {
node->next = list->head;
list->head = node;
}
list->count++;
} static __inline__ void slist_add_tail(slist *list, slist_node *node)
{
if (list->count == ) {
list->head = node;
list->tail = node;
}
else {
list->tail->next = node;
list->tail = node;
}
list->count++;
} static __inline__ slist_node * slist_del_head(slist *list)
{
slist_node *node = NULL;
if (list->count > ) {
node = list->head;
list->head = node->next;
node->next = NULL;
if (--list->count == ) {
list->tail = NULL;
}
}
return node;
} static __inline__ slist_node * slist_del_tail(slist *list) {
slist_node *node = NULL;
if (list->count > ) {
slist_node *ptr = list->head;
while (ptr) {
if (ptr->next == list->tail) {
node = list->tail;
ptr->next = NULL;
list->tail = ptr;
list->count--;
break;
}
ptr = ptr->next;
}
}
else if (list->count == ) {
node = list->tail;
list->head = list->tail = NULL;
list->count--;
} return node;
} static __inline__ slist_node * slist_del_node(slist *list, slist_node *node)
{
slist_node *curr = NULL;
slist_node *temp = NULL; if(list->head == node)
return slist_del_head(list); if(list->tail == node)
return slist_del_tail(list); //if(list->count <= 2)
//{
//ASSERT(0);
//} curr = list->head;
if(curr == NULL)
return NULL; while(curr->next != NULL)
{
if(curr->next == node)
{
temp = curr->next;
curr->next = temp->next;
list->count--; temp->next = NULL; /*
if(list->count >= 1)
{
if(list->head == NULL || list->tail == NULL)
{
SDBBP();
} if(list->count == 1 && list->head != list->tail)
{
SDBBP();
} if(list->count == 2 && list->head->next != list->tail)
{
SDBBP();
} if(list->tail->next != NULL)
{
SDBBP();
}
}
*/ return temp;
}
else
curr = curr->next;
} /*
if(list->count > 0)
{
if(list->tail->next != NULL)
{
SDBBP();
}
}
*/
return NULL;
} static __inline__ void slist_free(slist *list)
{
slist_node *node = NULL;
if(NULL == list)
return;
while(list->count > )
{
node = slist_del_head(list);
FREE(node);
}
} #define SLIST_COUNT(slist) ((slist)->count) /**
* SLIST_ENTRY - get the struct for this entry
* @ptr: the struct slist_node pointer.
* @type: the type of the struct @ptr embedded in.
* @member: the name of the slist_node within the struct.
*/
#define SLIST_ENTRY(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *))->member))) #ifdef __cplusplus
}
#endif /* __cplusplus */ #endif

singly_linked_list.h

下面展示如何使用以上链表操作。

//
typedef struct {
UINT32 idx;
CHAR *name;
CHAR *img_h;
CHAR *img_s;
}VdsChannel; typedef struct {
slist_node node;
VdsChannel channel;
CHAR *type;
CHAR *video_id;
CHAR *url;//需要url时,先查询链表,若没有再作请求
}VdsChanInfo; //declare
vds_list = (slist *)malloc(sizeof(slist));
vds_list->count = ;
vds_list->head = NULL;
vds_list->tail = NULL; //add
VdsChanInfo *channels = (VdsChanInfo *)malloc(sizeof(VdsChanInfo));
channels->type = ( CHAR * )malloc( strlen( value ) + );
channels->video_id = ( CHAR * )malloc( strlen( value ) + );
channels->channel.name = ( CHAR * )malloc( strlen( value ) + );
channels->channel.img_h = ( CHAR * )malloc( strlen( value ) + );
channels->channel.img_s = ( CHAR * )malloc( strlen( value ) + );
strcpy( channels->type , value );
strcpy( channels->video_id, value );
strcpy( channels->channel.name, value );
strcpy( channels->channel.img_h, value );
strcpy( channels->channel.img_s, value );
channels->channel.idx = i;
channels->url = NULL;
channels->node.next = NULL;
slist_add_tail(list, &(channels->node)); //search
slist_node *node;
VdsChanInfo *vds;
VdsChannel *channel_info = (VdsChannel *)malloc(sizeof(VdsChannel));;
for(node = vds_list->head; node != NULL; node = node->next)
{
vds = SLIST_ENTRY(node, VdsChanInfo, node);
if(idx == vds->channel.idx)
{
channel_info->idx = vds->channel.idx;
channel_info->img_h = vds->channel.img_h;
channel_info->img_s = vds->channel.img_s;
channel_info->name = vds->channel.name;
VDS_DBG(VDS_D_SYS, " . Got channel info.");
return channel_info;
}
} //free and destory
void vds_free(slist *list)
{
slist_node *node;
VdsChanInfo *vds;
for(node = list->head; node != NULL; node = node->next)
{
vds = SLIST_ENTRY(node, VdsChanInfo, node);
if(NULL != vds->type)
{
free(vds->type);
vds->type = NULL;
}
if(NULL != vds->video_id)
{
free(vds->video_id);
vds->video_id = NULL;
}
if(NULL != vds->url)
{
free(vds->url);
vds->url = NULL;
}
if(NULL != vds->channel.name)
{
free(vds->channel.name);
vds->channel.name = NULL;
}
if(NULL != vds->channel.img_h)
{
free(vds->channel.img_h);
vds->channel.img_h = NULL;
}
if(NULL != vds->channel.img_s)
{
free(vds->channel.img_s);
vds->channel.img_s = NULL;
}
} // slist_free(vds_list);
}
vds_free(vds_list);
slist_free(vds_list);