对于一个副项目,我正在阅读一些 systemd 源代码。我发现里面__pure__函数的用法很混乱。具体来说,为什么变量会存在于函数中?它存储结果但似乎从未真正使用过,并且该函数没有我可以看到的副作用。endswithcg_get_root_pathecg_get_root_pathendswithendswith
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;
}
Run Code Online (Sandbox Code Playgroud)