]>
git.saurik.com Git - apple/libinfo.git/blob - netinfo.subproj/ni_util.c
c2795103fb05e395f6692acb6475c6f4b4b2368b
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Utility routines for netinfo data structures
26 * Copyright (C) 1989 by NeXT, Inc.
28 #include <netinfo/ni.h>
44 MM_GROW_ARRAY(cl
->niil_val
, cl
->niil_len
);
45 for (i
= cl
->niil_len
; i
> where
; i
--) {
46 cl
->niil_val
[i
] = cl
->niil_val
[i
- 1];
61 for (i
= 0; i
< idlist
->niil_len
; i
++) {
62 if (idlist
->niil_val
[i
] == id
) {
63 for (j
= i
+ 1; j
< idlist
->niil_len
; j
++) {
64 idlist
->niil_val
[j
- 1] = idlist
->niil_val
[j
];
66 MM_SHRINK_ARRAY(idlist
->niil_val
, idlist
->niil_len
--);
79 if (idlist
->niil_val
!= NULL
) {
80 MM_FREE_ARRAY(idlist
->niil_val
, idlist
->niil_len
);
87 const ni_idlist idlist
93 newlist
.niil_len
= idlist
.niil_len
;
94 MM_ALLOC_ARRAY(newlist
.niil_val
, idlist
.niil_len
);
95 for (i
= 0; i
< idlist
.niil_len
; i
++) {
96 newlist
.niil_val
[i
] = idlist
.niil_val
[i
];
104 const ni_property prop
,
110 MM_GROW_ARRAY(pl
->nipl_val
, pl
->nipl_len
);
111 for (i
= pl
->nipl_len
; i
> where
; i
--) {
112 pl
->nipl_val
[i
] = pl
->nipl_val
[i
- 1];
114 pl
->nipl_val
[i
] = ni_prop_dup(prop
);
126 ni_prop_free(&pl
->nipl_val
[which
]);
127 for (i
= which
+ 1; i
< pl
->nipl_len
; i
++) {
128 pl
->nipl_val
[i
- 1] = pl
->nipl_val
[i
];
130 MM_SHRINK_ARRAY(pl
->nipl_val
, pl
->nipl_len
--);
140 if (pl
->nipl_val
== NULL
) {
143 for (i
= 0; i
< pl
->nipl_len
; i
++) {
144 ni_prop_free(&pl
->nipl_val
[i
]);
146 MM_FREE_ARRAY(pl
->nipl_val
, pl
->nipl_len
);
151 ni_proplist_list_free(
152 ni_proplist_list
*pll
157 if (pll
->nipll_val
== NULL
) {
160 for (i
= 0; i
< pll
->nipll_len
; i
++) {
161 ni_proplist_free(&pll
->nipll_val
[i
]);
163 MM_FREE_ARRAY(pll
->nipll_val
, pll
->nipll_len
);
175 newlist
.nipl_len
= pl
.nipl_len
;
176 MM_ALLOC_ARRAY(newlist
.nipl_val
, pl
.nipl_len
);
177 for (i
= 0; i
< pl
.nipl_len
; i
++) {
178 newlist
.nipl_val
[i
].nip_name
= ni_name_dup(pl
.nipl_val
[i
].nip_name
);
179 newlist
.nipl_val
[i
].nip_val
= ni_namelist_dup(pl
.nipl_val
[i
].nip_val
);
186 const ni_proplist pl
,
195 for (i
= 0; i
< pl
.nipl_len
; i
++) {
196 if (ni_name_match(pname
, pl
.nipl_val
[i
].nip_name
)) {
200 nl
= pl
.nipl_val
[i
].nip_val
;
201 for (j
= 0; j
< nl
.ninl_len
; j
++) {
202 if (ni_name_match(pval
, nl
.ninl_val
[j
])) {
209 return (NI_INDEX_NULL
);
215 const ni_property prop
220 newprop
.nip_name
= ni_name_dup(prop
.nip_name
);
221 newprop
.nip_val
= ni_namelist_dup(prop
.nip_val
);
230 ni_name_free(&prop
->nip_name
);
231 ni_namelist_free(&prop
->nip_val
);
240 return (strcmp(nm1
, nm2
) == 0);
248 return (strcpy(malloc(strlen(nm
) + 1), nm
));
271 newlist
.ninl_len
= nl
.ninl_len
;
272 MM_ALLOC_ARRAY(newlist
.ninl_val
, newlist
.ninl_len
);
273 for (i
= 0; i
< nl
.ninl_len
; i
++) {
274 newlist
.ninl_val
[i
] = ni_name_dup(nl
.ninl_val
[i
]);
286 if (nl
->ninl_val
== NULL
) {
289 for (i
= 0; i
< nl
->ninl_len
; i
++) {
290 ni_name_free(&nl
->ninl_val
[i
]);
292 MM_FREE_ARRAY(nl
->ninl_val
, nl
->ninl_len
);
305 MM_GROW_ARRAY(nl
->ninl_val
, nl
->ninl_len
);
306 for (i
= nl
->ninl_len
; i
> where
; i
--) {
307 nl
->ninl_val
[i
] = nl
->ninl_val
[i
- 1];
309 nl
->ninl_val
[i
] = ni_name_dup(nm
);
321 ni_name_free(&nl
->ninl_val
[which
]);
322 for (i
= which
+ 1; i
< nl
-> ninl_len
; i
++) {
323 nl
->ninl_val
[i
- 1] = nl
->ninl_val
[i
];
325 MM_SHRINK_ARRAY(nl
->ninl_val
, nl
->ninl_len
--);
330 const ni_namelist nl
,
336 for (i
= 0; i
< nl
.ninl_len
; i
++) {
337 if (ni_name_match(nl
.ninl_val
[i
], nm
)) {
341 return (NI_INDEX_NULL
);
352 MM_GROW_ARRAY(el
->niel_val
, el
->niel_len
);
354 if (en
.names
!= NULL
) {
355 MM_ALLOC(entry
.names
);
356 *entry
.names
= ni_namelist_dup(*en
.names
);
360 el
->niel_val
[el
->niel_len
++] = entry
;
371 if (el
->niel_val
[which
].names
!= NULL
) {
372 ni_namelist_free(el
->niel_val
[which
].names
);
374 for (i
= which
+ 1; i
< el
-> niel_len
; i
++) {
375 el
->niel_val
[i
- 1] = el
->niel_val
[i
];
377 MM_SHRINK_ARRAY(el
->niel_val
, el
->niel_len
--);
387 if (el
->niel_val
== NULL
) {
390 for (i
= 0; i
< el
->niel_len
; i
++) {
391 if (el
->niel_val
[i
].names
!= NULL
) {
392 ni_namelist_free(el
->niel_val
[i
].names
);
393 MM_FREE(el
->niel_val
[i
].names
);
396 MM_FREE_ARRAY(el
->niel_val
, el
->niel_len
);
403 * We can do this without an addition to the protocol
418 status
= ni_listprops(ni
, id
, &list
);
419 if (status
!= NI_OK
) {
422 which
= ni_namelist_match(list
, pname
);
423 ni_namelist_free(&list
);
424 if (which
== NI_INDEX_NULL
) {
427 return (ni_readprop(ni
, id
, which
, nl
));
431 * Search from local domain to root domain to locate a path.
434 ni_find(void **dom
, ni_id
*nid
, ni_name dirname
, unsigned int timeout
)
441 nid
->nii_object
= NI_INDEX_NULL
;
442 nid
->nii_instance
= NI_INDEX_NULL
;
444 status
= ni_open(NULL
, ".", &d
);
445 if (status
!= NI_OK
) return status
;
449 ni_setreadtimeout(d
, timeout
);
455 status
= ni_pathsearch(d
, &n
, dirname
);
463 status
= ni_open(d
, "..", &p
);
466 if (status
== NI_OK
) d
= p
;
473 ni_search(void *handle
, ni_id
*dir
, ni_name name
, ni_name expr
, int flags
, ni_entrylist
*entries
)
481 /* get subdirectory list */
483 status
= ni_list(handle
, dir
, name
, &el
);
484 if (status
!= NI_OK
) return status
;
486 cexp
= (regex_t
*)malloc(sizeof(regex_t
));
487 memset(cexp
, 0, sizeof(regex_t
));
488 i
= regcomp(cexp
, expr
, flags
);
489 if (i
!= 0) return NI_FAILED
;
491 for (i
= 0; i
< el
.ni_entrylist_len
; i
++)
493 if (el
.ni_entrylist_val
[i
].names
== NULL
) continue;
495 nl
= el
.ni_entrylist_val
[i
].names
;
497 for (j
= 0; j
< nl
->ni_namelist_len
; j
++)
500 if (regexec(cexp
, nl
->ni_namelist_val
[j
], 0, NULL
, 0) != 0) continue;
506 if (found
== 0) continue;
508 if (entries
->ni_entrylist_len
== 0)
510 entries
->ni_entrylist_val
= malloc(sizeof(ni_entry
));
514 entries
->ni_entrylist_val
= (ni_entry
*)realloc(entries
->ni_entrylist_val
, (entries
->ni_entrylist_len
+ 1) * sizeof(ni_entry
));
517 entries
->ni_entrylist_val
[entries
->ni_entrylist_len
].id
= el
.ni_entrylist_val
[i
].id
;
518 entries
->ni_entrylist_val
[entries
->ni_entrylist_len
].names
= el
.ni_entrylist_val
[i
].names
;
519 el
.ni_entrylist_val
[i
].names
= NULL
;
520 entries
->ni_entrylist_len
++;
523 ni_entrylist_free(&el
);