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

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

一、打印指针值:

  1. int a = ;
  2. int *p = &a;
  3. printf("%p", p);

二、

PHPStorm: PHP IDE

PyCharm: PYTHON IDE

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

coolshell.com : 酷壳网

三、

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

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

四、字符串的正确使用:

  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <string.h>
  6.  
  7. int main(int argc, char *argv[])
  8.  
  9. {
  10.  
  11. char str1[] = "abcdefghijklmnopqrstuvwxyz";//编译器将在末尾自动添加“\0”,但是需要保证,字符数组中有空余的位置
  12.  
  13. char *str2 = NULL;
  14.  
  15. str2 = (char *)malloc( + );
  16.  
  17. memset(str2, , + );
  18.  
  19. memcpy(str2, str1, );
  20.  
  21. printf("str1 length is %ld, size is %ld\n", strlen(str1), sizeof(str1));
  22.  
  23. printf("str2 length is %ld, size is %ld\n", strlen(str2), sizeof(str2));
  24.  
  25. int i = ;
  26.  
  27. for(i = ; i < ; i++)
  28.  
  29. printf("%02x ", str1[i]);
  30.  
  31. printf("\n");
  32.  
  33. for(i = ; i < ; i++)
  34.  
  35. printf(" %c ", str1[i]);
  36.  
  37. printf("\n");
  38.  
  39. if(NULL != str2) free(str2);
  40.  
  41. return ;
  42.  
  43. }

输出:

  1. hubery@hubery-VirtualBox:~/CP/HW$ ./shellInput
  2. str1 length is , size is
  3. str2 length is , size is
  4. 6a 6b 6c 6d 6e 6f 7a
  5. 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变量(声明时字符串大小不可知):

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int malloc_inner(char **p)
  5. {
  6. *p = (char *)malloc();
  7. return ;
  8. }
  9.  
  10. int main()
  11. {
  12. char *str = null;
  13. malloc_inner(&str);
  14. free(str);
  15. return ;
  16. }

六、命令行参数:

  1. //shellInput.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. printf("argc = %d, argv[0] = %s, argv[1] = %s\n", argc, argv[], argv[]);
  8. return ;
  9. }
  10.  
  11. //output
  12. #./shellInput hello
  13. 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读写文件:

  1. snprintf(filename, sizeof(filename), "%s.sig", arv[]);
  2. //write
  3. if((f = fopen(filename, "wb+")) == NULL)
  4. {
  5. ret = ;
  6. printf("...");
  7. //...
  8. }
  9. if(fwrite(buf, , olen ,f) != olen)
  10. {
  11. printf("...");
  12. //...
  13. }
  14. fclose(f);
  15.  
  16. //read
  17. f = fopen(filename,"rb");
  18. i = fread(buf, , sizeof(buf), f);
  19. fclose(f);

九、结构体

  1. typedef struct {
  2. slist_node node;
  3. VdsChannel channel;
  4. CHAR *type;
  5. CHAR *video_id;
  6. CHAR *url;//需要url时,先查询链表,若没有再作请求
  7. }VdsChanInfo;
  8.  
  9. struct timerr
  10. {
  11. time_t time_begin;
  12. unsigned int time_long;
  13. unsigned int time_refresh;
  14. char user_id[];
  15. char token[];
  16. };
  17. struct timerr timer ;//用法1
  18.  
  19. //结构体数组
  20. struct
  21. {
  22. UINT32 isValid;//1 for true, 0 for false
  23. UINT32 decode_type;//0 for all, 1 for sha1+rsa, 2 for aes+rsa
  24. CHAR *request_encrypt_key;
  25. CHAR *response_encrypt_key;
  26.  
  27. }keyPairs[KEY_PAIR_SUM];
  28.  
  29. //构造枚举类型
  30. typedef enum {
  31. VDS_READY,
  32. VDS_AUTH_OK,
  33. VDS_GET_KEY_OK,
  34. VDS_GET_CHAN_INIT_OK,
  35. VDS_GET_CHAN_URL_OK
  36. }VdsStatus;
  37.  
  38. typedef struct {
  39.  
  40. BOOL running;
  41. BOOL exit;
  42. VdsStatus status;
  43.  
  44. VdsIptvParam param;
  45. pthread_t thread_id;
  46. pthread_mutex_t lock;
  47. pthread_mutex_t debug_lock;
  48. UINT32 chan_num;
  49. slist channel_list;
  50.  
  51. }VdsIptvMgr;
  52.  
  53. VdsIptvMgr * get_mgr(void);//用法2

