void parse(char *file) { int i; Ndb *db; Ndbtuple *t, *nt, *tt, *ipnett; char *p; db = ndbopen(file); if(db == 0) exits("no database"); while(t = ndbparse(db)){ for(nt = t; nt; nt = nt->entry){ if(strcmp(nt->attr, "ip") == 0) break; if(strcmp(nt->attr, "flavor") == 0 && strcmp(nt->val, "console") == 0) return; } if(nt == 0){ ndbfree(t); continue; } /* dump anything not on our nets */ ipnett = 0; for(tt = t; tt; tt = tt->entry){ if(strcmp(tt->attr, "ipnet") == 0){ ipnett = tt; break; } if(strcmp(tt->attr, "dom") == 0){ i = strlen(tt->val); p = tt->val+i-domnamlen; if(p >= tt->val && strcmp(p, domname) == 0) break; } } if(tt == 0){ ndbfree(t); continue; } for(; nt; nt = nt->entry){ if(strcmp(nt->attr, "ip") != 0) continue; x[nx].it = nt; x[nx].nt = ipnett; x[nx++].t = t; } } }
/* * get the system name */ static void ipid(void) { uint8_t addr[6]; struct ndbtuple *t, *tt; char *p, *attr; struct ndbs s; int f; char buf[Maxpath]; /* use environment, ether addr, or ipaddr to get system name */ if (mysysname == 0) { /* * environment has priority. * * on the sgi power the default system name * is the ip address. ignore that. * */ p = getenv("sysname"); if (p && *p) { attr = ipattr(p); if (strcmp(attr, "ip") != 0) mysysname = strdup(p); } /* * the /net/ndb contains what the network * figured out from DHCP. use that name if * there is one. */ if (mysysname == 0 && netdb != NULL) { ndbreopen(netdb); for (tt = t = ndbparse(netdb); t != NULL; t = t->entry) { if (strcmp(t->attr, "sys") == 0) { mysysname = strdup(t->val); break; } } ndbfree(tt); } /* next network database, ip address, and ether address to find * a name */ if (mysysname == 0) { t = NULL; if (isvalidip(ipa)) free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t)); if (t == NULL) { for (f = 0; f < 3; f++) { snprintf(buf, sizeof(buf), "%s/ether%d", mntpt, f); if (myetheraddr(addr, buf) < 0) continue; snprintf(eaddr, sizeof(eaddr), "%E", addr); free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t)); if (t != NULL) break; } } for (tt = t; tt != NULL; tt = tt->entry) { if (strcmp(tt->attr, "sys") == 0) { mysysname = strdup(tt->val); break; } } ndbfree(t); } /* nothing else worked, use the ip address */ if (mysysname == 0 && isvalidip(ipa)) mysysname = strdup(ipaddr); /* set /dev/sysname if we now know it */ if (mysysname) { f = open("/dev/sysname", O_RDWR); if (f >= 0) { write(f, mysysname, strlen(mysysname)); close(f); } } } }
void main(int argc, char **argv) { Ndbtuple *t, *nt; int n; Dir *d; uint8_t buf[8]; char file[128]; int fd; uint32_t off; uint8_t *p; if(argc != 3){ fprint(2, "usage: mkhash file attribute\n"); exits("usage"); } db = ndbopen(argv[1]); if(db == 0){ fprint(2, "mkhash: can't open %s\n", argv[1]); exits(syserr()); } /* try a bigger than normal buffer */ Binits(&db->b, Bfildes(&db->b), OREAD, nbuf, sizeof(nbuf)); /* count entries to calculate hash size */ n = 0; while(nt = ndbparse(db)){ for(t = nt; t; t = t->entry){ if(strcmp(t->attr, argv[2]) == 0) n++; } ndbfree(nt); } /* allocate an array large enough for worst case */ hlen = 2*n+1; n = hlen*NDBPLEN + hlen*2*NDBPLEN; ht = mallocz(n, 1); if(ht == 0){ fprint(2, "mkhash: not enough memory\n"); exits(syserr()); } for(p = ht; p < &ht[n]; p += NDBPLEN) NDBPUTP(NDBNAP, p); nextchain = hlen*NDBPLEN; /* create the in core hash table */ Bseek(&db->b, 0, 0); off = 0; while(nt = ndbparse(db)){ for(t = nt; t; t = t->entry){ if(strcmp(t->attr, argv[2]) == 0) enter(t->val, off); } ndbfree(nt); off = Boffset(&db->b); } /* create the hash file */ snprint(file, sizeof(file), "%s.%s", argv[1], argv[2]); fd = create(file, ORDWR, 0664); if(fd < 0){ fprint(2, "mkhash: can't create %s\n", file); exits(syserr()); } NDBPUTUL(db->mtime, buf); NDBPUTUL(hlen, buf+NDBULLEN); if(write(fd, buf, NDBHLEN) != NDBHLEN){ fprint(2, "mkhash: writing %s\n", file); exits(syserr()); } if(write(fd, ht, nextchain) != nextchain){ fprint(2, "mkhash: writing %s\n", file); exits(syserr()); } close(fd); /* make sure file didn't change while we were making the hash */ d = dirstat(argv[1]); if(d == nil || d->qid.path != db->qid.path || d->qid.vers != db->qid.vers){ fprint(2, "mkhash: %s changed underfoot\n", argv[1]); remove(file); exits("changed"); } exits(0); }