Tizen 2.0 Release
[external/tizen-coreutils.git] / src / hostid.c
1 /* print the hexadecimal identifier for the current host
2
3    Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by Jim Meyering.  */
21
22 #include <config.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26
27 #include "system.h"
28 #include "long-options.h"
29 #include "error.h"
30 #include "quote.h"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "hostid"
34
35 #define AUTHORS "Jim Meyering"
36
37 /* The name this program was run with, for error messages. */
38 char *program_name;
39
40 void
41 usage (int status)
42 {
43   if (status != EXIT_SUCCESS)
44     fprintf (stderr, _("Try `%s --help' for more information.\n"),
45              program_name);
46   else
47     {
48       printf (_("\
49 Usage: %s\n\
50   or:  %s OPTION\n\
51 Print the numeric identifier (in hexadecimal) for the current host.\n\
52 \n\
53 "),
54               program_name, program_name);
55       fputs (HELP_OPTION_DESCRIPTION, stdout);
56       fputs (VERSION_OPTION_DESCRIPTION, stdout);
57       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
58     }
59   exit (status);
60 }
61
62 int
63 main (int argc, char **argv)
64 {
65   unsigned int id;
66
67   initialize_main (&argc, &argv);
68   program_name = argv[0];
69   setlocale (LC_ALL, "");
70   bindtextdomain (PACKAGE, LOCALEDIR);
71   textdomain (PACKAGE);
72
73   atexit (close_stdout);
74
75   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
76                       usage, AUTHORS, (char const *) NULL);
77   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
78     usage (EXIT_FAILURE);
79
80   if (optind < argc)
81     {
82       error (0, 0, _("extra operand %s"), quote (argv[optind]));
83       usage (EXIT_FAILURE);
84     }
85
86   id = gethostid ();
87
88   /* POSIX says gethostid returns a "32-bit identifier" but is silent
89      whether it's sign-extended.  Turn off any sign-extension.  This
90      is a no-op unless unsigned int is wider than 32 bits.  */
91   id &= 0xffffffff;
92
93   printf ("%08x\n", id);
94
95   exit (EXIT_SUCCESS);
96 }