DragonFly BSD
DragonFly submit List (threaded) for 2004-01
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

hostname(1) change


From: Kent Ibbetson <bsd@xxxxxxxxxx>
Date: Mon, 5 Jan 2004 10:47:00 +1000

Hi all,

Attached is a patch for hostname(1) which allows you set the hostname
based on a supplied IP address.  I use it to set the hostname when I
dial in.

hostname -r IP_ADDR  is much easier to type than
hostname `host IP_ADDR | awk '{print $5}'`


Maybe someone else will find it useful, maybe its just junk :)


Cheers,

-- 
Kent Ibbetson
bsd@xxxxxxxxxx
*** bin/hostname/hostname.c.orig	Mon Jan  5 09:54:13 2004
--- bin/hostname/hostname.c	Mon Jan  5 09:57:43 2004
***************
*** 44,61 ****
  #include <string.h>
  #include <unistd.h>
  
  int main (int, char *[]);
  void usage (void);
  
  int
  main(int argc, char **argv)
  {
! 	int ch, sflag;
  	char *p, hostname[MAXHOSTNAMELEN];
  
! 	sflag = 0;
! 	while ((ch = getopt(argc, argv, "s")) != -1)
  		switch (ch) {
  		case 's':
  			sflag = 1;
  			break;
--- 44,82 ----
  #include <string.h>
  #include <unistd.h>
  
+ #include <netdb.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ 
+ 
  int main (int, char *[]);
  void usage (void);
  
  int
  main(int argc, char **argv)
  {
! 	int ch,sflag,rflag,ret;
  	char *p, hostname[MAXHOSTNAMELEN];
+ 	char *srflag;
+ 	struct hostent *hst = NULL;
+ 	struct in_addr ia;
+ 
+ 	srflag = NULL;
+ 	sflag = rflag = 0;
  
! 	while ((ch = getopt(argc, argv, "r:s")) != -1)
  		switch (ch) {
+ 		case 'r':
+ 			srflag = (char*)calloc(1,sizeof(char) * (strlen((char*)optarg)+1));
+ 			if(srflag) {
+ 				rflag = 1;
+ 				strlcpy(srflag, (char*)optarg, strlen((char*)optarg)+1);
+ 			} else {
+ 				errx(1, "malloc");
+ 			}
+ 			break;
  		case 's':
  			sflag = 1;
  			break;
***************
*** 69,75 ****
  	if (argc > 1)
  		usage();
  
! 	if (*argv) {
  		if (sethostname(*argv, (int)strlen(*argv)))
  			err(1, "sethostname");
  	} else {
--- 90,118 ----
  	if (argc > 1)
  		usage();
  
! 	if (rflag && *argv) {
! 		free(srflag);
! 		usage();
! 	}
! 
! 
! 	if(rflag) {
! 		ret = inet_pton(AF_INET, srflag, &ia);
! 		if(ret != 1) {
! 			free(srflag);
! 			errx(1, "invalid ip address");
! 		}
! 		
! 		hst = gethostbyaddr((const char*)&ia, sizeof(ia), AF_INET);
! 		if(!hst) {
! 			free(srflag);
! 			if(h_errno == HOST_NOT_FOUND) 
! 				errx(1,"host not found\n");
! 		}
! 
! 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
! 			err(1, "sethostname");
! 	} else if (*argv) {
  		if (sethostname(*argv, (int)strlen(*argv)))
  			err(1, "sethostname");
  	} else {
***************
*** 86,91 ****
  usage(void)
  {
  
! 	(void)fprintf(stderr, "usage: hostname [-s] [name-of-host]\n");
  	exit(1);
  }
--- 129,134 ----
  usage(void)
  {
  
! 	(void)fprintf(stderr, "usage: hostname [-s] [name-of-host | -r ip-address]\n");
  	exit(1);
  }
*** bin/hostname/hostname.1.orig	Mon Jan  5 09:54:13 2004
--- bin/hostname/hostname.1	Mon Jan  5 09:54:38 2004
***************
*** 42,48 ****
  .Sh SYNOPSIS
  .Nm
  .Op Fl s
! .Op Ar name-of-host
  .Sh DESCRIPTION
  The
  .Nm
--- 42,48 ----
  .Sh SYNOPSIS
  .Nm
  .Op Fl s
! .Op Ar name-of-host | -r ip-address
  .Sh DESCRIPTION
  The
  .Nm
***************
*** 62,67 ****
--- 62,71 ----
  .It Fl s
  Trim off any domain information from the printed
  name.
+ .It Fl r
+ Retrive hostname via ip lookup.  The hostname is set to the first (official)
+ name returned for the supplied ip address.
+ 
  .El
  .Sh SEE ALSO
  .Xr gethostname 3 ,


[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]