home *** CD-ROM | disk | FTP | other *** search
- /*
- * nichildren
- *
- * usage: nichildren domain directory
- * compile with: cc -o nichildren nichildren.c
- *
- * This program lists the id and instance number of each of the
- * child directories of a given directory. For each child, it
- * looks up the parent and prints its id and instance too.
- *
- * Sample output:
- *
- * myhost> nichildren . /users
- * id = 25 instance = 4 id = 24 instance = 9
- * id = 27 instance = 2 id = 24 instance = 9
- * id = 29 instance = 2 id = 24 instance = 9
- * id = 31 instance = 2 id = 24 instance = 9
- * id = 33 instance = 2 id = 24 instance = 9
- * id = 35 instance = 2 id = 24 instance = 9
- * id = 37 instance = 2 id = 24 instance = 9
- * id = 39 instance = 4 id = 24 instance = 9
- *
- * 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 in;
- ni_idlist ilist;
- void *dom;
- ni_id dir, childdir, parentdir;
- ni_status ret;
- ni_index parentid;
-
- /* check usage */
- if (argc < 3) {
- fprintf(stderr, "usage: %s domain directory\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);
- }
-
- /* find subdirectories */
- ret = ni_children(dom, &dir, &ilist);
- if (ret != NI_OK) {
- fprintf(stderr, "ni_children: %s\n", ni_error(ret));
- exit(1);
- }
-
- /* for each entry */
- for (in = 0; in < ilist.ni_idlist_len; in++) {
- childdir.nii_object = ilist.ni_idlist_val[in];
-
- /* fetch the instance number for this directory */
- ret = ni_self(dom, &childdir);
- if (ret != NI_OK) {
- fprintf(stderr, "ni_self: %s\n", ni_error(ret));
- exit(1);
- }
-
- /* print the directory ID and instance number */
- printf("id = %ld instance = %ld\t",
- childdir.nii_object, childdir.nii_instance);
-
- /* now back up and find the child's parent */
- ret = ni_parent(dom, &childdir, &parentid);
- if (ret != NI_OK) {
- fprintf(stderr, "ni_parent: %s\n", ni_error(ret));
- exit(1);
- }
-
- /* fetch the instance number for the parent */
- parentdir.nii_object = parentid;
- ret = ni_self(dom, &parentdir);
- if (ret != NI_OK) {
- fprintf(stderr, "ni_self: %s\n", ni_error(ret));
- exit(1);
- }
-
- /* print the parent's id and instance next to the child */
- printf("id = %ld instance = %ld\n",
- parentdir.nii_object, parentdir.nii_instance);
- }
-
- /* clean up */
- ni_free(dom);
-
- exit(0);
- }