home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / amd / part02 / info_yp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-10  |  2.5 KB  |  118 lines

  1. /*
  2.  * $Id: info_yp.c,v 5.1 89/11/17 18:20:22 jsp Exp Locker: jsp $
  3.  *
  4.  * Copyright (c) 1989 Jan-Simon Pendry
  5.  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  6.  * Copyright (c) 1989 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * This code is derived from software contributed to Berkeley by
  10.  * Jan-Simon Pendry at Imperial College, London.
  11.  *
  12.  * Redistribution and use in source and binary forms are permitted
  13.  * provided that the above copyright notice and this paragraph are
  14.  * duplicated in all such forms and that any documentation,
  15.  * advertising materials, and other materials related to such
  16.  * distribution and use acknowledge that the software was developed
  17.  * by Imperial College of Science, Technology and Medicine, London, UK.
  18.  * The names of the College and University may not be used to endorse
  19.  * or promote products derived from this software without specific
  20.  * prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  *
  25.  *    %W% (Berkeley) %G%
  26.  */
  27.  
  28. /*
  29.  * Get info from YP map
  30.  */
  31.  
  32. #include "am.h"
  33.  
  34. #ifdef HAS_YP_MAPS
  35. #include <rpcsvc/yp_prot.h>
  36. #include <rpcsvc/ypclnt.h>
  37.  
  38. /*
  39.  * Figure out the yp domain name
  40.  */
  41. static int determine_yp_domain(P_void)
  42. {
  43. static     char default_domain[YPMAXDOMAIN];
  44.  
  45.     if (getdomainname(default_domain, sizeof(default_domain)) < 0) {
  46.         plog(XLOG_ERROR, "getdomainname: %m");
  47.         return EIO;
  48.     }
  49.  
  50.     domain = default_domain;
  51.     if (!*domain) {
  52.         plog(XLOG_ERROR, "YP domain name is not set");
  53.         return ENOENT;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. /*
  60.  * Try to locate a key using Yellow Pages.
  61.  * Modify time is ignored in YP - XXX
  62.  */
  63. int yp_search(m, map, key, val, tp)
  64. mnt_map *m;
  65. char *map;
  66. char *key;
  67. char **val;
  68. time_t *tp;
  69. {
  70.     int outlen;
  71.     int res;
  72.  
  73.     if (!domain) {
  74.         int error = determine_yp_domain();
  75.         if (error)
  76.             return error;
  77.     }
  78.  
  79.     res = yp_match(domain, map, key, strlen(key), val, &outlen);
  80.  
  81.     /*
  82.      * Do something interesting with the return code
  83.      */
  84.     switch (res) {
  85.     case 0:
  86.         return 0;
  87.  
  88.     case YPERR_KEY:
  89.         return ENOENT;
  90.  
  91.     default:
  92.         plog(XLOG_ERROR, "%s: %s", map, yperr_string(res));
  93.         return EIO;
  94.     }
  95. }
  96.  
  97. int yp_init(map)
  98. char *map;
  99. {
  100.     char *name = 0;
  101.  
  102.     if (!domain) {
  103.         int error = determine_yp_domain();
  104.         if (error)
  105.             return error;
  106.     }
  107.  
  108.     /*
  109.      * To see if the map exists, try to find
  110.      * a master for it.
  111.      */
  112.     if (yp_master(domain, map, &name))
  113.         return ENOENT;
  114.     free(name);
  115.     return 0;
  116. }
  117. #endif
  118.