Add -d flag that prints domain only.

PR:		212875
Submitted by:	Ben RUBSON <ben.rubson@gmail.com>
Reviewed by:	pi
This commit is contained in:
Marcelo Araujo 2016-11-08 11:36:33 +00:00
parent 9a639daf77
commit 31500ce90d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=308443
2 changed files with 17 additions and 6 deletions

View File

@ -29,7 +29,7 @@
.\" @(#)hostname.1 8.2 (Berkeley) 4/28/95 .\" @(#)hostname.1 8.2 (Berkeley) 4/28/95
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd December 7, 2006 .Dd November 9, 2016
.Dt HOSTNAME 1 .Dt HOSTNAME 1
.Os .Os
.Sh NAME .Sh NAME
@ -37,7 +37,8 @@
.Nd set or print name of current host system .Nd set or print name of current host system
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl fs .Op Fl f
.Op Fl s|d
.Op Ar name-of-host .Op Ar name-of-host
.Sh DESCRIPTION .Sh DESCRIPTION
The The
@ -62,6 +63,8 @@ This is the default behavior.
.It Fl s .It Fl s
Trim off any domain information from the printed Trim off any domain information from the printed
name. name.
.It Fl d
Only print domain information.
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr gethostname 3 , .Xr gethostname 3 ,

View File

@ -54,11 +54,12 @@ static void usage(void) __dead2;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ch, sflag; int ch, sflag, dflag;
char *p, hostname[MAXHOSTNAMELEN]; char *p, hostname[MAXHOSTNAMELEN];
sflag = 0; sflag = 0;
while ((ch = getopt(argc, argv, "fs")) != -1) dflag = 0;
while ((ch = getopt(argc, argv, "fsd")) != -1)
switch (ch) { switch (ch) {
case 'f': case 'f':
/* /*
@ -70,6 +71,9 @@ main(int argc, char *argv[])
case 's': case 's':
sflag = 1; sflag = 1;
break; break;
case 'd':
dflag = 1;
break;
case '?': case '?':
default: default:
usage(); usage();
@ -77,7 +81,7 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc > 1) if (argc > 1 || (sflag && dflag))
usage(); usage();
if (*argv) { if (*argv) {
@ -90,6 +94,10 @@ main(int argc, char *argv[])
p = strchr(hostname, '.'); p = strchr(hostname, '.');
if (p != NULL) if (p != NULL)
*p = '\0'; *p = '\0';
} else if (dflag) {
p = strchr(hostname, '.');
if (p != NULL)
strcpy(hostname, ++p);
} }
(void)printf("%s\n", hostname); (void)printf("%s\n", hostname);
} }
@ -100,6 +108,6 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "usage: hostname [-fs] [name-of-host]\n"); (void)fprintf(stderr, "usage: hostname [-f] [s|d] [name-of-host]\n");
exit(1); exit(1);
} }