/dev/fd device driver source code

Mike Ditto ford at kenobi.commodore.com
Mon Jul 3 06:30:40 AEST 1989


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	Install
#	dup.c
# This archive created: Sun Jul  2 16:35:21 1989
export PATH; PATH=/bin:$PATH
echo shar: extracting "'README'" '(948 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
cat << \SHAR_EOF > 'README'
The /dev/fd driver effectively allows you to specify an already-open
file descriptor number where a path name is normally required. This
allows a program that demands a path name to be used to process
standard input or standard output by specifying /dev/fd/0 or /dev/fd/1,
respectively.

/dev/stdin and /dev/sdtout are links to the respective entries in
/dev/fd/.

This is primarily useful when running a poorly-written program that
has no provision for using stdin/stdout.  For example:

	find . -cpio /dev/stdout | compress > /dev/rfp021

(The "-cpio" option of the "find" command demands a file name and
normally can not write to standard output.  This example gives it
the name "/dev/stdout" allowing it to be piped directly to compress
rather than writing to a temporary file.)

Note that some programs which demand a file name do so because
they must lseek(2) on the file, and thus will not work if told
to process stdin and stdin is a pipe.
SHAR_EOF
if test 948 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 948 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'Install'" '(794 characters)'
if test -f 'Install'
then
	echo shar: will not over-write existing file "'Install'"
else
cat << \SHAR_EOF > 'Install'
MODULE=dup
make ${MODULE}.o || exit	# Don't bother with anything if we can't make.

NOFILE=20			# Could be more than 20, but why bother?

set -e

[ -d /dev/fd ] && [ -c /dev/stdin ] ||
{
	major=`/etc/masterupd -c ${MODULE} 2>/dev/null ||
			/etc/masterupd -a char open release ${MODULE} &&
			/etc/masterupd -c ${MODULE} `
	rm -rf /dev/fd /dev/stdin /dev/stdout /dev/stderr
	mkdir /dev/fd
	minor=0
	while [ $minor -lt $NOFILE ]
	do
		mknod /dev/fd/$minor c $major $minor
		chmod 666 /dev/fd/$minor
		minor=`expr $minor + 1`
	done
	ln -f /dev/fd/0 /dev/stdin
	ln -f /dev/fd/1 /dev/stdout
	ln -f /dev/fd/2 /dev/stderr
}
cp ${MODULE}.o /etc/lddrv
cd /etc/lddrv
./lddrv -q ${MODULE} && ./lddrv -dv ${MODULE}
./lddrv -av ${MODULE}
grep "^${MODULE}$" drivers > /dev/null || echo ${MODULE} >> drivers
SHAR_EOF
if test 794 -ne "`wc -c < 'Install'`"
then
	echo shar: error transmitting "'Install'" '(should have been 794 characters)'
fi
chmod +x 'Install'
fi # end of overwriting check
echo shar: extracting "'dup.c'" '(1645 characters)'
if test -f 'dup.c'
then
	echo shar: will not over-write existing file "'dup.c'"
else
cat << \SHAR_EOF > 'dup.c'
/*
 * dup.c - /dev/fd driver
 *
 * open("/dev/fd/N") is (theoretically) equivalent to dup(N).
 *
 *
 * This program was written by me, Mike "Ford" Ditto, and
 * I hereby release it into the public domain in the interest
 * of promoting the development of free, quality software
 * for the hackers and users of the world.
 *
 * Feel free to use, copy, modify, improve, and redistribute
 * this program, but please keep in mind the spirit of this
 * contribution; always provide source, and always allow
 * free redistribution (shareware is fine with me).
 *
 *				-=] Ford [=-
 */

#define KERNEL		1

#include "sys/param.h"
#include "sys/types.h"
#include "sys/sysmacros.h"
#include "sys/systm.h"
#include "sys/file.h"
#include "sys/conf.h"
#ifdef SVR3
#include "sys/immu.h"
#include "sys/region.h"
#include "sys/stream.h"
#endif
#include "sys/proc.h"
#include "sys/tty.h"
#include "sys/signal.h"
#include "sys/dir.h"
#include "sys/user.h"
#include "sys/errno.h"


void dupopen(dev)
register dev_t dev;
{
    register struct file *fp;
    register int fd;

    /* First, validate the existing fd to be duped */
    if (!getf(minor(dev)))
	return;

    /* Make sure we are not about to return the same fd that was requested */
    fd = u.u_rval1;
    if (minor(dev) == fd)
    {
	u.u_error = EBADF;
	return;
    }

    /* Next, find (and undo) the half-open new fd */
    if (fp = getf(fd)) 
    {
	plock(fp->f_inode);
	iput(fp->f_inode);
	if (--fp->f_count <= 0)
	{
	    fp->f_next = ffreelist;
	    ffreelist = fp;
	}
    }

    /* Finally, dup the existing fd */
    ++(u.u_ofile[fd] = u.u_ofile[minor(dev)])->f_count;
}

void duprelease()
{
}
SHAR_EOF
if test 1645 -ne "`wc -c < 'dup.c'`"
then
	echo shar: error transmitting "'dup.c'" '(should have been 1645 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0

					-=] Ford [=-

"The number of Unix installations	(In Real Life:  Mike Ditto)
has grown to 10, with more expected."	ford at kenobi.commodore.com
- The Unix Programmer's Manual,		...!ucsd!crash!kenobi!ford
  2nd Edition, June, 1972.		ditto at amix.commodore.com



More information about the Unix-pc.sources mailing list