csgetval.c
来自「这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易」· C语言 代码 · 共 60 行
C
60 行
#include <u.h>#include <libc.h>#include <bio.h>#include <ndb.h>#include <ndbhf.h>/* * search for a tuple that has the given 'attr=val' and also 'rattr=x'. * copy 'x' into 'buf' and return the whole tuple. * * return 0 if not found. */Ndbtuple*csgetval(char *attr, char *val, char *rattr, char *buf){ Ndbtuple *t, *first, *last; int n, fd, linefound; char line[1024]; buf[0] = 0; fd = open("/net/cs", ORDWR); if(fd < 0) return 0; fprint(fd, "!%s=%s", attr, val); seek(fd, 0, 0); first = last = 0; linefound = 0; for(;;){ n = read(fd, line, sizeof(line)-2); if(n <= 0) break; line[n] = '\n'; line[n+1] = 0; t = _ndbparseline(line); if(t == 0) continue; if(first) last->entry = t; else first = t; last = t; while(last->entry) last = last->entry; for(; t; t = t->entry){ if(buf[0] == 0 || linefound == 0) if(strcmp(rattr, t->attr) == 0) strcpy(buf, t->val); if(linefound == 0) if(strcmp(attr, t->attr) == 0) linefound = 1; } } close(fd); return first;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?