[comp.os.minix] Hostname

pascal@hope.UUCP (Freeman Pascal) (05/15/88)

Hello,

Here is hostname(1) and get/sethostname(3) for MINIX.  I know that these
are not part of V7 but as we try to port utilities such as uupc or gnuucp
they would come in handy.  Plus - It's nice to give your system a name :-)

I have written hostname(1) and get/sethostname(3) to be BSD 2.9 compatible
as this was the only model I had available at the time.

I hope these can be of some use to you.

Freeman P. Pascal IV
pascal@hope

-------------------------------[  Cut Here  ]--------------------------------
echo x - Makefile
gres '^X' '' > Makefile << '/'
X#
X#	hostname - makefile
X#
XCFLAGS	= -i
XDEST	= /usr/lbin
XOWNER	= bin
XGROUP	= sys
XMEM	= 1024
XMODE	= a+rx-w
X
Xhostname:	hostname.c gethostname.s sethostname.s
X	cc $(CFLAGS) -o hostname hostname.c gethostname.s sethostname.s
X
Xgethostname.s:
X
Xsethostname.s:
X
Xinstall:
X	chown $(OWNER) hostname
X	chgrp $(GROUP) hostname
X	chmod $(MODE) hostname
X	chmem $(MEM) hostname
X	mv hostname $(DEST)
X
/
echo x - README
gres '^X' '' > README << '/'
XREADME                             hostname                             README
X
XIncluded here are the required files to build and install hostname(1),
Xgethostname(3), and sethostname(3).  These files offer MINIX the ability to
Xset and get a system host name through a standardized interface.  The library
Xroutines get/sethostname(3) are mechinisms through which this is accomplished.
XThese routines will become increasingly useful as other utilities are developed
Xfor MINIX that require these services, such as uucp or networking support.
X
XTo begin with copy parm.h to /usr/include/sys.  This file will contain system
Xdependent definitions such as paths and limits for assorted system utilities.
XAlso, if you have a directory for man pages you'll need to install the included
Xman pages.  They are written for my online man system which uses nro, but you
Xshould have no problem formatting them.  I hope to offer my manual system as
Xsoon as I complete it and Andy gives me the ok.
X
XTo install hostname and get/sethostname just 'make'.  You will have to install
Xget/sethostname(3) at the beginning libc.a (I don't have lorder running yet, so
Xthe front of libc.a is the safest place to avoid confilicts).  On my system
XI do this by issuing the following command:
X
X	ar -rb termcap.s /usr/lib/libc.a gethostname.s sethostname.s
X
Xwhere termcap.s is the first entry in /usr/lib/libc.a.  This assume, of course,
Xyou are using the latest version of ar that allows insertions and replacements.
X
XWhen you have successfuly compiled hostname and get/sethostname, type -
X
X	make install
X
XThis will copy hostname to your local directory and set ownership, memory 
Xusage, ect.  You might have to edit Makefile to change these defines for
Xyour system.
X
XHostname(1) is usually used during boot-up and is run during /etc/rc executes.
XAlso hostname can only be executed by the super-user to set the current host
Xname.  The current host name can be found in /etc/localhostname, but don't
Xrely on this fact.  Use gethostname(3) when wanting to retrieve the current
Xhost name.
X
XHostname and get/sethostname should now make it easier to port any future
Xutilities that require knowing the host name.  I hope you find them useful.
X
XComment, questions, bug reports are welcomed.
X
XFreeman P. Pascal IV
Xtoor@gizmo1  -or- ndsuvax!nomad2!gizmo1!toor/
echo x - gethostname.3
gres '^X' '' > gethostname.3 << '/'
X.m1 3
X.TH gethostname 3 "April 30, 1988" "MINIX 1.2" "MINIX Programmers Manual"
X.SH NAME
Xgethostname, sethostname - get/set name of host system
X.SH SYNOPSIS
Xgethostname( name, namelen )
X.br
Xchar  *name;
X.br
Xint   namelen;
X.br
X.sp
Xsethostname( name, namelen )
X.br
Xchar  *name;
X.br
Xint   namelen;
X.SH DESCRIPTION
X.B Gethostname
Xreturns the current name of host system previously set by 
X.B sethostname(3).
X'Namelen' specifies size of 'name.'
X.br
X.sp
X.B Sethostname
Xallows the super-user to set the current 
Xhost name to 'name', which has length 'namelen'.
X.SH "RETURN VALUE"
XA value of 0 is returned if operation succeeds, otherwise, -1 is returned
Xand 'errno' is set accordingly.
X.SH ERRORS
X.nf
X.ti +5
X[EIO]     could not open or read from /etc/localhostname
X.ti +5
X[EPERM]   must be super-user to set host name.
X.SH "SEE ALSO"
Xhostname(1)
X.SH BUGS
X.ju
XHost names limited to MAXHOSTNAMLEN found in <sys/param.h> which is
Xcurrenly set to 64.
/
echo x - gethostname.c
gres '^X' '' > gethostname.c << '/'
X/*
X *	gethostname - get current name of machine
X *
X */
X#include	<sys/file.h>
X#include	<sys/param.h>
X#include	<errno.h>
X
X#ifndef	MAXHOSTNAMELEN
X#  define	MAXHOSTNAMELEN	64		/* just like BSD */
X#endif
X
Xextern int	errno;
Xstatic char	namebuf[ MAXHOSTNAMELEN ];
X
Xgethostname( name, namelen )
X  char	*name;
X  int	namelen;
X{
X  int		fd;
X  char		*sp, *np;
X
X  if ((fd = open( HOSTNAME, O_RDONLY )) < 0) {
X	errno = EIO;					/* cannot open */
X	return( -1 );
X  }
X  if ((read( fd, namebuf, MAXHOSTNAMELEN )) < 0) {
X	errno = EIO;					/* cannot read */
X	return( -1 );
X  } else {
X	sp = namebuf;
X	while (( *sp ) && (--namelen))
X		if (*sp == '\n') {
X			*sp = '\0';
X			break;
X		} else
X			*name++ = *sp++;
X  }
X  close( fd );
X  return( 0 );
X}			/
echo x - hostname.1
gres '^X' '' > hostname.1 << '/'
X.TH hostname 1 "April 30, 1988" "MINIX 1.2" "MINIX User's Reference Manual"
X.SH NAME
Xhostname - get/set name of host system
X.SH SYNOPSIS
X.B hostname [nameofhost]
X.br
X.SH DESCRIPTION
XPrints current name of host system when no arguments are supplied. The
Xsuperuser can change the current host name by supplying the new host name
Xas an argument.
X.B Hostname
Xis usually used during bootup when /etc/rc is executed.
X.SH "SEE ALSO"
Xgethostname(3), sethostname(3)
X
/
echo x - hostname.c
gres '^X' '' > hostname.c << '/'
X/*
X *	hostname.c - return or set system name
X *
X *	Usage:  hostname [name of host]
X *
X */
X#include	<sys/param.h>
X#include	<errno.h>
X
Xextern int	errno;
Xchar		hostbuf[ MAXHOSTNAMELEN ];
X
Xmain( argc, argv )
X  char	**argv;
X{
X  if (argc >= 3) {
X	std_err( "Usage:\t" );
X	std_err( argv[0] );
X	std_err( " [nameofhost] \n" );
X	exit( 1 );
X  }
X  if (argc == 1) {
X	if (gethostname( hostbuf, MAXHOSTNAMELEN )) {
X		std_err( argv[0] );
X		std_err( ":  problem retrieving host's name.\n" );
X		exit( 1 );
X	} else
X		printf( "%s\n", hostbuf );
X  } else {
X	if (sethostname( argv[1], strlen( argv[1] ))) {
X		switch( errno ) {
X			case EPERM :	/* not superuser */
X				std_err( argv[0] );
X				std_err( ":  Must be superuser!\n" );
X				break;
X			case EIO :	/* /etc/localhostname doesn't exist */
X				std_err( argv[0] );
X				std_err( ":  Problem opening/writing " );
X				std_err( HOSTNAME );
X				std_err( ".\n" );
X				break;
X			default :	/* any other problems */
X				perror( argv[0] );
X		}
X		exit( 1 );
X	}
X  }
X  exit( 0 );
X}
X  /
echo x - param.h
gres '^X' '' > param.h << '/'
X/*
X *	param.h - misc. site dependent stuff
X */
X/*
X *  History:
X *
X *	30 April 88	FPP	Creation
X */
X
X#define	HOSTNAME		"/etc/localhostname"
X#define	MAXHOSTNAMELEN		64	/* Max size of host name */
/
echo x - sethostname.c
gres '^X' '' > sethostname.c << '/'
X/*
X *	sethostname - set current name of machine
X */
X#include	<sys/file.h>
X#include	<sys/param.h>
X#include	<errno.h>
X
X#ifndef	MAXHOSTNAMELEN
X#  define	MAXHOSTNAMELEN	64		/* just like BSD */
X#endif
X
X#define min( a, b )  (( a < b ) ? a : b )
X
Xextern int	errno;
Xstatic char	namebuf[ MAXHOSTNAMELEN ];
X
Xsethostname( name, namelen )
X  char	*name;
X  int	namelen;
X{
X  int		fd;
X
X  if ( getuid() != 0 ) {	/* must be superuser to do this */
X	errno = EPERM;
X	return( -1 );
X  }
X  if (creat( HOSTNAME, 0644 ) < 0)	/* (re)create /etc/localhostname */
X	return( -1 );
X  if ((fd = open( HOSTNAME, O_WRONLY )) < 0) {
X	errno = EIO;
X	return( -1 );
X  }
X
X  name[ min( namelen, MAXHOSTNAMELEN ) ] = '\n';
X  if (write( fd, name, min( namelen, MAXHOSTNAMELEN )) < 0) {
X	errno = EIO;
X	return( -1 );
X  }
X  close( fd );
X  return( 0 );
X}
/