工作总结(一):Linux C
这三个月以来一直忙着赶进度,没有停下来记录一些东西,很多很好的东西往往只能零零散散地记在草稿本上, 这样偶尔想起来自己都找不到,所以现在抽空总结下来。
这些天做了三件事,其一是在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);
工作总结(一):Linux C的更多相关文章
- 工作常用的linux/mysql/php/工具命令
工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...
- 工作中常用Linux命令--服务器运维
工作中常用Linux命令--服务器运维 lsof查看端口使用情况 lsof -i:8080更多lsof命令使用说明:http://www.cnblogs.com/peida/archive/2013/ ...
- 工作笔记 之 Linux服务搭建
No.1 linux环境下安装nginx步骤 Nginx (engine x) 是一款轻量级的Web 服务器.反向代理服务器.电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行. ...
- Xshell~工作中访问Linux服务器
1.下载Xshell 下载地址:https://xshell.en.softonic.com/ 2.安装(无特殊修改,自行安装即可) 3.使用 登录(1.新建->输入主机IP,点击确定-> ...
- 工作中常用Linux命令
建立软链接 ln -s 例:ln -s b a 解释:把文件夹a和文件夹b关联起来,访问文件夹a,实际访问的是问价夹b 删除软连接 rm -rf a 直接删掉a文件夹跟a和b的软连接. ...
- 清华申请退学博士作品:完全用Linux工作
http://www.cnblogs.com/cbscan/articles/3252872.html 下文地址 http://blog.oldboyedu.com/use-linux/ 按: 尽管我 ...
- 王垠:完全用Linux工作
来自: Zentaur(alles klar) 录一篇旧文 作者:王垠 完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作 ...
- Linux学习之路一计算机是如何工作的
初次接触MOOC课堂,里面有个很牛X的老师教Linux,恰好自己有兴趣学,顾有了此系列学习博文. 第一讲 计算机是如何工作的 学习Linux,涉及到了C语言和汇编以及操作系统的知识,顾第一讲要讲讲 ...
- 完全用 GNU/Linux 工作(转)
转自:http://www.chinaunix.net/old_jh/4/16102.html 看到一半,实在太长,但已觉得很好,转来分享一下. 完全用 GNU/Linux 工作 - 摈弃 Windo ...
- 清华申请退学博士作品:完全用Linux工作,凸Windows
清华申请退学博士作品:完全用Linux工作 按尽管我们已经不习惯看长篇大论, 但我还是要说, 这是一篇值得你从头读到尾的长篇文章. 2005年9月22日,清华在读博士生王垠在水木社区BLOG上发表了& ...
随机推荐
- NPOI创建DOCX常用操作
1. 创建文档 XWPFDocument m_Docx = new XWPFDocument(); 2. 页面设置 //1‘=1440twip=25.4mm=72pt(磅point)=96px(像 ...
- [z]分区truncate操作的介绍及对全局索引和空间释放影响的案例解析
[z]https://www.2cto.com/database/201301/181226.html 环境: [sql] [oracle@localhost ~]$ uname -r 2.6.18- ...
- 安装scrapy 出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools 错误
安装scrapy 出现以下 错误: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C ...
- Linux移植之make uImage编译过程分析
编译出uboot可以运行的linux内核代码的命令是make uImage,下面详细介绍下生成linux-2.6.22.6/arch/arm/boot/uImage的过程: 1.vmlinux.Ima ...
- 软件开发中 SQL SERVER 任务的用法
在软件开发中,经常性会用到定时任务.这个时候你可能会想到线程.但是事实中,线程方法比较麻烦.容易出错,资源竞争等问题,设计起来让你很头痛. 现在给大家提供一个新的思路,用SQL SERVER 的任务管 ...
- 探索未知种族之osg类生物---呼吸分解之事件循环三
那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...
- How to use bmw icom a2
Lan Connect Operation Details 1. Connect the LAN cable to ICOM A1/ICOM A2, another side to laptop LA ...
- lua 2.2 变种
1.修改 ~= 操作符为 != 2.取消 --[[ ]] 多行注释语法 下载源码
- c sharp multithreading
1. 静态方法 using System; using System.Threading; namespace PlusThread { class Program { static void Ma ...
- PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...