[comp.os.minix] tty utility and ttyname

ncpascal@ndsuvax.UUCP (Freeman Pascal) (12/17/87)

Hello,
     This is a little utility that I wrote when I first started to use
MINIX.  It behaves just like tty(1) for UNIX.  In writing it I also
needed to write ttyname(3) library call.  I hope both the tty(1) and
ttyname(3) is usefile.  Just follow the instructions in the "INSTALL"
file and enjoy.

				Freeman P. Pascal IV
				ncpascal@ndsuvax


#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 1)."
# Contents:  INSTALL Makefile makelibc tty.c ttyname.c
# Wrapped by ncpascal@ndsuvax on Wed Dec 16 23:30:02 1987
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f INSTALL -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"INSTALL\"
else
echo shar: Extracting \"INSTALL\" \(692 characters\)
sed "s/^X//" >INSTALL <<'END_OF_INSTALL'
XManifest:
X--------
X	Makefile	makefile for tty
X	makelibc	shell script to make C library
X	tty.c		utility to return tty name
X	ttyname.c	library function to return tty name (see ttyname(3))
X
XInstallation instructions:
X-------------------------
X
X1.  Compile ttyname.c as a library routine and place in /usr/lib/libc.a.
X    If you are using the makelibc shell script that was posted quite 
X    awhile back just append "ar av ttyname.s" to the beginning and run.
X    I am including my version if you don't have it.
X
X2.  Run makefile to compile tty.c.  It will automatically locate "tty" in
X    the /usr/bin direcory, set it's mode to "r-xr-xr-x" (555), and set it's
X    stack size to 1k.
X
X3.  Enjoy
END_OF_INSTALL
if test 692 -ne `wc -c <INSTALL`; then
    echo shar: \"INSTALL\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f Makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(229 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
X#
X#	Makefile for /usr/bin/tty
X#
XCFLAGS= -T. -i -O
XDEST= /usr/bin
XMODE= 555
XMEMSIZ= 1024
X
X$(DEST)/tty:	tty.c
X	cc $(CFLAGS) -o $(DEST)/tty tty.c
X	@chmod $(MODE) $(DEST)/tty
X	@chmem =$(MEMSIZ) $(DEST)/tty >/dev/null
X	@echo "done."
X
END_OF_Makefile
if test 229 -ne `wc -c <Makefile`; then
    echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f makelibc -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"makelibc\"
else
echo shar: Extracting \"makelibc\" \(2053 characters\)
sed "s/^X//" >makelibc <<'END_OF_makelibc'
X#
X#  makelibc - Make C library
X#
X#				- NOTE -
X#
X#  This shell script will -REMOVE- the old version of libc.a if it
X#  exists in the current directory.  It will also -REPLACE- the old
X#  /usr/lib/libc.a with version just packaged.
X#
Xif (test -f ./libc.a)		# remove old libc.a if it exists
X	then rm ./libc.a
Xfi
Xar av libc.a getgrp.s					# process groups
Xar av libc.a termcap.s gtty.s stty.s			# v1.2 update
Xar av libc.a popen.s ctime.s system.s qsort.s		# v1.2 upgrade
Xar av libc.a regexp.s regsub.s
Xar av libc.a getopt.s getgrent.s getpwent.s crypt.s
Xar av libc.a ttyname.s
Xar av libc.a fdopen.s
Xar av libc.a fgets.s fprintf.s fputs.s fread.s freopen.s fclose.s
Xar av libc.a fopen.s fseek.s ftell.s fwrite.s gets.s scanf.s getc.s printdat.s
Xar av libc.a fflush.s setbuf.s sprintf.s doprintf.s putc.s ungetc.s strcmp.s
Xar av libc.a access.s chdir.s chmod.s chown.s chroot.s creat.s dup.s dup2.s
Xar av libc.a exec.s exit.s cleanup.s fork.s isatty.s fstat.s getegid.s getenv.s
Xar av libc.a geteuid.s getgid.s getpass.s close.s getuid.s ioctl.s kill.s
Xar av libc.a link.s lseek.s malloc.s brk.s brk2.s brksize.s mknod.s mktemp.s
Xar av libc.a getpid.s mount.s open.s perror.s pipe.s prints.s read.s setgid.s
Xar av libc.a setuid.s sleep.s alarm.s pause.s signal.s catchsig.s stat.s
Xar av libc.a stime.s strcat.s strcpy.s strlen.s strncat.s strncmp.s strncpy.s
Xar av libc.a ftime.s
Xar av libc.a sync.s time.s times.s umask.s umount.s unlink.s utime.s wait.s
Xar av libc.a stderr.s write.s syslib.s call.s atoi.s message.s sendrec.s
Xar av libc.a printk.s abort.s itoa.s stb.s abs.s atol.s ctype.s index.s bcopy.s
Xar av libc.a getutil.s rand.s rindex.s adi.s and.s cii.s cms.s cmu4.s com.s
Xar av libc.a csa2.s csb2.s cuu.s .dup.s dvi.s dvi4.s dvu.s dvu4.s exg.s fakfp.s
Xar av libc.a gto.s iaar.s ilar.s inn.s ior.s isar.s lar2.s loi.s mli.s mli4.s
Xar av libc.a ngi.s nop.s rck.s rmi.s rmi4.s rmu.s rmu4.s rol.s ror.s sar2.s
Xar av libc.a sbi.s set.s sli.s sri.s sti.s xor.s error.s unknown.s trp.s
Xar av libc.a setjmp.s
X
Xcp libc.a /usr/lib
Xecho
Xecho "Done."
Xecho
X
END_OF_makelibc
if test 2053 -ne `wc -c <makelibc`; then
    echo shar: \"makelibc\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f tty.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"tty.c\"
