亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3204 | 回復(fù): 0
打印 上一主題 下一主題

[C] open函數(shù)返回值 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2014-08-22 16:39 |只看該作者 |倒序瀏覽
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <assert.h>
  4. #include <stdarg.h>
  5. #include <stdint.h>
  6. #include <time.h>
  7. #include <limits.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stddef.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <fcntl.h>

  16. #define false 0
  17. #define true 1


  18. #define TCFILEMODE     00644             // permission of a creating file
  19. #define TCIOBUFSIZ     16384             // size of an I/O buffer
  20. #define TCXSTRUNIT     12                // allocation unit size of an extensible string

  21. #define MYMALLOC       malloc
  22. #define MYCALLOC       calloc
  23. #define MYREALLOC      realloc
  24. #define MYFREE         free

  25. /* Alias of `tcxstrsize'. */
  26. #define TCXSTRSIZE(TC_xstr) \
  27.   ((TC_xstr)->size)

  28. /* Alias of `tcrealloc'. */
  29. #if defined(_MYFASTEST)
  30. #define TCREALLOC(TC_res, TC_ptr, TC_size) \
  31.   do { \
  32.     (TC_res) = MYREALLOC((TC_ptr), (TC_size)); \
  33.   } while(false)
  34. #else
  35. #define TCREALLOC(TC_res, TC_ptr, TC_size) \
  36.   do { \
  37.     if(!((TC_res) = MYREALLOC((TC_ptr), (TC_size)))) ; \
  38.   } while(false)
  39. #endif

  40. /* Alias of `tcmalloc'. */
  41. #if defined(_MYFASTEST)
  42. #define TCMALLOC(TC_res, TC_size) \
  43.   do { \
  44.     (TC_res) = MYMALLOC(TC_size); \
  45.   } while(false)
  46. #else
  47. #define TCMALLOC(TC_res, TC_size) \
  48.   do { \
  49.     if(!((TC_res) = MYMALLOC(TC_size))); \
  50.   } while(false)
  51. #endif

  52. /* Alias of `tcfree'. */
  53. #define TCFREE(TC_ptr) \
  54.   do { \
  55.     MYFREE(TC_ptr); \
  56.   } while(false)


  57. /* Convert an extensible string object into a usual allocated region. */
  58. typedef struct {                         /* type of structure for an extensible string object */
  59.   char *ptr;                             /* pointer to the region */
  60.   int size;                              /* size of the region */
  61.   int asize;                             /* size of the allocated region */
  62. } TCXSTR;


  63. /* Alias of `tcxstrcat'. */
  64. #define TCXSTRCAT(TC_xstr, TC_ptr, TC_size) \
  65.   do { \
  66.     int TC_mysize = (TC_size); \
  67.     int TC_nsize = (TC_xstr)->size + TC_mysize + 1; \
  68.     if((TC_xstr)->asize < TC_nsize){ \
  69.       while((TC_xstr)->asize < TC_nsize){ \
  70.         (TC_xstr)->asize *= 2; \
  71.         if((TC_xstr)->asize < TC_nsize) (TC_xstr)->asize = TC_nsize; \
  72.       } \
  73.       TCREALLOC((TC_xstr)->ptr, (TC_xstr)->ptr, (TC_xstr)->asize); \
  74.     } \
  75.     memcpy((TC_xstr)->ptr + (TC_xstr)->size, (TC_ptr), TC_mysize); \
  76.     (TC_xstr)->size += TC_mysize; \
  77.     (TC_xstr)->ptr[(TC_xstr)->size] = '\0'; \
  78.   } while(false)


  79. void *tcxstrtomalloc(TCXSTR *xstr){
  80.   assert(xstr);
  81.   char *ptr;
  82.   ptr = xstr->ptr;
  83.   TCFREE(xstr);
  84.   return ptr;
  85. }


  86. /* Create an extensible string object. */
  87. TCXSTR *tcxstrnew(void){
  88.   TCXSTR *xstr;
  89.   xstr = malloc(sizeof(*xstr));
  90.   xstr->ptr = malloc(TCXSTRUNIT);
  91.   xstr->size = 0;
  92.   xstr->asize = TCXSTRUNIT;
  93.   xstr->ptr[0] = '\0';
  94.   return xstr;
  95. }

  96. /* Get the larger value of two integers. */
  97. long tclmax(long a, long b){
  98.   return (a > b) ? a : b;
  99. }


  100. /* Get the lesser value of two integers. */
  101. long tclmin(long a, long b){
  102.   return (a < b) ? a : b;
  103. }

  104. /* Read whole data of a file. */
  105. void *tcreadfile(const char *path, int limit, int *sp){
  106.   int fd = path ? open(path, O_RDONLY, TCFILEMODE) : 0;
  107.   if(fd == -1) return NULL;
  108.   if(fd == 0){
  109.     TCXSTR *xstr = tcxstrnew();
  110.     char buf[TCIOBUFSIZ];
  111.     limit = limit > 0 ? limit : INT_MAX;
  112.     int rsiz;
  113.     while((rsiz = read(fd, buf, tclmin(TCIOBUFSIZ, limit))) > 0){
  114.       TCXSTRCAT(xstr, buf, rsiz);
  115.       limit -= rsiz;
  116.     }
  117.     if(sp) *sp = TCXSTRSIZE(xstr);
  118.     return tcxstrtomalloc(xstr);
  119.   }
  120.   struct stat sbuf;
  121.   if(fstat(fd, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)){
  122.     close(fd);
  123.     return NULL;
  124.   }
  125.   limit = limit > 0 ? tclmin((int)sbuf.st_size, limit) : sbuf.st_size;
  126.   char *buf;
  127.   TCMALLOC(buf, sbuf.st_size + 1);
  128.   char *wp = buf;
  129.   int rsiz;
  130.   while((rsiz = read(fd, wp, limit - (wp - buf))) > 0){
  131.     wp += rsiz;
  132.   }
  133.   *wp = '\0';
  134.   close(fd);
  135.   if(sp) *sp = wp - buf;
  136.   return buf;
  137. }


  138. int main()
  139. {
  140.         char *numstr = tcreadfile("/ttserver/ttserver.pid", -1, NULL);
  141. }
復(fù)制代碼
哪位大俠解釋一下open函數(shù)返回0是什么意思啊,為什么等于0的時候要用結(jié)構(gòu)體存取結(jié)構(gòu)體中的內(nèi)容
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP