annotate lib/getdomainname.c @ 17480:f40b3156a43e

selinux-at: omit unnecessary include * lib/selinux-at.c: Don't include dosname.h; not needed, since this source file doesn't use its macros, and subsidiary files that use the macros already include it.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 23 Aug 2013 13:53:46 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* getdomainname emulation for systems that doesn't have it.
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
2
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16935
diff changeset
3 Copyright (C) 2003, 2006, 2008, 2010-2013 Free Software Foundation, Inc.
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
5 This program is free software: you can redistribute it and/or modify
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
8 (at your option) any later version.
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Written by Simon Josefsson. */
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
20 #include <config.h>
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
10661
65c9436dc0d3 Move the getdomainname() declaration to <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
23 #include <unistd.h>
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
13911
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
25 #include <limits.h>
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
4732
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
27 #include <errno.h>
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
13911
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
29 #if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H /* IRIX, OSF/1, Solaris */
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
30 # include <sys/systeminfo.h>
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
31 #endif
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
32
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /* Return the NIS domain name of the machine.
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 WARNING! The NIS domain name is unrelated to the fully qualified host name
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 of the machine. It is also unrelated to email addresses.
4734
e73a97963451 Fix comment.
Bruno Haible <bruno@clisp.org>
parents: 4732
diff changeset
36 WARNING! The NIS domain name is usually the empty string or "(none)" when
e73a97963451 Fix comment.
Bruno Haible <bruno@clisp.org>
parents: 4732
diff changeset
37 not using NIS.
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 Put up to LEN bytes of the NIS domain name into NAME.
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 Null terminate it if the name is shorter than LEN.
4732
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
41 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 Return 0 if successful, otherwise set errno and return -1. */
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 int
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 getdomainname (char *name, size_t len)
13911
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
45 #undef getdomainname
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 {
16935
498a2211d839 Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
47 #if HAVE_GETDOMAINNAME /* Mac OS X, FreeBSD, AIX, IRIX, OSF/1 */
13911
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
48 extern int getdomainname (char *, int);
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
49
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
50 if (len > INT_MAX)
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
51 len = INT_MAX;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
52 return getdomainname (name, (int) len);
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
53 #elif HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H && defined SI_SRPC_DOMAIN
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
54 /* Solaris */
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
55 int ret;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
56
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
57 /* The third argument is a 'long', but the return value must fit in an
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
58 'int', therefore it's better to avoid arguments > INT_MAX. */
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
59 ret = sysinfo (SI_SRPC_DOMAIN, name, len > INT_MAX ? INT_MAX : len);
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
60 if (ret < 0)
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
61 /* errno is set here. */
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
62 return -1;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
63 if (ret > len)
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
64 {
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
65 errno = EINVAL;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
66 return -1;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
67 }
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
68 return 0;
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
69 #else /* HP-UX, Cygwin, mingw */
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10661
diff changeset
70 const char *result = ""; /* Hardcode your domain name if you want. */
4732
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
71 size_t result_len = strlen (result);
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
72
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
73 if (result_len > len)
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
74 {
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
75 errno = EINVAL;
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
76 return -1;
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
77 }
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
78 memcpy (name, result, result_len);
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
79 if (result_len < len)
3f6a23a84d27 Return -1/EINVAL when the buffer is too small.
Bruno Haible <bruno@clisp.org>
parents: 4730
diff changeset
80 name[result_len] = '\0';
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 return 0;
13911
e6744cae0421 getdomainname: Use the system function when possible.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
82 #endif
4730
64e0a1bfa9bb New module 'getdomainname'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 }