Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
d93680d6 KB |
2 | * Copyright (c) 1983, 1988 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
27c71911 | 5 | * %sccs.include.redist.c% |
bcf1365c DF |
6 | */ |
7 | ||
205a2d85 | 8 | #ifndef lint |
bcf1365c | 9 | char copyright[] = |
d93680d6 | 10 | "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\ |
bcf1365c | 11 | All rights reserved.\n"; |
d93680d6 | 12 | #endif /* not lint */ |
bcf1365c DF |
13 | |
14 | #ifndef lint | |
27c71911 | 15 | static char sccsid[] = "@(#)hostname.c 5.4 (Berkeley) %G%"; |
d93680d6 | 16 | #endif /* not lint */ |
bcf1365c | 17 | |
d3d3fc37 | 18 | #include <stdio.h> |
d93680d6 | 19 | #include <sys/param.h> |
d3d3fc37 KS |
20 | |
21 | main(argc,argv) | |
d93680d6 KB |
22 | int argc; |
23 | char **argv; | |
d3d3fc37 | 24 | { |
d93680d6 KB |
25 | extern int optind; |
26 | int ch, sflag; | |
27 | char hostname[MAXHOSTNAMELEN], *p, *index(); | |
28 | ||
29 | sflag = 0; | |
30 | while ((ch = getopt(argc, argv, "s")) != EOF) | |
31 | switch((char)ch) { | |
32 | case 's': | |
33 | sflag = 1; | |
34 | break; | |
35 | case '?': | |
36 | default: | |
37 | fputs("hostname [-s] [hostname]\n", stderr); | |
38 | exit(1); | |
39 | } | |
40 | argv += optind; | |
65ed1a40 | 41 | |
d93680d6 KB |
42 | if (*argv) { |
43 | if (sethostname(*argv, strlen(*argv))) { | |
d3d3fc37 | 44 | perror("sethostname"); |
d93680d6 KB |
45 | exit(1); |
46 | } | |
d3d3fc37 | 47 | } else { |
d93680d6 KB |
48 | if (gethostname(hostname, sizeof(hostname))) { |
49 | perror("gethostname"); | |
50 | exit(1); | |
51 | } | |
52 | if (sflag && (p = index(hostname, '.'))) | |
53 | *p = '\0'; | |
54 | puts(hostname); | |
d3d3fc37 | 55 | } |
d93680d6 | 56 | exit(0); |
d3d3fc37 | 57 | } |