Previous section To contents Next section

14.8 Yp

This module is an interface to the Yellow Pages functions. Yp is also known as NIS (Network Information System) and is most commonly used to distribute passwords and similar information within a network.

FUNCTION
Yp.default_yp_domain - get the default Yp domain

SYNTAX
string default_yp_domain();

DESCRIPTION
Returns the default yp-domain.

CLASS
Yp.YpDomain - class representing an Yp domain

SYNTAX
object(Yp.YpDomain) Yp.YpDomain(string|void domain);

DESCRIPTION
This creates a new YpDomain object.

If there is no YP server available for the domain, this function call will block until there is one. If no server appears in about ten minutes or so, an error will be returned. The timeout is not configurable from the C interface to Yp either.

If no domain is given, the default domain will be used. (As returned by Yp.default_yp_domain)

METHOD
Yp.YpDomain.bind - bind this object to another domain

SYNTAX
void bind(string|void domain);

DESCRIPTION
Re-bind the object to another (or the same) domain. If no domain is given, the default domain will be used.

METHOD
Yp.YpDomain.match - match a key in a map

SYNTAX
string match(string map, string key);

DESCRIPTION
If 'map' does not exist, an error will be generated. Otherwise the string matching the key will be returned. If there is no such key in the map, 0 will be returned.

arguments is the map Yp-map to search in. This must be a full map name, for example, you should use passwd.byname instead of just passwd. key is the key to search for. The key must match exactly, no pattern matching of any kind is done.

EXAMPLE
object dom = Yp.YpDomain();
write(dom->match("passwd.byname", "root"));

METHOD
Yp.YpDomain.all - return the whole map

SYNTAX
mapping(string:string) all(string map);

DESCRIPTION
Returns the whole map as a mapping. map is the YP-map to search in. This must be the full map name, you have to use passwd.byname instead of just passwd.

METHOD
Yp.YpDomain.map - call a function for each entry in an Yp map

SYNTAX
void map(string map, function(string,string:void) over);

DESCRIPTION
For each entry in 'map', call the function(s) specified by 'over'. Over will get two arguments, the first being the key, and the second the value. map is the YP-map to search in. This must be the full map name, as an example, passwd.byname instead of just passwd.

METHOD
Yp.YpDomain.server - find an Yp server

SYNTAX
string server(string map);

DESCRIPTION
Returns the hostname of the server serving the map map. map is the YP-map to search in. This must be the full map name, as an example, passwd.byname instead of just passwd.

METHOD
Yp.YpDomain.order - get the 'order' for specified map

SYNTAX
int order(string map);

DESCRIPTION
Returns the 'order' number for the map map. This is usually a time_t (see the global function time()). When the map is changed, this number will change as well. map is the YP-map to search in. This must be the full map name, as an example, passwd.byname instead of just passwd.

CLASS
Yp.YpMap - class representing one Yp map

SYNTAX
object(Yp.YpMap) Yp.Ypmap(string map, string|void domain);

DESCRIPTION
This creates a new YpMap object.

If there is no YP server available for the domain, this function call will block until there is one. If no server appears in about ten minutes or so, an error will be returned. The timeout is not configurable from the C-yp interface either. map is the YP-map to bind to. This must be the full map name, as an example, passwd.byname instead of just passwd. If no domain is specified, the default domain will be used. This is usually best.

METHOD
Yp.YpMap.match - find key in map

SYNTAX
string match(string key)
; string Yp.YpMap[string key];

DESCRIPTION
Search for the key key. If there is no key in the map, 0 will be returned, otherwise the string matching the key will be returned. key must match exactly, no pattern matching of any kind is done.

METHOD
Yp.YpMap.all - return the whole map as a mapping

SYNTAX
mapping(string:string) all();
(mapping) Yp.YpMap;

DESCRIPTION
Returns the whole map as a mapping.

METHOD
Yp.YpMap.map - call a function for each entry in the map

SYNTAX
void map(function(string,string:void) over);

DESCRIPTION
For each entry in the map, call the function(s) specified by 'over'. The function will be called like 'void over(string key, string value)'.

METHOD
Yp.YpMap.server - find what server servers this map

SYNTAX
string server();

DESCRIPTION
Returns the hostname of the server serving this map.

METHOD
Yp.YpMap.order - find the 'order' of this map

SYNTAX
int order();

DESCRIPTION
Returns the 'order' number for this map. This is usually a time_t (see the global function time())

METHOD
Yp.YpMap._sizeof - return the number of entries in the map

SYNTAX
int sizeof(Yp.YpMap);

DESCRIPTION
Returns the number of entries in the map. This is equivalent to sizeof((mapping)map);

METHOD
Yp.YpMap._indices - return the indices from the map

SYNTAX
array(string) indices(Yp.YpMap)

DESCRIPTION
Returns the indices of the map. If indices is called first, values must be called immediately after. If values is called first, it is the other way around.

SEE ALSO
Yp.YpMap->_values

METHOD
Yp.YpMap._values - return the values from the map

SYNTAX
array(string) values(Yp.Ypmap)

DESCRIPTION
Returns the values of the map. If values is called first, indices must be called immediately after. If indices is called first, it is the other way around.

SEE ALSO
Yp.YpMap->_indices
Here is an example program using the Yp module, it lists users and their GECOS field from the Yp map "passwd.byname" if your system uses Yp.
import Yp;

void print_entry(string key, string val)
{
    val = (val/":")[4];
    if(strlen(val))
    {
        string q = ".......... ";
        werror(key+q[strlen(key)..]+val+"\n");
    }
}
    
void main(int argc, array(string) argv)
{
    object (YpMap) o = YpMap("passwd.byname");

    werror("server.... "+ o->server() + "\n"
     "age....... "+ (-o->order()+time()) + "\n"
     "per....... "+ o["per"] + "\n"
     "size...... "+ sizeof(o) + "\n");
    
    o->map(print_entry); // Print username/GECOS pairs
}

Previous section To contents Next section