else
echo shar: Extracting \"tty.c\" \(437 characters\)
sed "s/^X//" >tty.c <<'END_OF_tty.c'
X/*
X * tty.c - Return tty name
X *
X * Freename P. Pascal IV
X *
X */
X/* 
X *  History:
X *
X *	31 July 87	fpp	Creation
X */
X#include <stdio.h>
X
Xchar	*ttyname();
X
Xmain(argc, argv)
Xint  argc;
Xchar *argv[];
X{
X  char *tty_name;
X
X  tty_name = ttyname( 0 );
X  if(( argc == 2 ) && ( !strcmp(argv[1], "-s") ))
X	/* do nothing - shhh! we're in silent mode */;
X  else
X	printf("%s\n", ((tty_name) ? tty_name : "not a tty"));
X  exit((tty_name) ? 0 : 1 );
X}
X
END_OF_tty.c
if test 437 -ne `wc -c <tty.c`; then
    echo shar: \"tty.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f ttyname.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ttyname.c\"
else
echo shar: Extracting \"ttyname.c\" \(1517 characters\)
sed "s/^X//" >ttyname.c <<'END_OF_ttyname.c'
X/*
X *  char  *tty_name( fd )
X *    FILE  fd;
X *
X *  EFFECTS:
X *
X *       returns the path to the tty DEVice referenced by 'fd'.
X *  if 'fd' does not reference a tty a NULL is returned.
X */
X#include <sys/file.h>
X#include <sys/types.h>
X#include <sys/dir.h>
X#include <sys/stat.h>
X
X#define	STDIN		0
X#define	NULL		0
X#define DEV_PATH	"/dev/"
X
Xchar	*strcpy();
Xchar	*strcat();
Xchar	*ttyname();
X
Xchar  *ttyname(fd)
X  register	fd;
X{
X  struct stat		fd_stat, dev_stat;
X  static char		buf[40];
X  struct direct		dir;
X  register		dev_fd;
X
X  if (!isatty(fd))				/* is fd a tty? 	*/ 
X	return( NULL );
X
X  if (fstat(fd, &fd_stat) < 0) 			/* get tty status 	*/ 
X	return( NULL );
X
X  if (( fd_stat.st_mode & S_IFMT ) != S_IFCHR)	/* is it a char device?	*/
X	return( NULL );
X
X  if (( dev_fd = open( DEV_PATH, O_RDONLY) ) < 0) {
X	perror( DEV_PATH );
X	return( NULL );
X  }
X
X  /*
X   *  read throuh /dev/ and look at each entry an compare.  If the inode 
X   *  equal then we have found our match.  else continue looking
X   */
X  while ( read(dev_fd, &dir, sizeof(dir) )) { 
X	if (dir.d_ino == NO_ENTRY)		/* there's no file here */
X		continue;
X	if (dir.d_ino != fd_stat.st_ino)
X		continue;
X
X	strcpy( buf, DEV_PATH );		/* build new device name */
X	strcat( buf, dir.d_name );
X	if ( stat( buf, &dev_stat ) < 0 ) {	/* get device status	*/
X		perror( buf );
X		continue;
X	}
X	/*
X	 *	if the inodes match we have found our tty.
X	 */
X	if ( dev_stat.st_ino == fd_stat.st_ino ) {
X		close( dev_fd );
X		return( buf );
X	}
X  }
X  close( dev_fd );
X  return( NULL );
X}
END_OF_ttyname.c
if test 1517 -ne `wc -c <ttyname.c`; then
    echo shar: \"ttyname.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 1 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0