akaros/user/ndblib/csgetval.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <stdio.h>
  12#include <parlib/parlib.h>
  13#include <unistd.h>
  14#include <signal.h>
  15#include <fcntl.h>
  16#include <iplib/iplib.h>
  17#include <ndblib/ndb.h>
  18
  19/*
  20 *  search for a tuple that has the given 'attr=val' and also 'rattr=x'.
  21 *  copy 'x' into 'buf' and return the whole tuple.
  22 *
  23 *  return 0 if not found.
  24 */
  25char*
  26csgetvalue(char *netroot, char *attr, char *val, char *rattr,
  27           struct ndbtuple **pp)
  28{
  29        struct ndbtuple *t, *first, *last;
  30        int n, linefound;
  31        char line[1024];
  32        int fd;
  33        int oops = 0;
  34        char *rv;
  35
  36        if(pp)
  37                *pp = NULL;
  38        rv = NULL;
  39
  40        if(netroot)
  41                snprintf(line, sizeof(line), "%s/cs", netroot);
  42        else
  43                strcpy(line, "/net/cs");
  44        fd = open(line, O_RDWR);
  45        if(fd < 0)
  46                return 0;
  47        lseek(fd, 0, 0);
  48        snprintf(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr);
  49        if(write(fd, line, strlen(line)) < 0){
  50                close(fd);
  51                return 0;
  52        }
  53        lseek(fd, 0, 0);
  54
  55        first = last = 0;
  56        linefound = 0;
  57        for(;;){
  58                n = read(fd, line, sizeof(line)-2);
  59                if(n <= 0)
  60                        break;
  61                line[n] = '\n';
  62                line[n+1] = 0;
  63
  64                t = _ndbparseline(line);
  65                if(t == 0)
  66                        continue;
  67                if(first)
  68                        last->entry = t;
  69                else
  70                        first = t;
  71                last = t;
  72
  73                while(last->entry)
  74                        last = last->entry;
  75
  76                for(; t; t = t->entry){
  77                        if(linefound == 0){
  78                                if(strcmp(rattr, t->attr) == 0){
  79                                        linefound = 1;
  80                                        rv = strdup(t->val);
  81                                }
  82                        }
  83                }
  84        }
  85        close(fd);
  86
  87        if(oops){
  88                werrstr("buffer too short");
  89                ndbfree(first);
  90                return NULL;
  91        }
  92
  93        if(pp){
  94                *pp = first;
  95        } else
  96                ndbfree(first);
  97
  98        return rv;
  99}
 100
 101struct ndbtuple*
 102csgetval(char *netroot, char *attr, char *val, char *rattr, char *buf)
 103{
 104        struct ndbtuple *t;
 105        char *p;
 106
 107        p = csgetvalue(netroot, attr, val, rattr, &t);
 108        if(p == NULL){
 109                if(buf != NULL)
 110                        *buf = 0;
 111        } else {
 112                if(buf != NULL){
 113                        strncpy(buf, p, Ndbvlen-1);
 114                        buf[Ndbvlen-1] = 0;
 115                }
 116                free(p);
 117        }
 118        ndbsetmalloctag(t, getcallerpc(&netroot));
 119        return t;
 120}
 121