十、链表使用

  1. //singly_linked_list.h
  2. #ifndef __SINGLY_LINKED_LIST_H__
  3. #define __SINGLY_LINKED_LIST_H__
  4. #include "porting.h"
  5.  
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9.  
  10. typedef struct _slist_node {
  11. struct _slist_node *next;
  12. } slist_node;
  13.  
  14. typedef struct {
  15. UINT32 count;
  16. slist_node *head;
  17. slist_node *tail;
  18. } slist;
  19.  
  20. static __inline__ void slist_add_head(slist *list, slist_node *node) {
  21. if (list->count == ) {
  22. list->head = node;
  23. list->tail = node;
  24. }
  25. else {
  26. node->next = list->head;
  27. list->head = node;
  28. }
  29. list->count++;
  30. }
  31.  
  32. static __inline__ void slist_add_tail(slist *list, slist_node *node)
  33. {
  34. if (list->count == ) {
  35. list->head = node;
  36. list->tail = node;
  37. }
  38. else {
  39. list->tail->next = node;
  40. list->tail = node;
  41. }
  42. list->count++;
  43. }
  44.  
  45. static __inline__ slist_node * slist_del_head(slist *list)
  46. {
  47. slist_node *node = NULL;
  48. if (list->count > ) {
  49. node = list->head;
  50. list->head = node->next;
  51. node->next = NULL;
  52. if (--list->count == ) {
  53. list->tail = NULL;
  54. }
  55. }
  56. return node;
  57. }
  58.  
  59. static __inline__ slist_node * slist_del_tail(slist *list) {
  60. slist_node *node = NULL;
  61. if (list->count > ) {
  62. slist_node *ptr = list->head;
  63. while (ptr) {
  64. if (ptr->next == list->tail) {
  65. node = list->tail;
  66. ptr->next = NULL;
  67. list->tail = ptr;
  68. list->count--;
  69. break;
  70. }
  71. ptr = ptr->next;
  72. }
  73. }
  74. else if (list->count == ) {
  75. node = list->tail;
  76. list->head = list->tail = NULL;
  77. list->count--;
  78. }
  79.  
  80. return node;
  81. }
  82.  
  83. static __inline__ slist_node * slist_del_node(slist *list, slist_node *node)
  84. {
  85. slist_node *curr = NULL;
  86. slist_node *temp = NULL;
  87.  
  88. if(list->head == node)
  89. return slist_del_head(list);
  90.  
  91. if(list->tail == node)
  92. return slist_del_tail(list);
  93.  
  94. //if(list->count <= 2)
  95. //{
  96. //ASSERT(0);
  97. //}
  98.  
  99. curr = list->head;
  100. if(curr == NULL)
  101. return NULL;
  102.  
  103. while(curr->next != NULL)
  104. {
  105. if(curr->next == node)
  106. {
  107. temp = curr->next;
  108. curr->next = temp->next;
  109. list->count--;
  110.  
  111. temp->next = NULL;
  112.  
  113. /*
  114. if(list->count >= 1)
  115. {
  116. if(list->head == NULL || list->tail == NULL)
  117. {
  118. SDBBP();
  119. }
  120.  
  121. if(list->count == 1 && list->head != list->tail)
  122. {
  123. SDBBP();
  124. }
  125.  
  126. if(list->count == 2 && list->head->next != list->tail)
  127. {
  128. SDBBP();
  129. }
  130.  
  131. if(list->tail->next != NULL)
  132. {
  133. SDBBP();
  134. }
  135. }
  136. */
  137.  
  138. return temp;
  139. }
  140. else
  141. curr = curr->next;
  142. }
  143.  
  144. /*
  145. if(list->count > 0)
  146. {
  147. if(list->tail->next != NULL)
  148. {
  149. SDBBP();
  150. }
  151. }
  152. */
  153. return NULL;
  154. }
  155.  
  156. static __inline__ void slist_free(slist *list)
  157. {
  158. slist_node *node = NULL;
  159. if(NULL == list)
  160. return;
  161. while(list->count > )
  162. {
  163. node = slist_del_head(list);
  164. FREE(node);
  165. }
  166. }
  167.  
  168. #define SLIST_COUNT(slist) ((slist)->count)
  169.  
  170. /**
  171. * SLIST_ENTRY - get the struct for this entry
  172. * @ptr: the struct slist_node pointer.
  173. * @type: the type of the struct @ptr embedded in.
  174. * @member: the name of the slist_node within the struct.
  175. */
  176. #define SLIST_ENTRY(ptr, type, member) \
  177. ((type *)((char *)(ptr)-(unsigned long)(&((type *))->member)))
  178.  
  179. #ifdef __cplusplus
  180. }
  181. #endif /* __cplusplus */
  182.  
  183. #endif

