关键词搜索

源码搜索 ×
×

漫话Redis源码之十九

发布2021-11-28浏览796次

详情内容

看一下这个代码片段,再次展现了linux的严谨,既考虑了linux, 也考虑了 APPLE

  1. #if __linux__
  2. if (!(tmp = strdup(program_invocation_name)))
  3. goto syerr;
  4. program_invocation_name = tmp;
  5. if (!(tmp = strdup(program_invocation_short_name)))
  6. goto syerr;
  7. program_invocation_short_name = tmp;
  8. #elif __APPLE__
  9. if (!(tmp = strdup(getprogname())))
  10. goto syerr;
  11. setprogname(tmp);
  12. #endif
  13. /* Now make a full deep copy of the environment and argv[] */
  14. if ((error = spt_copyenv(envc, envp)))
  15. goto error;
  16. if ((error = spt_copyargs(argc, argv)))
  17. goto error;
  18. SPT.nul = nul;
  19. SPT.base = base;
  20. SPT.end = end;
  21. return;
  22. syerr:
  23. error = errno;
  24. error:
  25. SPT.error = error;
  26. } /* spt_init() */
  27. #ifndef SPT_MAXTITLE
  28. #define SPT_MAXTITLE 255
  29. #endif
  30. void setproctitle(const char *fmt, ...) {
  31. char buf[SPT_MAXTITLE + 1]; /* use buffer in case argv[0] is passed */
  32. va_list ap;
  33. char *nul;
  34. int len, error;
  35. if (!SPT.base)
  36. return;
  37. if (fmt) {
  38. va_start(ap, fmt);
  39. len = vsnprintf(buf, sizeof buf, fmt, ap);
  40. va_end(ap);
  41. } else {
  42. len = snprintf(buf, sizeof buf, "%s", SPT.arg0);
  43. }
  44. if (len <= 0)
  45. { error = errno; goto error; }
  46. if (!SPT.reset) {
  47. memset(SPT.base, 0, SPT.end - SPT.base);
  48. SPT.reset = 1;
  49. } else {
  50. memset(SPT.base, 0, spt_min(sizeof buf, SPT.end - SPT.base));
  51. }
  52. len = spt_min(len, spt_min(sizeof buf, SPT.end - SPT.base) - 1);
  53. memcpy(SPT.base, buf, len);
  54. nul = &SPT.base[len];
  55. if (nul < SPT.nul) {
  56. *SPT.nul = '.';
  57. } else if (nul == SPT.nul && &nul[1] < SPT.end) {
  58. *SPT.nul = ' ';
  59. *++nul = '\0';
  60. }
  61. return;
  62. error:
  63. SPT.error = error;
  64. } /* setproctitle() */
  65. #endif /* __linux || __APPLE__ */
  66. #endif /* !HAVE_SETPROCTITLE */
  67. #ifdef SETPROCTITLE_TEST_MAIN
  68. int main(int argc, char *argv[]) {
  69. spt_init(argc, argv);
  70. printf("SPT.arg0: [%p] '%s'\n", SPT.arg0, SPT.arg0);
  71. printf("SPT.base: [%p] '%s'\n", SPT.base, SPT.base);
  72. printf("SPT.end: [%p] (%d bytes after base)'\n", SPT.end, (int) (SPT.end - SPT.base));
  73. return 0;
  74. }
  75. #endif

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载