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

4.11
Last change on this file since fef6dd1 was fef6dd1, checked in by Christian Mauderer <Christian.Mauderer@…>, on Jul 15, 2016 at 5:32:56 AM

freebsd: Don't use new wrappers for old ports.

Some of the commands have been adapted manually. So the wrapper
currently don't necessarily work as expected. For example ifconfig calls
malloc outside of the program call.

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