singly_linked_list.h

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

  1. //
  2. typedef struct {
  3. UINT32 idx;
  4. CHAR *name;
  5. CHAR *img_h;
  6. CHAR *img_s;
  7. }VdsChannel;
  8.  
  9. typedef struct {
  10. slist_node node;
  11. VdsChannel channel;
  12. CHAR *type;
  13. CHAR *video_id;
  14. CHAR *url;//需要url时,先查询链表,若没有再作请求
  15. }VdsChanInfo;
  16.  
  17. //declare
  18. vds_list = (slist *)malloc(sizeof(slist));
  19. vds_list->count = ;
  20. vds_list->head = NULL;
  21. vds_list->tail = NULL;
  22.  
  23. //add
  24. VdsChanInfo *channels = (VdsChanInfo *)malloc(sizeof(VdsChanInfo));
  25. channels->type = ( CHAR * )malloc( strlen( value ) + );
  26. channels->video_id = ( CHAR * )malloc( strlen( value ) + );
  27. channels->channel.name = ( CHAR * )malloc( strlen( value ) + );
  28. channels->channel.img_h = ( CHAR * )malloc( strlen( value ) + );
  29. channels->channel.img_s = ( CHAR * )malloc( strlen( value ) + );
  30. strcpy( channels->type , value );
  31. strcpy( channels->video_id, value );
  32. strcpy( channels->channel.name, value );
  33. strcpy( channels->channel.img_h, value );
  34. strcpy( channels->channel.img_s, value );
  35. channels->channel.idx = i;
  36. channels->url = NULL;
  37. channels->node.next = NULL;
  38. slist_add_tail(list, &(channels->node));
  39.  
  40. //search
  41. slist_node *node;
  42. VdsChanInfo *vds;
  43. VdsChannel *channel_info = (VdsChannel *)malloc(sizeof(VdsChannel));;
  44. for(node = vds_list->head; node != NULL; node = node->next)
  45. {
  46. vds = SLIST_ENTRY(node, VdsChanInfo, node);
  47. if(idx == vds->channel.idx)
  48. {
  49. channel_info->idx = vds->channel.idx;
  50. channel_info->img_h = vds->channel.img_h;
  51. channel_info->img_s = vds->channel.img_s;
  52. channel_info->name = vds->channel.name;
  53. VDS_DBG(VDS_D_SYS, " . Got channel info.");
  54. return channel_info;
  55. }
  56. }
  57.  
  58. //free and destory
  59. void vds_free(slist *list)
  60. {
  61. slist_node *node;
  62. VdsChanInfo *vds;
  63. for(node = list->head; node != NULL; node = node->next)
  64. {
  65. vds = SLIST_ENTRY(node, VdsChanInfo, node);
  66. if(NULL != vds->type)
  67. {
  68. free(vds->type);
  69. vds->type = NULL;
  70. }
  71. if(NULL != vds->video_id)
  72. {
  73. free(vds->video_id);
  74. vds->video_id = NULL;
  75. }
  76. if(NULL != vds->url)
  77. {
  78. free(vds->url);
  79. vds->url = NULL;
  80. }
  81. if(NULL != vds->channel.name)
  82. {
  83. free(vds->channel.name);
  84. vds->channel.name = NULL;
  85. }
  86. if(NULL != vds->channel.img_h)
  87. {
  88. free(vds->channel.img_h);
  89. vds->channel.img_h = NULL;
  90. }
  91. if(NULL != vds->channel.img_s)
  92. {
  93. free(vds->channel.img_s);
  94. vds->channel.img_s = NULL;
  95. }
  96. }
  97.  
  98. // slist_free(vds_list);
  99. }
  100. vds_free(vds_list);
  101. slist_free(vds_list);

