Apple Enterprise - NeXTanswers Support Archive
/*
* nilist
*
* usage: nilist domain directory
* compile with: cc -o nilist nilist.c
*
* Similar to "niutil -list domain directory [key]"
*
* Sample output:
*
* myhost> nilist . /users
* 25 "root"
* 27 "nobody"
* 29 "agent"
* 31 "daemon"
* 33 "uucp"
* 35 "news"
* 37 "sybase"
* 39 "me"
* myhost> nilist . /users uid
* 25 "0"
* 27 "-2"
* 29 "1"
* 31 "1"
* 33 "4"
* 35 "6"
* 37 "8"
* 39 "20"
*
* Author: Marc Majka
* You may freely copy, distribute and reuse the code in this example.
* NeXT disclaims any warranty of any kind, expressed or implied, as to
* its fitness for any particular use.
*/
#import <ansi/stdio.h>
#import <bsd/errno.h>
#import <netinfo/ni.h>
main(int argc, char *argv[]) {
int en, vn;
ni_entrylist elist;
ni_entry entry;
ni_namelist values;
void *dom;
ni_id dir;
ni_status ret;
char listkey[256];
/* check usage */
if (argc < 3) {
fprintf(stderr, "usage: %s domain directory [key]\n", argv[0]);
exit(1);
}
/* argv[1] should be a domain name */
ret = ni_open(NULL, argv[1], &dom);
if (ret != NI_OK) {
fprintf(stderr, "ni_open: %s\n", ni_error(ret));
exit(1);
}
/* argv[2] should be a directory specification */
ret = ni_pathsearch(dom, &dir, argv[2]);
if (ret != NI_OK) {
fprintf(stderr, "ni_pathsearch: %s\n", ni_error(ret));
exit(1);
}
/* if there's a third argument, it's a property key */
/* by default, we'll list directories by name */
if (argc > 3) {
strcpy(listkey, argv[3]);
}
else {
strcpy(listkey, "name");
}
/* get the values of the specified key for all subdirectories */
ret = ni_list(dom, &dir, listkey, &elist);
if (ret != NI_OK) {
fprintf(stderr, "ni_list: %s\n", ni_error(ret));
exit(1);
}
/* for each entry */
for (en = 0; en < elist.ni_entrylist_len; en++) {
entry = elist.ni_entrylist_val[en];
/* print the directory ID */
printf("%ld\t", entry.id);
/* entries contain a pointer to a namelist of values */
/* if there are no values for the key, the pointer is null */
if (entry.names != NULL) {
values = *entry.names;
/* for each value in the namelist for this property */
for (vn = 0; vn < values.ni_namelist_len; vn++) {
/* print the value enclosed in quotes */
printf(" \"%s\"", values.ni_namelist_val[vn]);
}
}
printf("\n");
}
/* clean up */
ni_free(dom);
exit(0);
}