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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e6405ea was e6405ea, checked in by Sebastian Huber <sebastian.huber@…>, on 11/10/14 at 07:27:55

HOSTNAME(1): Add -m flag

  • Property mode set to 100644
File size: 4.4 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#include <rtems/mdns.h>
50#endif /* __rtems__ */
51#include <sys/cdefs.h>
52__FBSDID("$FreeBSD$");
53
54#include <rtems/bsd/sys/param.h>
55
56#include <err.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62static void usage(void);
63
64#ifdef __rtems__
65static int main(int argc, char *argv[]);
66
67static int hostname_command(int argc, char *argv[])
68{
69        int exit_code;
70
71        rtems_bsd_program_lock();
72
73        exit_code = rtems_bsd_program_call_main("hostname", main, argc, argv);
74
75        rtems_bsd_program_unlock();
76
77        return exit_code;
78}
79
80rtems_shell_cmd_t rtems_shell_HOSTNAME_Command = {
81  .name = "hostname",
82  .usage = "hostname [-fms] [name-of-host]",
83  .topic = "net",
84  .command = hostname_command
85};
86#endif /* __rtems__ */
87int
88main(int argc, char *argv[])
89{
90        int ch, sflag;
91        char *p, hostname[MAXHOSTNAMELEN];
92#ifdef __rtems__
93        struct getopt_data getopt_data;
94        memset(&getopt_data, 0, sizeof(getopt_data));
95#define optind getopt_data.optind
96#define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data)
97        int mflag = 0;
98#endif /* __rtems__ */
99
100        sflag = 0;
101#ifndef __rtems__
102        while ((ch = getopt(argc, argv, "fs")) != -1)
103#else /* __rtems__ */
104        while ((ch = getopt(argc, argv, "fms")) != -1)
105#endif /* __rtems__ */
106                switch (ch) {
107                case 'f':
108                        /*
109                         * On Linux, "hostname -f" prints FQDN.
110                         * BSD "hostname" always prints FQDN by
111                         * default, so we accept but ignore -f.
112                         */
113                        break;
114                case 's':
115                        sflag = 1;
116                        break;
117#ifdef __rtems__
118                case 'm':
119                        mflag = 1;
120                        break;
121#endif /* __rtems__ */
122                case '?':
123                default:
124                        usage();
125                }
126        argc -= optind;
127        argv += optind;
128
129        if (argc > 1)
130                usage();
131
132        if (*argv) {
133#ifdef __rtems__
134                if (mflag) {
135                        if (rtems_mdns_sethostname(*argv)) {
136                                err(1, "rtems_mdns_sethostname");
137                        }
138                } else {
139#endif /* __rtems__ */
140                if (sethostname(*argv, (int)strlen(*argv)))
141                        err(1, "sethostname");
142#ifdef __rtems__
143                }
144        } else if (mflag) {
145                if (rtems_mdns_gethostname(hostname, sizeof(hostname))) {
146                        err(1, "rtems_mdns_gethostname");
147                }
148
149                (void)printf("%s\n", hostname);
150#endif /* __rtems__ */
151        } else {
152                if (gethostname(hostname, (int)sizeof(hostname)))
153                        err(1, "gethostname");
154                if (sflag) {
155                        p = strchr(hostname, '.');
156                        if (p != NULL)
157                                *p = '\0';
158                }
159                (void)printf("%s\n", hostname);
160        }
161        exit(0);
162}
163
164static void
165usage(void)
166{
167
168        (void)fprintf(stderr, "usage: hostname [-fms] [name-of-host]\n");
169        exit(1);
170}
Note: See TracBrowser for help on using the repository browser.