annotate lib/xgetdomainname.c @ 4734:e73a97963451

Fix comment.
author Bruno Haible <bruno@clisp.org>
date Thu, 25 Sep 2003 11:01:25 +0000
parents d160b0c1905e
children dc1fbdf89b31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4731
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* xgetdomainname.c -- Return the NIS domain name, without size limitations.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 1992, 1996, 2000, 2001, 2003 Free Software Foundation, Inc.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Based on xgethostname.c, written by Jim Meyering. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 # include <config.h>
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "xgetdomainname.h"
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* Get getdomainname. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "getdomainname.h"
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Get errno. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <errno.h>
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include "xalloc.h"
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #ifndef INITIAL_DOMAINNAME_LENGTH
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 # define INITIAL_DOMAINNAME_LENGTH 34
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #endif
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Return the NIS domain name of the machine, in malloc'd storage.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 WARNING! The NIS domain name is unrelated to the fully qualified host name
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 of the machine. It is also unrelated to email addresses.
4734
e73a97963451 Fix comment.
Bruno Haible <bruno@clisp.org>
parents: 4731
diff changeset
42 WARNING! The NIS domain name is usually the empty string or "(none)" when
e73a97963451 Fix comment.
Bruno Haible <bruno@clisp.org>
parents: 4731
diff changeset
43 not using NIS.
4731
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 If malloc fails, exit.
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 Upon any other failure, set errno and return NULL. */
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 char *
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 xgetdomainname (void)
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 {
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 char *domainname;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 size_t size;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 size = INITIAL_DOMAINNAME_LENGTH;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 domainname = xmalloc (size);
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 while (1)
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 int k = size - 1;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 int err;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 errno = 0;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 domainname[k] = '\0';
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 err = getdomainname (domainname, size);
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (err >= 0 && domainname[k] == '\0')
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 break;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 else if (err < 0 && errno != EINVAL)
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 int saved_errno = errno;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 free (domainname);
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 errno = saved_errno;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 return NULL;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 }
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 size *= 2;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 domainname = xrealloc (domainname, size);
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return domainname;
d160b0c1905e New modules 'xgetdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }