Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
d93680d6 KB |
2 | * Copyright (c) 1983, 1988 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b8c620d6 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bcf1365c DF |
16 | */ |
17 | ||
205a2d85 | 18 | #ifndef lint |
bcf1365c | 19 | char copyright[] = |
d93680d6 | 20 | "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\ |
bcf1365c | 21 | All rights reserved.\n"; |
d93680d6 | 22 | #endif /* not lint */ |
bcf1365c DF |
23 | |
24 | #ifndef lint | |
b8c620d6 | 25 | static char sccsid[] = "@(#)hostname.c 5.3 (Berkeley) %G%"; |
d93680d6 | 26 | #endif /* not lint */ |
bcf1365c | 27 | |
d3d3fc37 | 28 | #include <stdio.h> |
d93680d6 | 29 | #include <sys/param.h> |
d3d3fc37 KS |
30 | |
31 | main(argc,argv) | |
d93680d6 KB |
32 | int argc; |
33 | char **argv; | |
d3d3fc37 | 34 | { |
d93680d6 KB |
35 | extern int optind; |
36 | int ch, sflag; | |
37 | char hostname[MAXHOSTNAMELEN], *p, *index(); | |
38 | ||
39 | sflag = 0; | |
40 | while ((ch = getopt(argc, argv, "s")) != EOF) | |
41 | switch((char)ch) { | |
42 | case 's': | |
43 | sflag = 1; | |
44 | break; | |
45 | case '?': | |
46 | default: | |
47 | fputs("hostname [-s] [hostname]\n", stderr); | |
48 | exit(1); | |
49 | } | |
50 | argv += optind; | |
65ed1a40 | 51 | |
d93680d6 KB |
52 | if (*argv) { |
53 | if (sethostname(*argv, strlen(*argv))) { | |
d3d3fc37 | 54 | perror("sethostname"); |
d93680d6 KB |
55 | exit(1); |
56 | } | |
d3d3fc37 | 57 | } else { |
d93680d6 KB |
58 | if (gethostname(hostname, sizeof(hostname))) { |
59 | perror("gethostname"); | |
60 | exit(1); | |
61 | } | |
62 | if (sflag && (p = index(hostname, '.'))) | |
63 | *p = '\0'; | |
64 | puts(hostname); | |
d3d3fc37 | 65 | } |
d93680d6 | 66 | exit(0); |
d3d3fc37 | 67 | } |