Add tests with filenames containing newline and backslash characters.
[coreutils.git] / src / hostname.c
blob0ff26fb1ac355b82bca1faca8d0d2f6dd2b53144
1 /* hostname - set or print the name of current host system
2 Copyright (C) 94, 95, 96, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Jim Meyering <meyering@comco.com> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "long-options.h"
26 #include "error.h"
28 #if !defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO) && \
29 defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)
30 # include <sys/systeminfo.h>
32 int
33 sethostname (name, namelen)
34 char *name;
35 int namelen;
37 /* Using sysinfo() is the SVR4 mechanism to set a hostname. */
38 int result;
40 result = sysinfo (SI_SET_HOSTNAME, name, namelen);
42 return (result == -1 ? result : 0);
45 # define HAVE_SETHOSTNAME 1 /* Now we have it... */
46 #endif
48 char *xgethostname ();
50 /* The name this program was run with. */
51 char *program_name;
53 static void
54 usage (int status)
56 if (status != 0)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 program_name);
59 else
61 printf (_("\
62 Usage: %s [NAME]\n\
63 or: %s OPTION\n\
64 Print the hostname of the current system.\n\
65 \n\
66 --help display this help and exit\n\
67 --version output version information and exit\n\
69 , program_name, program_name);
70 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
72 exit (status);
75 int
76 main (int argc, char **argv)
78 char *hostname;
80 program_name = argv[0];
81 setlocale (LC_ALL, "");
82 bindtextdomain (PACKAGE, LOCALEDIR);
83 textdomain (PACKAGE);
85 parse_long_options (argc, argv, "hostname", GNU_PACKAGE, VERSION, usage);
87 #ifdef HAVE_SETHOSTNAME
88 if (argc == 2)
90 int err;
92 /* Set hostname to argv[1]. */
93 err = sethostname (argv[1], strlen (argv[1]));
94 if (err != 0)
95 error (1, errno, "%s", argv[1]);
96 exit (0);
98 #else
99 if (argc == 2)
100 error (1, 0,
101 _("cannot set hostname; this system lacks the functionality"));
102 #endif
104 if (argc == 1)
106 hostname = xgethostname ();
107 if (hostname == NULL)
108 error (1, errno, _("cannot determine hostname"));
109 printf ("%s\n", hostname);
111 else
113 error (2, 0, _("too many arguments"));
114 usage (1);
117 exit (0);