build: ensure sys/select.h is included
[coreutils.git] / src / hostid.c
blob06b05b9c357e2186d9e209e60ad1dc1d798ea8f5
1 /* print the hexadecimal identifier for the current host
3 Copyright (C) 1997-2019 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Jim Meyering. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "long-options.h"
27 #include "error.h"
28 #include "quote.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "hostid"
33 #define AUTHORS proper_name ("Jim Meyering")
35 static struct option const long_options[] =
37 {NULL, 0, NULL, 0}
40 void
41 usage (int status)
43 if (status != EXIT_SUCCESS)
44 emit_try_help ();
45 else
47 printf (_("\
48 Usage: %s [OPTION]\n\
49 Print the numeric identifier (in hexadecimal) for the current host.\n\
50 \n\
51 "), program_name);
52 fputs (HELP_OPTION_DESCRIPTION, stdout);
53 fputs (VERSION_OPTION_DESCRIPTION, stdout);
54 emit_ancillary_info (PROGRAM_NAME);
56 exit (status);
59 int
60 main (int argc, char **argv)
62 unsigned int id;
64 initialize_main (&argc, &argv);
65 set_program_name (argv[0]);
66 setlocale (LC_ALL, "");
67 bindtextdomain (PACKAGE, LOCALEDIR);
68 textdomain (PACKAGE);
70 atexit (close_stdout);
72 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
73 usage, AUTHORS, (char const *) NULL);
74 if (getopt_long (argc, argv, "", long_options, NULL) != -1)
75 usage (EXIT_FAILURE);
77 if (optind < argc)
79 error (0, 0, _("extra operand %s"), quote (argv[optind]));
80 usage (EXIT_FAILURE);
83 id = gethostid ();
85 /* POSIX says gethostid returns a "32-bit identifier" but is silent
86 whether it's sign-extended. Turn off any sign-extension. This
87 is a no-op unless unsigned int is wider than 32 bits. */
88 id &= 0xffffffff;
90 printf ("%08x\n", id);
92 return EXIT_SUCCESS;