1 | /* Copyright 1988, 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: get_servername.c,v 1.1 1999-12-08 22:06:44 danw Exp $"; |
---|
17 | |
---|
18 | #include "globalmessage.h" |
---|
19 | #include <hesiod.h> |
---|
20 | #include "hesiod_err.h" |
---|
21 | |
---|
22 | Code_t get_servername(char ***ret_name) |
---|
23 | { |
---|
24 | char **retval, **_data; /* for copying out the hesiod data */ |
---|
25 | int datacnt, i; /* number of hesiod records returned */ |
---|
26 | |
---|
27 | /* Guard against NULL arguments */ |
---|
28 | if ((!ret_name)) { |
---|
29 | return(GMS_NULL_ARG_ERR); |
---|
30 | } |
---|
31 | |
---|
32 | /* Fetch the list of servers from Hesiod. */ |
---|
33 | _data = hes_resolve(GMS_NAME_CLASS, GMS_NAME_TYPE); |
---|
34 | |
---|
35 | if(!_data) { |
---|
36 | /* deal with hesiod error */ |
---|
37 | return(hesiod_error()); |
---|
38 | } |
---|
39 | |
---|
40 | /* Copy the Hesiod data into stable space. */ |
---|
41 | for(datacnt=0; _data[datacnt]; datacnt++); /* count the data */ |
---|
42 | |
---|
43 | if (!datacnt) { |
---|
44 | /* an answer, but no contents! */ |
---|
45 | return(HESIOD_ER_INVALID); /* XXX */ |
---|
46 | } |
---|
47 | retval = (char **)malloc(datacnt * sizeof(char *)); |
---|
48 | for(i=0; i<datacnt; i++) { |
---|
49 | retval[i] = malloc(strlen(_data[i])+1); |
---|
50 | if(!retval[i]) { |
---|
51 | /* malloc failed... */ |
---|
52 | for(;--i;free(retval[i])); |
---|
53 | return(GMS_MALLOC_ERR); |
---|
54 | } |
---|
55 | strcpy(retval[i], _data[i]); |
---|
56 | } |
---|
57 | |
---|
58 | /* Clean up and return normally. */ |
---|
59 | *ret_name = retval; |
---|
60 | return(0); |
---|
61 | } |
---|