home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / dup / part01 < prev    next >
Encoding:
Text File  |  1993-01-29  |  9.2 KB  |  309 lines

  1. Newsgroups: comp.sources.unix
  2. From: xenitec!golem!davidf@uunet.ca (David J. Fiander)
  3. Subject: v26i087: dup - a /dev/fd driver for Xenix/386 2.3+, Part01/01
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: xenitec!golem!davidf@uunet.ca (David J. Fiander)
  8. Posting-Number: Volume 26, Issue 87
  9. Archive-Name: dup/part01
  10.  
  11. [ This source is particular to SCO Xenix/386 2.3+.  If you don't have that,
  12.   you probably don't want this.  I don't have it, so I didn't test this.
  13.   --vix ]
  14.  
  15. The /dev/fd driver effectively allows you to specify an already-open file
  16. descriptor number where a path name is normally required. This allows a
  17. program that demands a path name to be used to process standard input or
  18. standard output by specifying /dev/fd/0 or /dev/fd/1, respectively.
  19.  
  20.     xenitec!golem!davidf@uunet.ca (David J. Fiander)
  21.  
  22. #! /bin/sh
  23. # This is a shell archive.  Remove anything before this line, then unpack
  24. # it by saving it into a file and typing "sh file".  To overwrite existing
  25. # files, type "sh file -c".  You can also feed this as standard input via
  26. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  27. # will see the following message at the end:
  28. #        "End of archive 1 (of 1)."
  29. # Contents:  Install MANIFEST Makefile README dup.c
  30. # Wrapped by vixie@cognition.pa.dec.com on Sat Jan 30 17:03:06 1993
  31. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  32. if test -f 'Install' -a "${1}" != "-c" ; then 
  33.   echo shar: Will not clobber existing file \"'Install'\"
  34. else
  35. echo shar: Extracting \"'Install'\" \(931 characters\)
  36. sed "s/^X//" >'Install' <<'END_OF_FILE'
  37. MODULE=dup
  38. make ${MODULE}.o || exit    # Don't bother with anything if we can't make.
  39. X
  40. NOFILE=20    # Do we really need more?
  41. X
  42. set -e
  43. X
  44. cd /usr/sys/conf
  45. X
  46. X[ -d /dev/fd ] && [ -c /dev/stdin ] ||
  47. X{
  48. X    echo 'Making files in /dev'
  49. X    major=`./configure -j NEXTMAJOR`
  50. X
  51. X    echo 'The major device number for the /dev/fd driver is' $major
  52. X
  53. X    echo 'Adding "../io/dup/dup.o" to link_xenix'
  54. X    ed - ./link_xenix <<\SCRIPT
  55. X/uts.o/a
  56. X    ../io/dup/dup.o \
  57. X.
  58. w
  59. q
  60. SCRIPT
  61. X
  62. X    echo 'Updating master and xenixconf files'
  63. X    ./configure -c -m $major -a dupopen
  64. X
  65. X    rm -rf /dev/fd /dev/stdin /dev/stdout /dev/stderr
  66. X    mkdir /dev/fd
  67. X    chmod 755 /dev/fd
  68. X    minor=0
  69. X    while [ $minor -lt $NOFILE ]
  70. X    do
  71. X        mknod /dev/fd/$minor c $major $minor
  72. X        chmod 666 /dev/fd/$minor
  73. X        minor=`expr $minor + 1`
  74. X    done
  75. X    ln -f /dev/fd/0 /dev/stdin
  76. X    ln -f /dev/fd/1 /dev/stdout
  77. X    ln -f /dev/fd/2 /dev/stderr
  78. X}
  79. X
  80. echo 'Building new xenix'
  81. X./link_xenix
  82. X
  83. echo 'You must now install the new xenix in / and reboot'
  84. END_OF_FILE
  85. if test 931 -ne `wc -c <'Install'`; then
  86.     echo shar: \"'Install'\" unpacked with wrong size!
  87. fi
  88. # end of 'Install'
  89. fi
  90. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  91.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  92. else
  93. echo shar: Extracting \"'MANIFEST'\" \(251 characters\)
  94. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  95. X   File Name        Archive #    Description
  96. X-----------------------------------------------------------
  97. X Install                    1    
  98. X MANIFEST                   1    
  99. X Makefile                   1    
  100. X README                     1    
  101. X dup.c                      1    
  102. END_OF_FILE
  103. if test 251 -ne `wc -c <'MANIFEST'`; then
  104.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  105. fi
  106. # end of 'MANIFEST'
  107. fi
  108. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  109.   echo shar: Will not clobber existing file \"'Makefile'\"
  110. else
  111. echo shar: Extracting \"'Makefile'\" \(93 characters\)
  112. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  113. CFLAGS= -O -DXENIX -DM_KERNEL -M3e -Zp4 -I/usr/sys/h
  114. X
  115. dup.o: dup.c
  116. X    $(CC) $(CFLAGS) -c dup.c
  117. END_OF_FILE
  118. if test 93 -ne `wc -c <'Makefile'`; then
  119.     echo shar: \"'Makefile'\" unpacked with wrong size!
  120. fi
  121. # end of 'Makefile'
  122. fi
  123. if test -f 'README' -a "${1}" != "-c" ; then 
  124.   echo shar: Will not clobber existing file \"'README'\"
  125. else
  126. echo shar: Extracting \"'README'\" \(1777 characters\)
  127. sed "s/^X//" >'README' <<'END_OF_FILE'
  128. The Source
  129. X----------
  130. X
  131. This /dev/fd driver is loosely based on one written by Mike
  132. X"Ford" Ditto for the AT&T UNIX pc.  This version runs on SCO
  133. XXENIX 386, version 2.3+.  The install script included will
  134. configure the kernel, edit /usr/sys/conf/link_xenix, and build
  135. the new kernel and device files in /dev/fd.
  136. X
  137. Building the driver
  138. X-------------------
  139. X
  140. To properly build everything, move the Makefile, dup.c and
  141. Install script into a new directory called /usr/sys/io/dup, and
  142. run make in that directory.  Then run Install.
  143. X
  144. What /dev/fd is
  145. X---------------
  146. The /dev/fd driver effectively allows you to specify an already-open
  147. file descriptor number where a path name is normally required. This
  148. allows a program that demands a path name to be used to process
  149. standard input or standard output by specifying /dev/fd/0 or /dev/fd/1,
  150. respectively.
  151. X
  152. X/dev/stdin and /dev/sdtout are links to the respective entries in
  153. X/dev/fd/.
  154. X
  155. This is primarily useful when running a poorly-written program that
  156. has no provision for using stdin/stdout.  For example:
  157. X
  158. X    find . -cpio /dev/stdout | compress > /dev/rfp021
  159. X
  160. X(The "-cpio" option of the "find" command demands a file name and
  161. normally can not write to standard output.  This example gives it
  162. the name "/dev/stdout" allowing it to be piped directly to compress
  163. rather than writing to a temporary file.)
  164. X
  165. Note that some programs which demand a file name do so because
  166. they must lseek(2) on the file, and thus will not work if told
  167. to process stdin and stdin is a pipe.
  168. X
  169. The most common use for the /dev/fd driver that I have found is
  170. for "process substitution" under the shell rc, and those
  171. versions of the Korn shell that support it.  For example, to
  172. use jove's "parse-error-file" function, you could say
  173. X
  174. X    ; jove -p <{egrep -n funcName *.c}
  175. END_OF_FILE
  176. if test 1777 -ne `wc -c <'README'`; then
  177.     echo shar: \"'README'\" unpacked with wrong size!
  178. fi
  179. # end of 'README'
  180. fi
  181. if test -f 'dup.c' -a "${1}" != "-c" ; then 
  182.   echo shar: Will not clobber existing file \"'dup.c'\"
  183. else
  184. echo shar: Extracting \"'dup.c'\" \(2750 characters\)
  185. sed "s/^X//" >'dup.c' <<'END_OF_FILE'
  186. X/*
  187. X * dup.c - /dev/fd driver
  188. X *
  189. X * open("/dev/fd/N") is (theoretically) equivalent to dup(N).
  190. X *
  191. X *
  192. X * The Ditto version of this driver was ported to SCO XENIX by
  193. X * David J. Fiander, with several enhancements added on the way.
  194. X * This version is _not_ in the public domain.  The following
  195. X * copyright notice now applies.
  196. X *
  197. X * Copyright 1992, David J.  Fiander.  Free distribution of
  198. X * unmodified versions of this source is permitted.
  199. X *
  200. X * Redistribution and use in source and binary forms are permitted
  201. X * provided that the above copyright notice is duplicated in all
  202. X * such forms and that any documentation related to such
  203. X * distribution and use acknowlege that the software was
  204. X * developed by David J. Fiander.
  205. X *
  206. X * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  207. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  208. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  209. X * PURPOSE.
  210. X *
  211. X * (Hey, it worked for Mark Colburn, didn't it?)
  212. X *
  213. X * This program was written by me, Mike "Ford" Ditto, and
  214. X * I hereby release it into the public domain in the interest
  215. X * of promoting the development of free, quality software
  216. X * for the hackers and users of the world.
  217. X *
  218. X * Feel free to use, copy, modify, improve, and redistribute
  219. X * this program, but please keep in mind the spirit of this
  220. X * contribution; always provide source, and always allow
  221. X * free redistribution (shareware is fine with me).
  222. X *
  223. X *                -=] Ford [=-
  224. X */
  225. X
  226. X#define KERNEL        1
  227. X
  228. X#include "sys/types.h"
  229. X#include "sys/param.h"
  230. X#include "sys/sysmacros.h"
  231. X#include "sys/dir.h"
  232. X#include "sys/signal.h"
  233. X#include "sys/page.h"
  234. X#include "sys/seg.h"
  235. X#include "sys/user.h"
  236. X#include "sys/file.h"
  237. X#include "sys/systm.h"
  238. X#include "sys/errno.h"
  239. X
  240. void dupopen(dev, flag, id)
  241. register dev_t    dev;
  242. int        flag;
  243. int        id;
  244. X{
  245. X    register struct file *fp;
  246. X    register int fd;
  247. X
  248. X    /* First, validate the existing fd to be duped */
  249. X    if (!(fp = getf(minor(dev))))
  250. X    return;
  251. X
  252. X    /* Make sure we are not about to return the same fd that was requested */
  253. X    fd = u.u_rval1;
  254. X    if (minor(dev) == fd)
  255. X    {
  256. X    u.u_error = EBADF;
  257. X    return;
  258. X    }
  259. X
  260. X    /*
  261. X     * Make sure that the flags requested are a subset of the
  262. X     * flags already open. That is make sure that the
  263. X     * intersection of the requested flags and the existing flags
  264. X     * == the requested flags
  265. X     */
  266. X    if (((fp->f_flag & (FREAD|FWRITE)) & flag) != (flag & (FREAD|FWRITE)))
  267. X    {
  268. X    u.u_error = EBADF;
  269. X    return;
  270. X    }
  271. X    
  272. X    /* Next, find (and undo) the half-open new fd */
  273. X    if (fp = getf(fd)) 
  274. X    {
  275. X    plock(fp->f_inode);
  276. X    iput(fp->f_inode);
  277. X    if (--fp->f_count <= 0)
  278. X    {
  279. X        fp->f_inode = NULL;
  280. X    }
  281. X    }
  282. X
  283. X    /* Finally, dup the existing fd */
  284. X    ++(u.u_ofile[fd] = u.u_ofile[minor(dev)])->f_count;
  285. X}
  286. END_OF_FILE
  287. if test 2750 -ne `wc -c <'dup.c'`; then
  288.     echo shar: \"'dup.c'\" unpacked with wrong size!
  289. fi
  290. # end of 'dup.c'
  291. fi
  292. echo shar: End of archive 1 \(of 1\).
  293. cp /dev/null ark1isdone
  294. MISSING=""
  295. for I in 1 ; do
  296.     if test ! -f ark${I}isdone ; then
  297.     MISSING="${MISSING} ${I}"
  298.     fi
  299. done
  300. if test "${MISSING}" = "" ; then
  301.     echo You have the archive.
  302.     rm -f ark[1-9]isdone
  303. else
  304.     echo You still need to unpack the following archives:
  305.     echo "        " ${MISSING}
  306. fi
  307. ##  End of shell archive.
  308. exit 0
  309.