source: rtems-libbsd/freebsd/bin/hostname/hostname.c @ 50e82a6

4.115-freebsd-12freebsd-9.3
Last change on this file since 50e82a6 was 50e82a6, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 5, 2014 at 1:27:18 PM

HOSTNAME(1): Import from FreeBSD

  • Property mode set to 100644
File size: 3.8 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * Copyright (c) 1988, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if 0
33#ifndef lint
34static char const copyright[] =
35"@(#) Copyright (c) 1988, 1993\n\
36        The Regents of the University of California.  All rights reserved.\n";
37#endif /* not lint */
38
39#ifndef lint
40static char sccsid[] = "@(#)hostname.c  8.1 (Berkeley) 5/31/93";
41#endif /* not lint */
42#endif
43#ifdef __rtems__
44#define __need_getopt_newlib
45#include <getopt.h>
46#include <rtems/netcmds-config.h>
47#include <machine/rtems-bsd-program.h>
48#include <machine/rtems-bsd-commands.h>
49#endif /* __rtems__ */
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD$");
52
53#include <rtems/bsd/sys/param.h>
54
55#include <err.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61static void usage(void);
62
63#ifdef __rtems__
64static int main(int argc, char *argv[]);
65
66static int hostname_command(int argc, char *argv[])
67{
68        int exit_code;
69
70        rtems_bsd_program_lock();
71
72        exit_code = rtems_bsd_program_call_main("hostname", main, argc, argv);
73
74        rtems_bsd_program_unlock();
75
76        return exit_code;
77}
78
79rtems_shell_cmd_t rtems_shell_HOSTNAME_Command = {
80  .name = "hostname",
81  .usage = "hostname [-fs] [name-of-host]",
82  .topic = "net",
83  .command = hostname_command
84};
85#endif /* __rtems__ */
86int
87main(int argc, char *argv[])
88{
89        int ch, sflag;
90        char *p, hostname[MAXHOSTNAMELEN];
91#ifdef __rtems__
92        struct getopt_data getopt_data;
93        memset(&getopt_data, 0, sizeof(getopt_data));
94#define optind getopt_data.optind
95#define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data)
96#endif /* __rtems__ */
97
98        sflag = 0;
99        while ((ch = getopt(argc, argv, "fs")) != -1)
100                switch (ch) {
101                case 'f':
102                        /*
103                         * On Linux, "hostname -f" prints FQDN.
104                         * BSD "hostname" always prints FQDN by
105                         * default, so we accept but ignore -f.
106                         */
107                        break;
108                case 's':
109                        sflag = 1;
110                        break;
111                case '?':
112                default:
113                        usage();
114                }
115        argc -= optind;
116        argv += optind;
117
118        if (argc > 1)
119                usage();
120
121        if (*argv) {
122                if (sethostname(*argv, (int)strlen(*argv)))
123                        err(1, "sethostname");
124        } else {
125                if (gethostname(hostname, (int)sizeof(hostname)))
126                        err(1, "gethostname");
127                if (sflag) {
128                        p = strchr(hostname, '.');
129                        if (p != NULL)
130                                *p = '\0';
131                }
132                (void)printf("%s\n", hostname);
133        }
134        exit(0);
135}
136
137static void
138usage(void)
139{
140
141        (void)fprintf(stderr, "usage: hostname [-fs] [name-of-host]\n");
142        exit(1);
143}
Note: See TracBrowser for help on using the repository browser.