119842Sdist /*
260669Sbostic * Copyright (c) 1988, 1993
360669Sbostic * The Regents of the University of California. All rights reserved.
434655Sbostic *
542534Sbostic * %sccs.include.redist.c%
619842Sdist */
719842Sdist
814465Ssam #ifndef lint
960669Sbostic static char copyright[] =
1060669Sbostic "@(#) Copyright (c) 1988, 1993\n\
1160669Sbostic The Regents of the University of California. All rights reserved.\n";
1234655Sbostic #endif /* not lint */
1319842Sdist
1419842Sdist #ifndef lint
15*68982Sbostic static char sccsid[] = "@(#)hostname.c 8.2 (Berkeley) 04/28/95";
1634655Sbostic #endif /* not lint */
1719842Sdist
1834655Sbostic #include <sys/param.h>
196488Ssklower
2059495Sbostic #include <err.h>
2159495Sbostic #include <stdio.h>
2259495Sbostic #include <stdlib.h>
2359495Sbostic #include <string.h>
2459495Sbostic #include <unistd.h>
2559495Sbostic
26*68982Sbostic void usage __P((void));
27*68982Sbostic
2859495Sbostic int
main(argc,argv)296488Ssklower main(argc,argv)
3034655Sbostic int argc;
3159495Sbostic char *argv[];
326488Ssklower {
3334655Sbostic int ch, sflag;
3459495Sbostic char *p, hostname[MAXHOSTNAMELEN];
3511773Srrh
3634655Sbostic sflag = 0;
37*68982Sbostic while ((ch = getopt(argc, argv, "s")) != -1)
3859495Sbostic switch (ch) {
3934655Sbostic case 's':
4034655Sbostic sflag = 1;
4134655Sbostic break;
4234655Sbostic case '?':
4334655Sbostic default:
44*68982Sbostic usage();
4534655Sbostic }
4659495Sbostic argc -= optind;
4734655Sbostic argv += optind;
4834655Sbostic
4934655Sbostic if (*argv) {
5059495Sbostic if (sethostname(*argv, strlen(*argv)))
5159495Sbostic err(1, "sethostname");
526488Ssklower } else {
5359495Sbostic if (gethostname(hostname, sizeof(hostname)))
5459495Sbostic err(1, "gethostname");
5559495Sbostic if (sflag && (p = strchr(hostname, '.')))
5634655Sbostic *p = '\0';
5759495Sbostic (void)printf("%s\n", hostname);
586488Ssklower }
5934655Sbostic exit(0);
606488Ssklower }
61*68982Sbostic
62*68982Sbostic void
usage()63*68982Sbostic usage()
64*68982Sbostic {
65*68982Sbostic
66*68982Sbostic (void)fprintf(stderr, "usage: hostname [-s] [hostname]\n");
67*68982Sbostic exit(1);
68*68982Sbostic }
69