对于副项目,我正在阅读一些系统化的源代码。我发现__pure__
内部的endswith
函数cg_get_root_path
的用法令人困惑。具体来说,为什么变量e
完全存在于cg_get_root_path
函数中?它存储endswith
的结果,但似乎从未真正使用过,并且函数endswith
没有我看到的副作用。
char *endswith(const char *s, const char *postfix) _pure_;
int cg_get_root_path(char **path) {
char *p, *e;
int r;
assert(path);
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
if (r < 0)
return r;
e = endswith(p, "/" SPECIAL_INIT_SCOPE);
if (!e)
e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
if (!e)
e = endswith(p, "/system"); /* even more legacy */
if (e)
*e = 0;
*path = p;
return 0;
}
char* endswith(const char *s, const char *postfix) {
size_t sl, pl;
assert(s);
assert(postfix);
sl = strlen(s);
pl = strlen(postfix);
if (pl == 0)
return (char*) s + sl;
if (sl < pl)
return NULL;
if (memcmp(s + sl - pl, postfix, pl) != 0)
return NULL;
return (char*) s + sl - pl;
}
最终在一行中使用了e
:*e = 0
写入内存。