ipquery.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ndb.h>
  13. #include <ip.h>
  14. /*
  15. * search the database for matches
  16. */
  17. void
  18. usage(void)
  19. {
  20. fprint(2, "usage: ipquery [-f ndb-root] attr value rattribute\n");
  21. exits("usage");
  22. }
  23. void
  24. search(Ndb *db, char *attr, char *val, char **rattr, int nrattr)
  25. {
  26. Ndbtuple *t, *tt;
  27. tt = ndbipinfo(db, attr, val, rattr, nrattr);
  28. for(t = tt; t; t = t->entry)
  29. print("%s=%s ", t->attr, t->val);
  30. print("\n");
  31. ndbfree(tt);
  32. }
  33. void
  34. main(int argc, char **argv)
  35. {
  36. Ndb *db;
  37. char *dbfile = 0;
  38. ARGBEGIN{
  39. case 'f':
  40. dbfile = ARGF();
  41. break;
  42. default:
  43. usage();
  44. break;
  45. }ARGEND;
  46. if(argc < 3)
  47. usage();
  48. db = ndbopen(dbfile);
  49. if(db == 0){
  50. fprint(2, "no db files\n");
  51. exits("no db");
  52. }
  53. search(db, argv[0], argv[1], argv+2, argc-2);
  54. ndbclose(db);
  55. exits(0);
  56. }