]> git.saurik.com Git - apple/libinfo.git/blobdiff - netinfo.subproj/ni_util.c
Libinfo-129.4.tar.gz
[apple/libinfo.git] / netinfo.subproj / ni_util.c
index 2f6f81d9e93e5af6bf1bde5af8e3a37ea0bf7742..e99f78ce21f0374e892d061ac8b5914b902a0af5 100644 (file)
@@ -436,22 +436,26 @@ ni_find(void **dom, ni_id *nid, ni_name dirname, unsigned int timeout)
        void *d, *p;
        ni_id n;
        ni_status status;
+       struct sockaddr_in addr;
 
        *dom = NULL;
        nid->nii_object = NI_INDEX_NULL;
        nid->nii_instance = NI_INDEX_NULL;
        
-       status = ni_open(NULL, ".", &d);
-       if (status != NI_OK) return status;
+       memset(&addr, 0, sizeof(struct sockaddr_in));
+       addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 
-       if (timeout > 0)
-       {
-               ni_setreadtimeout(d, timeout);
-               ni_setabort(d, 1);
-       }
+       d = ni_connect(&addr, "local");
+       if (d == NULL) return NI_FAILED;
 
        while (d != NULL)
        {
+               if (timeout > 0)
+               {
+                       ni_setreadtimeout(d, timeout);
+                       ni_setabort(d, 1);
+               }
+
                status = ni_pathsearch(d, &n, dirname);
                if (status == NI_OK)
                {
@@ -478,11 +482,7 @@ ni_search(void *handle, ni_id *dir, ni_name name, ni_name expr, int flags, ni_en
        ni_namelist *nl;
        ni_status status;
 
-       /* get subdirectory list */
-       NI_INIT(&el);
-       status = ni_list(handle, dir, name, &el);
-       if (status != NI_OK) return status;
-
+       /* compile the regular expression */
        cexp = (regex_t *)malloc(sizeof(regex_t));
        memset(cexp, 0, sizeof(regex_t));
        i = regcomp(cexp, expr, flags);
@@ -492,17 +492,26 @@ ni_search(void *handle, ni_id *dir, ni_name name, ni_name expr, int flags, ni_en
                return NI_FAILED;
        }
 
+       /* get subdirectory list */
+       NI_INIT(&el);
+       status = ni_list(handle, dir, name, &el);
+       if (status != NI_OK)
+       {
+               regfree(cexp);
+               free(cexp);
+               return status;
+       }
+
        for (i = 0; i < el.ni_entrylist_len; i++)
        {
                if (el.ni_entrylist_val[i].names == NULL) continue;
 
                nl = el.ni_entrylist_val[i].names;
 
+               found = 0;
                for (j = 0; j < nl->ni_namelist_len; j++)
                {
-                       found = 0;
                        if (regexec(cexp, nl->ni_namelist_val[j], 0, NULL, 0) != 0) continue;
-
                        found = 1;
                        break;
                }