Path: utzoo!mnetor!uunet!ndsuvax!ncpascal From: ncpascal@ndsuvax.UUCP (Freeman Pascal) Newsgroups: comp.os.minix Subject: tty utility and ttyname(3) library call for MINIX Message-ID: <582@ndsuvax.UUCP> Date: 17 Dec 87 05:39:19 GMT Reply-To: ncpascal@ndsuvax.UUCP (Freeman Pascal) Organization: North Dakota State University Fargo, ND Lines: 272 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 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 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 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 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 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 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 X#include X#include X#include 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