工作总结(一):Linux C的更多相关文章

  1. 工作常用的linux/mysql/php/工具命令

    工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...

  2. 工作中常用Linux命令--服务器运维

    工作中常用Linux命令--服务器运维 lsof查看端口使用情况 lsof -i:8080更多lsof命令使用说明:http://www.cnblogs.com/peida/archive/2013/ ...

  3. 工作笔记 之 Linux服务搭建

    No.1 linux环境下安装nginx步骤 Nginx (engine x) 是一款轻量级的Web 服务器.反向代理服务器.电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行. ...

  4. Xshell~工作中访问Linux服务器

    1.下载Xshell 下载地址:https://xshell.en.softonic.com/ 2.安装(无特殊修改,自行安装即可) 3.使用 登录(1.新建->输入主机IP,点击确定-> ...

  5. 工作中常用Linux命令

    建立软链接  ln -s      例:ln -s b a 解释:把文件夹a和文件夹b关联起来,访问文件夹a,实际访问的是问价夹b 删除软连接  rm -rf a  直接删掉a文件夹跟a和b的软连接. ...

  6. 清华申请退学博士作品:完全用Linux工作

    http://www.cnblogs.com/cbscan/articles/3252872.html 下文地址 http://blog.oldboyedu.com/use-linux/ 按: 尽管我 ...

  7. 王垠:完全用Linux工作

    来自: Zentaur(alles klar) 录一篇旧文 作者:王垠 完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作 ...

  8. Linux学习之路一计算机是如何工作的

    初次接触MOOC课堂,里面有个很牛X的老师教Linux,恰好自己有兴趣学,顾有了此系列学习博文. 第一讲   计算机是如何工作的 学习Linux,涉及到了C语言和汇编以及操作系统的知识,顾第一讲要讲讲 ...

  9. 完全用 GNU/Linux 工作(转)

    转自:http://www.chinaunix.net/old_jh/4/16102.html 看到一半,实在太长,但已觉得很好,转来分享一下. 完全用 GNU/Linux 工作 - 摈弃 Windo ...

  10. 清华申请退学博士作品:完全用Linux工作,凸Windows

    清华申请退学博士作品:完全用Linux工作 按尽管我们已经不习惯看长篇大论, 但我还是要说, 这是一篇值得你从头读到尾的长篇文章. 2005年9月22日,清华在读博士生王垠在水木社区BLOG上发表了& ...

随机推荐

  1. NPOI创建DOCX常用操作

    1.  创建文档 XWPFDocument m_Docx = new XWPFDocument(); 2.  页面设置 //1‘=1440twip=25.4mm=72pt(磅point)=96px(像 ...

  2. [z]分区truncate操作的介绍及对全局索引和空间释放影响的案例解析

    [z]https://www.2cto.com/database/201301/181226.html 环境: [sql] [oracle@localhost ~]$ uname -r 2.6.18- ...

  3. 安装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 ...

  4. Linux移植之make uImage编译过程分析

    编译出uboot可以运行的linux内核代码的命令是make uImage,下面详细介绍下生成linux-2.6.22.6/arch/arm/boot/uImage的过程: 1.vmlinux.Ima ...

  5. 软件开发中 SQL SERVER 任务的用法

    在软件开发中,经常性会用到定时任务.这个时候你可能会想到线程.但是事实中,线程方法比较麻烦.容易出错,资源竞争等问题,设计起来让你很头痛. 现在给大家提供一个新的思路,用SQL SERVER 的任务管 ...

  6. 探索未知种族之osg类生物---呼吸分解之事件循环三

    那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...

  7. 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 ...

  8. lua 2.2 变种

    1.修改 ~= 操作符为 != 2.取消 --[[ ]] 多行注释语法 下载源码

  9. c sharp multithreading

    1.  静态方法 using System; using System.Threading; namespace PlusThread { class Program { static void Ma ...

  10. PropertyPlaceholderConfigurer

    PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...