8 Commits

Author SHA1 Message Date
Ben Gras
2ec255bb5e fix from trunk: don't re-use mountpoints 2010-06-11 11:43:31 +00:00
Ben Gras
cd445fe22a assert conditions fix from trunk. 2010-06-09 12:56:44 +00:00
Ben Gras
64b1205e6c fix from trunk for assert(ptproc == newptproc) without vm_stop on shutdown. 2010-06-07 22:24:21 +00:00
Ben Gras
94881e6d4a make includes fix from trunk 2010-06-07 15:51:50 +00:00
Ben Gras
6677a8c8e0 mfs fix by tveerman from trunk. 2010-06-07 15:46:26 +00:00
Arun Thomas
255ad2ab37 Remove gcc-4.1.1 from packages list 2010-06-04 12:40:15 +00:00
Ben Gras
7dc782dd1a from trunk:
------------------------------------------------------------------------
r7154 | thruby | 2010-06-04 10:54:43 +0000 (Fri, 04 Jun 2010) | 1 line

Removed a buggy assert unintentionally commted in r7044
2010-06-04 11:06:44 +00:00
Ben Gras
e9eb2c4f8b 3.1.7 branch. 2010-06-03 11:33:56 +00:00
48869 changed files with 569085 additions and 8903613 deletions

43
.gitignore vendored
View File

@@ -1,43 +0,0 @@
cscope.*
*.swap
.*.sw[po]
.sw[po]
*~
*.orig
*.diff
*.patch
*.rej
*.[1-9].gz
*.o
*.[psS]o
*.pico
lib*.so*
*.a
*.so.*
*.d
.depend
nbsdsrc/*
TAGS
tags
GPATH
GRTAGS
GSYMS
GTAGS
\#*#
CVS
!/.gitignore
.gitignore
.svn
minix-port.patch
*.worldstone.log
.worldstone*
usr.bin/mdocml/man/*.7
etc/passwd
etc/pwd.db
etc/spwd.db
releasetools/image
releasetools/kernel
releasetools/revision
share/zoneinfo/builddir
lib/libc/compat__*
.gdbinit

View File

@@ -1,17 +0,0 @@
# MINIX 3 code review setup.
#
# This gerrit code review configuration is meant for performing per commit
# internal code reviews. If you are an external contributor please read and
# apply http://wiki.minix3.org/en/HowToContribute
#
# Consider reading http://wiki.openstack.org/GerritWorkflow to learn how
# to install git review on a Linux setup. On Minix you can use pkgin to
# install git-review.
#
[gerrit]
# The git-minix host is to be configured in your .ssh/config
#
host=gerrit.minix3.org
port=2222
project=minix
defaultbranch=master

638
Makefile
View File

@@ -1,576 +1,92 @@
# $NetBSD: Makefile,v 1.303 2013/07/16 09:52:21 joerg Exp $
# Master Makefile to compile everything in /usr/src except the system.
#
# This is the top-level makefile for building NetBSD. For an outline of
# how to build a snapshot or release, as well as other release engineering
# information, see http://www.NetBSD.org/developers/releng/index.html
#
# Not everything you can set or do is documented in this makefile. In
# particular, you should review the files in /usr/share/mk (especially
# bsd.README) for general information on building programs and writing
# Makefiles within this structure, and see the comments in src/etc/Makefile
# for further information on installation and release set options.
#
# Variables listed below can be set on the make command line (highest
# priority), in /etc/mk.conf (middle priority), or in the environment
# (lowest priority).
#
# Variables:
# DESTDIR is the target directory for installation of the compiled
# software. It defaults to /. Note that programs are built against
# libraries installed in DESTDIR.
# MKMAN, if `no', will prevent building of manual pages.
# MKOBJDIRS, if not `no', will build object directories at
# an appropriate point in a build.
# MKSHARE, if `no', will prevent building and installing
# anything in /usr/share.
# MKUPDATE, if not `no', will avoid a `make cleandir' at the start of
# `make build', as well as having the effects listed in
# /usr/share/mk/bsd.README.
# NOCLEANDIR, if defined, will avoid a `make cleandir' at the start
# of the `make build'.
# NOINCLUDES will avoid the `make includes' usually done by `make build'.
#
# See mk.conf(5) for more details.
#
#
# Targets:
# build:
# Builds a full release of NetBSD in DESTDIR, except for the
# /etc configuration files.
# If BUILD_DONE is set, this is an empty target.
# distribution:
# Builds a full release of NetBSD in DESTDIR, including the /etc
# configuration files.
# buildworld:
# As per `make distribution', except that it ensures that DESTDIR
# is not the root directory.
# installworld:
# Install the distribution from DESTDIR to INSTALLWORLDDIR (which
# defaults to the root directory). Ensures that INSTALLWORLDDIR
# is not the root directory if cross compiling.
# release:
# Does a `make distribution', and then tars up the DESTDIR files
# into ${RELEASEDIR}/${RELEASEMACHINEDIR}, in release(7) format.
# (See etc/Makefile for more information on this.)
# regression-tests:
# Runs the regression tests in "regress" on this host.
# sets:
# Populate ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
# from ${DESTDIR}
# sourcesets:
# Populate ${RELEASEDIR}/source/sets from ${NETBSDSRCDIR}
# syspkgs:
# Populate ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/syspkgs
# from ${DESTDIR}
# iso-image:
# Create CD-ROM image in RELEASEDIR/iso.
# RELEASEDIR must already have been populated by `make release'
# or equivalent.
# iso-image-source:
# Create CD-ROM image with source in RELEASEDIR/iso.
# RELEASEDIR must already have been populated by
# `make release sourcesets' or equivalent.
# live-image:
# Create bootable live image for emulators or USB stick etc.
# in RELEASEDIR/liveimage.
# RELEASEDIR must already have been populated by `make release'
# or equivalent.
# install-image:
# Create bootable installation image for USB stick etc.
# in RELEASEDIR/installimage.
# RELEASEDIR must already have been populated by `make release'
# or equivalent.
#
# Targets invoked by `make build,' in order:
# cleandir: cleans the tree.
# do-top-obj: creates the top level object directory.
# do-tools-obj: creates object directories for the host toolchain.
# do-tools: builds host toolchain.
# params: record the values of variables that might affect the
# build.
# obj: creates object directories.
# do-distrib-dirs: creates the distribution directories.
# includes: installs include files.
# do-lib: builds and installs prerequisites from lib
# if ${MKCOMPAT} != "no".
# do-compat-lib: builds and installs prerequisites from compat/lib
# if ${MKCOMPAT} != "no".
# do-build: builds and installs the entire system.
# do-x11: builds and installs X11 if ${MKX11} != "no"; either
# X11R7 from src/external/mit/xorg if ${X11FLAVOUR} == "Xorg"
# or X11R6 from src/x11
# do-extsrc: builds and installs extsrc if ${MKEXTSRC} != "no".
# do-obsolete: installs the obsolete sets (for the postinstall-* targets).
#
MAKE=make
.if ${.MAKEFLAGS:M${.CURDIR}/share/mk} == ""
.MAKEFLAGS: -m ${.CURDIR}/share/mk
usage:
@echo ""
@echo "Master Makefile for MINIX commands and utilities."
@echo "Root privileges are required for some actions."
@echo ""
@echo "Usage:"
@echo " make world # Compile everything (libraries & commands)"
@echo " make includes # Install include files from src/"
@echo " make libraries # Compile and install libraries"
@echo " make commands # Compile all, commands, but don't install"
@echo " make install # Compile and install commands"
@echo " make depend # Generate required .depend files"
@echo " make gnu-includes # Install include files for GCC"
@echo " make gnu-libraries # Compile and install libraries for GCC"
@echo " make clean # Remove all compiler results"
@echo ""
@echo "Run 'make' in tools/ to create a new MINIX configuration."
@echo ""
# world has to be able to make a new system, even if there
# is no complete old system. it has to install commands, for which
# it has to install libraries, for which it has to install includes,
# for which it has to install /etc (for users and ownerships).
# etcfiles also creates a directory hierarchy in its
# 'make install' target.
#
# etcfiles has to be done first.
.if ${COMPILER_TYPE} == "ack"
world: mkfiles includes depend libraries install
.elif ${COMPILER_TYPE} == "gnu"
world: mkfiles includes depend gnu-libraries install
.endif
#
# If _SRC_TOP_OBJ_ gets set here, we will end up with a directory that may
# not be the top level objdir, because "make obj" can happen in the *middle*
# of "make build" (long after <bsd.own.mk> is calculated it). So, pre-set
# _SRC_TOP_OBJ_ here so it will not be added to ${.MAKEOVERRIDES}.
#
_SRC_TOP_OBJ_=
mkfiles:
cp etc/mk/*.mk /etc/mk/
.include <bsd.own.mk>
includes:
cd include && $(MAKE) includes
#
# Sanity check: make sure that "make build" is not invoked simultaneously
# with a standard recursive target.
#
libraries:
cd lib && sh ack_build.sh obj depend all install
.if make(build) || make(release) || make(snapshot)
.for targ in ${TARGETS:Nobj:Ncleandir}
.if make(${targ}) && !target(.BEGIN)
.BEGIN:
@echo 'BUILD ABORTED: "make build" and "make ${targ}" are mutually exclusive.'
@false
.endif
.endfor
.endif
MKHEADERS411=/usr/gnu/libexec/gcc/i386-pc-minix/4.1.1/install-tools/mkheaders
MKHEADERS443=/usr/gnu/libexec/gcc/i686-pc-minix/4.4.3/install-tools/mkheaders
gnu-includes: includes
SHELL=/bin/sh; if [ -f $(MKHEADERS411) ] ; then sh -e $(MKHEADERS411) ; fi
SHELL=/bin/sh; if [ -f $(MKHEADERS443) ] ; then sh -e $(MKHEADERS443) ; fi
#
# _SUBDIR is used to set SUBDIR, after removing directories that have
# BUILD_${dir}=no, or that have no ${dir}/Makefile.
#
_SUBDIR= tools lib include gnu external crypto/external bin games
_SUBDIR+= libexec sbin usr.bin
_SUBDIR+= usr.sbin share sys etc tests compat
_SUBDIR+= .WAIT rescue .WAIT distrib regress
.if defined(__MINIX)
# the minix subdir depends on some other things (e.g. lib/)
_SUBDIR+= .WAIT minix
.endif # defined(__MINIX)
gnu-libraries:
cd lib && sh gnu_build.sh obj depend all install
.for dir in ${_SUBDIR}
.if "${dir}" == ".WAIT" \
|| (${BUILD_${dir}:Uyes} != "no" && exists(${dir}/Makefile))
SUBDIR+= ${dir}
.endif
.endfor
commands:
cd commands && $(MAKE) all
.if exists(regress)
regression-tests: .PHONY .MAKE
@echo Running regression tests...
${MAKEDIRTARGET} regress regress
.endif
depend::
cd boot && $(MAKE) depend
cd commands && $(MAKE) depend
cd kernel && $(MAKE) depend
cd servers && $(MAKE) depend
cd drivers && $(MAKE) depend
.if ${MKUNPRIVED} != "no"
NOPOSTINSTALL= # defined
.endif
etcfiles::
cd etc && $(MAKE) install
afterinstall: .PHONY .MAKE
.if ${MKMAN} != "no"
.if !defined(__MINIX)
${MAKEDIRTARGET} share/man makedb
.else
${MAKEDIRTARGET} minix/man makedb
.endif # !defined(__MINIX)
.endif
.if (${MKUNPRIVED} != "no" && ${MKINFO} != "no")
${MAKEDIRTARGET} gnu/usr.bin/texinfo/install-info infodir-meta
.endif
.if !defined(NOPOSTINSTALL)
${MAKEDIRTARGET} . postinstall-check
.endif
all::
cd boot && $(MAKE) all
cd commands && $(MAKE) all
cd tools && $(MAKE) all
_POSTINSTALL= ${.CURDIR}/usr.sbin/postinstall/postinstall
_POSTINSTALL_ENV= \
AWK=${TOOL_AWK:Q} \
DB=${TOOL_DB:Q} \
HOST_SH=${HOST_SH:Q} \
MAKE=${MAKE:Q} \
PWD_MKDB=${TOOL_PWD_MKDB:Q} \
STAT=${TOOL_STAT:Q}
install::
cd boot && $(MAKE) install
cd man && $(MAKE) install makedb
cd commands && $(MAKE) install
cd share && $(MAKE) install
cd tools && $(MAKE) install
postinstall-check: .PHONY
@echo " === Post installation checks ==="
${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} -d ${DESTDIR}/ check; if [ $$? -gt 1 ]; then exit 1; fi
@echo " ================================"
clean::
cd boot && $(MAKE) clean
cd commands && $(MAKE) clean
cd tools && $(MAKE) clean
cd lib && sh ack_build.sh clean
cd lib && sh gnu_build.sh clean
cd test && $(MAKE) clean
postinstall-fix: .NOTMAIN .PHONY
@echo " === Post installation fixes ==="
${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} -d ${DESTDIR}/ fix
@echo " ==============================="
postinstall-fix-obsolete: .NOTMAIN .PHONY
@echo " === Removing obsolete files ==="
${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} -d ${DESTDIR}/ fix obsolete
@echo " ==============================="
#
# Targets (in order!) called by "make build".
#
.if defined(HAVE_GCC)
.if ${HAVE_GCC} == "4"
LIBGCC_EXT=4
BUILD_CC_LIB_BASEDIR= gnu/lib
BUILD_CC_LIB_BASETARGET= gnu-lib
.else
LIBGCC_EXT=
BUILD_CC_LIB_BASEDIR= external/gpl3/gcc/lib
BUILD_CC_LIB_BASETARGET= external-gpl3-gcc-lib
.endif
.endif
BUILDTARGETS+= check-tools
.if ${MKUPDATE} == "no" && !defined(NOCLEANDIR)
BUILDTARGETS+= cleandir
.endif
.if ${MKOBJDIRS} != "no"
BUILDTARGETS+= do-top-obj
.endif
.if ${USETOOLS} == "yes" # {
.if ${MKOBJDIRS} != "no"
BUILDTARGETS+= do-tools-obj
.endif
BUILDTARGETS+= do-tools
.endif # USETOOLS # }
BUILDTARGETS+= params
.if ${MKOBJDIRS} != "no"
BUILDTARGETS+= obj
.endif
BUILDTARGETS+= clean_METALOG
.if !defined(NODISTRIBDIRS)
BUILDTARGETS+= do-distrib-dirs
.endif
.if !defined(NOINCLUDES)
BUILDTARGETS+= includes
.endif
BUILDTARGETS+= do-lib
.if !defined(__MINIX)
# LSC Not used in MINIX3
BUILDTARGETS+= do-compat-lib
.endif # !defined(__MINIX)
BUILDTARGETS+= do-build
.if ${MKX11} != "no"
BUILDTARGETS+= do-x11
.endif
.if ${MKEXTSRC} != "no"
BUILDTARGETS+= do-extsrc
.endif
.if defined(__MINIX) && ${MKSRC} == "yes"
BUILDTARGETS+= do-installsrc
.endif # defined(__MINIX)
BUILDTARGETS+= do-obsolete
#
# Enforce proper ordering of some rules.
#
.ORDER: ${BUILDTARGETS}
includes-lib: .PHONY includes-include includes-sys
includes-gnu: .PHONY includes-lib
#
# Record the values of variables that might affect the build.
# If no values have changed, avoid updating the timestamp
# of the params file.
#
# This is referenced by _NETBSD_VERSION_DEPENDS in <bsd.own.mk>.
#
.include "${NETBSDSRCDIR}/etc/Makefile.params"
CLEANDIRFILES+= params
params: .EXEC
${_MKMSG_CREATE} params
@${PRINT_PARAMS} >${.TARGET}.new
@if cmp -s ${.TARGET}.new ${.TARGET} > /dev/null 2>&1; then \
: "params is unchanged" ; \
rm ${.TARGET}.new ; \
else \
: "params has changed or is new" ; \
mv ${.TARGET}.new ${.TARGET} ; \
fi
#
# Display current make(1) parameters
#
show-params: .PHONY .MAKE
@${PRINT_PARAMS}
#
# Build the system and install into DESTDIR.
#
START_TIME!= date
.if defined(__MINIX)
world: build .PHONY .MAKE
${MAKEDIRTARGET} . etcfiles
@echo "WARNING: "
@echo "WARNING: The 'world' target is obsolete, please use 'build' instead."
@echo "WARNING: "
etcfiles: .PHONY .MAKE
${MAKEDIRTARGET} etc install-etc-files-safe DESTDIR=${DESTDIR:U/}
etcforce: .PHONY .MAKE
${MAKEDIRTARGET} etc install-etc-files DESTDIR=${DESTDIR:U/}
.endif # defined(__MINIX)
build: .PHONY .MAKE
.if defined(BUILD_DONE)
@echo "Build already installed into ${DESTDIR}"
.else
@echo "Build started at: ${START_TIME}"
.for tgt in ${BUILDTARGETS}
${MAKEDIRTARGET} . ${tgt}
.endfor
${MAKEDIRTARGET} etc install-etc-release
.if defined(__MINIX)
${MAKEDIRTARGET} etc install-etc-files-safe DESTDIR=${DESTDIR:U/}
${MAKEDIRTARGET} releasetools do-hdboot
.endif # defined(__MINIX)
@echo "Build started at: ${START_TIME}"
@printf "Build finished at: " && date
.endif
#
# Build a full distribution, but not a release (i.e. no sets into
# ${RELEASEDIR}). "buildworld" enforces a build to ${DESTDIR} != /
#
distribution buildworld: .PHONY .MAKE
.if make(buildworld) && \
(!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
@echo "Won't make ${.TARGET} with DESTDIR=/"
@false
.endif
${MAKEDIRTARGET} . build NOPOSTINSTALL=1
${MAKEDIRTARGET} etc distribution INSTALL_DONE=1
.if defined(DESTDIR) && ${DESTDIR} != "" && ${DESTDIR} != "/"
${MAKEDIRTARGET} . postinstall-fix-obsolete
${MAKEDIRTARGET} distrib/sets checkflist
.endif
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Install the distribution from $DESTDIR to $INSTALLWORLDDIR (defaults to `/')
# If installing to /, ensures that the host's operating system is NetBSD and
# the host's `uname -m` == ${MACHINE}.
#
HOST_UNAME_S!= uname -s
.if ${HOST_OSTYPE:C/\-.*//} != "Minix"
HOST_UNAME_M!= uname -m
.else
HOST_UNAME_M:= ${MACHINE}
.endif
installworld: .PHONY .MAKE
.if (!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
@echo "Can't make ${.TARGET} to DESTDIR=/"
@false
.endif
.if !defined(INSTALLWORLDDIR) || \
${INSTALLWORLDDIR} == "" || ${INSTALLWORLDDIR} == "/"
.if (${HOST_UNAME_S} != "NetBSD") && (${HOST_UNAME_S} != "Minix")
@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_S} to Minix with INSTALLWORLDDIR=/"
@false
.endif
.if (${HOST_UNAME_M} != ${MACHINE})
@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_M} to ${MACHINE} with INSTALLWORLDDIR=/"
@false
.endif
.endif
${MAKEDIRTARGET} distrib/sets installsets \
INSTALLDIR=${INSTALLWORLDDIR:U/} INSTALLSETS=${INSTALLSETS:Q}
${MAKEDIRTARGET} . postinstall-check DESTDIR=${INSTALLWORLDDIR}
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Install modules from $DESTDIR to $INSTALLMODULESDIR
#
installmodules: .PHONY .MAKE
.if (!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
@echo "Can't make ${.TARGET} to DESTDIR=/"
@false
.endif
.if !defined(INSTALLMODULESDIR) || \
${INSTALLMODULESDIR} == "" || ${INSTALLMODULESDIR} == "/"
.if (${HOST_UNAME_S} != "NetBSD")
@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_S} to NetBSD with INSTALLMODULESDIR=/"
@false
.endif
.if (${HOST_UNAME_M} != ${MACHINE})
@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_M} to ${MACHINE} with INSTALLMODULESDIR=/"
@false
.endif
.endif
${MAKEDIRTARGET} sys/modules install DESTDIR=${INSTALLMODULESDIR:U/}
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Create sets from $DESTDIR or $NETBSDSRCDIR into $RELEASEDIR
#
.for tgt in sets sourcesets syspkgs
${tgt}: .PHONY .MAKE
${MAKEDIRTARGET} distrib/sets ${tgt}
.endfor
#
# Build a release or snapshot (implies "make distribution"). Note that
# in this case, the set lists will be checked before the tar files
# are made.
#
release snapshot: .PHONY .MAKE
${MAKEDIRTARGET} . distribution
${MAKEDIRTARGET} etc release DISTRIBUTION_DONE=1
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Create a CD-ROM image.
#
iso-image: .PHONY
${MAKEDIRTARGET} distrib iso_image
${MAKEDIRTARGET} etc iso-image
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
iso-image-source: .PHONY
${MAKEDIRTARGET} distrib iso_image CDSOURCE=true
${MAKEDIRTARGET} etc iso-image
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Create bootable live images.
#
live-image: .PHONY
${MAKEDIRTARGET} etc live-image
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Create bootable installation images.
#
install-image: .PHONY
${MAKEDIRTARGET} etc install-image
@echo "make ${.TARGET} started at: ${START_TIME}"
@printf "make ${.TARGET} finished at: " && date
#
# Special components of the "make build" process.
#
check-tools: .PHONY
.if ${TOOLCHAIN_MISSING} != "no" && !defined(EXTERNAL_TOOLCHAIN)
@echo '*** WARNING: Building on MACHINE=${MACHINE} with missing toolchain.'
@echo '*** May result in a failed build or corrupt binaries!'
.elif defined(EXTERNAL_TOOLCHAIN)
@echo '*** Using external toolchain rooted at ${EXTERNAL_TOOLCHAIN}.'
.endif
.if defined(NBUILDJOBS)
@echo '*** WARNING: NBUILDJOBS is obsolete; use -j directly instead!'
.endif
# Delete or sanitise a leftover METALOG from a previous build.
clean_METALOG: .PHONY .MAKE
.if ${MKUPDATE} != "no"
${MAKEDIRTARGET} distrib/sets clean_METALOG
.endif
do-distrib-dirs: .PHONY .MAKE
.if !defined(DESTDIR) || ${DESTDIR} == ""
${MAKEDIRTARGET} etc distrib-dirs DESTDIR=/
.else
${MAKEDIRTARGET} etc distrib-dirs DESTDIR=${DESTDIR}
.endif
.for targ in cleandir obj includes
do-${targ}: .PHONY ${targ}
@true
.endfor
do-tools: .PHONY .MAKE
${MAKEDIRTARGET} tools build_install
do-lib: .PHONY .MAKE
${MAKEDIRTARGET} lib build_install
do-compat-lib: .PHONY .MAKE
${MAKEDIRTARGET} compat build_install BOOTSTRAP_SUBDIRS="../../../lib"
do-top-obj: .PHONY .MAKE
${MAKEDIRTARGET} . obj NOSUBDIR=
do-tools-obj: .PHONY .MAKE
${MAKEDIRTARGET} tools obj
do-build: .PHONY .MAKE
.for targ in dependall install
${MAKEDIRTARGET} . ${targ} BUILD_tools=no BUILD_lib=no
.endfor
do-x11: .PHONY .MAKE
.if ${MKX11} != "no"
.if ${X11FLAVOUR} == "Xorg"
${MAKEDIRTARGET} external/mit/xorg build
.else
${MAKEDIRTARGET} x11 build
.endif
.else
@echo "MKX11 is not enabled"
@false
.endif
do-extsrc: .PHONY .MAKE
.if ${MKEXTSRC} != "no"
${MAKEDIRTARGET} extsrc build
.else
@echo "MKEXTSRC is not enabled"
@false
.endif
do-obsolete: .PHONY .MAKE
${MAKEDIRTARGET} etc install-obsolete-lists
#
# Speedup stubs for some subtrees that don't need to run these rules.
# (Tells <bsd.subdir.mk> not to recurse for them.)
#
.for dir in bin etc distrib games libexec regress sbin usr.bin usr.sbin tools
includes-${dir}: .PHONY
@true
.endfor
.for dir in etc distrib regress
install-${dir}: .PHONY
@true
.endfor
.if defined(__MINIX)
SRCTAR=src.tar.gz
do-installsrc:
cd ${.CURDIR} && git ls-tree -r HEAD --name-only | tar czf ${.OBJDIR}/$(SRCTAR) -T -
${INSTALL_FILE} ${.OBJDIR}/$(SRCTAR) ${DESTDIR}/usr/src/
.endif # defined(__MINIX)
#
# XXX this needs to change when distrib Makefiles are recursion compliant
# XXX many distrib subdirs need "cd etc && make snap_pre snap_kern" first...
#
dependall-distrib depend-distrib all-distrib: .PHONY
@true
.include <bsd.sys.mk>
.include <bsd.obj.mk>
.include <bsd.kernobj.mk>
.include <bsd.subdir.mk>
cleandepend::
cd boot && $(MAKE) cleandepend
cd commands && $(MAKE) cleandepend
cd tools && $(MAKE) cleandepend

View File

@@ -1,16 +0,0 @@
# $NetBSD: Makefile.inc,v 1.4 2002/04/10 14:53:43 lukem Exp $
.ifndef ABSTOP
ABSTOP!= cd ${.PARSEDIR}; pwd
.if (${.PARSEDIR} == ${.CURDIR})
RELTOP:= .
.else
RELTOP:= ${.PARSEDIR:S,^${.CURDIR},,}
.endif
# you can set BSD{SRC,OBJ}DIR in Makefile.local.inc
.if exists(${ABSTOP}/Makefile.local.inc)
.include "${ABSTOP}/Makefile.local.inc"
.endif
.endif

7
benchmarks/Makefile Normal file
View File

@@ -0,0 +1,7 @@
# Makefile for the benchmarks.
all::
chmod 755 run
all clean::
for b in *bench*; do cd $$b && $(MAKE) $@; done

51
benchmarks/run Normal file
View File

@@ -0,0 +1,51 @@
#!/bin/sh
set -e
make
BENCHDIR=/usr/local/benchmarks
basebenchmarks=`echo *bench*`
if [ -d $BENCHDIR ]
then packagebenchmarks=`(cd $BENCHDIR && echo *bench*)`
fi
runbench() {
bench=$1
out="Results/$bench.`date +%Y%m%d.%H%M%S`"
if [ -d $bench ]
then dir=$bench
fi
if [ -d $BENCHDIR/$bench ]
then dir=$BENCHDIR/$bench
fi
clear
echo "Running $dir."
echo "Saving output to $out."
echo ""
( cd $dir && sh run.sh 2>&1 ) | tee $out
}
clear
n=1
for b in $basebenchmarks $packagebenchmarks
do echo "$n. $b"
eval "n$n=$b"
n=`expr $n + 1`
done
echo
echo -n "Run which benchmark or 'all'? "
read bench
eval var=\$n$bench
if [ "$bench" = all ]
then for b in $basebenchmarks $packagebenchmarks
do runbench $b
done
else if [ -d "$var" -o -d "$BENCHDIR/$var" ]
then runbench $var
else echo "Unknown benchmark $var."
exit 1
fi
fi

View File

@@ -0,0 +1,246 @@
##############################################################################
# UnixBench v5.1.1
# Based on The BYTE UNIX Benchmarks - Release 3
# Module: Makefile SID: 3.9 5/15/91 19:30:15
#
##############################################################################
# Bug reports, patches, comments, suggestions should be sent to:
# David C Niemi <niemi@tux.org>
#
# Original Contacts at Byte Magazine:
# Ben Smith or Tom Yager at BYTE Magazine
# bensmith@bytepb.byte.com tyager@bytepb.byte.com
#
##############################################################################
# Modification Log: 7/28/89 cleaned out workload files
# 4/17/90 added routines for installing from shar mess
# 7/23/90 added compile for dhrystone version 2.1
# (this is not part of Run file. still use old)
# removed HZ from everything but dhry.
# HZ is read from the environment, if not
# there, you must define it in this file
# 10/30/90 moved new dhrystone into standard set
# new pgms (dhry included) run for a specified
# time rather than specified number of loops
# 4/5/91 cleaned out files not needed for
# release 3 -- added release 3 files -ben
# 10/22/97 added compiler options for strict ANSI C
# checking for gcc and DEC's cc on
# Digital Unix 4.x (kahn@zk3.dec.com)
# 09/26/07 changes for UnixBench 5.0
# 09/30/07 adding ubgears, GRAPHIC_TESTS switch
# 10/14/07 adding large.txt
##############################################################################
##############################################################################
# CONFIGURATION
##############################################################################
SHELL = /bin/sh
# GRAPHICS TESTS: Uncomment the definition of "GRAPHIC_TESTS" to enable
# the building of the graphics benchmarks. This will require the
# X11 libraries on your system.
#
# Comment the line out to disable these tests.
# GRAPHIC_TESTS = defined
# Set "GL_LIBS" to the libraries needed to link a GL program.
GL_LIBS = -lGL -lXext -lX11
# OPTIMISATION SETTINGS:
## Very generic
OPTON = -O
## For Linux 486/Pentium, GCC 2.7.x and 2.8.x
#OPTON = -O2 -fomit-frame-pointer -fforce-addr -fforce-mem -ffast-math \
# -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2
## For Linux, GCC previous to 2.7.0
#OPTON = -O2 -fomit-frame-pointer -fforce-addr -fforce-mem -ffast-math -m486
#OPTON = -O2 -fomit-frame-pointer -fforce-addr -fforce-mem -ffast-math \
# -m386 -malign-loops=1 -malign-jumps=1 -malign-functions=1
## For Solaris 2, or general-purpose GCC 2.7.x
#OPTON = -O2 -fomit-frame-pointer -fforce-addr -ffast-math -Wall
## For Digital Unix v4.x, with DEC cc v5.x
#OPTON = -O4
#CFLAGS = -DTIME -std1 -verbose -w0
## generic gcc CFLAGS. -DTIME must be included.
CFLAGS += $(CPPFLAGS) -DTIME -DMINIX=1 -D_MINIX=1 -D_POSIX_SOURCE=1
##############################################################################
# END CONFIGURATION
##############################################################################
# local directories
PROGDIR = ./pgms
SRCDIR = ./src
TESTDIR = ./testdir
RESULTDIR = ./results
TMPDIR = ./tmp
# other directories
INCLDIR = /usr/include
LIBDIR = /lib
SCRIPTS = unixbench.logo multi.sh tst.sh index.base
SOURCES = arith.c big.c context1.c \
dummy.c execl.c \
fstime.c hanoi.c \
pipe.c spawn.c \
syscall.c looper.c timeit.c time-polling.c \
dhry_1.c dhry_2.c dhry.h whets.c ubgears.c
TESTS = sort.src cctest.c dc.dat large.txt
# ifdef GRAPHIC_TESTS
# GRAPHIC_BINS = $(PROGDIR)/ubgears
# else
GRAPHIC_BINS =
# endif
# Program binaries.
BINS = $(PROGDIR)/arithoh $(PROGDIR)/register $(PROGDIR)/short \
$(PROGDIR)/int $(PROGDIR)/long $(PROGDIR)/float $(PROGDIR)/double \
$(PROGDIR)/hanoi $(PROGDIR)/syscall $(PROGDIR)/context1 \
$(PROGDIR)/pipe $(PROGDIR)/spawn $(PROGDIR)/execl \
$(PROGDIR)/dhry2 $(PROGDIR)/dhry2reg $(PROGDIR)/looper \
$(PROGDIR)/fstime $(PROGDIR)/whetstone-double $(GRAPHIC_BINS)
## These compile only on some platforms...
# $(PROGDIR)/poll $(PROGDIR)/poll2 $(PROGDIR)/select
# Required non-binary files.
REQD = $(BINS) $(PROGDIR)/unixbench.logo \
$(PROGDIR)/multi.sh $(PROGDIR)/tst.sh $(PROGDIR)/index.base \
$(PROGDIR)/gfx-x11 \
$(TESTDIR)/sort.src $(TESTDIR)/cctest.c $(TESTDIR)/dc.dat \
$(TESTDIR)/large.txt
# ######################### the big ALL ############################
all: distr programs
## Ick!!! What is this about??? How about let's not chmod everything bogusly.
# @chmod 744 * $(SRCDIR)/* $(PROGDIR)/* $(TESTDIR)/* $(DOCDIR)/*
# ####################### a check for Run ######################
check: $(REQD)
make all
# ##############################################################
# distribute the files out to subdirectories if they are in this one
distr:
@echo "Checking distribution of files"
# scripts
@if test ! -d $(PROGDIR) \
; then \
mkdir $(PROGDIR) \
; mv $(SCRIPTS) $(PROGDIR) \
; else \
echo "$(PROGDIR) exists" \
; fi
# C sources
@if test ! -d $(SRCDIR) \
; then \
mkdir $(SRCDIR) \
; mv $(SOURCES) $(SRCDIR) \
; else \
echo "$(SRCDIR) exists" \
; fi
# test data
@if test ! -d $(TESTDIR) \
; then \
mkdir $(TESTDIR) \
; mv $(TESTS) $(TESTDIR) \
; else \
echo "$(TESTDIR) exists" \
; fi
# temporary work directory
@if test ! -d $(TMPDIR) \
; then \
mkdir $(TMPDIR) \
; else \
echo "$(TMPDIR) exists" \
; fi
# directory for results
@if test ! -d $(RESULTDIR) \
; then \
mkdir $(RESULTDIR) \
; else \
echo "$(RESULTDIR) exists" \
; fi
programs: $(BINS)
# Individual programs
$(PROGDIR)/arithoh: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/arithoh ${CFLAGS} ${OPTON} -Darithoh $(SRCDIR)/arith.c
$(PROGDIR)/register: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/register ${CFLAGS} ${OPTON} -Ddatum='register int' $(SRCDIR)/arith.c
$(PROGDIR)/short: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/short ${CFLAGS} ${OPTON} -Ddatum=short $(SRCDIR)/arith.c
$(PROGDIR)/int: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/int ${CFLAGS} ${OPTON} -Ddatum=int $(SRCDIR)/arith.c
$(PROGDIR)/long: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/long ${CFLAGS} ${OPTON} -Ddatum=long $(SRCDIR)/arith.c
$(PROGDIR)/float: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/float ${CFLAGS} ${OPTON} -Ddatum=float $(SRCDIR)/arith.c
$(PROGDIR)/double: $(SRCDIR)/arith.c
$(CC) -o $(PROGDIR)/double ${CFLAGS} ${OPTON} -Ddatum=double $(SRCDIR)/arith.c
$(PROGDIR)/whetstone-double: $(SRCDIR)/whets.c
$(CC) -o $(PROGDIR)/whetstone-double ${CFLAGS} ${OPTON} -DDP -DUNIX -DUNIXBENCH $(SRCDIR)/whets.c -lm
$(PROGDIR)/hanoi: $(SRCDIR)/hanoi.c
$(CC) -o $(PROGDIR)/hanoi ${CFLAGS} ${OPTON} $(SRCDIR)/hanoi.c
$(PROGDIR)/poll: $(SRCDIR)/time-polling.c
$(CC) -DHAS_POLL -DUNIXBENCH -o $(PROGDIR)/poll ${CFLAGS} ${OPTON} $(SRCDIR)/time-polling.c
$(PROGDIR)/poll2: $(SRCDIR)/time-polling.c
$(CC) -DHAS_POLL2 -DUNIXBENCH -o $(PROGDIR)/poll2 ${CFLAGS} ${OPTON} $(SRCDIR)/time-polling.c
$(PROGDIR)/select: $(SRCDIR)/time-polling.c
$(CC) -DHAS_SELECT -DUNIXBENCH -o $(PROGDIR)/select ${CFLAGS} ${OPTON} $(SRCDIR)/time-polling.c
$(PROGDIR)/fstime: $(SRCDIR)/fstime.c
$(CC) -o $(PROGDIR)/fstime ${CFLAGS} ${OPTON} $(SRCDIR)/fstime.c
$(PROGDIR)/syscall: $(SRCDIR)/syscall.c
$(CC) -o $(PROGDIR)/syscall ${CFLAGS} ${OPTON} $(SRCDIR)/syscall.c
$(PROGDIR)/context1: $(SRCDIR)/context1.c
$(CC) -o $(PROGDIR)/context1 ${CFLAGS} ${OPTON} $(SRCDIR)/context1.c
$(PROGDIR)/pipe: $(SRCDIR)/pipe.c
$(CC) -o $(PROGDIR)/pipe ${CFLAGS} ${OPTON} $(SRCDIR)/pipe.c
$(PROGDIR)/spawn: $(SRCDIR)/spawn.c
$(CC) -o $(PROGDIR)/spawn ${CFLAGS} ${OPTON} $(SRCDIR)/spawn.c
$(PROGDIR)/execl: $(SRCDIR)/execl.c $(SRCDIR)/big.c
$(CC) -o $(PROGDIR)/execl ${CFLAGS} ${OPTON} $(SRCDIR)/execl.c
$(PROGDIR)/dhry2: $(SRCDIR)/dhry_1.c $(SRCDIR)/dhry_2.c $(SRCDIR)/dhry.h
cd $(SRCDIR); $(CC) -c ${CFLAGS} -DHZ=${HZ} ${OPTON} dhry_1.c
cd $(SRCDIR); $(CC) -c ${CFLAGS} -DHZ=${HZ} ${OPTON} dhry_2.c
$(CC) -o $(PROGDIR)/dhry2 ${CFLAGS} ${OPTON} $(SRCDIR)/dhry_1.o $(SRCDIR)/dhry_2.o
cd $(SRCDIR); rm -f dhry_1.o dhry_2.o
$(PROGDIR)/dhry2reg: $(SRCDIR)/dhry_1.c $(SRCDIR)/dhry_2.c $(SRCDIR)/dhry.h
cd $(SRCDIR); $(CC) -c ${CFLAGS} -DREG=register -DHZ=${HZ} ${OPTON} dhry_1.c
cd $(SRCDIR); $(CC) -c ${CFLAGS} -DREG=register -DHZ=${HZ} ${OPTON} dhry_2.c
$(CC) -o $(PROGDIR)/dhry2reg ${CFLAGS} ${OPTON} $(SRCDIR)/dhry_1.o $(SRCDIR)/dhry_2.o
cd $(SRCDIR); rm -f dhry_1.o dhry_2.o
$(PROGDIR)/looper: $(SRCDIR)/looper.c
$(CC) -o $(PROGDIR)/looper ${CFLAGS} ${OPTON} $(SRCDIR)/looper.c
$(PROGDIR)/ubgears: $(SRCDIR)/ubgears.c
$(CC) -o $(PROGDIR)/ubgears ${CFLAGS} ${OPTON} $(SRCDIR)/ubgears.c $(GL_LIBS)
# Run the benchmarks and create the reports
run:
sh ./Run
clean:
rm -f $(BINS) core *~ */*~
spotless: clean
rm -f $(RESULTDIR)/* $(TMPDIR)/*
## END ##

View File

@@ -0,0 +1,406 @@
Version 5.1.2 -- 2007-12-26
================================================================
To use Unixbench:
1. UnixBench from version 5.1 on has both system and graphics tests.
If you want to use the graphic tests, edit the Makefile and make sure
that the line "GRAPHIC_TESTS = defined" is not commented out; then check
that the "GL_LIBS" definition is OK for your system. Also make sure
that the "x11perf" command is on your search path.
If you don't want the graphics tests, then comment out the
"GRAPHIC_TESTS = defined" line. Note: comment it out, don't
set it to anything.
2. Do "make".
3. Do "Run" to run the system test; "Run graphics" to run the graphics
tests; "Run gindex" to run both.
You will need perl, as Run is written in perl.
For more information on using the tests, read "USAGE".
For information on adding tests into the benchmark, see "WRITING_TESTS".
===================== RELEASE NOTES =====================================
======================== Dec 07 ==========================
v5.1.2
One big fix: if unixbench is installed in a directory whose pathname contains
a space, it should now run (previously it failed).
To avoid possible clashes, the environment variables unixbench uses are now
prefixed with "UB_". These are all optional, and for most people will be
completely unnecessary, but if you want you can set these:
UB_BINDIR Directory where the test programs live.
UB_TMPDIR Temp directory, for temp files.
UB_RESULTDIR Directory to put results in.
UB_TESTDIR Directory where the tests are executed.
And a couple of tiny fixes:
* In pgms/tst.sh, changed "sort -n +1" to "sort -n -k 1"
* In Makefile, made it clearer that GRAPHIC_TESTS should be commented
out (not set to 0) to disable graphics
Thanks to nordi for pointing these out.
Ian Smith, December 26, 2007
johantheghost at yahoo period com
======================== Oct 07 ==========================
v5.1.1
It turns out that the setting of LANG is crucial to the results. This
explains why people in different regions were seeing odd results, and also
why runlevel 1 produced odd results -- runlevel 1 doesn't set LANG, and
hence reverts to ASCII, whereas most people use a UTF-8 encoding, which is
much slower in some tests (eg. shell tests).
So now we manually set LANG to "en_US.utf8", which is configured with the
variable "$language". Don't change this if you want to share your results.
We also report the language settings in use.
See "The Language Setting" in USAGE for more info. Thanks to nordi for
pointing out the LANG issue.
I also added the "grep" and "sysexec" tests. These are non-index tests,
and "grep" uses the system's grep, so it's not much use for comparing
different systems. But some folks on the OpenSuSE list have been finding
these useful. They aren't in any of the main test groups; do "Run grep
sysexec" to run them.
Index Changes
-------------
The setting of LANG will affect consistency with systems where this is
not the default value. However, it should produce more consistent results
in future.
Ian Smith, October 15, 2007
johantheghost at yahoo period com
======================== Oct 07 ==========================
v5.1
The major new feature in this version is the addition of graphical
benchmarks. Since these may not compile on all systems, you can enable/
disable them with the GRAPHIC_TESTS variable in the Makefile.
As before, each test is run for 3 or 10 iterations. However, we now discard
the worst 1/3 of the scores before averaging the remainder. The logic is
that a glitch in the system (background process waking up, for example) may
make one or two runs go slow, so let's discard those. Hopefully this will
produce more consistent and repeatable results. Check the log file
for a test run to see the discarded scores.
Made the tests compile and run on x86-64/Linux (fixed an execl bug passing
int instead of pointer).
Also fixed some general bugs.
Thanks to Stefan Esser for help and testing / bug reporting.
Index Changes
-------------
The tests are now divided into categories, and each category generates
its own index. This keeps the graphics test results separate from
the system tests.
The "graphics" test and corresponding index are new.
The "discard the worst scores" strategy should produce slightly higher
test scores, but at least they should (hopefully!) be more consistent.
The scores should not be higher than the best scores you would have got
with 5.0, so this should not be a huge consistency issue.
Ian Smith, October 11, 2007
johantheghost at yahoo period com
======================== Sep 07 ==========================
v5.0
All the work I've done on this release is Linux-based, because that's
the only Unix I have access to. I've tried to make it more OS-agnostic
if anything; for example, it no longer has to figure out the format reported
by /usr/bin/time. However, it's possible that portability has been damaged.
If anyone wants to fix this, please feel free to mail me patches.
In particular, the analysis of the system's CPUs is done via /proc/cpuinfo.
For systems which don't have this, please make appropriate changes in
getCpuInfo() and getSystemInfo().
The big change has been to make the tests multi-CPU aware. See the
"Multiple CPUs" section in "USAGE" for details. Other changes:
* Completely rewrote Run in Perl; drastically simplified the way data is
processed. The confusing system of interlocking shell and awk scripts is
now just one script. Various intermediate files used to store and process
results are now replaced by Perl data structures internal to the script.
* Removed from the index runs file system read and write tests which were
ignored for the index and wasted about 10 minutes per run (see fstime.c).
The read and write tests can now be selected individually. Made fstime.c
take parameters, so we no longer need to build 3 versions of it.
* Made the output file names unique; they are built from
hostname-date-sequence.
* Worked on result reporting, error handling, and logging. See TESTS.
We now generate both text and HTML reports.
* Removed some obsolete files.
Index Changes
-------------
The index is still based on David Niemi's SPARCstation 20-61 (rated at 10.0),
and the intention in the changes I've made has been to keep the tests
unchanged, in order to maintain consistency with old result sets.
However, the following changes have been made to the index:
* The Pipe-based Context Switching test (context1) was being dropped
from the index report in v4.1.0 due to a bug; I've put it back in.
* I've added shell1 to the index, to get a measure of how the shell tests
scale with multiple CPUs (shell8 already exercises all the CPUs, even
in single-copy mode). I made up the baseline score for this by
extrapolation.
Both of these test can be dropped, if you wish, by editing the "TEST
SPECIFICATIONS" section of Run.
Ian Smith, September 20, 2007
johantheghost at yahoo period com
======================== Aug 97 ==========================
v4.1.0
Double precision Whetstone put in place instead of the old "double" benchmark.
Removal of some obsolete files.
"system" suite adds shell8.
perlbench and poll added as "exhibition" (non-index) benchmarks.
Incorporates several suggestions by Andre Derrick Balsa <andrewbalsa@usa.net>
Code cleanups to reduce compiler warnings by David C Niemi <niemi@tux.org>
and Andy Kahn <kahn@zk3.dec.com>; Digital Unix options by Andy Kahn.
======================== Jun 97 ==========================
v4.0.1
Minor change to fstime.c to fix overflow problems on fast machines. Counting
is now done in units of 256 (smallest BUFSIZE) and unsigned longs are used,
giving another 23 dB or so of headroom ;^) Results should be virtually
identical aside from very small rounding errors.
======================== Dec 95 ==========================
v4.0
Byte no longer seems to have anything to do with this benchmark, and I was
unable to reach any of the original authors; so I have taken it upon myself
to clean it up.
This is version 4. Major assumptions made in these benchmarks have changed
since they were written, but they are nonetheless popular (particularly for
measuring hardware for Linux). Some changes made:
- The biggest change is to put a lot more operating system-oriented
tests into the index. I experimented for a while with a decibel-like
logarithmic scale, but finally settled on using a geometric mean for
the final index (the individual scores are a normalized, and their
logs are averaged; the resulting value is exponentiated).
"George", certain SPARCstation 20-61 with 128 MB RAM, a SPARC Storage
Array, and Solaris 2.3 is my new baseline; it is rated at 10.0 in each
of the index scores for a final score of 10.0.
Overall I find the geometric averaging is a big improvement for
avoiding the skew that was once possible (e.g. a Pentium-75 which got
40 on the buggy version of fstime, such that fstime accounted for over
half of its total score and hence wildly skewed its average).
I also expect that the new numbers look different enough from the old
ones that no one is too likely to casually mistake them for each other.
I am finding new SPARCs running Solaris 2.4 getting about 15-20, and
my 486 DX2-66 Compaq running Linux 1.3.45 got a 9.1. It got
understandably poor scores on CPU and FPU benchmarks (a horrible
1.8 on "double" and 1.3 on "fsdisk"); but made up for it by averaging
over 20 on the OS-oriented benchmarks. The Pentium-75 running
Linux gets about 20 (and it *still* runs Windows 3.1 slowly. Oh well).
- It is difficult to get a modern compiler to even consider making
dhry2 without registers, short of turning off *all* optimizations.
This is also not a terribly meaningful test, even if it were possible,
as noone compiles without registers nowadays. Replaced this benchmark
with dhry2reg in the index, and dropped it out of usage in general as
it is so hard to make a legitimate one.
- fstime: this had some bugs when compiled on modern systems which return
the number of bytes read/written for read(2)/write(2) calls. The code
assumed that a negative return code was given for EOF, but most modern
systems return 0 (certainly on SunOS 4, Solaris2, and Linux, which is
what counts for me). The old code yielded wildly inflated read scores,
would eat up tens of MB of disk space on fast systems, and yielded
roughly 50% lower than normal copy scores than it should have.
Also, it counted partial blocks *fully*; made it count the proportional
part of the block which was actually finished.
Made bigger and smaller variants of fstime which are designed to beat
up the disk I/O and the buffer cache, respectively. Adjusted the
sleeps so that they are short for short benchmarks.
- Instead of 1,2,4, and 8-shell benchmarks, went to 1, 8, and 16 to
give a broader range of information (and to run 1 fewer test).
The only real problem with this is that not many iterations get
done with 16 at a time on slow systems, so there are some significant
rounding errors; 8 therefore still used for the benchmark. There is
also the problem that the last (uncompleted) loop is counted as a full
loop, so it is impossible to score below 1.0 lpm (which gave my laptop
a break). Probably redesigning Shell to do each loop a bit more
quickly (but with less intensity) would be a good idea.
This benchmark appears to be very heavily influenced by the speed
of the loader, by which shell is being used as /bin/sh, and by how
well-compiled some of the common shell utilities like grep, sed, and
sort are. With a consistent tool set it is also a good indicator of
the bandwidth between main memory and the CPU (e.g. Pentia score about
twice as high as 486es due to their 64-bit bus). Small, sometimes
broken shells like "ash-linux" do particularly well here, while big,
robust shells like bash do not.
- "dc" is a somewhat iffy benchmark, because there are two versions of
it floating around, one being small, very fast, and buggy, and one
being more correct but slow. It was never in the index anyway.
- Execl is a somewhat troubling benchmark in that it yields much higher
scores if compiled statically. I frown on this practice because it
distorts the scores away from reflecting how programs are really used
(i.e. dynamically linked).
- Arithoh is really more an indicator of the compiler quality than of
the computer itself. For example, GCC 2.7.x with -O2 and a few extra
options optimizes much of it away, resulting in about a 1200% boost
to the score. Clearly not a good one for the index.
I am still a bit unhappy with the variance in some of the benchmarks, most
notably the fstime suite; and with how long it takes to run. But I think
it gets significantly more reliable results than the older version in less
time.
If anyone has ideas on how to make these benchmarks faster, lower-variance,
or more meaningful; or has nice, new, portable benchmarks to add, don't
hesitate to e-mail me.
David C Niemi <niemi@tux.org> 7 Dec 1995
======================== May 91 ==========================
This is version 3. This set of programs should be able to determine if
your system is BSD or SysV. (It uses the output format of time (1)
to see. If you have any problems, contact me (by email,
preferably): ben@bytepb.byte.com
---
The document doc/bench.doc describes the basic flow of the
benchmark system. The document doc/bench3.doc describes the major
changes in design of this version. As a user of the benchmarks,
you should understand some of the methods that have been
implemented to generate loop counts:
Tests that are compiled C code:
The function wake_me(second, func) is included (from the file
timeit.c). This function uses signal and alarm to set a countdown
for the time request by the benchmark administration script
(Run). As soon as the clock is started, the test is run with a
counter keeping track of the number of loops that the test makes.
When alarm sends its signal, the loop counter value is sent to stderr
and the program terminates. Since the time resolution, signal
trapping and other factors don't insure that the test is for the
precise time that was requested, the test program is also run
from the time (1) command. The real time value returned from time
(1) is what is used in calculating the number of loops per second
(or minute, depending on the test). As is obvious, there is some
overhead time that is not taken into account, therefore the
number of loops per second is not absolute. The overhead of the
test starting and stopping and the signal and alarm calls is
common to the overhead of real applications. If a program loads
quickly, the number of loops per second increases; a phenomenon
that favors systems that can load programs quickly. (Setting the
sticky bit of the test programs is not considered fair play.)
Test that use existing UNIX programs or shell scripts:
The concept is the same as that of compiled tests, except the
alarm and signal are contained in separate compiled program,
looper (source is looper.c). Looper uses an execvp to invoke the
test with its arguments. Here, the overhead includes the
invocation and execution of looper.
--
The index numbers are generated from a baseline file that is in
pgms/index.base. You can put tests that you wish in this file.
All you need to do is take the results/log file from your
baseline machine, edit out the comment and blank lines, and sort
the result (vi/ex command: 1,$!sort). The sort in necessary
because the process of generating the index report uses join (1).
You can regenerate the reports by running "make report."
--
========================= Jan 90 =============================
Tom Yager has joined the effort here at BYTE; he is responsible
for many refinements in the UNIX benchmarks.
The memory access tests have been deleted from the benchmarks.
The file access tests have been reversed so that the test is run
for a fixed time. The amount of data transfered (written, read,
and copied) is the variable. !WARNING! This test can eat up a
large hunk of disk space.
The initial line of all shell scripts has been changed from the
SCO and XENIX form (:) to the more standard form "#! /bin/sh".
But different systems handle shell switching differently. Check
the documentation on your system and find out how you are
supposed to do it. Or, simpler yet, just run the benchmarks from
the Bourne shell. (You may need to set SHELL=/bin/sh as well.)
The options to Run have not been checked in a while. They may no
longer function. Next time, I'll get back on them. There needs to
be another option added (next time) that halts testing between
each test. !WARNING! Some systems have caches that are not getting flushed
before the next test or iteration is run. This can cause
erroneous values.
========================= Sept 89 =============================
The database (db) programs now have a tuneable message queue space.
queue space. The default set in the Run script is 1024 bytes.
Other major changes are in the format of the times. We now show
Arithmetic and Geometric mean and standard deviation for User
Time, System Time, and Real Time. Generally, in reporting, we
plan on using the Real Time values with the benchs run with one
active user (the bench user). Comments and arguments are requested.
contact: BIX bensmith or rick_g

1868
benchmarks/unixbench-5.1.2/Run Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,394 @@
Running the Tests
=================
All the tests are executed using the "Run" script in the top-level directory.
The simplest way to generate results is with the commmand:
./Run
This will run a standard "index" test (see "The BYTE Index" below), and
save the report in the "results" directory, with a filename like
hostname-2007-09-23-01
An HTML version is also saved.
If you want to generate both the basic system index and the graphics index,
then do:
./Run gindex
If your system has more than one CPU, the tests will be run twice -- once
with a single copy of each test running at once, and once with N copies,
where N is the number of CPUs. Some categories of tests, however (currently
the graphics tests) will only run with a single copy.
Since the tests are based on constant time (variable work), a "system"
run usually takes about 29 minutes; the "graphics" part about 18 minutes.
A "gindex" run on a dual-core machine will do 2 "system" passes (single-
and dual-processing) and one "graphics" run, for a total around one and
a quarter hours.
============================================================================
Detailed Usage
==============
The Run script takes a number of options which you can use to customise a
test, and you can specify the names of the tests to run. The full usage
is:
Run [ -q | -v ] [-i <n> ] [-c <n> [-c <n> ...]] [test ...]
The option flags are:
-q Run in quiet mode.
-v Run in verbose mode.
-i <count> Run <count> iterations for each test -- slower tests
use <count> / 3, but at least 1. Defaults to 10 (3 for
slow tests).
-c <n> Run <n> copies of each test in parallel.
The -c option can be given multiple times; for example:
./Run -c 1 -c 4
will run a single-streamed pass, then a 4-streamed pass. Note that some
tests (currently the graphics tests) will only run in a single-streamed pass.
The remaining non-flag arguments are taken to be the names of tests to run.
The default is to run "index". See "Tests" below.
When running the tests, I do *not* recommend switching to single-user mode
("init 1"). This seems to change the results in ways I don't understand,
and it's not realistic (unless your system will actually be running in this
mode, of course). However, if using a windowing system, you may want to
switch to a minimal window setup (for example, log in to a "twm" session),
so that randomly-churning background processes don't randomise the results
too much. This is particularly true for the graphics tests.
============================================================================
Tests
=====
The available tests are organised into categories; when generating index
scores (see "The BYTE Index" below) the results for each category are
produced separately. The categories are:
system The original Unix system tests (not all are actually
in the index)
2d 2D graphics tests (not all are actually in the index)
3d 3D graphics tests
misc Various non-indexed tests
The following individual tests are available:
system:
dhry2reg Dhrystone 2 using register variables
whetstone-double Double-Precision Whetstone
syscall System Call Overhead
pipe Pipe Throughput
context1 Pipe-based Context Switching
spawn Process Creation
execl Execl Throughput
fstime-w File Write 1024 bufsize 2000 maxblocks
fstime-r File Read 1024 bufsize 2000 maxblocks
fstime File Copy 1024 bufsize 2000 maxblocks
fsbuffer-w File Write 256 bufsize 500 maxblocks
fsbuffer-r File Read 256 bufsize 500 maxblocks
fsbuffer File Copy 256 bufsize 500 maxblocks
fsdisk-w File Write 4096 bufsize 8000 maxblocks
fsdisk-r File Read 4096 bufsize 8000 maxblocks
fsdisk File Copy 4096 bufsize 8000 maxblocks
shell1 Shell Scripts (1 concurrent) (runs "looper 60 multi.sh 1")
shell8 Shell Scripts (8 concurrent) (runs "looper 60 multi.sh 8")
shell16 Shell Scripts (8 concurrent) (runs "looper 60 multi.sh 16")
2d:
2d-rects 2D graphics: rectangles
2d-lines 2D graphics: lines
2d-circle 2D graphics: circles
2d-ellipse 2D graphics: ellipses
2d-shapes 2D graphics: polygons
2d-aashapes 2D graphics: aa polygons
2d-polys 2D graphics: complex polygons
2d-text 2D graphics: text
2d-blit 2D graphics: images and blits
2d-window 2D graphics: windows
3d:
ubgears 3D graphics: gears
misc:
C C Compiler Throughput ("looper 60 $cCompiler cctest.c")
arithoh Arithoh (huh?)
short Arithmetic Test (short) (this is arith.c configured for
"short" variables; ditto for the ones below)
int Arithmetic Test (int)
long Arithmetic Test (long)
float Arithmetic Test (float)
double Arithmetic Test (double)
dc Dc: sqrt(2) to 99 decimal places (runs
"looper 30 dc < dc.dat", using your system's copy of "dc")
hanoi Recursion Test -- Tower of Hanoi
grep Grep for a string in a large file, using your system's
copy of "grep"
sysexec Exercise fork() and exec().
The following pseudo-test names are aliases for combinations of other
tests:
arithmetic Runs arithoh, short, int, long, float, double,
and whetstone-double
dhry Alias for dhry2reg
dhrystone Alias for dhry2reg
whets Alias for whetstone-double
whetstone Alias for whetstone-double
load Runs shell1, shell8, and shell16
misc Runs C, dc, and hanoi
speed Runs the arithmetic and system groups
oldsystem Runs execl, fstime, fsbuffer, fsdisk, pipe, context1,
spawn, and syscall
system Runs oldsystem plus shell1, shell8, and shell16
fs Runs fstime-w, fstime-r, fstime, fsbuffer-w,
fsbuffer-r, fsbuffer, fsdisk-w, fsdisk-r, and fsdisk
shell Runs shell1, shell8, and shell16
index Runs the tests which constitute the official index:
the oldsystem group, plus dhry2reg, whetstone-double,
shell1, and shell8
See "The BYTE Index" below for more information.
graphics Runs the tests which constitute the graphics index:
2d-rects, 2d-ellipse, 2d-aashapes, 2d-text, 2d-blit,
2d-window, and ubgears
gindex Runs the index and graphics groups, to generate both
sets of index results
all Runs all tests
============================================================================
The BYTE Index
==============
The purpose of this test is to provide a basic indicator of the performance
of a Unix-like system; hence, multiple tests are used to test various
aspects of the system's performance. These test results are then compared
to the scores from a baseline system to produce an index value, which is
generally easier to handle than the raw sores. The entire set of index
values is then combined to make an overall index for the system.
Since 1995, the baseline system has been "George", a SPARCstation 20-61
with 128 MB RAM, a SPARC Storage Array, and Solaris 2.3, whose ratings
were set at 10.0. (So a system which scores 520 is 52 times faster than
this machine.) Since the numbers are really only useful in a relative
sense, there's no particular reason to update the base system, so for the
sake of consistency it's probably best to leave it alone. George's scores
are in the file "pgms/index.base"; this file is used to calculate the
index scores for any particular run.
Over the years, various changes have been made to the set of tests in the
index. Although there is a desire for a consistent baseline, various tests
have been determined to be misleading, and have been removed; and a few
alternatives have been added. These changes are detailed in the README,
and should be born in mind when looking at old scores.
A number of tests are included in the benchmark suite which are not part of
the index, for various reasons; these tests can of course be run manually.
See "Tests" above.
============================================================================
Graphics Tests
==============
As of version 5.1, UnixBench now contains some graphics benchmarks. These
are intended to give a rough idea of the general graphics performance of
a system.
The graphics tests are in categories "2d" and "3d", so the index scores
for these tests are separate from the basic system index. This seems
like a sensible division, since the graphics performance of a system
depends largely on the graphics adaptor.
The tests currently consist of some 2D "x11perf" tests and "ubgears".
* The 2D tests are a selection of the x11perf tests, using the host
system's x11perf command (which must be installed and in the search
path). Only a few of the x11perf tests are used, in the interests
of completing a test run in a reasonable time; if you want to do
detailed diagnosis of an X server or graphics chip, then use x11perf
directly.
* The 3D test is "ubgears", a modified version of the familiar "glxgears".
This version runs for 5 seconds to "warm up", then performs a timed
run and displays the average frames-per-second.
On multi-CPU systems, the graphics tests will only run in single-processing
mode. This is because the meaning of running two copies of a test at once
is dubious; and the test windows tend to overlay each other, meaning that
the window behind isn't actually doing any work.
============================================================================
Multiple CPUs
=============
If your system has multiple CPUs, the default behaviour is to run the selected
tests twice -- once with one copy of each test program running at a time,
and once with N copies, where N is the number of CPUs. (You can override
this with the "-c" option; see "Detailed Usage" above.) This is designed to
allow you to assess:
- the performance of your system when running a single task
- the performance of your system when running multiple tasks
- the gain from your system's implementation of parallel processing
The results, however, need to be handled with care. Here are the results
of two runs on a dual-processor system, one in single-processing mode, one
dual-processing:
Test Single Dual Gain
-------------------- ------ ------ ----
Dhrystone 2 562.5 1110.3 97%
Double Whetstone 320.0 640.4 100%
Execl Throughput 450.4 880.3 95%
File Copy 1024 759.4 595.9 -22%
File Copy 256 535.8 438.8 -18%
File Copy 4096 1261.8 1043.4 -17%
Pipe Throughput 481.0 979.3 104%
Pipe-based Switching 326.8 1229.0 276%
Process Creation 917.2 1714.1 87%
Shell Scripts (1) 1064.9 1566.3 47%
Shell Scripts (8) 1567.7 1709.9 9%
System Call Overhead 944.2 1445.5 53%
-------------------- ------ ------ ----
Index Score: 678.2 1026.2 51%
As expected, the heavily CPU-dependent tasks -- dhrystone, whetstone,
execl, pipe throughput, process creation -- show close to 100% gain when
running 2 copies in parallel.
The Pipe-based Context Switching test measures context switching overhead
by sending messages back and forth between 2 processes. I don't know why
it shows such a huge gain with 2 copies (ie. 4 processes total) running,
but it seems to be consistent on my system. I think this may be an issue
with the SMP implementation.
The System Call Overhead shows a lesser gain, presumably because it uses a
lot of CPU time in single-threaded kernel code. The shell scripts test with
8 concurrent processes shows no gain -- because the test itself runs 8
scripts in parallel, it's already using both CPUs, even when the benchmark
is run in single-stream mode. The same test with one process per copy
shows a real gain.
The filesystem throughput tests show a loss, instead of a gain, when
multi-processing. That there's no gain is to be expected, since the tests
are presumably constrained by the throughput of the I/O subsystem and the
disk drive itself; the drop in performance is presumably down to the
increased contention for resources, and perhaps greater disk head movement.
So what tests should you use, how many copies should you run, and how should
you interpret the results? Well, that's up to you, since it depends on
what it is you're trying to measure.
Implementation
--------------
The multi-processing mode is implemented at the level of test iterations.
During each iteration of a test, N slave processes are started using fork().
Each of these slaves executes the test program using fork() and exec(),
reads and stores the entire output, times the run, and prints all the
results to a pipe. The Run script reads the pipes for each of the slaves
in turn to get the results and times. The scores are added, and the times
averaged.
The result is that each test program has N copies running at once. They
should all finish at around the same time, since they run for constant time.
If a test program itself starts off K multiple processes (as with the shell8
test), then the effect will be that there are N * K processes running at
once. This is probably not very useful for testing multi-CPU performance.
============================================================================
The Language Setting
====================
The $LANG environment variable determines how programs abnd library
routines interpret text. This can have a big impact on the test results.
If $LANG is set to POSIX, or is left unset, text is treated as ASCII; if
it is set to en_US.UTF-8, foir example, then text is treated as being
encoded in UTF-8, which is more complex and therefore slower. Setting
it to other languages can have varying results.
To ensure consistency between test runs, the Run script now (as of version
5.1.1) sets $LANG to "en_US.utf8".
This setting which is configured with the variable "$language". You
should not change this if you want to share your results to allow
comparisons between systems; however, you may want to change it to see
how different language settings affect performance.
Each test report now includes the language settings in use. The reported
language is what is set in $LANG, and is not necessarily supported by the
system; but we also report the character mapping and collation order which
are actually in use (as reported by "locale").
============================================================================
Interpreting the Results
========================
Interpreting the results of these tests is tricky, and totally depends on
what you're trying to measure.
For example, are you trying to measure how fast your CPU is? Or how good
your compiler is? Because these tests are all recompiled using your host
system's compiler, the performance of the compiler will inevitably impact
the performance of the tests. Is this a problem? If you're choosing a
system, you probably care about its overall speed, which may well depend
on how good its compiler is; so including that in the test results may be
the right answer. But you may want to ensure that the right compiler is
used to build the tests.
On the other hand, with the vast majority of Unix systems being x86 / PC
compatibles, running Linux and the GNU C compiler, the results will tend
to be more dependent on the hardware; but the versions of the compiler and
OS can make a big difference. (I measured a 50% gain between SUSE 10.1
and OpenSUSE 10.2 on the same machine.) So you may want to make sure that
all your test systems are running the same version of the OS; or at least
publish the OS and compuiler versions with your results. Then again, it may
be compiler performance that you're interested in.
The C test is very dubious -- it tests the speed of compilation. If you're
running the exact same compiler on each system, OK; but otherwise, the
results should probably be discarded. A slower compilation doesn't say
anything about the speed of your system, since the compiler may simply be
spending more time to super-optimise the code, which would actually make it
faster.
This will be particularly true on architectures like IA-64 (Itanium etc.)
where the compiler spends huge amounts of effort scheduling instructions
to run in parallel, with a resultant significant gain in execution speed.
Some tests are even more dubious in terms of host-dependency -- for example,
the "dc" test uses the host's version of dc (a calculator program). The
version of this which is available can make a huge difference to the score,
which is why it's not in the index group. Read through the release notes
for more on these kinds of issues.
Another age-old issue is that of the benchmarks being too trivial to be
meaningful. With compilers getting ever smarter, and performing more
wide-ranging flow path analyses, the danger of parts of the benchmarks
simply being optimised out of existance is always present.
All in all, the "index" and "gindex" tests (see above) are designed to
give a reasonable measure of overall system performance; but the results
of any test run should always be used with care.

View File

@@ -0,0 +1,133 @@
Writing a Test
==============
Writing a test program is pretty easy. Basically, a test is configured via
a monster array in the Run script, which specifics (among other things) the
program to execute and the parameters to pass it.
The test itself is simply a program which is given the optional parameters
on the command line, and produces logging data on stdout and its results on
stderr.
============================================================================
Test Configuration
==================
In Run, all tests are named in the "$testList" array. This names the
individual tests, and also sets up aliases for groups of tests, eg. "index".
The test specifications are in the "$testParams" array. This contains the
details of each individual test as a hash. The fields in the hash are:
* "logmsg": the full name to display for this test.
* "cat": the category this test belongs to; must be configured
in $testCats.
* "prog": the name of the program to execute; defaults to the name of
the benchmark.
* "repeat": number of passes to run; either 'short' (the default),
'long', or 'single'. For 'short' and 'long', the actual numbers of
passes are given by $shortIterCount and $longIterCount, which are
configured at the top of the script or by the "-i" flag. 'single'
means just run one pass; this should be used for test which do their
own multi-pass handling internally.
* "stdout": non-0 to add the test's stdout to the log file; defaults to 1.
Set to 0 for tests that are too wordy.
* "stdin": name of a file to send to the program's stdin; default null.
* "options": options to be put on the program's command line; default null.
============================================================================
Output Format
=============
The results on stderr take the form of a line header and fields, separated
by "|" characters. A result line can be one of:
COUNT|score|timebase|label
TIME|seconds
ERROR|message
Any other text on stderr is treated as if it were:
ERROR|text
Any output to stdout is placed in a log file, and can be used for debugging.
COUNT
-----
The COUNT line is the line used to report a test score.
* "score" is the result, typically the number of loops performed during
the run
* "timebase" is the time base used for the final report to the user. A
value of 1 reports the score as is; a value of 60, for example, divides
the time taken by 60 to get loops per minute. Atimebase of zero indicates
that the score is already a rate, ie. a count of things per second.
* "label" is the label to use for the score; like "lps" (loops per
second), etc.
TIME
----
The TIME line is optionally used to report the time taken. The Run script
normally measures this, but if your test has signifant overhead outside the
actual test loop, you should use TIME to report the time taken for the actual
test. The argument is the time in seconds in floating-point.
ERROR
-----
The argument is an error message; this will abort the benchmarking run and
display the message.
Any output to stderr which is not a formatted line will be treated as an
error message, so use of ERROR is optional.
============================================================================
Test Examples
=============
Iteration Count
---------------
The simplest thing is to count the number of loops executed in a given time;
see eg. arith.c. The utlilty functions in timeit.c can be used to implement
the fixed time interval, which is generally passed in on the command line.
The result is reported simply as the number of iterations completed:
fprintf(stderr,"COUNT|%lu|1|lps\n", iterations);
The bnenchmark framework will measure the time taken itself. If the test
code has significant overhead (eg. a "pump-priming" pass), then you should
explicitly report the time taken for the test by adding a line like this:
fprintf(stderr, "TIME|%.1f\n", seconds);
If you want results reported as loops per minute, then set timebase to 60:
fprintf(stderr,"COUNT|%lu|60|lpm\n", iterations);
Note that this only affects the final report; all times passed to or
from the test are still in seconds.
Rate
----
The other technique is to calculate the rate (things per second) in the test,
and report that directly. To do this, just set timebase to 0:
fprintf(stderr, "COUNT|%ld|0|KBps\n", kbytes_per_sec);
Again, you can use TIME to explicitly report the time taken:
fprintf(stderr, "TIME|%.1f\n", end - start);
but this isn't so important since you've already calculated the rate.

View File

@@ -0,0 +1,476 @@
#!/usr/bin/perl -w
use strict;
############################################################################
# gfx-x11: a front-end for x11perf. Runs a selected x11perf test, and
# produces output in the format needed by UnixBench.
############################################################################
# Modification Log:
# 2007.09.26 Ian Smith Created
############################################################################
# This program runs sets of x11perf tests, indexes the results against
# a common base reference system (see $testData below), and reports the
# final score.
#
# Usage:
# gfx-x11 <group> <reps> <time>
# where:
# <group> is one of the test groups defined in $testGroups below
# <reps> is the number of repeats to do per test (the -repeat
# argument to x11perf)
# <time> is the number of seconds to run each repeat for (the
# -time argument to x11perf)
# Note that x11perf runs a calibration pass before the requested number
# of test passes. The score we compute for a test is the average of the
# test passes, divided by the base value in $testData, times 1000.0.
# The final score we report is the average of the test scores.
############################################################################
# TEST DATA
############################################################################
# This array lists all of the x11perf tests, together with their scores
# on an HP Compaq nc8430, with an ATI Mobility Radeon X1600 (256MB)
# graphics adapter. There isn't anything special about this reference
# system, but scaling all the scores back to a single reference system
# allows us to average them in a roughly meaningful way, which in turn
# allows us to produce sensible scores for the test groups defined below.
#
# The results we report are indexed to these values, at a base of 1000.0.
my $testData = {
'dot' => [ 31700000.0, "Dot" ],
'rect1' => [ 18400000.0, "1x1 rectangle" ],
'rect10' => [ 7180000.0, "10x10 rectangle" ],
'rect100' => [ 110000.0, "100x100 rectangle" ],
'rect500' => [ 4110.0, "500x500 rectangle" ],
'srect1' => [ 15800000.0, "1x1 stippled rectangle (8x8 stipple)" ],
'srect10' => [ 7400000.0, "10x10 stippled rectangle (8x8 stipple)" ],
'srect100' => [ 110000.0, "100x100 stippled rectangle (8x8 stipple)" ],
'srect500' => [ 4110.0, "500x500 stippled rectangle (8x8 stipple)" ],
'osrect1' => [ 15900000.0, "1x1 opaque stippled rectangle (8x8 stipple)" ],
'osrect10' => [ 7170000.0, "10x10 opaque stippled rectangle (8x8 stipple)" ],
'osrect100' => [ 110000.0, "100x100 opaque stippled rectangle (8x8 stipple)" ],
'osrect500' => [ 4110.0, "500x500 opaque stippled rectangle (8x8 stipple)" ],
'tilerect1' => [ 15800000.0, "1x1 tiled rectangle (4x4 tile)" ],
'tilerect10' => [ 7170000.0, "10x10 tiled rectangle (4x4 tile)" ],
'tilerect100' => [ 110000.0, "100x100 tiled rectangle (4x4 tile)" ],
'tilerect500' => [ 4110.0, "500x500 tiled rectangle (4x4 tile)" ],
'oddsrect1' => [ 2990000.0, "1x1 stippled rectangle (17x15 stipple)" ],
'oddsrect10' => [ 1490000.0, "10x10 stippled rectangle (17x15 stipple)" ],
'oddsrect100' => [ 55600.0, "100x100 stippled rectangle (17x15 stipple)" ],
'oddsrect500' => [ 2360.0, "500x500 stippled rectangle (17x15 stipple)" ],
'oddosrect1' => [ 2990000.0, "1x1 opaque stippled rectangle (17x15 stipple)" ],
'oddosrect10' => [ 1430000.0, "10x10 opaque stippled rectangle (17x15 stipple)" ],
'oddosrect100' => [ 54500.0, "100x100 opaque stippled rectangle (17x15 stipple)" ],
'oddosrect500' => [ 2320.0, "500x500 opaque stippled rectangle (17x15 stipple)" ],
'oddtilerect1' => [ 2990000.0, "1x1 tiled rectangle (17x15 tile)" ],
'oddtilerect10' => [ 1430000.0, "10x10 tiled rectangle (17x15 tile)" ],
'oddtilerect100' => [ 54500.0, "100x100 tiled rectangle (17x15 tile)" ],
'oddtilerect500' => [ 2320.0, "500x500 tiled rectangle (17x15 tile)" ],
'bigsrect1' => [ 4300000.0, "1x1 stippled rectangle (161x145 stipple)" ],
'bigsrect10' => [ 705000.0, "10x10 stippled rectangle (161x145 stipple)" ],
'bigsrect100' => [ 12300.0, "100x100 stippled rectangle (161x145 stipple)" ],
'bigsrect500' => [ 524.0, "500x500 stippled rectangle (161x145 stipple)" ],
'bigosrect1' => [ 3980000.0, "1x1 opaque stippled rectangle (161x145 stipple)" ],
'bigosrect10' => [ 644000.0, "10x10 opaque stippled rectangle (161x145 stipple)" ],
'bigosrect100' => [ 12800.0, "100x100 opaque stippled rectangle (161x145 stipple)" ],
'bigosrect500' => [ 584.0, "500x500 opaque stippled rectangle (161x145 stipple)" ],
'bigtilerect1' => [ 5970000.0, "1x1 tiled rectangle (161x145 tile)" ],
'bigtilerect10' => [ 684000.0, "10x10 tiled rectangle (161x145 tile)" ],
'bigtilerect100' => [ 16200.0, "100x100 tiled rectangle (161x145 tile)" ],
'bigtilerect500' => [ 872.0, "500x500 tiled rectangle (161x145 tile)" ],
'eschertilerect1' => [ 5940000.0, "1x1 tiled rectangle (216x208 tile)" ],
'eschertilerect10' => [ 639000.0, "10x10 tiled rectangle (216x208 tile)" ],
'eschertilerect100' => [ 18000.0, "100x100 tiled rectangle (216x208 tile)" ],
'eschertilerect500' => [ 922.0, "500x500 tiled rectangle (216x208 tile)" ],
'seg1' => [ 28800000.0, "1-pixel line segment" ],
'seg10' => [ 4460000.0, "10-pixel line segment" ],
'seg100' => [ 470000.0, "100-pixel line segment" ],
'seg500' => [ 94600.0, "500-pixel line segment" ],
'seg100c1' => [ 449000.0, "100-pixel line segment (1 kid)" ],
'seg100c2' => [ 432000.0, "100-pixel line segment (2 kids)" ],
'seg100c3' => [ 421000.0, "100-pixel line segment (3 kids)" ],
'dseg10' => [ 3720000.0, "10-pixel dashed segment" ],
'dseg100' => [ 687000.0, "100-pixel dashed segment" ],
'ddseg100' => [ 454000.0, "100-pixel double-dashed segment" ],
'hseg10' => [ 7020000.0, "10-pixel horizontal line segment" ],
'hseg100' => [ 2170000.0, "100-pixel horizontal line segment" ],
'hseg500' => [ 456000.0, "500-pixel horizontal line segment" ],
'vseg10' => [ 3990000.0, "10-pixel vertical line segment" ],
'vseg100' => [ 411000.0, "100-pixel vertical line segment" ],
'vseg500' => [ 82400.0, "500-pixel vertical line segment" ],
'whseg10' => [ 2880000.0, "10x1 wide horizontal line segment" ],
'whseg100' => [ 616000.0, "100x10 wide horizontal line segment" ],
'whseg500' => [ 33300.0, "500x50 wide horizontal line segment" ],
'wvseg10' => [ 2890000.0, "10x1 wide vertical line segment" ],
'wvseg100' => [ 584000.0, "100x10 wide vertical line segment" ],
'wvseg500' => [ 31700.0, "500x50 wide vertical line segment" ],
'line1' => [ 28300000.0, "1-pixel line" ],
'line10' => [ 4470000.0, "10-pixel line" ],
'line100' => [ 472000.0, "100-pixel line" ],
'line500' => [ 94200.0, "500-pixel line" ],
'dline10' => [ 3640000.0, "10-pixel dashed line" ],
'dline100' => [ 673000.0, "100-pixel dashed line" ],
'ddline100' => [ 453000.0, "100-pixel double-dashed line" ],
'wline10' => [ 908000.0, "10x1 wide line" ],
'wline100' => [ 146000.0, "100x10 wide line" ],
'wline500' => [ 30600.0, "500x50 wide line" ],
'wdline100' => [ 69900.0, "100x10 wide dashed line" ],
'wddline100' => [ 60600.0, "100x10 wide double-dashed line" ],
'orect10' => [ 5100000.0, "10x10 rectangle outline" ],
'orect100' => [ 709000.0, "100x100 rectangle outline" ],
'orect500' => [ 146000.0, "500x500 rectangle outline" ],
'worect10' => [ 4530000.0, "10x10 wide rectangle outline" ],
'worect100' => [ 204000.0, "100x100 wide rectangle outline" ],
'worect500' => [ 9790.0, "500x500 wide rectangle outline" ],
'circle1' => [ 5160000.0, "1-pixel circle" ],
'circle10' => [ 1160000.0, "10-pixel circle" ],
'circle100' => [ 141000.0, "100-pixel circle" ],
'circle500' => [ 28900.0, "500-pixel circle" ],
'dcircle100' => [ 98400.0, "100-pixel dashed circle" ],
'ddcircle100' => [ 75000.0, "100-pixel double-dashed circle" ],
'wcircle10' => [ 780000.0, "10-pixel wide circle" ],
'wcircle100' => [ 90900.0, "100-pixel wide circle" ],
'wcircle500' => [ 11300.0, "500-pixel wide circle" ],
'wdcircle100' => [ 8100.0, "100-pixel wide dashed circle" ],
'wddcircle100' => [ 8300.0, "100-pixel wide double-dashed circle" ],
'pcircle10' => [ 1270000.0, "10-pixel partial circle" ],
'pcircle100' => [ 212000.0, "100-pixel partial circle" ],
'wpcircle10' => [ 104000.0, "10-pixel wide partial circle" ],
'wpcircle100' => [ 39000.0, "100-pixel wide partial circle" ],
'fcircle1' => [ 61300000.0, "1-pixel solid circle" ],
'fcircle10' => [ 1720000.0, "10-pixel solid circle" ],
'fcircle100' => [ 120000.0, "100-pixel solid circle" ],
'fcircle500' => [ 5170.0, "500-pixel solid circle" ],
'fcpcircle10' => [ 981000.0, "10-pixel fill chord partial circle" ],
'fcpcircle100' => [ 205000.0, "100-pixel fill chord partial circle" ],
'fspcircle10' => [ 876000.0, "10-pixel fill slice partial circle" ],
'fspcircle100' => [ 187000.0, "100-pixel fill slice partial circle" ],
'ellipse10' => [ 1410000.0, "10-pixel ellipse" ],
'ellipse100' => [ 172000.0, "100-pixel ellipse" ],
'ellipse500' => [ 35100.0, "500-pixel ellipse" ],
'dellipse100' => [ 114000.0, "100-pixel dashed ellipse" ],
'ddellipse100' => [ 88900.0, "100-pixel double-dashed ellipse" ],
'wellipse10' => [ 889000.0, "10-pixel wide ellipse" ],
'wellipse100' => [ 124000.0, "100-pixel wide ellipse" ],
'wellipse500' => [ 15600.0, "500-pixel wide ellipse" ],
'wdellipse100' => [ 7730.0, "100-pixel wide dashed ellipse" ],
'wddellipse100' => [ 6680.0, "100-pixel wide double-dashed ellipse" ],
'pellipse10' => [ 1350000.0, "10-pixel partial ellipse" ],
'pellipse100' => [ 260000.0, "100-pixel partial ellipse" ],
'wpellipse10' => [ 97900.0, "10-pixel wide partial ellipse" ],
'wpellipse100' => [ 16800.0, "100-pixel wide partial ellipse" ],
'fellipse10' => [ 2110000.0, "10-pixel filled ellipse" ],
'fellipse100' => [ 212000.0, "100-pixel filled ellipse" ],
'fellipse500' => [ 11000.0, "500-pixel filled ellipse" ],
'fcpellipse10' => [ 1060000.0, "10-pixel fill chord partial ellipse" ],
'fcpellipse100' => [ 296000.0, "100-pixel fill chord partial ellipse" ],
'fspellipse10' => [ 945000.0, "10-pixel fill slice partial ellipse" ],
'fspellipse100' => [ 269000.0, "100-pixel fill slice partial ellipse" ],
'triangle1' => [ 2460000.0, "Fill 1x1 equivalent triangle" ],
'triangle10' => [ 969000.0, "Fill 10x10 equivalent triangle" ],
'triangle100' => [ 97000.0, "Fill 100x100 equivalent triangle" ],
'trap1' => [ 2630000.0, "Fill 1x1 trapezoid" ],
'trap10' => [ 1260000.0, "Fill 10x10 trapezoid" ],
'trap100' => [ 106000.0, "Fill 100x100 trapezoid" ],
'trap300' => [ 11600.0, "Fill 300x300 trapezoid" ],
'strap1' => [ 2010000.0, "Fill 1x1 stippled trapezoid (8x8 stipple)" ],
'strap10' => [ 910000.0, "Fill 10x10 stippled trapezoid (8x8 stipple)" ],
'strap100' => [ 104000.0, "Fill 100x100 stippled trapezoid (8x8 stipple)" ],
'strap300' => [ 11700.0, "Fill 300x300 stippled trapezoid (8x8 stipple)" ],
'ostrap1' => [ 2000000.0, "Fill 1x1 opaque stippled trapezoid (8x8 stipple)" ],
'ostrap10' => [ 907000.0, "Fill 10x10 opaque stippled trapezoid (8x8 stipple)" ],
'ostrap100' => [ 104000.0, "Fill 100x100 opaque stippled trapezoid (8x8 stipple)" ],
'ostrap300' => [ 11600.0, "Fill 300x300 opaque stippled trapezoid (8x8 stipple)" ],
'tiletrap1' => [ 1430000.0, "Fill 1x1 tiled trapezoid (4x4 tile)" ],
'tiletrap10' => [ 778000.0, "Fill 10x10 tiled trapezoid (4x4 tile)" ],
'tiletrap100' => [ 104000.0, "Fill 100x100 tiled trapezoid (4x4 tile)" ],
'tiletrap300' => [ 11600.0, "Fill 300x300 tiled trapezoid (4x4 tile)" ],
'oddstrap1' => [ 1700000.0, "Fill 1x1 stippled trapezoid (17x15 stipple)" ],
'oddstrap10' => [ 296000.0, "Fill 10x10 stippled trapezoid (17x15 stipple)" ],
'oddstrap100' => [ 18600.0, "Fill 100x100 stippled trapezoid (17x15 stipple)" ],
'oddstrap300' => [ 2090.0, "Fill 300x300 stippled trapezoid (17x15 stipple)" ],
'oddostrap1' => [ 1830000.0, "Fill 1x1 opaque stippled trapezoid (17x15 stipple)" ],
'oddostrap10' => [ 296000.0, "Fill 10x10 opaque stippled trapezoid (17x15 stipple)" ],
'oddostrap100' => [ 18400.0, "Fill 100x100 opaque stippled trapezoid (17x15 stipple)" ],
'oddostrap300' => [ 2080.0, "Fill 300x300 opaque stippled trapezoid (17x15 stipple)" ],
'oddtiletrap1' => [ 1710000.0, "Fill 1x1 tiled trapezoid (17x15 tile)" ],
'oddtiletrap10' => [ 296000.0, "Fill 10x10 tiled trapezoid (17x15 tile)" ],
'oddtiletrap100' => [ 18400.0, "Fill 100x100 tiled trapezoid (17x15 tile)" ],
'oddtiletrap300' => [ 2080.0, "Fill 300x300 tiled trapezoid (17x15 tile)" ],
'bigstrap1' => [ 1510000.0, "Fill 1x1 stippled trapezoid (161x145 stipple)" ],
'bigstrap10' => [ 235000.0, "Fill 10x10 stippled trapezoid (161x145 stipple)" ],
'bigstrap100' => [ 9110.0, "Fill 100x100 stippled trapezoid (161x145 stipple)" ],
'bigstrap300' => [ 1260.0, "Fill 300x300 stippled trapezoid (161x145 stipple)" ],
'bigostrap1' => [ 1480000.0, "Fill 1x1 opaque stippled trapezoid (161x145 stipple)" ],
'bigostrap10' => [ 213000.0, "Fill 10x10 opaque stippled trapezoid (161x145 stipple)" ],
'bigostrap100' => [ 8830.0, "Fill 100x100 opaque stippled trapezoid (161x145 stipple)" ],
'bigostrap300' => [ 1420.0, "Fill 300x300 opaque stippled trapezoid (161x145 stipple)" ],
'bigtiletrap1' => [ 1630000.0, "Fill 1x1 tiled trapezoid (161x145 tile)" ],
'bigtiletrap10' => [ 272000.0, "Fill 10x10 tiled trapezoid (161x145 tile)" ],
'bigtiletrap100' => [ 12900.0, "Fill 100x100 tiled trapezoid (161x145 tile)" ],
'bigtiletrap300' => [ 2350.0, "Fill 300x300 tiled trapezoid (161x145 tile)" ],
'eschertiletrap1' => [ 1650000.0, "Fill 1x1 tiled trapezoid (216x208 tile)" ],
'eschertiletrap10' => [ 273000.0, "Fill 10x10 tiled trapezoid (216x208 tile)" ],
'eschertiletrap100' => [ 13400.0, "Fill 100x100 tiled trapezoid (216x208 tile)" ],
'eschertiletrap300' => [ 2450.0, "Fill 300x300 tiled trapezoid (216x208 tile)" ],
'aatrap1' => [ 260000.0, "Fill 1x1 aa trap" ],
'aatrap10' => [ 23500.0, "Fill 10x10 aa trap" ],
'aatrap100' => [ 13300.0, "Fill 100x100 aa trap" ],
'aatrap300' => [ 4450.0, "Fill 300x300 aa trap" ],
'aa4trap1' => [ 2150.0, "Fill 1x1 aa trap with 4 bit alpha" ],
'aa4trap10' => [ 2130.0, "Fill 10x10 aa trap with 4 bit alpha" ],
'aa4trap100' => [ 1890.0, "Fill 100x100 aa trap with 4 bit alpha" ],
'aa4trap300' => [ 1460.0, "Fill 300x300 aa trap with 4 bit alpha" ],
'aa1trap1' => [ 2200000.0, "Fill 1x1 aa trap with 1 bit alpha" ],
'aa1trap10' => [ 357000.0, "Fill 10x10 aa trap with 1 bit alpha" ],
'aa1trap100' => [ 167000.0, "Fill 100x100 aa trap with 1 bit alpha" ],
'aa1trap300' => [ 67000.0, "Fill 300x300 aa trap with 1 bit alpha" ],
'aatrap2x1' => [ 368000.0, "Fill 2x1 aa trap" ],
'aatrap2x10' => [ 25700.0, "Fill 2x10 aa trap" ],
'aatrap2x100' => [ 12400.0, "Fill 2x100 aa trap" ],
'aatrap2x300' => [ 5710.0, "Fill 2x300 aa trap" ],
'aatrapezoid1' => [ 372000.0, "Fill 1x1 aa trapezoid" ],
'aatrapezoid10' => [ 137000.0, "Fill 10x10 aa trapezoid" ],
'aatrapezoid100' => [ 9590.0, "Fill 100x100 aa trapezoid" ],
'aatrapezoid300' => [ 1420.0, "Fill 300x300 aa trapezoid" ],
'addaatrapezoid1' => [ 433000.0, "Fill 1x1 aa pre-added trapezoid" ],
'addaatrapezoid10' => [ 24100.0, "Fill 10x10 aa pre-added trapezoid" ],
'addaatrapezoid100' => [ 13300.0, "Fill 100x100 aa pre-added trapezoid" ],
'addaatrapezoid300' => [ 4460.0, "Fill 300x300 aa pre-added trapezoid" ],
'complex10' => [ 655000.0, "Fill 10x10 equivalent complex polygon" ],
'complex100' => [ 87000.0, "Fill 100x100 equivalent complex polygons" ],
'64poly10convex' => [ 481000.0, "Fill 10x10 64-gon (Convex)" ],
'64poly100convex' => [ 105000.0, "Fill 100x100 64-gon (Convex)" ],
'64poly10complex' => [ 353000.0, "Fill 10x10 64-gon (Complex)" ],
'64poly100complex' => [ 105000.0, "Fill 100x100 64-gon (Complex)" ],
'ftext' => [ 2200000.0, "Char in 80-char line (6x13)" ],
'f8text' => [ 1970000.0, "Char in 70-char line (8x13)" ],
'f9text' => [ 1690000.0, "Char in 60-char line (9x15)" ],
'f14text16' => [ 679000.0, "Char16 in 40-char line (k14)" ],
'f24text16' => [ 272000.0, "Char16 in 23-char line (k24)" ],
'tr10text' => [ 2520000.0, "Char in 80-char line (TR 10)" ],
'tr24text' => [ 940000.0, "Char in 30-char line (TR 24)" ],
'polytext' => [ 2230000.0, "Char in 20/40/20 line (6x13, TR 10)" ],
'polytext16' => [ 369000.0, "Char16 in 7/14/7 line (k14, k24)" ],
'fitext' => [ 1350000.0, "Char in 80-char image line (6x13)" ],
'f8itext' => [ 1130000.0, "Char in 70-char image line (8x13)" ],
'f9itext' => [ 902000.0, "Char in 60-char image line (9x15)" ],
'f14itext16' => [ 449000.0, "Char16 in 40-char image line (k14)" ],
'f24itext16' => [ 169000.0, "Char16 in 23-char image line (k24)" ],
'tr10itext' => [ 1590000.0, "Char in 80-char image line (TR 10)" ],
'tr24itext' => [ 435000.0, "Char in 30-char image line (TR 24)" ],
'aa10text' => [ 53200.0, "Char in 80-char aa line (Charter 10)" ],
'aa24text' => [ 13300.0, "Char in 30-char aa line (Charter 24)" ],
'aaftext' => [ 45200.0, "Char in 80-char aa line (Courier 12)" ],
'a10text' => [ 53100.0, "Char in 80-char a line (Charter 10)" ],
'a24text' => [ 13300.0, "Char in 30-char a line (Charter 24)" ],
'aftext' => [ 45200.0, "Char in 80-char a line (Courier 12)" ],
'rgb10text' => [ 49400.0, "Char in 80-char rgb line (Charter 10)" ],
'rgb24text' => [ 10200.0, "Char in 30-char rgb line (Charter 24)" ],
'rgbftext' => [ 42200.0, "Char in 80-char rgb line (Courier 12)" ],
'caa10text' => [ 15300.0, "Char in 80-char aa core line (Charter 10)" ],
'caa24text' => [ 2540.0, "Char in 30-char aa core line (Charter 24)" ],
'caaftext' => [ 10900.0, "Char in 80-char aa core line (Courier 12)" ],
'ca10text' => [ 15300.0, "Char in 80-char a core line (Charter 10)" ],
'ca24text' => [ 2540.0, "Char in 30-char a core line (Charter 24)" ],
'caftext' => [ 10900.0, "Char in 80-char a core line (Courier 12)" ],
'rgb10text' => [ 15000.0, "Char in 80-char rgb core line (Charter 10)" ],
'rgb24text' => [ 2510.0, "Char in 30-char rgb core line (Charter 24)" ],
'rgbftext' => [ 10700.0, "Char in 80-char rgb core line (Courier 12)" ],
'scroll10' => [ 1310000.0, "Scroll 10x10 pixels" ],
'scroll100' => [ 52000.0, "Scroll 100x100 pixels" ],
'scroll500' => [ 2190.0, "Scroll 500x500 pixels" ],
'copywinwin10' => [ 1030000.0, "Copy 10x10 from window to window" ],
'copywinwin100' => [ 52200.0, "Copy 100x100 from window to window" ],
'copywinwin500' => [ 2080.0, "Copy 500x500 from window to window" ],
'copypixwin10' => [ 502000.0, "Copy 10x10 from pixmap to window" ],
'copypixwin100' => [ 20300.0, "Copy 100x100 from pixmap to window" ],
'copypixwin500' => [ 1020.0, "Copy 500x500 from pixmap to window" ],
'copywinpix10' => [ 7730.0, "Copy 10x10 from window to pixmap" ],
'copywinpix100' => [ 127.0, "Copy 100x100 from window to pixmap" ],
'copywinpix500' => [ 5.0, "Copy 500x500 from window to pixmap" ],
'copypixpix10' => [ 1260000.0, "Copy 10x10 from pixmap to pixmap" ],
'copypixpix100' => [ 56300.0, "Copy 100x100 from pixmap to pixmap" ],
'copypixpix500' => [ 2470.0, "Copy 500x500 from pixmap to pixmap" ],
'copyplane10' => [ 466000.0, "Copy 10x10 1-bit deep plane" ],
'copyplane100' => [ 13700.0, "Copy 100x100 1-bit deep plane" ],
'copyplane500' => [ 671.0, "Copy 500x500 1-bit deep plane" ],
'deepcopyplane10' => [ 151000.0, "Copy 10x10 n-bit deep plane" ],
'deepcopyplane100' => [ 6090.0, "Copy 100x100 n-bit deep plane" ],
'deepcopyplane500' => [ 278.0, "Copy 500x500 n-bit deep plane" ],
'putimage10' => [ 434000.0, "PutImage 10x10 square" ],
'putimage100' => [ 13600.0, "PutImage 100x100 square" ],
'putimage500' => [ 713.0, "PutImage 500x500 square" ],
'putimagexy10' => [ 321.0, "PutImage XY 10x10 square" ],
'putimagexy100' => [ 3.2, "PutImage XY 100x100 square" ],
'putimagexy500' => [ 0.1, "PutImage XY 500x500 square" ],
'shmput10' => [ 465000.0, "ShmPutImage 10x10 square" ],
'shmput100' => [ 20200.0, "ShmPutImage 100x100 square" ],
'shmput500' => [ 1020.0, "ShmPutImage 500x500 square" ],
'shmputxy10' => [ 31400.0, "ShmPutImage XY 10x10 square" ],
'shmputxy100' => [ 458.0, "ShmPutImage XY 100x100 square" ],
'shmputxy500' => [ 19.0, "ShmPutImage XY 500x500 square" ],
'getimage10' => [ 6650.0, "GetImage 10x10 square" ],
'getimage100' => [ 77.0, "GetImage 100x100 square" ],
'getimage500' => [ 3.1, "GetImage 500x500 square" ],
'getimagexy10' => [ 320.0, "GetImage XY 10x10 square" ],
'getimagexy100' => [ 3.2, "GetImage XY 100x100 square" ],
'getimagexy500' => [ 0.1, "GetImage XY 500x500 square" ],
'noop' => [ 8760000.0, "X protocol NoOperation" ],
'pointer' => [ 54800.0, "QueryPointer" ],
'prop' => [ 50900.0, "GetProperty" ],
'gc' => [ 1190000.0, "Change graphics context" ],
'create' => [ 597000.0, "Create and map subwindows (25 kids)" ],
'ucreate' => [ 1100000.0, "Create unmapped window (25 kids)" ],
'map' => [ 1350000.0, "Map window via parent (25 kids)" ],
'unmap' => [ 3360000.0, "Unmap window via parent (25 kids)" ],
'destroy' => [ 1190000.0, "Destroy window via parent (25 kids)" ],
'popup' => [ 660000.0, "Hide/expose window via popup (25 kids)" ],
'move' => [ 120000.0, "Move window (25 kids)" ],
'umove' => [ 1990000.0, "Moved unmapped window (25 kids)" ],
'movetree' => [ 877000.0, "Move window via parent (25 kids)" ],
'resize' => [ 136000.0, "Resize window (25 kids)" ],
'uresize' => [ 1870000.0, "Resize unmapped window (25 kids)" ],
'circulate' => [ 56300.0, "Circulate window (25 kids)" ],
'ucirculate' => [ 3630000.0, "Circulate Unmapped window (25 kids)" ],
};
# This array defines named groups of tests. This is designed to allow
# for simpler runs of related tests.
#
# Note that this array does *not* include all the x11perf tests. The idea
# here is to run a representative sampling of the available tests, to get
# a general idea of a system's performance, without taking forever to
# do it. If you want to do detailed analysis of an X server or graphics
# chip, then use x11perf directly.
my $testGroups = {
'rects' => [ "rect10", "rect100", "oddtilerect10", "eschertilerect100" ],
'lines' => [ "seg100c3", "wvseg100", "ddline100", "worect500" ],
'circle' => [ "circle500", "wddcircle100", "wpcircle100", "fspcircle100" ],
'ellipse' => [ "ddellipse100", "wddellipse100", "pellipse10", "fspellipse100" ],
'shapes' => [ "triangle10", "trap300", "oddostrap300", "eschertiletrap300" ],
'aashapes' => [ "aa4trap300", "aa1trap10", "aatrap2x300", "addaatrapezoid300" ],
'polys' => [ "complex10", "64poly100convex", "64poly10complex", "64poly100complex" ],
'text' => [ "polytext16", "rgb24text", "caa10text", "ca24text" ],
'blit' => [ "scroll100", "copypixwin10", "deepcopyplane10", "putimagexy500" ],
'window' => [ "popup", "move", "movetree", "resize" ],
};
############################################################################
# CODE
############################################################################
# Exec the given command, and catch its standard output.
# We return an array containing the PID and the filehandle on the
# process' standard output. It's up to the caller to wait for the command
# to terminate.
sub command {
my ( $cmd ) = @_;
my $pid = open(my $childFd, "-|");
if (!defined($pid)) {
die("Run: fork() failed (undef)\n");
} elsif ($pid == 0) {
exec($cmd);
die("Run: exec() failed (returned)\n");
}
return ( $pid, $childFd );
}
# Get data from running a system command. Used for things like getting
# the host OS from `uname -o` etc.
#
# Ignores initial blank lines from the command and returns the first
# non-blank line, with white space trimmed off.
sub runTest {
my ( $test, $reps, $time ) = @_;
my $tdata = $testData->{$test};
if (!defined($tdata)) {
printf STDERR "gfx-x11: No such test: %s\n", $test;
exit(9);
}
my $cmd = sprintf "x11perf -repeat %d -subs 25 -time %d -%s",
$reps, $time, $test;
my ( $pid, $fd ) = command($cmd);
my $average = 0;
while (<$fd>) {
chomp;
# Display the output for logging.
printf "%s\n", $_;
# Save the score.
my ( $reps, $per, $rate ) =
( m:([0-9]+)\s+trep\s+@\s+([0-9.]+)\s+msec\s+\(\s*([0-9.]+)/sec\): );
$average = $rate if (defined($rate));
}
# Close the command and wait for it to die. Bomb out if it failed.
# close($fd);
my $p = waitpid($pid, 0);
my $status = $?;
exit($status) if ($status != 0);
# Calculate and return the weighted result.
my $score = $average / $tdata->[0] * 1000.0;
printf "Test %s: %d --> %.1f\n", $test, $average, $score;
return $score;
}
sub runGroup {
my ( $group, $reps, $time ) = @_;
my $gdata = $testGroups->{$group};
if (!defined($gdata)) {
printf STDERR "gfx-x11: No such test group: %s\n", $group;
exit(9);
}
my $count = 0;
my $total = 0;
foreach my $test (@$gdata) {
$total += runTest($test, $reps, $time);
++$count;
}
$total /= $count;
$total;
}
############################################################################
# MAIN
############################################################################
sub main {
my @args = @_;
if (scalar(@args) < 3) {
printf STDERR "Usage: gfx-x11 group reps time\n";
exit(9);
}
my $reps = $args[1];
my $time = $args[2];
my $score = runGroup($args[0], $reps, $time);
printf STDERR "COUNT|%.1f|0|score\n", $score;
return 0;
}
exit(main(@ARGV));

View File

@@ -0,0 +1,46 @@
# Baseline benchmark scores, used for calculating index results.
# Scores from "George", a SPARCstation 20-61.
dhry2reg|10|lps|116700|116700|2
whetstone-double|10|MWIPS|55.0|55.0|2
execl|20|lps|43.0|43.0|1
fstime|20|KBps|3960|3960|1
fsbuffer|20|KBps|1655|1655|1
fsdisk|20|KBps|5800|5800|1
pipe|10|lps|12440|12440|2
context1|10|lps|4000|4000|2
spawn|20|lps|126|126|1
shell8|60|lpm|6|6|1
syscall|10|lps|15000|15000|2
# The shell1 test was added to the index in 5.0, and this baseline score
# was extrapolated to roughly match George's performance.
shell1|60|lpm|42.4|42.4|1
# The 2D baseline scores were derived from a test run on an HP Compaq nc8430
# with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly
# common modern adaptor with 3D. The baseline scores here are then
# 1/66.6 of the values from that run, to bring them roughly in line with
# George. (The HP has an index score of 666.6 single-process.)
2d-rects|3|score|15|15|1
#2d-lines|3|score|15|15|1
#2d-circle|3|score|15|15|1
2d-ellipse|3|score|15|15|1
#2d-shapes|3|score|15|15|1
2d-aashapes|3|score|15|15|1
#2d-polys|3|score|15|15|1
2d-text|3|score|15|15|1
2d-blit|3|score|15|15|1
2d-window|3|score|15|15|1
# The gears test score is derived from a test run on an HP Compaq nc8430
# with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly
# common modern adaptor with 3D. The baseline scores here are then
# 1/66.6 of the values from that run, to bring them roughly in line with
# George.
ubgears|20|fps|33.4|33.4|3
# The grep and sysexec tests were added in 5.1.1; they are not index tests,
# but these baseline scores were added for convenience.
grep|30|lpm|1|1|3
sysexec|10|lps|25|25|10

View File

@@ -0,0 +1,23 @@
#! /bin/sh
###############################################################################
# The BYTE UNIX Benchmarks - Release 3
# Module: multi.sh SID: 3.4 5/15/91 19:30:24
#
###############################################################################
# Bug reports, patches, comments, suggestions should be sent to:
#
# Ben Smith or Rick Grehan at BYTE Magazine
# ben@bytepb.UUCP rick_g@bytepb.UUCP
#
###############################################################################
# Modification Log:
#
###############################################################################
ID="@(#)multi.sh:3.4 -- 5/15/91 19:30:24";
instance=1
while [ $instance -le $1 ]; do
/bin/sh "$UB_BINDIR/tst.sh" &
instance=`expr $instance + 1`
done
wait

View File

@@ -0,0 +1,19 @@
#! /bin/sh
###############################################################################
# The BYTE UNIX Benchmarks - Release 3
# Module: tst.sh SID: 3.4 5/15/91 19:30:24
#
###############################################################################
# Bug reports, patches, comments, suggestions should be sent to:
#
# Ben Smith or Rick Grehan at BYTE Magazine
# ben@bytepb.UUCP rick_g@bytepb.UUCP
#
###############################################################################
# Modification Log:
#
###############################################################################
ID="@(#)tst.sh:3.4 -- 5/15/91 19:30:24";
sort >sort.$$ <sort.src
grep the sort.$$ | tee grep.$$ | wc > wc.$$
rm sort.$$ grep.$$ wc.$$

View File

@@ -0,0 +1,14 @@
# # # # # # # ##### ###### # # #### # #
# # ## # # # # # # # ## # # # # #
# # # # # # ## ##### ##### # # # # ######
# # # # # # ## # # # # # # # # #
# # # ## # # # # # # # ## # # # #
#### # # # # # ##### ###### # # #### # #
Version 5.1.2 Based on the Byte Magazine Unix Benchmark
Multi-CPU version Version 5 revisions by Ian Smith,
Sunnyvale, CA, USA
December 22, 2007 johantheghost at yahoo period com

View File

@@ -0,0 +1,4 @@
#!/bin/sh
export CC=cc
export MINIX=1
./Run

View File

@@ -0,0 +1,108 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: arith.c SID: 3.3 5/15/91 19:30:19
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* May 12, 1989 - modified empty loops to avoid nullifying by optimizing
* compilers
* August 28, 1990 - changed timing relationship--now returns total number
* of iterations (ty)
* November 9, 1990 - made changes suggested by Keith Cantrell
* (digi!kcantrel) to defeat optimization
* to non-existence
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)arith.c:3.3 -- 5/15/91 19:30:19";
/*
* arithmetic test
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "timeit.c"
int dumb_stuff(int);
unsigned long iter;
/* this function is called when the alarm expires */
void report(int sig)
{
fprintf(stderr,"COUNT|%ld|1|lps\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
int duration;
int result = 0;
if (argc != 2) {
printf("Usage: %s duration\n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
/* set up alarm call */
iter = 0; /* init iteration count */
wake_me(duration, report);
/* this loop will be interrupted by the alarm call */
while (1)
{
/* in switching to time-based (instead of iteration-based),
the following statement was added. It should not skew
the timings too much--there was an increment and test
in the "while" expression above. The only difference is
that now we're incrementing a long instead of an int. (ty) */
++iter;
/* the loop calls a function to insure that something is done
the results of the function are fed back in (just so they
they won't be thrown away. A loop with
unused assignments may get optimized out of existence */
result = dumb_stuff(result);
}
}
/************************** dumb_stuff *******************/
int dumb_stuff(i)
int i;
{
#ifndef arithoh
datum x, y, z;
z = 0;
#endif
/*
* 101
* sum i*i/(i*i-1)
* i=2
*/
/* notice that the i value is always reset by the loop */
for (i=2; i<=101; i++)
{
#ifndef arithoh
x = i;
y = x*x;
z += y/(y-1);
}
return(x+y+z);
#else
}
return(0);
#endif
}

View File

@@ -0,0 +1,595 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: big.c SID: 3.3 5/15/91 19:30:18
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* 10/22/97 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
/*
* dummy code for execl test [ old version of makework.c ]
*
* makework [ -r rate ] [ -c copyfile ] nusers
*
* job streams are specified on standard input with lines of the form
* full_path_name_for_command [ options ] [ <standard_input_file ]
*
* "standard input" is send to all nuser instances of the commands in the
* job streams at a rate not in excess of "rate" characters per second
* per command
*
*/
/* this code is included in other files and therefore has no SCCSid */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/wait.h>
#define DEF_RATE 5.0
#define GRANULE 5
#define CHUNK 60
#define MAXCHILD 12
#define MAXWORK 10
/* Can't seem to get this declared in the headers... */
extern int kill(pid_t pid, int sig);
void wrapup(char *);
void onalarm(int);
void pipeerr(int sig);
void grunt(int sig);
void getwork(void);
#if debug
void dumpwork(void);
#endif
void fatal(char *s);
float thres;
float est_rate = DEF_RATE;
int nusers; /* number of concurrent users to be simulated by
* this process */
int firstuser; /* ordinal identification of first user for this
* process */
int nwork = 0; /* number of job streams */
int exit_status = 0; /* returned to parent */
int sigpipe; /* pipe write error flag */
struct st_work {
char *cmd; /* name of command to run */
char **av; /* arguments to command */
char *input; /* standard input buffer */
int inpsize; /* size of standard input buffer */
char *outf; /* standard output (filename) */
} work[MAXWORK];
struct {
int xmit; /* # characters sent */
char *bp; /* std input buffer pointer */
int blen; /* std input buffer length */
int fd; /* stdin to command */
int pid; /* child PID */
char *line; /* start of input line */
int firstjob; /* inital piece of work */
int thisjob; /* current piece of work */
} child[MAXCHILD], *cp;
int main(int argc, char *argv[])
{
int i;
int l;
int fcopy = 0; /* fd for copy output */
int master = 1; /* the REAL master, == 0 for clones */
int nchild; /* no. of children for a clone to run */
int done; /* count of children finished */
int output; /* aggregate output char count for all
children */
int c;
int thiswork = 0; /* next job stream to allocate */
int nch; /* # characters to write */
int written; /* # characters actully written */
char logname[15]; /* name of the log file(s) */
int pvec[2]; /* for pipes */
char *p;
char *prog; /* my name */
#if ! debug
freopen("masterlog.00", "a", stderr);
#endif
prog = argv[0];
while (argc > 1 && argv[1][0] == '-') {
p = &argv[1][1];
argc--;
argv++;
while (*p) {
switch (*p) {
case 'r':
est_rate = atoi(argv[1]);
sscanf(argv[1], "%f", &est_rate);
if (est_rate <= 0) {
fprintf(stderr, "%s: bad rate, reset to %.2f chars/sec\n", prog, DEF_RATE);
est_rate = DEF_RATE;
}
argc--;
argv++;
break;
case 'c':
fcopy = open(argv[1], 1);
if (fcopy < 0)
fcopy = creat(argv[1], 0600);
if (fcopy < 0) {
fprintf(stderr, "%s: cannot open copy file '%s'\n",
prog, argv[1]);
exit(2);
}
lseek(fcopy, 0L, 2); /* append at end of file */
argc--;
argv++;
break;
default:
fprintf(stderr, "%s: bad flag '%c'\n", prog, *p);
exit(4);
}
p++;
}
}
if (argc < 2) {
fprintf(stderr, "%s: missing nusers\n", prog);
exit(4);
}
nusers = atoi(argv[1]);
if (nusers < 1) {
fprintf(stderr, "%s: impossible nusers (%d<-%s)\n", prog, nusers, argv[1]);
exit(4);
}
fprintf(stderr, "%d Users\n", nusers);
argc--;
argv++;
/* build job streams */
getwork();
#if debug
dumpwork();
#endif
/* clone copies of myself to run up to MAXCHILD jobs each */
firstuser = MAXCHILD;
fprintf(stderr, "master pid %d\n", getpid());
fflush(stderr);
while (nusers > MAXCHILD) {
fflush(stderr);
if (nusers >= 2*MAXCHILD)
/* the next clone must run MAXCHILD jobs */
nchild = MAXCHILD;
else
/* the next clone must run the leftover jobs */
nchild = nusers - MAXCHILD;
if ((l = fork()) == -1) {
/* fork failed */
fatal("** clone fork failed **\n");
goto bepatient;
} else if (l > 0) {
fprintf(stderr, "master clone pid %d\n", l);
/* I am the master with nchild fewer jobs to run */
nusers -= nchild;
firstuser += MAXCHILD;
continue;
} else {
/* I am a clone, run MAXCHILD jobs */
#if ! debug
sprintf(logname, "masterlog.%02d", firstuser/MAXCHILD);
freopen(logname, "w", stderr);
#endif
master = 0;
nusers = nchild;
break;
}
}
if (master)
firstuser = 0;
close(0);
for (i = 0; i < nusers; i++ ) {
fprintf(stderr, "user %d job %d ", firstuser+i, thiswork);
if (pipe(pvec) == -1) {
/* this is fatal */
fatal("** pipe failed **\n");
goto bepatient;
}
fflush(stderr);
if ((child[i].pid = fork()) == 0) {
int fd;
/* the command */
if (pvec[0] != 0) {
close(0);
dup(pvec[0]);
}
#if ! debug
sprintf(logname, "userlog.%02d", firstuser+i);
freopen(logname, "w", stderr);
#endif
for (fd = 3; fd < 24; fd++)
close(fd);
if (work[thiswork].outf[0] != '\0') {
/* redirect std output */
char *q;
for (q = work[thiswork].outf; *q != '\n'; q++) ;
*q = '\0';
if (freopen(work[thiswork].outf, "w", stdout) == NULL) {
fprintf(stderr, "makework: cannot open %s for std output\n",
work[thiswork].outf);
fflush(stderr);
}
*q = '\n';
}
execv(work[thiswork].cmd, work[thiswork].av);
/* don't expect to get here! */
fatal("** exec failed **\n");
goto bepatient;
}
else if (child[i].pid == -1) {
fatal("** fork failed **\n");
goto bepatient;
}
else {
close(pvec[0]);
child[i].fd = pvec[1];
child[i].line = child[i].bp = work[thiswork].input;
child[i].blen = work[thiswork].inpsize;
child[i].thisjob = thiswork;
child[i].firstjob = thiswork;
fprintf(stderr, "pid %d pipe fd %d", child[i].pid, child[i].fd);
if (work[thiswork].outf[0] != '\0') {
char *q;
fprintf(stderr, " > ");
for (q=work[thiswork].outf; *q != '\n'; q++)
fputc(*q, stderr);
}
fputc('\n', stderr);
thiswork++;
if (thiswork >= nwork)
thiswork = 0;
}
}
fflush(stderr);
srand(time(0));
thres = 0;
done = output = 0;
for (i = 0; i < nusers; i++) {
if (child[i].blen == 0)
done++;
else
thres += est_rate * GRANULE;
}
est_rate = thres;
signal(SIGALRM, onalarm);
signal(SIGPIPE, pipeerr);
alarm(GRANULE);
while (done < nusers) {
for (i = 0; i < nusers; i++) {
cp = &child[i];
if (cp->xmit >= cp->blen) continue;
l = rand() % CHUNK + 1; /* 1-CHUNK chars */
if (l == 0) continue;
if (cp->xmit + l > cp->blen)
l = cp->blen - cp->xmit;
p = cp->bp;
cp->bp += l;
cp->xmit += l;
#if debug
fprintf(stderr, "child %d, %d processed, %d to go\n", i, cp->xmit, cp->blen - cp->xmit);
#endif
while (p < cp->bp) {
if (*p == '\n' || (p == &cp->bp[-1] && cp->xmit >= cp->blen)) {
/* write it out */
nch = p - cp->line + 1;
if ((written = write(cp->fd, cp->line, nch)) != nch) {
/* argh! */
cp->line[nch] = '\0';
fprintf(stderr, "user %d job %d cmd %s ",
firstuser+i, cp->thisjob, cp->line);
fprintf(stderr, "write(,,%d) returns %d\n", nch, written);
if (sigpipe)
fatal("** SIGPIPE error **\n");
else
fatal("** write error **\n");
goto bepatient;
}
if (fcopy)
write(fcopy, cp->line, p - cp->line + 1);
#if debug
fprintf(stderr, "child %d gets \"", i);
{
char *q = cp->line;
while (q <= p) {
if (*q >= ' ' && *q <= '~')
fputc(*q, stderr);
else
fprintf(stderr, "\\%03o", *q);
q++;
}
}
fputc('"', stderr);
#endif
cp->line = &p[1];
}
p++;
}
if (cp->xmit >= cp->blen) {
done++;
close(cp->fd);
#if debug
fprintf(stderr, "child %d, close std input\n", i);
#endif
}
output += l;
}
while (output > thres) {
pause();
#if debug
fprintf(stderr, "after pause: output, thres, done %d %.2f %d\n", output, thres, done);
#endif
}
}
bepatient:
alarm(0);
/****
* If everything is going OK, we should simply be able to keep
* looping unitil 'wait' fails, however some descendent process may
* be in a state from which it can never exit, and so a timeout
* is used.
* 5 minutes should be ample, since the time to run all jobs is of
* the order of 5-10 minutes, however some machines are painfully slow,
* so the timeout has been set at 20 minutes (1200 seconds).
****/
signal(SIGALRM, grunt);
alarm(1200);
while ((c = wait(&l)) != -1) {
for (i = 0; i < nusers; i++) {
if (c == child[i].pid) {
fprintf(stderr, "user %d job %d pid %d done", firstuser+i, child[i].thisjob, c);
if (l != 0) {
if (l & 0x7f)
fprintf(stderr, " status %d", l & 0x7f);
if (l & 0xff00)
fprintf(stderr, " exit code %d", (l>>8) & 0xff);
exit_status = 4;
}
fputc('\n', stderr);
c = child[i].pid = -1;
break;
}
}
if (c != -1) {
fprintf(stderr, "master clone done, pid %d ", c);
if (l != 0) {
if (l & 0x7f)
fprintf(stderr, " status %d", l & 0x7f);
if (l & 0xff00)
fprintf(stderr, " exit code %d", (l>>8) & 0xff);
exit_status = 4;
}
fputc('\n', stderr);
}
}
alarm(0);
wrapup("Finished waiting ...");
exit(0);
}
void onalarm(int foo)
{
thres += est_rate;
signal(SIGALRM, onalarm);
alarm(GRANULE);
}
void grunt(int sig)
{
/* timeout after label "bepatient" in main */
exit_status = 4;
wrapup("Timed out waiting for jobs to finish ...");
}
void pipeerr(int sig)
{
sigpipe++;
}
void wrapup(char *reason)
{
int i;
int killed = 0;
fflush(stderr);
for (i = 0; i < nusers; i++) {
if (child[i].pid > 0 && kill(child[i].pid, SIGKILL) != -1) {
if (!killed) {
killed++;
fprintf(stderr, "%s\n", reason);
fflush(stderr);
}
fprintf(stderr, "user %d job %d pid %d killed off\n", firstuser+i, child[i].thisjob, child[i].pid);
fflush(stderr);
}
}
exit(exit_status);
}
#define MAXLINE 512
void getwork(void)
{
int i;
int f;
int ac=0;
char *lp = (void *)0;
char *q = (void *)0;
struct st_work *w = (void *)0;
char line[MAXLINE];
char c;
while (fgets(line, MAXLINE, stdin) != NULL) {
if (nwork >= MAXWORK) {
fprintf(stderr, "Too many jobs specified, .. increase MAXWORK\n");
exit(4);
}
w = &work[nwork];
lp = line;
i = 1;
while (*lp && *lp != ' ') {
i++;
lp++;
}
w->cmd = (char *)malloc(i);
strncpy(w->cmd, line, i-1);
w->cmd[i-1] = '\0';
w->inpsize = 0;
w->input = "";
/* start to build arg list */
ac = 2;
w->av = (char **)malloc(2*sizeof(char *));
q = w->cmd;
while (*q) q++;
q--;
while (q >= w->cmd) {
if (*q == '/') {
q++;
break;
}
q--;
}
w->av[0] = q;
while (*lp) {
if (*lp == ' ') {
/* space */
lp++;
continue;
}
else if (*lp == '<') {
/* standard input for this job */
q = ++lp;
while (*lp && *lp != ' ') lp++;
c = *lp;
*lp = '\0';
if ((f = open(q, 0)) == -1) {
fprintf(stderr, "cannot open input file (%s) for job %d\n",
q, nwork);
exit(4);
}
/* gobble input */
w->input = (char *)malloc(512);
while ((i = read(f, &w->input[w->inpsize], 512)) > 0) {
w->inpsize += i;
w->input = (char *)realloc(w->input, w->inpsize+512);
}
w->input = (char *)realloc(w->input, w->inpsize);
close(f);
/* extract stdout file name from line beginning "C=" */
w->outf = "";
for (q = w->input; q < &w->input[w->inpsize-10]; q++) {
if (*q == '\n' && strncmp(&q[1], "C=", 2) == 0) {
w->outf = &q[3];
break;
}
}
#if debug
if (*w->outf) {
fprintf(stderr, "stdout->");
for (q=w->outf; *q != '\n'; q++)
fputc(*q, stderr);
fputc('\n', stderr);
}
#endif
}
else {
/* a command option */
ac++;
w->av = (char **)realloc(w->av, ac*sizeof(char *));
q = lp;
i = 1;
while (*lp && *lp != ' ') {
lp++;
i++;
}
w->av[ac-2] = (char *)malloc(i);
strncpy(w->av[ac-2], q, i-1);
w->av[ac-2][i-1] = '\0';
}
}
w->av[ac-1] = (char *)0;
nwork++;
}
}
#if debug
void dumpwork(void)
{
int i;
int j;
for (i = 0; i < nwork; i++) {
fprintf(stderr, "job %d: cmd: %s\n", i, work[i].cmd);
j = 0;
while (work[i].av[j]) {
fprintf(stderr, "argv[%d]: %s\n", j, work[i].av[j]);
j++;
}
fprintf(stderr, "input: %d chars text: ", work[i].inpsize);
if (work[i].input == (char *)0)
fprintf(stderr, "<NULL>\n");
else {
register char *pend;
char *p;
char c;
p = work[i].input;
while (*p) {
pend = p;
while (*pend && *pend != '\n')
pend++;
c = *pend;
*pend = '\0';
fprintf(stderr, "%s\n", p);
*pend = c;
p = &pend[1];
}
}
}
}
#endif
void fatal(char *s)
{
int i;
fprintf(stderr, s);
fflush(stderr);
perror("Reason?");
fflush(stderr);
for (i = 0; i < nusers; i++) {
if (child[i].pid > 0 && kill(child[i].pid, SIGKILL) != -1) {
fprintf(stderr, "pid %d killed off\n", child[i].pid);
fflush(stderr);
}
}
exit_status = 4;
}

View File

@@ -0,0 +1,125 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: context1.c SID: 3.3 5/15/91 19:30:18
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta $
* August 28, 1990 - changed timing routines--now returns total number of
* iterations in specified time period
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)context1.c:3.3 -- 5/15/91 19:30:18";
/*
* Context switching via synchronized unbuffered pipe i/o
*
*/
#ifdef MINIX
#include <signal.h>
#endif
#ifdef LINUX
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "timeit.c"
unsigned long iter;
#ifdef MINIX
pid_t child;
#endif
void report(int sig)
{
fprintf(stderr, "COUNT|%lu|1|lps\n", iter);
#ifdef MINIX
kill(child, SIGKILL);
#endif
exit(0);
}
int main(int argc, char *argv[])
{
int duration;
unsigned long check;
int p1[2], p2[2];
if (argc != 2) {
fprintf(stderr, "Usage: context duration\n");
exit(1);
}
duration = atoi(argv[1]);
/* set up alarm call */
iter = 0;
wake_me(duration, report);
if (pipe(p1) || pipe(p2)) {
perror("pipe create failed");
exit(1);
}
#ifndef MINIX
if (fork()) { /* parent process */
#else
if ((child = fork())) {
#endif
/* master, write p1 & read p2 */
close(p1[0]); close(p2[1]);
while (1) {
if (write(p1[1], (char *)&iter, sizeof(iter)) != sizeof(iter)) {
if ((errno != 0) && (errno != EINTR))
perror("master write failed");
exit(1);
}
if (read(p2[0], (char *)&check, sizeof(check)) != sizeof(check)) {
if ((errno != 0) && (errno != EINTR))
perror("master read failed");
exit(1);
}
if (check != iter) {
fprintf(stderr, "Master sync error: expect %lu, got %lu\n",
iter, check);
exit(2);
}
iter++;
}
}
else { /* child process */
unsigned long iter1;
iter1 = 0;
/* slave, read p1 & write p2 */
close(p1[1]); close(p2[0]);
while (1) {
if (read(p1[0], (char *)&check, sizeof(check)) != sizeof(check)) {
if ((errno != 0) && (errno != EINTR))
perror("slave read failed");
exit(1);
}
if (check != iter1) {
fprintf(stderr, "Slave sync error: expect %lu, got %lu\n",
iter, check);
exit(2);
}
if (write(p2[1], (char *)&iter1, sizeof(iter1)) != sizeof(check)) {
if ((errno != 0) && (errno != EINTR))
perror("slave write failed");
exit(1);
}
iter1++;
}
}
}

View File

@@ -0,0 +1,435 @@
/*****************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: dhry.h SID: 3.4 5/15/91 19:30:21
*
*****************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*****************************************************************************
* Modification Log:
* addapted from:
*
*
* "DHRYSTONE" Benchmark Program
* -----------------------------
*
* Version: C, Version 2.1
*
* File: dhry.h (part 1 of 3)
*
* Date: May 25, 1988
*
* Author: Reinhold P. Weicker
* Siemens AG, AUT E 51
* Postfach 3220
* 8520 Erlangen
* Germany (West)
* Phone: [+49]-9131-7-20330
* (8-17 Central European Time)
* Usenet: ..!mcvax!unido!estevax!weicker
*
* Original Version (in Ada) published in
* "Communications of the ACM" vol. 27., no. 10 (Oct. 1984),
* pp. 1013 - 1030, together with the statistics
* on which the distribution of statements etc. is based.
*
* In this C version, the following C library functions are used:
* - strcpy, strcmp (inside the measurement loop)
* - printf, scanf (outside the measurement loop)
* In addition, Berkeley UNIX system calls "times ()" or "time ()"
* are used for execution time measurement. For measurements
* on other systems, these calls have to be changed.
*
* Collection of Results:
* Reinhold Weicker (address see above) and
*
* Rick Richardson
* PC Research. Inc.
* 94 Apple Orchard Drive
* Tinton Falls, NJ 07724
* Phone: (201) 834-1378 (9-17 EST)
* Usenet: ...!seismo!uunet!pcrat!rick
*
* Please send results to Rick Richardson and/or Reinhold Weicker.
* Complete information should be given on hardware and software used.
* Hardware information includes: Machine type, CPU, type and size
* of caches; for microprocessors: clock frequency, memory speed
* (number of wait states).
* Software information includes: Compiler (and runtime library)
* manufacturer and version, compilation switches, OS version.
* The Operating System version may give an indication about the
* compiler; Dhrystone itself performs no OS calls in the measurement loop.
*
* The complete output generated by the program should be mailed
* such that at least some checks for correctness can be made.
*
***************************************************************************
*
* History: This version C/2.1 has been made for two reasons:
*
* 1) There is an obvious need for a common C version of
* Dhrystone, since C is at present the most popular system
* programming language for the class of processors
* (microcomputers, minicomputers) where Dhrystone is used most.
* There should be, as far as possible, only one C version of
* Dhrystone such that results can be compared without
* restrictions. In the past, the C versions distributed
* by Rick Richardson (Version 1.1) and by Reinhold Weicker
* had small (though not significant) differences.
*
* 2) As far as it is possible without changes to the Dhrystone
* statistics, optimizing compilers should be prevented from
* removing significant statements.
*
* This C version has been developed in cooperation with
* Rick Richardson (Tinton Falls, NJ), it incorporates many
* ideas from the "Version 1.1" distributed previously by
* him over the UNIX network Usenet.
* I also thank Chaim Benedelac (National Semiconductor),
* David Ditzel (SUN), Earl Killian and John Mashey (MIPS),
* Alan Smith and Rafael Saavedra-Barrera (UC at Berkeley)
* for their help with comments on earlier versions of the
* benchmark.
*
* Changes: In the initialization part, this version follows mostly
* Rick Richardson's version distributed via Usenet, not the
* version distributed earlier via floppy disk by Reinhold Weicker.
* As a concession to older compilers, names have been made
* unique within the first 8 characters.
* Inside the measurement loop, this version follows the
* version previously distributed by Reinhold Weicker.
*
* At several places in the benchmark, code has been added,
* but within the measurement loop only in branches that
* are not executed. The intention is that optimizing compilers
* should be prevented from moving code out of the measurement
* loop, or from removing code altogether. Since the statements
* that are executed within the measurement loop have NOT been
* changed, the numbers defining the "Dhrystone distribution"
* (distribution of statements, operand types and locality)
* still hold. Except for sophisticated optimizing compilers,
* execution times for this version should be the same as
* for previous versions.
*
* Since it has proven difficult to subtract the time for the
* measurement loop overhead in a correct way, the loop check
* has been made a part of the benchmark. This does have
* an impact - though a very minor one - on the distribution
* statistics which have been updated for this version.
*
* All changes within the measurement loop are described
* and discussed in the companion paper "Rationale for
* Dhrystone version 2".
*
* Because of the self-imposed limitation that the order and
* distribution of the executed statements should not be
* changed, there are still cases where optimizing compilers
* may not generate code for some statements. To a certain
* degree, this is unavoidable for small synthetic benchmarks.
* Users of the benchmark are advised to check code listings
* whether code is generated for all statements of Dhrystone.
*
* Version 2.1 is identical to version 2.0 distributed via
* the UNIX network Usenet in March 1988 except that it corrects
* some minor deficiencies that were found by users of version 2.0.
* The only change within the measurement loop is that a
* non-executed "else" part was added to the "if" statement in
* Func_3, and a non-executed "else" part removed from Proc_3.
*
***************************************************************************
*
* Defines: The following "Defines" are possible:
* -DREG=register (default: Not defined)
* As an approximation to what an average C programmer
* might do, the "register" storage class is applied
* (if enabled by -DREG=register)
* - for local variables, if they are used (dynamically)
* five or more times
* - for parameters if they are used (dynamically)
* six or more times
* Note that an optimal "register" strategy is
* compiler-dependent, and that "register" declarations
* do not necessarily lead to faster execution.
* -DNOSTRUCTASSIGN (default: Not defined)
* Define if the C compiler does not support
* assignment of structures.
* -DNOENUMS (default: Not defined)
* Define if the C compiler does not support
* enumeration types.
* -DTIMES (default)
* -DTIME
* The "times" function of UNIX (returning process times)
* or the "time" function (returning wallclock time)
* is used for measurement.
* For single user machines, "time ()" is adequate. For
* multi-user machines where you cannot get single-user
* access, use the "times ()" function. If you have
* neither, use a stopwatch in the dead of night.
* "printf"s are provided marking the points "Start Timer"
* and "Stop Timer". DO NOT use the UNIX "time(1)"
* command, as this will measure the total time to
* run this program, which will (erroneously) include
* the time to allocate storage (malloc) and to perform
* the initialization.
* -DHZ=nnn
* In Berkeley UNIX, the function "times" returns process
* time in 1/HZ seconds, with HZ = 60 for most systems.
* CHECK YOUR SYSTEM DESCRIPTION BEFORE YOU JUST APPLY
* A VALUE.
*
***************************************************************************
*
* Compilation model and measurement (IMPORTANT):
*
* This C version of Dhrystone consists of three files:
* - dhry.h (this file, containing global definitions and comments)
* - dhry_1.c (containing the code corresponding to Ada package Pack_1)
* - dhry_2.c (containing the code corresponding to Ada package Pack_2)
*
* The following "ground rules" apply for measurements:
* - Separate compilation
* - No procedure merging
* - Otherwise, compiler optimizations are allowed but should be indicated
* - Default results are those without register declarations
* See the companion paper "Rationale for Dhrystone Version 2" for a more
* detailed discussion of these ground rules.
*
* For 16-Bit processors (e.g. 80186, 80286), times for all compilation
* models ("small", "medium", "large" etc.) should be given if possible,
* together with a definition of these models for the compiler system used.
*
**************************************************************************
*
* Dhrystone (C version) statistics:
*
* [Comment from the first distribution, updated for version 2.
* Note that because of language differences, the numbers are slightly
* different from the Ada version.]
*
* The following program contains statements of a high level programming
* language (here: C) in a distribution considered representative:
*
* assignments 52 (51.0 %)
* control statements 33 (32.4 %)
* procedure, function calls 17 (16.7 %)
*
* 103 statements are dynamically executed. The program is balanced with
* respect to the three aspects:
*
* - statement type
* - operand type
* - operand locality
* operand global, local, parameter, or constant.
*
* The combination of these three aspects is balanced only approximately.
*
* 1. Statement Type:
* ----------------- number
*
* V1 = V2 9
* (incl. V1 = F(..)
* V = Constant 12
* Assignment, 7
* with array element
* Assignment, 6
* with record component
* --
* 34 34
*
* X = Y +|-|"&&"|"|" Z 5
* X = Y +|-|"==" Constant 6
* X = X +|- 1 3
* X = Y *|/ Z 2
* X = Expression, 1
* two operators
* X = Expression, 1
* three operators
* --
* 18 18
*
* if .... 14
* with "else" 7
* without "else" 7
* executed 3
* not executed 4
* for ... 7 | counted every time
* while ... 4 | the loop condition
* do ... while 1 | is evaluated
* switch ... 1
* break 1
* declaration with 1
* initialization
* --
* 34 34
*
* P (...) procedure call 11
* user procedure 10
* library procedure 1
* X = F (...)
* function call 6
* user function 5
* library function 1
* --
* 17 17
* ---
* 103
*
* The average number of parameters in procedure or function calls
* is 1.82 (not counting the function values as implicit parameters).
*
*
* 2. Operators
* ------------
* number approximate
* percentage
*
* Arithmetic 32 50.8
*
* + 21 33.3
* - 7 11.1
* * 3 4.8
* / (int div) 1 1.6
*
* Comparison 27 42.8
*
* == 9 14.3
* /= 4 6.3
* > 1 1.6
* < 3 4.8
* >= 1 1.6
* <= 9 14.3
*
* Logic 4 6.3
*
* && (AND-THEN) 1 1.6
* | (OR) 1 1.6
* ! (NOT) 2 3.2
*
* -- -----
* 63 100.1
*
*
* 3. Operand Type (counted once per operand reference):
* ---------------
* number approximate
* percentage
*
* Integer 175 72.3 %
* Character 45 18.6 %
* Pointer 12 5.0 %
* String30 6 2.5 %
* Array 2 0.8 %
* Record 2 0.8 %
* --- -------
* 242 100.0 %
*
* When there is an access path leading to the final operand (e.g. a record
* component), only the final data type on the access path is counted.
*
*
* 4. Operand Locality:
* -------------------
* number approximate
* percentage
*
* local variable 114 47.1 %
* global variable 22 9.1 %
* parameter 45 18.6 %
* value 23 9.5 %
* reference 22 9.1 %
* function result 6 2.5 %
* constant 55 22.7 %
* --- -------
* 242 100.0 %
*
*
* The program does not compute anything meaningful, but it is syntactically
* and semantically correct. All variables have a value assigned to them
* before they are used as a source operand.
*
* There has been no explicit effort to account for the effects of a
* cache, or to balance the use of long or short displacements for code or
* data.
*
***************************************************************************
*/
/* Compiler and system dependent definitions: */
#ifndef TIME
#define TIMES
#endif
/* Use times(2) time function unless */
/* explicitly defined otherwise */
#ifdef TIMES
#include <sys/types.h>
#include <sys/times.h>
/* for "times" */
#endif
#define Mic_secs_Per_Second 1000000.0
/* Berkeley UNIX C returns process times in seconds/HZ */
#ifdef NOSTRUCTASSIGN
#define structassign(d, s) memcpy(&(d), &(s), sizeof(d))
#else
#define structassign(d, s) d = s
#endif
#ifdef NOENUM
#define Ident_1 0
#define Ident_2 1
#define Ident_3 2
#define Ident_4 3
#define Ident_5 4
typedef int Enumeration;
#else
typedef enum {Ident_1, Ident_2, Ident_3, Ident_4, Ident_5}
Enumeration;
#endif
/* for boolean and enumeration types in Ada, Pascal */
/* General definitions: */
#include <stdio.h>
/* for strcpy, strcmp */
#define Null 0
/* Value of a Null pointer */
#define true 1
#define false 0
typedef int One_Thirty;
typedef int One_Fifty;
typedef char Capital_Letter;
typedef int Boolean;
typedef char Str_30 [31];
typedef int Arr_1_Dim [50];
typedef int Arr_2_Dim [50] [50];
typedef struct record
{
struct record *Ptr_Comp;
Enumeration Discr;
union {
struct {
Enumeration Enum_Comp;
int Int_Comp;
char Str_Comp [31];
} var_1;
struct {
Enumeration E_Comp_2;
char Str_2_Comp [31];
} var_2;
struct {
char Ch_1_Comp;
char Ch_2_Comp;
} var_3;
} variant;
} Rec_Type, *Rec_Pointer;

View File

@@ -0,0 +1,427 @@
/*****************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: dhry_1.c SID: 3.4 5/15/91 19:30:21
*
*****************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*****************************************************************************
*
* *** WARNING **** With BYTE's modifications applied, results obtained with
* ******* this version of the Dhrystone program may not be applicable
* to other versions.
*
* Modification Log:
* 10/22/97 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
* Adapted from:
*
* "DHRYSTONE" Benchmark Program
* -----------------------------
*
* Version: C, Version 2.1
*
* File: dhry_1.c (part 2 of 3)
*
* Date: May 25, 1988
*
* Author: Reinhold P. Weicker
*
***************************************************************************/
char SCCSid[] = "@(#) @(#)dhry_1.c:3.4 -- 5/15/91 19:30:21";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dhry.h"
#include "timeit.c"
unsigned long Run_Index;
Enumeration Func_1(Capital_Letter, Capital_Letter);
void report(int sig)
{
fprintf(stderr,"COUNT|%ld|1|lps\n", Run_Index);
exit(0);
}
/* Global Variables: */
Rec_Pointer Ptr_Glob,
Next_Ptr_Glob;
int Int_Glob;
Boolean Bool_Glob;
char Ch_1_Glob,
Ch_2_Glob;
int Arr_1_Glob [50];
int Arr_2_Glob [50] [50];
#ifndef REG
Boolean Reg = false;
#define REG
/* REG becomes defined as empty */
/* i.e. no register variables */
#else
Boolean Reg = true;
#endif
/* variables for time measurement: */
#ifdef TIMES
struct tms time_info;
extern int times ();
/* see library function "times" */
#define Too_Small_Time 120
/* Measurements should last at least about 2 seconds */
#endif
#ifdef TIME
#define Too_Small_Time 2
/* Measurements should last at least 2 seconds */
#endif
long Begin_Time,
End_Time,
User_Time;
float Microseconds,
Dhrystones_Per_Second;
/* end of variables for time measurement */
void Proc_1 (REG Rec_Pointer Ptr_Val_Par);
void Proc_2 (One_Fifty *Int_Par_Ref);
void Proc_3 (Rec_Pointer *Ptr_Ref_Par);
void Proc_4 (void);
void Proc_5 (void);
extern Boolean Func_2(Str_30, Str_30);
extern void Proc_6(Enumeration, Enumeration *);
extern void Proc_7(One_Fifty, One_Fifty, One_Fifty *);
extern void Proc_8(Arr_1_Dim, Arr_2_Dim, int, int);
int main (int argc, char *argv[])
/* main program, corresponds to procedures */
/* Main and Proc_0 in the Ada version */
{
int duration;
One_Fifty Int_1_Loc;
REG One_Fifty Int_2_Loc;
One_Fifty Int_3_Loc;
REG char Ch_Index;
Enumeration Enum_Loc;
Str_30 Str_1_Loc;
Str_30 Str_2_Loc;
/* Initializations */
Next_Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));
Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
Ptr_Glob->Discr = Ident_1;
Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
Ptr_Glob->variant.var_1.Int_Comp = 40;
strcpy (Ptr_Glob->variant.var_1.Str_Comp,
"DHRYSTONE PROGRAM, SOME STRING");
strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
Arr_2_Glob [8][7] = 10;
/* Was missing in published program. Without this statement, */
/* Arr_2_Glob [8][7] would have an undefined value. */
/* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
/* overflow may occur for this array element. */
#ifdef PRATTLE
printf ("\n");
printf ("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
printf ("\n");
if (Reg)
{
printf ("Program compiled with 'register' attribute\n");
printf ("\n");
}
else
{
printf ("Program compiled without 'register' attribute\n");
printf ("\n");
}
printf ("Please give the number of runs through the benchmark: ");
{
int n;
scanf ("%d", &n);
Number_Of_Runs = n;
}
printf ("\n");
printf ("Execution starts, %d runs through Dhrystone\n", Number_Of_Runs);
#endif /* PRATTLE */
if (argc != 2) {
fprintf(stderr, "Usage: %s duration\n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
Run_Index = 0;
wake_me(duration, report);
/***************/
/* Start timer */
/***************/
#ifdef SELF_TIMED
#ifdef TIMES
times (&time_info);
Begin_Time = (long) time_info.tms_utime;
#endif
#ifdef TIME
Begin_Time = time ( (long *) 0);
#endif
#endif /* SELF_TIMED */
for (Run_Index = 1; ; ++Run_Index)
{
Proc_5();
Proc_4();
/* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
Int_1_Loc = 2;
Int_2_Loc = 3;
strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
Enum_Loc = Ident_2;
Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
/* Bool_Glob == 1 */
while (Int_1_Loc < Int_2_Loc) /* loop body executed once */
{
Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
/* Int_3_Loc == 7 */
Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
/* Int_3_Loc == 7 */
Int_1_Loc += 1;
} /* while */
/* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
/* Int_Glob == 5 */
Proc_1 (Ptr_Glob);
for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
/* loop body executed twice */
{
if (Enum_Loc == Func_1 (Ch_Index, 'C'))
/* then, not executed */
{
Proc_6 (Ident_1, &Enum_Loc);
strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
Int_2_Loc = Run_Index;
Int_Glob = Run_Index;
}
}
/* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
Int_2_Loc = Int_2_Loc * Int_1_Loc;
Int_1_Loc = Int_2_Loc / Int_3_Loc;
Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
/* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
Proc_2 (&Int_1_Loc);
/* Int_1_Loc == 5 */
} /* loop "for Run_Index" */
/**************/
/* Stop timer */
/**************/
#ifdef SELF_TIMED
#ifdef TIMES
times (&time_info);
End_Time = (long) time_info.tms_utime;
#endif
#ifdef TIME
End_Time = time ( (long *) 0);
#endif
#endif /* SELF_TIMED */
/* BYTE version never executes this stuff */
#ifdef SELF_TIMED
printf ("Execution ends\n");
printf ("\n");
printf ("Final values of the variables used in the benchmark:\n");
printf ("\n");
printf ("Int_Glob: %d\n", Int_Glob);
printf (" should be: %d\n", 5);
printf ("Bool_Glob: %d\n", Bool_Glob);
printf (" should be: %d\n", 1);
printf ("Ch_1_Glob: %c\n", Ch_1_Glob);
printf (" should be: %c\n", 'A');
printf ("Ch_2_Glob: %c\n", Ch_2_Glob);
printf (" should be: %c\n", 'B');
printf ("Arr_1_Glob[8]: %d\n", Arr_1_Glob[8]);
printf (" should be: %d\n", 7);
printf ("Arr_2_Glob[8][7]: %d\n", Arr_2_Glob[8][7]);
printf (" should be: Number_Of_Runs + 10\n");
printf ("Ptr_Glob->\n");
printf (" Ptr_Comp: %d\n", (int) Ptr_Glob->Ptr_Comp);
printf (" should be: (implementation-dependent)\n");
printf (" Discr: %d\n", Ptr_Glob->Discr);
printf (" should be: %d\n", 0);
printf (" Enum_Comp: %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
printf (" should be: %d\n", 2);
printf (" Int_Comp: %d\n", Ptr_Glob->variant.var_1.Int_Comp);
printf (" should be: %d\n", 17);
printf (" Str_Comp: %s\n", Ptr_Glob->variant.var_1.Str_Comp);
printf (" should be: DHRYSTONE PROGRAM, SOME STRING\n");
printf ("Next_Ptr_Glob->\n");
printf (" Ptr_Comp: %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
printf (" should be: (implementation-dependent), same as above\n");
printf (" Discr: %d\n", Next_Ptr_Glob->Discr);
printf (" should be: %d\n", 0);
printf (" Enum_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
printf (" should be: %d\n", 1);
printf (" Int_Comp: %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
printf (" should be: %d\n", 18);
printf (" Str_Comp: %s\n",
Next_Ptr_Glob->variant.var_1.Str_Comp);
printf (" should be: DHRYSTONE PROGRAM, SOME STRING\n");
printf ("Int_1_Loc: %d\n", Int_1_Loc);
printf (" should be: %d\n", 5);
printf ("Int_2_Loc: %d\n", Int_2_Loc);
printf (" should be: %d\n", 13);
printf ("Int_3_Loc: %d\n", Int_3_Loc);
printf (" should be: %d\n", 7);
printf ("Enum_Loc: %d\n", Enum_Loc);
printf (" should be: %d\n", 1);
printf ("Str_1_Loc: %s\n", Str_1_Loc);
printf (" should be: DHRYSTONE PROGRAM, 1'ST STRING\n");
printf ("Str_2_Loc: %s\n", Str_2_Loc);
printf (" should be: DHRYSTONE PROGRAM, 2'ND STRING\n");
printf ("\n");
User_Time = End_Time - Begin_Time;
if (User_Time < Too_Small_Time)
{
printf ("Measured time too small to obtain meaningful results\n");
printf ("Please increase number of runs\n");
printf ("\n");
}
else
{
#ifdef TIME
Microseconds = (float) User_Time * Mic_secs_Per_Second
/ (float) Number_Of_Runs;
Dhrystones_Per_Second = (float) Number_Of_Runs / (float) User_Time;
#else
Microseconds = (float) User_Time * Mic_secs_Per_Second
/ ((float) HZ * ((float) Number_Of_Runs));
Dhrystones_Per_Second = ((float) HZ * (float) Number_Of_Runs)
/ (float) User_Time;
#endif
printf ("Microseconds for one run through Dhrystone: ");
printf ("%6.1f \n", Microseconds);
printf ("Dhrystones per Second: ");
printf ("%6.1f \n", Dhrystones_Per_Second);
printf ("\n");
}
#endif /* SELF_TIMED */
}
void Proc_1 (REG Rec_Pointer Ptr_Val_Par)
/* executed once */
{
REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
/* == Ptr_Glob_Next */
/* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
/* corresponds to "rename" in Ada, "with" in Pascal */
structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);
Ptr_Val_Par->variant.var_1.Int_Comp = 5;
Next_Record->variant.var_1.Int_Comp
= Ptr_Val_Par->variant.var_1.Int_Comp;
Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
Proc_3 (&Next_Record->Ptr_Comp);
/* Ptr_Val_Par->Ptr_Comp->Ptr_Comp
== Ptr_Glob->Ptr_Comp */
if (Next_Record->Discr == Ident_1)
/* then, executed */
{
Next_Record->variant.var_1.Int_Comp = 6;
Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,
&Next_Record->variant.var_1.Enum_Comp);
Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,
&Next_Record->variant.var_1.Int_Comp);
}
else /* not executed */
structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
} /* Proc_1 */
void Proc_2 (One_Fifty *Int_Par_Ref)
/* executed once */
/* *Int_Par_Ref == 1, becomes 4 */
{
One_Fifty Int_Loc;
Enumeration Enum_Loc;
Enum_Loc = 0;
Int_Loc = *Int_Par_Ref + 10;
do /* executed once */
if (Ch_1_Glob == 'A')
/* then, executed */
{
Int_Loc -= 1;
*Int_Par_Ref = Int_Loc - Int_Glob;
Enum_Loc = Ident_1;
} /* if */
while (Enum_Loc != Ident_1); /* true */
} /* Proc_2 */
void Proc_3 (Rec_Pointer *Ptr_Ref_Par)
/* executed once */
/* Ptr_Ref_Par becomes Ptr_Glob */
{
if (Ptr_Glob != Null)
/* then, executed */
*Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
} /* Proc_3 */
void Proc_4 (void) /* without parameters */
/* executed once */
{
Boolean Bool_Loc;
Bool_Loc = Ch_1_Glob == 'A';
Bool_Glob = Bool_Loc | Bool_Glob;
Ch_2_Glob = 'B';
} /* Proc_4 */
void Proc_5 (void) /* without parameters */
/*******/
/* executed once */
{
Ch_1_Glob = 'A';
Bool_Glob = false;
} /* Proc_5 */
/* Procedure for the assignment of structures, */
/* if the C compiler doesn't support this feature */
#ifdef NOSTRUCTASSIGN
memcpy (d, s, l)
register char *d;
register char *s;
register int l;
{
while (l--) *d++ = *s++;
}
#endif

View File

@@ -0,0 +1,209 @@
/*****************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: dhry_2.c SID: 3.4 5/15/91 19:30:22
*
*****************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*****************************************************************************
* Modification Log:
* 10/22/97 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
* Adapted from:
*
* "DHRYSTONE" Benchmark Program
* -----------------------------
*
* **** WARNING **** See warning in n.dhry_1.c
*
* Version: C, Version 2.1
*
* File: dhry_2.c (part 3 of 3)
*
* Date: May 25, 1988
*
* Author: Reinhold P. Weicker
*
****************************************************************************/
/* SCCSid is defined in dhry_1.c */
#include <string.h>
#include "dhry.h"
#ifndef REG
#define REG
/* REG becomes defined as empty */
/* i.e. no register variables */
#endif
extern int Int_Glob;
extern char Ch_1_Glob;
void Proc_6(Enumeration, Enumeration *);
void Proc_7(One_Fifty, One_Fifty, One_Fifty *);
void Proc_8(Arr_1_Dim, Arr_2_Dim, int, int);
Enumeration Func_1(Capital_Letter, Capital_Letter);
Boolean Func_2(Str_30, Str_30);
Boolean Func_3(Enumeration);
void Proc_6 (Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par)
/* executed once */
/* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */
{
*Enum_Ref_Par = Enum_Val_Par;
if (! Func_3 (Enum_Val_Par))
/* then, not executed */
*Enum_Ref_Par = Ident_4;
switch (Enum_Val_Par)
{
case Ident_1:
*Enum_Ref_Par = Ident_1;
break;
case Ident_2:
if (Int_Glob > 100)
/* then */
*Enum_Ref_Par = Ident_1;
else *Enum_Ref_Par = Ident_4;
break;
case Ident_3: /* executed */
*Enum_Ref_Par = Ident_2;
break;
case Ident_4: break;
case Ident_5:
*Enum_Ref_Par = Ident_3;
break;
} /* switch */
} /* Proc_6 */
void Proc_7 (Int_1_Par_Val, Int_2_Par_Val, Int_Par_Ref)
One_Fifty Int_1_Par_Val;
One_Fifty Int_2_Par_Val;
One_Fifty *Int_Par_Ref;
/**********************************************/
/* executed three times */
/* first call: Int_1_Par_Val == 2, Int_2_Par_Val == 3, */
/* Int_Par_Ref becomes 7 */
/* second call: Int_1_Par_Val == 10, Int_2_Par_Val == 5, */
/* Int_Par_Ref becomes 17 */
/* third call: Int_1_Par_Val == 6, Int_2_Par_Val == 10, */
/* Int_Par_Ref becomes 18 */
{
One_Fifty Int_Loc;
Int_Loc = Int_1_Par_Val + 2;
*Int_Par_Ref = Int_2_Par_Val + Int_Loc;
} /* Proc_7 */
void Proc_8 (Arr_1_Par_Ref, Arr_2_Par_Ref, Int_1_Par_Val, Int_2_Par_Val)
/*********************************************************************/
/* executed once */
/* Int_Par_Val_1 == 3 */
/* Int_Par_Val_2 == 7 */
Arr_1_Dim Arr_1_Par_Ref;
Arr_2_Dim Arr_2_Par_Ref;
int Int_1_Par_Val;
int Int_2_Par_Val;
{
REG One_Fifty Int_Index;
REG One_Fifty Int_Loc;
Int_Loc = Int_1_Par_Val + 5;
Arr_1_Par_Ref [Int_Loc] = Int_2_Par_Val;
Arr_1_Par_Ref [Int_Loc+1] = Arr_1_Par_Ref [Int_Loc];
Arr_1_Par_Ref [Int_Loc+30] = Int_Loc;
for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index)
Arr_2_Par_Ref [Int_Loc] [Int_Index] = Int_Loc;
Arr_2_Par_Ref [Int_Loc] [Int_Loc-1] += 1;
Arr_2_Par_Ref [Int_Loc+20] [Int_Loc] = Arr_1_Par_Ref [Int_Loc];
Int_Glob = 5;
} /* Proc_8 */
Enumeration Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val)
/*************************************************/
/* executed three times */
/* first call: Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R' */
/* second call: Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C' */
/* third call: Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C' */
{
Capital_Letter Ch_1_Loc;
Capital_Letter Ch_2_Loc;
Ch_1_Loc = Ch_1_Par_Val;
Ch_2_Loc = Ch_1_Loc;
if (Ch_2_Loc != Ch_2_Par_Val)
/* then, executed */
return (Ident_1);
else /* not executed */
{
Ch_1_Glob = Ch_1_Loc;
return (Ident_2);
}
} /* Func_1 */
Boolean Func_2 (Str_1_Par_Ref, Str_2_Par_Ref)
/*************************************************/
/* executed once */
/* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */
/* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */
Str_30 Str_1_Par_Ref;
Str_30 Str_2_Par_Ref;
{
REG One_Thirty Int_Loc;
Capital_Letter Ch_Loc;
Ch_Loc = 'A';
Int_Loc = 2;
while (Int_Loc <= 2) /* loop body executed once */
if (Func_1 (Str_1_Par_Ref[Int_Loc],
Str_2_Par_Ref[Int_Loc+1]) == Ident_1)
/* then, executed */
{
Ch_Loc = 'A';
Int_Loc += 1;
} /* if, while */
if (Ch_Loc >= 'W' && Ch_Loc < 'Z')
/* then, not executed */
Int_Loc = 7;
if (Ch_Loc == 'R')
/* then, not executed */
return (true);
else /* executed */
{
if (strcmp (Str_1_Par_Ref, Str_2_Par_Ref) > 0)
/* then, not executed */
{
Int_Loc += 7;
Int_Glob = Int_Loc;
return (true);
}
else /* executed */
return (false);
} /* if Ch_Loc */
} /* Func_2 */
Boolean Func_3 (Enum_Par_Val)
/***************************/
/* executed once */
/* Enum_Par_Val == Ident_3 */
Enumeration Enum_Par_Val;
{
Enumeration Enum_Loc;
Enum_Loc = Enum_Par_Val;
if (Enum_Loc == Ident_3)
/* then, executed */
return (true);
else /* not executed */
return (false);
} /* Func_3 */

View File

@@ -0,0 +1,319 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: dummy.c SID: 3.3 5/15/91 19:30:19
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* 10/22/97 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
/*
* Hacked up C program for use in the standard shell.? scripts of
* the multiuser test. This is based upon makework.c, and is typically
* edited using edscript.2 before compilation.
*
* $Header: dummy.c,v 3.4 87/06/23 15:54:53 kjmcdonell Beta $
*/
char SCCSid[] = "@(#) @(#)dummy.c:3.3 -- 5/15/91 19:30:19";
#include <stdio.h>
#include <signal.h>
#define DEF_RATE 5.0
#define GRANULE 5
#define CHUNK 60
#define MAXCHILD 12
#define MAXWORK 10
float thres;
float est_rate = DEF_RATE;
int nusers; /* number of concurrent users to be simulated by
* this process */
int firstuser; /* ordinal identification of first user for this
* process */
int nwork = 0; /* number of job streams */
int exit_status = 0; /* returned to parent */
int sigpipe; /* pipe write error flag */
struct st_work {
char *cmd; /* name of command to run */
char **av; /* arguments to command */
char *input; /* standard input buffer */
int inpsize; /* size of standard input buffer */
} work[MAXWORK];
struct {
int xmit; /* # characters sent */
char *bp; /* std input buffer pointer */
int blen; /* std input buffer length */
int fd; /* stdin to command */
int pid; /* child PID */
char *line; /* start of input line */
int firstjob; /* inital piece of work */
int thisjob; /* current piece of work */
} child[MAXCHILD], *cp;
main(argc, argv)
int argc;
char *argv[];
{
int i;
int l;
int fcopy = 0; /* fd for copy output */
int master = 1; /* the REAL master, == 0 for clones */
int nchild; /* no. of children for a clone to run */
int done; /* count of children finished */
int output; /* aggregate output char count for all
children */
int c;
int thiswork = 0; /* next job stream to allocate */
int nch; /* # characters to write */
int written; /* # characters actully written */
char logname[15]; /* name of the log file(s) */
void onalarm(void);
void pipeerr(void);
void wrapup(void);
void grunt(void);
char *malloc();
int pvec[2]; /* for pipes */
char *p;
char *prog; /* my name */
#if ! debug
freopen("masterlog.00", "a", stderr);
#endif
fprintf(stderr, "*** New Run *** ");
prog = argv[0];
while (argc > 1 && argv[1][0] == '-') {
p = &argv[1][1];
argc--;
argv++;
while (*p) {
switch (*p) {
case 'r':
/* code DELETED here */
argc--;
argv++;
break;
case 'c':
/* code DELETED here */
lseek(fcopy, 0L, 2); /* append at end of file */
break;
default:
fprintf(stderr, "%s: bad flag '%c'\n", prog, *p);
exit(4);
}
p++;
}
}
if (argc < 2) {
fprintf(stderr, "%s: missing nusers\n", prog);
exit(4);
}
nusers = atoi(argv[1]);
if (nusers < 1) {
fprintf(stderr, "%s: impossible nusers (%d<-%s)\n", prog, nusers, argv[1]);
exit(4);
}
fprintf(stderr, "%d Users\n", nusers);
argc--;
argv++;
/* build job streams */
getwork();
#if debug
dumpwork();
#endif
/* clone copies of myself to run up to MAXCHILD jobs each */
firstuser = MAXCHILD;
fprintf(stderr, "master pid %d\n", getpid());
fflush(stderr);
while (nusers > MAXCHILD) {
fflush(stderr);
if (nusers >= 2*MAXCHILD)
/* the next clone must run MAXCHILD jobs */
nchild = MAXCHILD;
else
/* the next clone must run the leftover jobs */
nchild = nusers - MAXCHILD;
if ((l = fork()) == -1) {
/* fork failed */
fatal("** clone fork failed **\n");
goto bepatient;
} else if (l > 0) {
fprintf(stderr, "master clone pid %d\n", l);
/* I am the master with nchild fewer jobs to run */
nusers -= nchild;
firstuser += MAXCHILD;
continue;
} else {
/* I am a clone, run MAXCHILD jobs */
#if ! debug
sprintf(logname, "masterlog.%02d", firstuser/MAXCHILD);
freopen(logname, "w", stderr);
#endif
master = 0;
nusers = nchild;
break;
}
}
if (master)
firstuser = 0;
close(0);
/* code DELETED here */
fflush(stderr);
srand(time(0));
thres = 0;
done = output = 0;
for (i = 0; i < nusers; i++) {
if (child[i].blen == 0)
done++;
else
thres += est_rate * GRANULE;
}
est_rate = thres;
signal(SIGALRM, onalarm);
signal(SIGPIPE, pipeerr);
alarm(GRANULE);
while (done < nusers) {
for (i = 0; i < nusers; i++) {
cp = &child[i];
if (cp->xmit >= cp->blen) continue;
l = rand() % CHUNK + 1; /* 1-CHUNK chars */
if (l == 0) continue;
if (cp->xmit + l > cp->blen)
l = cp->blen - cp->xmit;
p = cp->bp;
cp->bp += l;
cp->xmit += l;
#if debug
fprintf(stderr, "child %d, %d processed, %d to go\n", i, cp->xmit, cp->blen - cp->xmit);
#endif
while (p < cp->bp) {
if (*p == '\n' || (p == &cp->bp[-1] && cp->xmit >= cp->blen)) {
/* write it out */
nch = p - cp->line + 1;
if ((written = write(cp->fd, cp->line, nch)) != nch) {
/* code DELETED here */
}
if (fcopy)
write(fcopy, cp->line, p - cp->line + 1);
#if debug
fprintf(stderr, "child %d gets \"", i);
{
char *q = cp->line;
while (q <= p) {
if (*q >= ' ' && *q <= '~')
fputc(*q, stderr);
else
fprintf(stderr, "\\%03o", *q);
q++;
}
}
fputc('"', stderr);
#endif
cp->line = &p[1];
}
p++;
}
if (cp->xmit >= cp->blen) {
done++;
close(cp->fd);
#if debug
fprintf(stderr, "child %d, close std input\n", i);
#endif
}
output += l;
}
while (output > thres) {
pause();
#if debug
fprintf(stderr, "after pause: output, thres, done %d %.2f %d\n", output, thres, done);
#endif
}
}
bepatient:
alarm(0);
/****
* If everything is going OK, we should simply be able to keep
* looping unitil 'wait' fails, however some descendent process may
* be in a state from which it can never exit, and so a timeout
* is used.
* 5 minutes should be ample, since the time to run all jobs is of
* the order of 5-10 minutes, however some machines are painfully slow,
* so the timeout has been set at 20 minutes (1200 seconds).
****/
/* code DELETED here */
}
onalarm()
{
thres += est_rate;
signal(SIGALRM, onalarm);
alarm(GRANULE);
}
grunt()
{
/* timeout after label "bepatient" in main */
exit_status = 4;
wrapup();
}
pipeerr()
{
sigpipe++;
}
wrapup()
{
/* DUMMY, real code dropped */
}
getwork()
{
/* DUMMY, real code dropped */
gets();
strncpy();
malloc(); realloc();
open(); close();
}
fatal(s)
char *s;
{
int i;
fprintf(stderr, s);
fflush(stderr);
perror("Reason?");
for (i = 0; i < nusers; i++) {
if (child[i].pid > 0 && kill(child[i].pid, SIGKILL) != -1)
fprintf(stderr, "pid %d killed off\n", child[i].pid);
}
fflush(stderr);
exit_status = 4;
return;
}

View File

@@ -0,0 +1,91 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: execl.c SID: 3.3 5/15/91 19:30:19
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: execl.c,v 3.5 87/06/22 15:37:08 kjmcdonell Beta $
* August 28, 1990 - Modified timing routines
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
/*
* Execing
*
*/
char SCCSid[] = "@(#) @(#)execl.c:3.3 -- 5/15/91 19:30:19";
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
char bss[8*1024]; /* something worthwhile */
#define main dummy
#include "big.c" /* some real code */
#undef main
int main(int argc, char *argv[]) /* the real program */
{
unsigned long iter = 0;
char *ptr;
char *fullpath;
int duration;
char count_str[12], start_str[24], path_str[256], *dur_str;
time_t start_time, this_time;
#ifdef DEBUG
int count;
for(count = 0; count < argc; ++ count)
printf("%s ",argv[count]);
printf("\n");
#endif
if (argc < 2)
{
fprintf(stderr, "Usage: %s duration\n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
if (duration > 0)
/* the first invocation */
{
dur_str = argv[1];
if((ptr = getenv("UB_BINDIR")) != NULL)
sprintf(path_str,"%s/execl",ptr);
fullpath=path_str;
time(&start_time);
}
else /* one of those execl'd invocations */
{
/* real duration follow the phoney null duration */
duration = atoi(argv[2]);
dur_str = argv[2];
iter = (unsigned long)atoi(argv[3]); /* where are we now ? */
sscanf(argv[4], "%lu", (unsigned long *) &start_time);
fullpath = argv[0];
}
sprintf(count_str, "%lu", ++iter); /* increment the execl counter */
sprintf(start_str, "%lu", (unsigned long) start_time);
time(&this_time);
if (this_time - start_time >= duration) { /* time has run out */
fprintf(stderr, "COUNT|%lu|1|lps\n", iter);
exit(0);
}
execl(fullpath, fullpath, "0", dur_str, count_str, start_str, (void *) 0);
fprintf(stderr, "Exec failed at iteration %lu\n", iter);
perror("Reason");
exit(1);
}

View File

@@ -0,0 +1,471 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: fstime.c SID: 3.5 5/15/91 19:30:19
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: fstime.c,v 3.4 87/06/22 14:23:05 kjmcdonell Beta $
* 10/19/89 - rewrote timing calcs and added clock check (Ben Smith)
* 10/26/90 - simplify timing, change defaults (Tom Yager)
* 11/16/90 - added better error handling and changed output format (Ben Smith)
* 11/17/90 - changed the whole thing around (Ben Smith)
* 2/22/91 - change a few style elements and improved error handling (Ben Smith)
* 4/17/91 - incorporated suggestions from Seckin Unlu (seckin@sumac.intel.com)
* 4/17/91 - limited size of file, will rewind when reaches end of file
* 7/95 - fixed mishandling of read() and write() return codes
* Carl Emilio Prelz <fluido@telepac.pt>
* 12/95 - Massive changes. Made sleep time proportional increase with run
* time; added fsbuffer and fsdisk variants; added partial counting
* of partial reads/writes (was *full* credit); added dual syncs.
* David C Niemi <niemi@tux.org>
* 10/22/97 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
* 9/24/07 - Separate out the read and write tests;
* output the actual time used in the results.
* Ian Smith <johantheghost at yahoo period com>
******************************************************************************/
char SCCSid[] = "@(#) @(#)fstime.c:3.5 -- 5/15/91 19:30:19";
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#define SECONDS 10
#define MAX_BUFSIZE 8192
/* This must be set to the smallest BUFSIZE or 1024, whichever is smaller */
#define COUNTSIZE 256
#define HALFCOUNT (COUNTSIZE/2) /* Half of COUNTSIZE */
#define FNAME0 "dummy0"
#define FNAME1 "dummy1"
#ifndef MINIX
extern void sync(void);
#else
extern int sync(void);
#endif
int w_test(int timeSecs);
int r_test(int timeSecs);
int c_test(int timeSecs);
long read_score = 1, write_score = 1, copy_score = 1;
/****************** GLOBALS ***************************/
/* The buffer size for the tests. */
int bufsize = 1024;
/*
* The max number of 1024-byte blocks in the file.
* Don't limit it much, so that memory buffering
* can be overcome.
*/
int max_blocks = 2000;
/* The max number of BUFSIZE blocks in the file. */
int max_buffs = 2000;
/* Countable units per 1024 bytes */
int count_per_k;
/* Countable units per bufsize */
int count_per_buf;
/* The actual buffer. */
/* char *buf = 0; */
/* Let's carry on using a static buffer for this, like older versions
* of the code did. It turns out that if you use a malloc buffer,
* it goes 50% slower on reads, when using a 4k buffer -- at least on
* my OpenSUSE 10.2 system.
* What up wit dat?
*/
char buf[MAX_BUFSIZE];
int f;
int g;
int i;
void stop_count(int);
void clean_up(int);
int sigalarm = 0;
/******************** MAIN ****************************/
int main(int argc, char *argv[])
{
/* The number of seconds to run for. */
int seconds = SECONDS;
/* The type of test to run. */
char test = 'c';
int status;
int i;
for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'c':
case 'r':
case 'w':
test = argv[i][1];
break;
case 'b':
bufsize = atoi(argv[++i]);
break;
case 'm':
max_blocks = atoi(argv[++i]);
break;
case 't':
seconds = atoi(argv[++i]);
break;
case 'd':
if (chdir(argv[++i]) < 0) {
perror("fstime: chdir");
exit(1);
}
break;
default:
fprintf(stderr, "Usage: fstime [-c|-r|-w] [-b <bufsize>] [-m <max_blocks>] [-t <seconds>]\n");
exit(2);
}
} else {
fprintf(stderr, "Usage: fstime [-c|-r|-w] [-b <bufsize>] [-m <max_blocks>] [-t <seconds>]\n");
exit(2);
}
}
if (bufsize < COUNTSIZE || bufsize > MAX_BUFSIZE) {
fprintf(stderr, "fstime: buffer size must be in range %d-%d\n",
COUNTSIZE, 1024*1024);
exit(3);
}
if (max_blocks < 1 || max_blocks > 1024*1024) {
fprintf(stderr, "fstime: max blocks must be in range %d-%d\n",
1, 1024*1024);
exit(3);
}
if (seconds < 1 || seconds > 3600) {
fprintf(stderr, "fstime: time must be in range %d-%d seconds\n",
1, 3600);
exit(3);
}
max_buffs = max_blocks * 1024 / bufsize;
count_per_k = 1024 / COUNTSIZE;
count_per_buf = bufsize / COUNTSIZE;
/*
if ((buf = malloc(bufsize)) == 0) {
fprintf(stderr, "fstime: failed to malloc %d bytes\n", bufsize);
exit(4);
}
*/
if((f = creat(FNAME0, 0600)) == -1) {
perror("fstime: creat");
exit(1);
}
close(f);
if((g = creat(FNAME1, 0600)) == -1) {
perror("fstime: creat");
exit(1);
}
close(g);
if( (f = open(FNAME0, 2)) == -1) {
perror("fstime: open");
exit(1);
}
if( ( g = open(FNAME1, 2)) == -1 ) {
perror("fstime: open");
exit(1);
}
/* fill buffer */
for (i=0; i < bufsize; ++i)
buf[i] = i & 0xff;
signal(SIGKILL,clean_up);
/*
* Run the selected test.
* When I got here, this program ran full 30-second tests for
* write, read, and copy, outputting the results for each. BUT
* only the copy results are actually used in the benchmark index.
* With multiple iterations and three sets of FS tests, that amounted
* to about 10 minutes of wasted time per run.
*
* So, I've made the test selectable. Except that the read and write
* passes are used to create the test file and calibrate the rates used
* to tweak the results of the copy test. So, for copy tests, we do
* a few seconds of write and read to prime the pump.
*
* Note that this will also pull the file into the FS cache on any
* modern system prior to the copy test. Whether this is good or
* bad is a matter of perspective, but it's how it was when I got
* here.
*
* Ian Smith <johantheghost at yahoo period com> 21 Sep 2007
*/
switch (test) {
case 'w':
status = w_test(seconds);
break;
case 'r':
w_test(2);
status = r_test(seconds);
break;
case 'c':
w_test(2);
r_test(2);
status = c_test(seconds);
break;
default:
fprintf(stderr, "fstime: unknown test \'%c\'\n", test);
exit(6);
}
if (status) {
clean_up(0);
exit(1);
}
clean_up(0);
exit(0);
}
static double getFloatTime(void)
{
struct timeval t;
gettimeofday(&t, 0);
return (double) t.tv_sec + (double) t.tv_usec / 1000000.0;
}
/*
* Run the write test for the time given in seconds.
*/
int w_test(int timeSecs)
{
unsigned long counted = 0L;
unsigned long tmp;
long f_blocks;
double start, end;
extern int sigalarm;
/* Sync and let it settle */
sync();
sleep(2);
sync();
sleep(2);
/* Set an alarm. */
sigalarm = 0;
signal(SIGALRM, stop_count);
alarm(timeSecs);
start = getFloatTime();
while (!sigalarm) {
for(f_blocks=0; f_blocks < max_buffs; ++f_blocks) {
if ((tmp=write(f, buf, bufsize)) != bufsize) {
if (errno != EINTR) {
perror("fstime: write");
return(-1);
}
stop_count(0);
counted += ((tmp+HALFCOUNT)/COUNTSIZE);
} else
counted += count_per_buf;
}
lseek(f, 0L, 0); /* rewind */
}
/* stop clock */
end = getFloatTime();
write_score = (long) ((double) counted / ((end - start) * count_per_k));
printf("Write done: %ld in %.4f, score %ld\n",
counted, end - start, write_score);
/*
* Output the test results. Use the true time.
*/
fprintf(stderr, "COUNT|%ld|0|KBps\n", write_score);
fprintf(stderr, "TIME|%.1f\n", end - start);
return(0);
}
/*
* Run the read test for the time given in seconds.
*/
int r_test(int timeSecs)
{
unsigned long counted = 0L;
unsigned long tmp;
double start, end;
extern int sigalarm;
extern int errno;
/* Sync and let it settle */
sync();
sleep(2);
sync();
sleep(2);
/* rewind */
errno = 0;
lseek(f, 0L, 0);
/* Set an alarm. */
sigalarm = 0;
signal(SIGALRM, stop_count);
alarm(timeSecs);
start = getFloatTime();
while (!sigalarm) {
/* read while checking for an error */
if ((tmp=read(f, buf, bufsize)) != bufsize) {
switch(errno) {
case 0:
case EINVAL:
lseek(f, 0L, 0); /* rewind at end of file */
counted += (tmp+HALFCOUNT)/COUNTSIZE;
continue;
case EINTR:
stop_count(0);
counted += (tmp+HALFCOUNT)/COUNTSIZE;
break;
default:
perror("fstime: read");
return(-1);
break;
}
} else
counted += count_per_buf;
}
/* stop clock */
end = getFloatTime();
read_score = (long) ((double) counted / ((end - start) * count_per_k));
printf("Read done: %ld in %.4f, score %ld\n",
counted, end - start, read_score);
/*
* Output the test results. Use the true time.
*/
fprintf(stderr, "COUNT|%ld|0|KBps\n", read_score);
fprintf(stderr, "TIME|%.1f\n", end - start);
return(0);
}
/*
* Run the copy test for the time given in seconds.
*/
int c_test(int timeSecs)
{
unsigned long counted = 0L;
unsigned long tmp;
double start, end;
extern int sigalarm;
sync();
sleep(2);
sync();
sleep(1);
/* rewind */
errno = 0;
lseek(f, 0L, 0);
/* Set an alarm. */
sigalarm = 0;
signal(SIGALRM, stop_count);
alarm(timeSecs);
start = getFloatTime();
while (!sigalarm) {
if ((tmp=read(f, buf, bufsize)) != bufsize) {
switch(errno) {
case 0:
case EINVAL:
lseek(f, 0L, 0); /* rewind at end of file */
lseek(g, 0L, 0); /* rewind the output too */
continue;
case EINTR:
/* part credit for leftover bytes read */
counted += ( (tmp * write_score) /
(read_score + write_score)
+ HALFCOUNT) / COUNTSIZE;
stop_count(0);
break;
default:
perror("fstime: copy read");
return(-1);
break;
}
} else {
if ((tmp=write(g, buf, bufsize)) != bufsize) {
if (errno != EINTR) {
perror("fstime: copy write");
return(-1);
}
counted += (
/* Full credit for part of buffer written */
tmp +
/* Plus part credit having read full buffer */
( ((bufsize - tmp) * write_score) /
(read_score + write_score) )
+ HALFCOUNT) / COUNTSIZE;
stop_count(0);
} else
counted += count_per_buf;
}
}
/* stop clock */
end = getFloatTime();
copy_score = (long) ((double) counted / ((end - start) * count_per_k));
printf("Copy done: %ld in %.4f, score %ld\n",
counted, end - start, copy_score);
/*
* Output the test results. Use the true time.
*/
fprintf(stderr, "COUNT|%ld|0|KBps\n", copy_score);
fprintf(stderr, "TIME|%.1f\n", end - start);
return(0);
}
void stop_count(int sig)
{
extern int sigalarm;
sigalarm = 1;
}
void clean_up(int sig)
{
unlink(FNAME0);
unlink(FNAME1);
}

View File

@@ -0,0 +1,75 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: hanoi.c SID: 3.3 5/15/91 19:30:20
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
* August 28, 1990 - Modified timing routines (ty)
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)hanoi.c:3.3 -- 5/15/91 19:30:20";
#define other(i,j) (6-(i+j))
#include <stdio.h>
#include <stdlib.h>
#include "timeit.c"
void mov(int n, int f, int t);
unsigned long iter = 0;
int num[4];
long cnt;
void report(int sig)
{
fprintf(stderr,"COUNT|%ld|1|lps\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
int disk=10, /* default number of disks */
duration;
if (argc < 2) {
fprintf(stderr,"Usage: %s duration [disks]\n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
if(argc > 2) disk = atoi(argv[2]);
num[1] = disk;
wake_me(duration, report);
while(1) {
mov(disk,1,3);
iter++;
}
exit(0);
}
void mov(int n, int f, int t)
{
int o;
if(n == 1) {
num[f]--;
num[t]++;
return;
}
o = other(f,t);
mov(n-1,f,o);
mov(1,f,t);
mov(n-1,o,t);
}

View File

@@ -0,0 +1,103 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 1
* Module: looper.c SID: 1.4 5/15/91 19:30:22
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith or Tom Yager at BYTE Magazine
* ben@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
*
* February 25, 1991 -- created (Ben S.)
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)looper.c:1.4 -- 5/15/91 19:30:22";
/*
* Shell Process creation
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "timeit.c"
unsigned long iter;
char *cmd_argv[28];
int cmd_argc;
void report(int sig)
{
fprintf(stderr,"COUNT|%lu|60|lpm\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
int slave, count, duration;
int status;
if (argc < 2)
{
fprintf(stderr,"Usage: %s duration command [args..]\n", argv[0]);
fprintf(stderr," duration in seconds\n");
exit(1);
}
if((duration = atoi(argv[1])) < 1)
{
fprintf(stderr,"Usage: %s duration command [arg..]\n", argv[0]);
fprintf(stderr," duration in seconds\n");
exit(1);
}
/* get command */
cmd_argc=argc-2;
for( count=2;count < argc; ++count)
cmd_argv[count-2]=argv[count];
#ifdef DEBUG
printf("<<%s>>",cmd_argv[0]);
for(count=1;count < cmd_argc; ++count)
printf(" <%s>", cmd_argv[count]);
putchar('\n');
exit(0);
#endif
iter = 0;
wake_me(duration, report);
while (1)
{
if ((slave = fork()) == 0)
{ /* execute command */
execvp(cmd_argv[0],cmd_argv);
exit(99);
}
else if (slave < 0)
{
/* woops ... */
fprintf(stderr,"Fork failed at iteration %lu\n", iter);
perror("Reason");
exit(2);
}
else
/* master */
wait(&status);
if (status == 99 << 8)
{
fprintf(stderr, "Command \"%s\" didn't exec\n", cmd_argv[0]);
exit(2);
}
else if (status != 0)
{
fprintf(stderr,"Bad wait status: 0x%x\n", status);
exit(2);
}
iter++;
}
}

View File

@@ -0,0 +1,66 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: pipe.c SID: 3.3 5/15/91 19:30:20
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: pipe.c,v 3.5 87/06/22 14:32:36 kjmcdonell Beta $
* August 29, 1990 - modified timing routines (ty)
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)pipe.c:3.3 -- 5/15/91 19:30:20";
/*
* pipe -- test single process pipe throughput (no context switching)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "timeit.c"
unsigned long iter;
void report(int sig)
{
fprintf(stderr,"COUNT|%ld|1|lps\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
char buf[512];
int pvec[2], duration;
if (argc != 2) {
fprintf(stderr,"Usage: %s duration\n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
pipe(pvec);
wake_me(duration, report);
iter = 0;
while (1) {
if (write(pvec[1], buf, sizeof(buf)) != sizeof(buf)) {
if ((errno != EINTR) && (errno != 0))
fprintf(stderr,"write failed, error %d\n", errno);
}
if (read(pvec[0], buf, sizeof(buf)) != sizeof(buf)) {
if ((errno != EINTR) && (errno != 0))
fprintf(stderr,"read failed, error %d\n", errno);
}
iter++;
}
}

View File

@@ -0,0 +1,78 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: spawn.c SID: 3.3 5/15/91 19:30:20
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yagerat BYTE Magazine
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: spawn.c,v 3.4 87/06/22 14:32:48 kjmcdonell Beta $
* August 29, 1990 - Modified timing routines (ty)
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)spawn.c:3.3 -- 5/15/91 19:30:20";
/*
* Process creation
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "timeit.c"
unsigned long iter;
void report(int sig)
{
fprintf(stderr,"COUNT|%lu|1|lps\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
int slave, duration;
int status;
if (argc != 2) {
fprintf(stderr,"Usage: %s duration \n", argv[0]);
exit(1);
}
duration = atoi(argv[1]);
iter = 0;
wake_me(duration, report);
while (1) {
if ((slave = fork()) == 0) {
/* slave .. boring */
#if debug
printf("fork OK\n");
#endif
/* kill it right away */
exit(0);
} else if (slave < 0) {
/* woops ... */
fprintf(stderr,"Fork failed at iteration %lu\n", iter);
perror("Reason");
exit(2);
} else
/* master */
wait(&status);
if (status != 0) {
fprintf(stderr,"Bad wait status: 0x%x\n", status);
exit(2);
}
iter++;
#if debug
printf("Child %d done.\n", slave);
#endif
}
}

View File

@@ -0,0 +1,107 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 3
* Module: syscall.c SID: 3.3 5/15/91 19:30:21
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager at BYTE Magazine
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* $Header: syscall.c,v 3.4 87/06/22 14:32:54 kjmcdonell Beta $
* August 29, 1990 - Modified timing routines
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
/*
* syscall -- sit in a loop calling the system
*
*/
char SCCSid[] = "@(#) @(#)syscall.c:3.3 -- 5/15/91 19:30:21";
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "timeit.c"
unsigned long iter;
void report(int sig)
{
fprintf(stderr,"COUNT|%ld|1|lps\n", iter);
exit(0);
}
int main(int argc, char *argv[])
{
char *test;
int duration;
if (argc < 2) {
fprintf(stderr,"Usage: %s duration [ test ]\n", argv[0]);
fprintf(stderr,"test is one of:\n");
fprintf(stderr," \"mix\" (default), \"close\", \"getpid\", \"exec\"\n");
exit(1);
}
if (argc > 2)
test = argv[2];
else
test = "mix";
duration = atoi(argv[1]);
iter = 0;
wake_me(duration, report);
switch (test[0]) {
case 'm':
while (1) {
close(dup(0));
getpid();
getuid();
umask(022);
iter++;
}
/* NOTREACHED */
case 'c':
while (1) {
close(dup(0));
iter++;
}
/* NOTREACHED */
case 'g':
while (1) {
getpid();
iter++;
}
/* NOTREACHED */
case 'e':
while (1) {
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr,"%s: fork failed\n", argv[0]);
exit(1);
} else if (pid == 0) {
execl("/bin/true", (char *) 0);
fprintf(stderr,"%s: exec /bin/true failed\n", argv[0]);
exit(1);
} else {
if (waitpid(pid, NULL, 0) < 0) {
fprintf(stderr,"%s: waitpid failed\n", argv[0]);
exit(1);
}
}
iter++;
}
/* NOTREACHED */
}
exit(9);
}

View File

@@ -0,0 +1,573 @@
/* Programme to test how long it takes to select(2), poll(2) and poll2(2) a
large number of file descriptors.
Copyright 1997 Richard Gooch rgooch@atnf.csiro.au
Distributed under the GNU General Public License.
To compile this programme, use gcc -O2 -o time-polling time-polling.c
Extra compile flags:
Add -DHAS_SELECT if your operating system has the select(2) system call
Add -DHAS_POLL if your operating system has the poll(2) system call
Add -DHAS_POLL2 if your operating system has the poll2(2) system call
Usage: time-polling [num_iter] [num_to_test] [num_active] [-v]
NOTE: on many systems the default limit on file descriptors is less than
1024. You should try to increase this limit to 1024 before doing the test.
Something like "limit descriptors 1024" or "limit openfiles 1024" should do
the trick. On some systems (like IRIX), doing the test on a smaller number
gives a *much* smaller time per descriptor, which shows that time taken
does not scale linearly with number of descriptors, which is non-optimal.
In the tests I've done, I try to use 1024 descriptors.
The benchmark results are available at:
http://www.atnf.csiro.au/~rgooch/benchmarks.html
If you want to contribute results, please email them to me. Please specify
if you want to be acknowledged.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Richard Gooch may be reached by email at rgooch@atnf.csiro.au
The postal address is:
Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/
#ifdef UNIXBENCH
#define OUT stdout
#else
#define OUT stderr
#endif
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef HAS_POLL
# include <sys/poll.h>
#endif
#ifdef HAS_POLL2
# include <linux/poll2.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#ifdef UNIXBENCH
#define MAX_ITERATIONS 1000
#else
#define MAX_ITERATIONS 30
#endif
#define MAX_FDS 40960
#define CONST const
#define ERRSTRING strerror (errno)
typedef int flag;
/*
static inline int find_first_set_bit (CONST void *array, int size)
*/
static int find_first_set_bit (CONST void *array, int size)
/* [SUMMARY] Find the first bit set in a bitfield.
<array> A pointer to the bitfield. This must be aligned on a long boundary.
<size> The number of bits in the bitfield.
[RETURNS] The index of the first set bit. If no bits are set, <<size>> + 1
is returned.
*/
{
int index;
unsigned long word;
unsigned int ul_size = 8 * sizeof (unsigned long);
CONST unsigned long *ul_array = array;
/* Find first word with any bit set */
for (index = 0; (*ul_array == 0) && (index < size);
index += ul_size, ++ul_array);
/* Find first bit set in word */
for (word = *ul_array; !(word & 1) && (index < size);
++index, word = word >> 1);
return (index);
} /* End Function find_first_set_bit */
/*
static inline int find_next_set_bit (CONST void *array, int size, int offset)
*/
static int find_next_set_bit (CONST void *array, int size, int offset)
/* [SUMMARY] Find the next bit set in a bitfield.
<array> A pointer to the bitfield. This must be aligned on a long boundary.
<size> The number of bits in the bitfield.
<offset> The offset of the current bit in the bitfield. The current bit is
ignored.
[RETURNS] The index of the next set bit. If no more bits are set,
<<size>> + 1 is returned.
*/
{
int index, tmp;
unsigned long word;
unsigned int ul_size = 8 * sizeof (unsigned long);
CONST unsigned long *ul_array = array;
if (++offset >= size) return (offset);
index = offset;
/* Jump to the long word containing the next bit */
tmp = offset / ul_size;
ul_array += tmp;
offset -= tmp * ul_size;
if ( (offset == 0) || (*ul_array == 0) )
return (find_first_set_bit (ul_array, size - index) + index);
/* There is a bit set somewhere in this word */
if ( ( (word = *ul_array) != 0 ) && ( (word = word >> offset) != 0 ) )
{
/* There is a bit set somewhere in this word at or after the offset
position */
for (; (word & 1) == 0; word = word >> 1, ++index);
return (index);
}
/* Have to go to subsequent word(s) */
index += ul_size - offset;
return (find_first_set_bit (++ul_array, size - index) + index);
} /* End Function find_next_set_bit */
struct callback_struct
{
void (*input_func) (void *info);
void (*output_func) (void *info);
void (*exception_func) (void *info);
void *info;
};
static int total_bits = 0;
struct callback_struct callbacks[MAX_FDS];
static void test_func (void *info)
{
++total_bits;
}
#ifdef HAS_SELECT
static void time_select (fd_set *input_fds, fd_set *output_fds,
fd_set *exception_fds, int max_fd, int num_iter,
long *times)
/* [SUMMARY] Time how long it takes to select(2) file descriptors.
<input_fds> The input masks.
<output_fds> The output masks.
<exception_fds> The exception masks.
<max_fd> The highest file descriptor in the fd_sets.
<num_iter> The number of iterations.
<times> The time taken (in microseconds) for each iteration.
[RETURNS] Nothing.
*/
{
int fd, count, nready;
fd_set i_fds, o_fds, e_fds;
struct timeval time1, time2, tv;
/* Warm the cache a bit */
memcpy (&i_fds, input_fds, sizeof i_fds);
memcpy (&o_fds, output_fds, sizeof i_fds);
memcpy (&e_fds, exception_fds, sizeof i_fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
select (max_fd + 1, &i_fds, &o_fds, &e_fds, &tv);
for (count = 0; count < num_iter; ++count)
{
total_bits = 0;
gettimeofday (&time1, NULL);
memcpy (&i_fds, input_fds, sizeof i_fds);
memcpy (&o_fds, output_fds, sizeof i_fds);
memcpy (&e_fds, exception_fds, sizeof i_fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
nready = select (max_fd + 1, &i_fds, &o_fds, &e_fds, &tv);
if (nready == -1)
{
fprintf (stderr, "Error selecting\t%s\n", ERRSTRING);
exit (2);
}
if (nready < 1)
{
fprintf (stderr, "Error: nready: %d\n", nready);
exit (1);
}
/* Scan the output */
for (fd = find_first_set_bit (&e_fds, sizeof e_fds * 8); fd <= max_fd;
fd = find_next_set_bit (&e_fds, sizeof e_fds * 8, fd) )
{
(*callbacks[fd].exception_func) (callbacks[fd].info);
}
for (fd = find_first_set_bit (&i_fds, sizeof i_fds * 8); fd <= max_fd;
fd = find_next_set_bit (&i_fds, sizeof i_fds * 8, fd) )
{
(*callbacks[fd].input_func) (callbacks[fd].info);
}
for (fd = find_first_set_bit (&o_fds, sizeof o_fds * 8); fd <= max_fd;
fd = find_next_set_bit (&o_fds, sizeof o_fds * 8, fd) )
{
(*callbacks[fd].output_func) (callbacks[fd].info);
}
gettimeofday (&time2, NULL);
times[count] = (time2.tv_sec - time1.tv_sec) * 1000000;
times[count] += time2.tv_usec - time1.tv_usec;
}
} /* End Function time_select */
#endif /* HAS_SELECT */
#ifdef HAS_POLL
static void time_poll (struct pollfd *pollfd_array, int start_index,
int num_to_test, int num_iter, long *times)
/* [SUMMARY] Time how long it takes to poll(2) file descriptors.
<pollfd_array> The array of pollfd structures.
<start_index> The start index in the array of pollfd structures.
<num_to_test> The number of file descriptors to test.
<num_iter> The number of iterations.
<times> The time taken (in microseconds) for each iteration.
[RETURNS] Nothing.
*/
{
short revents;
int fd, count, nready;
struct timeval time1, time2;
struct pollfd *pollfd_ptr, *stop_pollfd;
/* Warm the cache a bit */
poll (pollfd_array + start_index, num_to_test, 0);
for (count = 0; count < num_iter; ++count)
{
total_bits = 0;
gettimeofday (&time1, NULL);
nready = poll (pollfd_array + start_index, num_to_test, 0);
if (nready == -1)
{
fprintf (stderr, "Error polling\t%s\n", ERRSTRING);
exit (2);
}
if (nready < 1)
{
fprintf (stderr, "Error: nready: %d\n", nready);
exit (1);
}
stop_pollfd = pollfd_array + start_index + num_to_test;
for (pollfd_ptr = pollfd_array + start_index; TRUE; ++pollfd_ptr)
{
if (pollfd_ptr->revents == 0) continue;
/* Have an active descriptor */
revents = pollfd_ptr->revents;
fd = pollfd_ptr->fd;
if (revents & POLLPRI)
(*callbacks[fd].exception_func) (callbacks[fd].info);
if (revents & POLLIN)
(*callbacks[fd].input_func) (callbacks[fd].info);
if (revents & POLLOUT)
(*callbacks[fd].output_func) (callbacks[fd].info);
if (--nready == 0) break;
}
gettimeofday (&time2, NULL);
times[count] = (time2.tv_sec - time1.tv_sec) * 1000000;
times[count] += time2.tv_usec - time1.tv_usec;
}
} /* End Function time_poll */
#endif /* HAS_POLL */
#ifdef HAS_POLL2
static void time_poll2 (struct poll2ifd *poll2ifd_array, int start_index,
int num_to_test, int num_iter, long *times)
/* [SUMMARY] Time how long it takes to poll2(2) file descriptors.
<poll2ifd_array> The array of poll2ifd structures.
<start_index> The start index in the array of pollfd structures.
<num_to_test> The number of file descriptors to test.
<num_iter> The number of iterations.
<times> The time taken (in microseconds) for each iteration.
[RETURNS] Nothing.
*/
{
short revents;
int fd, count, nready, i;
struct timeval time1, time2;
struct poll2ofd poll2ofd_array[MAX_FDS];
/* Warm the cache a bit */
poll2 (poll2ifd_array + start_index, poll2ofd_array, num_to_test, 0);
for (count = 0; count < num_iter; ++count)
{
total_bits = 0;
gettimeofday (&time1, NULL);
nready = poll2 (poll2ifd_array + start_index, poll2ofd_array,
num_to_test, 0);
if (nready == -1)
{
times[count] = -1;
if (errno == ENOSYS) return; /* Must do this first */
fprintf (stderr, "Error calling poll2(2)\t%s\n", ERRSTRING);
exit (2);
}
if (nready < 1)
{
fprintf (stderr, "Error: nready: %d\n", nready);
exit (1);
}
for (i = 0; i < nready; ++i)
{
revents = poll2ofd_array[i].revents;
fd = poll2ofd_array[i].fd;
if (revents & POLLPRI)
(*callbacks[fd].exception_func) (callbacks[fd].info);
if (revents & POLLIN)
(*callbacks[fd].input_func) (callbacks[fd].info);
if (revents & POLLOUT)
(*callbacks[fd].output_func) (callbacks[fd].info);
}
gettimeofday (&time2, NULL);
times[count] = (time2.tv_sec - time1.tv_sec) * 1000000;
times[count] += time2.tv_usec - time1.tv_usec;
}
} /* End Function time_poll2 */
#endif /* HAS_POLL2 */
int main (argc, argv)
int argc;
char *argv[];
{
flag failed = FALSE;
flag verbose = FALSE;
int first_fd = -1;
int fd, max_fd, count, total_fds;
int num_to_test, num_active;
#ifdef UNIXBENCH
int max_iter = 1000;
#else
int max_iter = 10;
#endif
#ifdef HAS_SELECT
long select_total = 0;
fd_set input_fds, output_fds, exception_fds;
long select_times[MAX_ITERATIONS];
#endif
#ifdef HAS_POLL
int start_index;
long poll_total = 0;
struct pollfd pollfd_array[MAX_FDS];
long poll_times[MAX_ITERATIONS];
#endif
#ifdef HAS_POLL2
long poll2_total = 0;
struct poll2ifd poll2ifd_array[MAX_FDS];
struct poll2ofd poll2ofd_array[MAX_FDS];
long poll2_times[MAX_ITERATIONS];
#endif
#if 0
extern char *sys_errlist[];
#endif
#ifdef HAS_SELECT
FD_ZERO (&input_fds);
FD_ZERO (&output_fds);
FD_ZERO (&exception_fds);
#endif
#ifdef HAS_POLL
memset (pollfd_array, 0, sizeof pollfd_array);
#endif
/* Allocate file descriptors */
total_fds = 0;
max_fd = 0;
while (!failed)
{
if ( ( fd = dup (1) ) == -1 )
{
if (errno != EMFILE)
{
fprintf (stderr, "Error dup()ing\t%s\n", ERRSTRING);
exit (1);
}
failed = TRUE;
continue;
}
if (fd >= MAX_FDS)
{
fprintf (stderr, "File descriptor: %d larger than max: %d\n",
fd, MAX_FDS - 1);
exit (1);
}
callbacks[fd].input_func = test_func;
callbacks[fd].output_func = test_func;
callbacks[fd].exception_func = test_func;
callbacks[fd].info = NULL;
if (fd > max_fd) max_fd = fd;
if (first_fd < 0) first_fd = fd;
#ifdef HAS_POLL
pollfd_array[fd].fd = fd;
pollfd_array[fd].events = 0;
#endif
#ifdef HAS_POLL2
poll2ifd_array[fd].fd = fd;
poll2ifd_array[fd].events = 0;
#endif
}
total_fds = max_fd + 1;
/* Process the command-line arguments */
if (argc > 5)
{
fputs ("Usage:\ttime-polling [num_iter] [num_to_test] [num_active] [-v]\n",
stderr);
exit (1);
}
if (argc > 1) max_iter = atoi (argv[1]);
if (max_iter > MAX_ITERATIONS)
{
fprintf (stderr, "num_iter too large\n");
exit (1);
}
if (argc > 2) num_to_test = atoi (argv[2]);
else num_to_test = total_fds - first_fd;
if (argc > 3) num_active = atoi (argv[3]);
else num_active = 1;
if (argc > 4)
{
if (strcmp (argv[4], "-v") != 0)
{
fputs ("Usage:\ttime-polling [num_iter] [num_to_test] [num_active] [-v]\n",
stderr);
exit (1);
}
verbose = TRUE;
}
/* Sanity tests */
if (num_to_test > total_fds - first_fd) num_to_test = total_fds - first_fd;
if (num_active > total_fds - first_fd) num_active = total_fds - first_fd;
/* Set activity monitoring flags */
for (fd = total_fds - num_to_test; fd < total_fds; ++fd)
{
#ifdef HAS_SELECT
FD_SET (fd, &exception_fds);
FD_SET (fd, &input_fds);
#endif
#ifdef HAS_POLL
pollfd_array[fd].events = POLLPRI | POLLIN;
#endif
#ifdef HAS_POLL2
poll2ifd_array[fd].events = POLLPRI | POLLIN;
#endif
}
for (fd = total_fds - num_active; fd < total_fds; ++fd)
{
#ifdef HAS_SELECT
FD_SET (fd, &output_fds);
#endif
#ifdef HAS_POLL
pollfd_array[fd].events |= POLLOUT;
#endif
#ifdef HAS_POLL2
poll2ifd_array[fd].events |= POLLOUT;
#endif
}
fprintf (OUT, "Num fds: %d, polling descriptors %d-%d\n",
total_fds, total_fds - num_to_test, max_fd);
/* First do all the tests, then print the results */
#ifdef HAS_SELECT
time_select (&input_fds, &output_fds, &exception_fds, max_fd, max_iter,
select_times);
#endif
#ifdef HAS_POLL
start_index = total_fds - num_to_test;
time_poll (pollfd_array, start_index, num_to_test, max_iter, poll_times);
#endif
#ifdef HAS_POLL2
start_index = total_fds - num_to_test;
time_poll2 (poll2ifd_array, start_index, num_to_test, max_iter,
poll2_times);
#endif
/* Now print out all the times */
fputs ("All times in microseconds\n", OUT);
fputs ("ITERATION\t", OUT);
#ifdef HAS_SELECT
fprintf (OUT, "%-12s", "select(2)");
#endif
#ifdef HAS_POLL
fprintf (OUT, "%-12s", "poll(2)");
#endif
#ifdef HAS_POLL2
if (poll2_times[0] >= 0) fprintf (OUT, "%-12s", "poll2(2)");
#endif
for (count = 0; count < max_iter; ++count)
{
if (verbose) fprintf (OUT, "\n%d\t\t", count);
#ifdef HAS_SELECT
if (verbose) fprintf (OUT, "%-12ld", select_times[count]);
select_total += select_times[count];
#endif
#ifdef HAS_POLL
if (verbose) fprintf (OUT, "%-12ld", poll_times[count]);
poll_total += poll_times[count];
#endif
#ifdef HAS_POLL2
if ( verbose && (poll2_times[0] >= 0) )
fprintf (OUT, "%-12ld", poll2_times[count]);
poll2_total += poll2_times[count];
#endif
}
fputs ("\n\naverage\t\t", OUT);
#ifdef HAS_SELECT
fprintf (OUT, "%-12ld", select_total / max_iter);
#endif
#ifdef HAS_POLL
fprintf (OUT, "%-12ld", poll_total / max_iter);
#endif
#ifdef HAS_POLL2
if (poll2_times[0] >= 0)
fprintf (OUT, "%-12ld", poll2_total / max_iter);
#endif
putc ('\n', OUT);
fputs ("Per fd\t\t", OUT);
#ifdef HAS_SELECT
fprintf (OUT, "%-12.2f",
(float) select_total / (float) max_iter / (float) num_to_test);
#ifdef UNIXBENCH
fprintf (stderr, "lps\t%.2f\t%.1f\n",
1000000 * (float) max_iter * (float) num_to_test
/ (float) select_total, (float)select_total / 1000000);
#endif
#endif
#ifdef HAS_POLL
fprintf (OUT, "%-12.2f",
(float) poll_total / (float) max_iter / (float) num_to_test);
#ifdef UNIXBENCH
fprintf (stderr, "lps\t%.2f\t%.1f\n",
1000000 * (float) max_iter * (float) num_to_test
/ (float) poll_total, (float)poll_total / 1000000);
#endif
#endif
#ifdef HAS_POLL2
if (poll2_times[0] >= 0) {
fprintf (OUT, "%-12.2f",
(float) poll2_total / (float) max_iter / (float) num_to_test);
#ifdef UNIXBENCH
fprintf (stderr, "lps\t%.2f\t%.1f\n",
1000000 * (float) max_iter * (float) num_to_test
/ (float) poll2_total, (float)poll2_total / 1000000);
#endif
}
#endif
fputs ("<- the most important value\n", OUT);
exit(0);
} /* End Function main */

View File

@@ -0,0 +1,39 @@
/*******************************************************************************
*
* The BYTE UNIX Benchmarks - Release 3
* Module: timeit.c SID: 3.3 5/15/91 19:30:21
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
*
*******************************************************************************
* Modification Log:
* May 12, 1989 - modified empty loops to avoid nullifying by optimizing
* compilers
* August 28, 1990 - changed timing relationship--now returns total number
* of iterations (ty)
* October 22, 1997 - code cleanup to remove ANSI C compiler warnings
* Andy Kahn <kahn@zk3.dec.com>
*
******************************************************************************/
/* this module is #included in other modules--no separate SCCS ID */
/*
* Timing routine
*
*/
#include <signal.h>
#include <unistd.h>
void wake_me(int seconds, void (*func)(int))
{
/* set up the signal handler */
signal(SIGALRM, func);
/* get the clock running */
alarm(seconds);
}

View File

@@ -0,0 +1,650 @@
/*
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* $XFree86: xc/programs/glxgears/glxgears.c,v 1.3tsi Exp $ */
/*
* This is a port of the infamous "gears" demo to straight GLX (i.e. no GLUT)
* Port by Brian Paul 23 March 2001
*
* Exact timing added by Behdad Esfahbod to achieve a fixed speed regardless
* of frame rate. November 2003
*
* Printer support added by Roland Mainz <roland.mainz@nrubsig.org>. April 2004
*
* This version modified by Ian Smith, 30 Sept 2007, to make ubgears.
* ubgears is cusoimised for use in the UnixBench benchmarking suite.
* Some redundant stuff is gone, and the -time option is added.
* Mainly it's forked so we don't use the host's version, which could change
* from platform to platform.
*
* Command line options:
* -display Set X11 display for output.
* -info Print additional GLX information.
* -time <t> Run for <t> seconds and produce a performance report.
* -h Print this help page.
* -v Verbose output.
*
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <sys/time.h>
#include <sched.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.14159265
#endif /* !M_PI */
/* Turn a NULL pointer string into an empty string */
#define NULLSTR(x) (((x)!=NULL)?(x):(""))
#define Log(x) { if(verbose) printf x; }
#define Msg(x) { printf x; }
/* Globla vars */
/* program name (from argv[0]) */
static const char *ProgramName;
/* verbose output what the program is doing */
static Bool verbose = False;
/* time in microseconds to run for; -1 means forever. */
static int runTime = -1;
/* Time at which start_time(void) was called. */
static struct timeval clockStart;
/* XXX this probably isn't very portable */
/* return current time (in seconds) */
static void
start_time(void)
{
(void) gettimeofday(&clockStart, 0);
}
/*
* return time (in microseconds) since start_time(void) was called.
*
* The older version of this function randomly returned negative results.
* This version won't, up to 2000 seconds and some.
*/
static long
current_time(void)
{
struct timeval tv;
long secs, micros;
(void) gettimeofday(&tv, 0);
secs = tv.tv_sec - clockStart.tv_sec;
micros = tv.tv_usec - clockStart.tv_usec;
if (micros < 0) {
--secs;
micros += 1000000;
}
return secs * 1000000 + micros;
}
static
void usage(void)
{
fprintf (stderr, "usage: %s [options]\n", ProgramName);
fprintf (stderr, "-display\tSet X11 display for output.\n");
fprintf (stderr, "-info\t\tPrint additional GLX information.\n");
fprintf (stderr, "-time t\t\tRun for t seconds and report performance.\n");
fprintf (stderr, "-h\t\tPrint this help page.\n");
fprintf (stderr, "-v\t\tVerbose output.\n");
fprintf (stderr, "\n");
exit(EXIT_FAILURE);
}
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
static GLint gear1, gear2, gear3;
static GLfloat angle = 0.0;
static GLint speed = 60;
static GLboolean printInfo = GL_FALSE;
/*
*
* Draw a gear wheel. You'll probably want to call this function when
* building a display list since we do a lot of trig here.
*
* Input: inner_radius - radius of hole at center
* outer_radius - radius at center of teeth
* width - width of gear
* teeth - number of teeth
* tooth_depth - depth of tooth
*/
static void
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
GLint teeth, GLfloat tooth_depth)
{
GLint i;
GLfloat r0, r1, r2, maxr2, minr2;
GLfloat angle, da;
GLfloat u, v, len;
r0 = inner_radius;
r1 = outer_radius - tooth_depth / 2.0;
maxr2 = r2 = outer_radius + tooth_depth / 2.0;
minr2 = r2;
da = 2.0 * M_PI / teeth / 4.0;
glShadeModel(GL_FLAT);
glNormal3f(0.0, 0.0, 1.0);
/* draw front face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
if (i < teeth) {
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
width * 0.5);
}
}
glEnd();
/* draw front sides of teeth */
glBegin(GL_QUADS);
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
width * 0.5);
r2 = minr2;
}
r2 = maxr2;
glEnd();
glNormal3f(0.0, 0.0, -1.0);
/* draw back face */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
if (i < teeth) {
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
-width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
}
}
glEnd();
/* draw back sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / teeth / 4.0;
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
-width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
-width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
r2 = minr2;
}
r2 = maxr2;
glEnd();
/* draw outward faces of teeth */
glBegin(GL_QUAD_STRIP);
for (i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
u = r2 * cos(angle + da) - r1 * cos(angle);
v = r2 * sin(angle + da) - r1 * sin(angle);
len = sqrt(u * u + v * v);
u /= len;
v /= len;
glNormal3f(v, -u, 0.0);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glNormal3f(cos(angle + 1.5 * da), sin(angle + 1.5 * da), 0.0);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
-width * 0.5);
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
glNormal3f(v, -u, 0.0);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
-width * 0.5);
glNormal3f(cos(angle + 3.5 * da), sin(angle + 3.5 * da), 0.0);
r2 = minr2;
}
r2 = maxr2;
glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
glEnd();
glShadeModel(GL_SMOOTH);
/* draw inside radius cylinder */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glNormal3f(-cos(angle), -sin(angle), 0.0);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
}
glEnd();
}
static void
draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glCallList(gear1);
glPopMatrix();
glPushMatrix();
glTranslatef(3.1, -2.0, 0.0);
glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
glCallList(gear2);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.1, 4.2, 0.0);
glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
glCallList(gear3);
glPopMatrix();
glPopMatrix();
}
/* new window size or exposure */
static void
reshape(int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;
glViewport(0, 0, (GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* fit width and height */
if (h >= 1.0)
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
else
glFrustum(-1.0/h, 1.0/h, -1.0, 1.0, 5.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0);
}
static void
init(void)
{
static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
/* make the gears */
gear1 = glGenLists(1);
glNewList(gear1, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
gear(1.0, 4.0, 1.0, 20, 0.7);
glEndList();
gear2 = glGenLists(1);
glNewList(gear2, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
gear(0.5, 2.0, 2.0, 10, 0.7);
glEndList();
gear3 = glGenLists(1);
glNewList(gear3, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
gear(1.3, 2.0, 0.5, 10, 0.7);
glEndList();
glEnable(GL_NORMALIZE);
}
/*
* Create an RGB, double-buffered window.
* Return the window and context handles.
*/
static void
make_window( Display *dpy, Screen *scr,
const char *name,
int x, int y, int width, int height,
Window *winRet, GLXContext *ctxRet)
{
int attrib[] = { GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
None };
int scrnum;
XSetWindowAttributes attr;
unsigned long mask;
Window root;
Window win;
GLXContext ctx;
XVisualInfo *visinfo;
GLint max[2] = { 0, 0 };
scrnum = XScreenNumberOfScreen(scr);
root = XRootWindow(dpy, scrnum);
visinfo = glXChooseVisual( dpy, scrnum, attrib );
if (!visinfo) {
fprintf(stderr, "%s: Error: couldn't get an RGB, Double-buffered visual.\n", ProgramName);
exit(EXIT_FAILURE);
}
/* window attributes */
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
win = XCreateWindow( dpy, root, x, y, width, height,
0, visinfo->depth, InputOutput,
visinfo->visual, mask, &attr );
/* set hints and properties */
{
XSizeHints sizehints;
sizehints.x = x;
sizehints.y = y;
sizehints.width = width;
sizehints.height = height;
sizehints.flags = USSize | USPosition;
XSetNormalHints(dpy, win, &sizehints);
XSetStandardProperties(dpy, win, name, name,
None, (char **)NULL, 0, &sizehints);
}
ctx = glXCreateContext( dpy, visinfo, NULL, True );
if (!ctx) {
fprintf(stderr, "%s: Error: glXCreateContext failed.\n", ProgramName);
exit(EXIT_FAILURE);
}
XFree(visinfo);
XMapWindow(dpy, win);
glXMakeCurrent(dpy, win, ctx);
/* Check for maximum size supported by the GL rasterizer */
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, max);
if (printInfo)
printf("GL_MAX_VIEWPORT_DIMS=%d/%d\n", (int)max[0], (int)max[1]);
if (width > max[0] || height > max[1]) {
fprintf(stderr, "%s: Error: Requested window size (%d/%d) larger than "
"maximum supported by GL engine (%d/%d).\n",
ProgramName, width, height, (int)max[0], (int)max[1]);
exit(EXIT_FAILURE);
}
*winRet = win;
*ctxRet = ctx;
}
static void
event_loop(Display *dpy, Window win)
{
while (1) {
/* Process interactive events */
while (XPending(dpy) > 0) {
XEvent event;
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
Log(("Event: Expose\n"));
/* we'll redraw below */
break;
case ConfigureNotify:
Log(("Event: ConfigureNotify\n"));
reshape(event.xconfigure.width, event.xconfigure.height);
break;
}
}
{
/* Time at which we started measuring. */
static long startTime = 0;
/* Time of the previous frame. */
static long lastFrame = 0;
/* Time of the previous FPS report. */
static long lastFps = 0;
/* Number of frames we've done. */
static int frames = 0;
/* Number of frames we've done in the measured run. */
static long runFrames = 0;
long t = current_time();
long useconds;
if (!lastFrame)
lastFrame = t;
if (!lastFps)
lastFps = t;
/* How many microseconds since the previous frame? */
useconds = t - lastFrame;
if (!useconds) /* assume 100FPS if we don't have timer */
useconds = 10000;
/* Calculate how far the gears need to move and redraw. */
angle = angle + ((double)speed * useconds) / 1000000.0;
if (angle > 360.0)
angle = angle - 360.0; /* don't lose precision! */
draw();
glXSwapBuffers(dpy, win);
/* Done this frame. */
lastFrame = t;
frames++;
/* Every 5 seconds, print the FPS. */
if (t - lastFps >= 5000000L) {
GLfloat seconds = (t - lastFps) / 1000000.0;
GLfloat fps = frames / seconds;
printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
fps);
lastFps = t;
frames = 0;
/*
* Set the start time now -- ie. after one report. This
* gives us pump-priming time before we start for real.
*/
if (runTime > 0 && startTime == 0) {
printf("Start timing!\n");
startTime = t;
}
}
if (startTime > 0)
++runFrames;
/* If our run time is done, finish. */
if (runTime > 0 && startTime > 0 && t - startTime > runTime) {
double time = (double) (t - startTime) / 1000000.0;
fprintf(stderr, "COUNT|%ld|1|fps\n", runFrames);
fprintf(stderr, "TIME|%.1f\n", time);
exit(0);
}
/* Need to give cpu away in order to get precise timing next cycle,
* otherwise, gettimeofday would return almost the same value. */
sched_yield();
}
}
}
int
main(int argc, char *argv[])
{
Bool use_threadsafe_api = False;
Display *dpy;
Window win;
Screen *screen;
GLXContext ctx;
char *dpyName = NULL;
int i;
XRectangle winrect;
ProgramName = argv[0];
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
int len = strlen(arg);
if (strcmp(argv[i], "-display") == 0) {
if (++i >= argc)
usage();
dpyName = argv[i];
}
else if (strcmp(argv[i], "-info") == 0) {
printInfo = GL_TRUE;
}
else if (strcmp(argv[i], "-time") == 0) {
if (++i >= argc)
usage();
runTime = atoi(argv[i]) * 1000000;
}
else if (!strncmp("-v", arg, len)) {
verbose = True;
printInfo = GL_TRUE;
}
else if( !strncmp("-debug_use_threadsafe_api", arg, len) )
{
use_threadsafe_api = True;
}
else if (!strcmp(argv[i], "-h")) {
usage();
}
else
{
fprintf(stderr, "%s: Unsupported option '%s'.\n", ProgramName, argv[i]);
usage();
}
}
/* Init X threading API on demand (for debugging) */
if( use_threadsafe_api )
{
if( !XInitThreads() )
{
fprintf(stderr, "%s: XInitThreads() failure.\n", ProgramName);
exit(EXIT_FAILURE);
}
}
dpy = XOpenDisplay(dpyName);
if (!dpy) {
fprintf(stderr, "%s: Error: couldn't open display '%s'\n", ProgramName, dpyName);
return EXIT_FAILURE;
}
screen = XDefaultScreenOfDisplay(dpy);
winrect.x = 0;
winrect.y = 0;
winrect.width = 300;
winrect.height = 300;
Log(("Window x=%d, y=%d, width=%d, height=%d\n",
(int)winrect.x, (int)winrect.y, (int)winrect.width, (int)winrect.height));
make_window(dpy, screen, "ubgears", winrect.x, winrect.y, winrect.width, winrect.height, &win, &ctx);
reshape(winrect.width, winrect.height);
if (printInfo) {
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
}
init();
start_time();
event_loop(dpy, win);
glXDestroyContext(dpy, ctx);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return EXIT_SUCCESS;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,156 @@
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 1
* Module: cctest.c SID: 1.2 7/10/89 18:55:45
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith or Rick Grehan at BYTE Magazine
* bensmith@bixpb.UUCP rick_g@bixpb.UUCP
*
*******************************************************************************
* Modification Log:
* $Header: cctest.c,v 3.4 87/06/22 14:22:47 kjmcdonell Beta $
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)cctest.c:1.2 -- 7/10/89 18:55:45";
#include <stdio.h>
/*
* C compile and load speed test file.
* Based upon fstime.c from MUSBUS 3.1, with all calls to ftime() replaced
* by calls to time(). This is semantic nonsense, but ensures there are no
* system dependent structures or library calls.
*
*/
#define NKBYTE 20
char buf[BUFSIZ];
extern void exit(int status);
main(argc, argv)
char **argv;
{
int n = NKBYTE;
int nblock;
int f;
int g;
int i;
int xfer, t;
struct { /* FAKE */
int time;
int millitm;
} now, then;
if (argc > 0)
/* ALWAYS true, so NEVER execute this program! */
exit(4);
if (argc > 1)
n = atoi(argv[1]);
#if debug
printf("File size: %d Kbytes\n", n);
#endif
nblock = (n * 1024) / BUFSIZ;
if (argc == 3 && chdir(argv[2]) != -1) {
#if debug
printf("Create files in directory: %s\n", argv[2]);
#endif
}
close(creat("dummy0", 0600));
close(creat("dummy1", 0600));
f = open("dummy0", 2);
g = open("dummy1", 2);
unlink("dummy0");
unlink("dummy1");
for (i = 0; i < sizeof(buf); i++)
buf[i] = i & 0177;
time();
for (i = 0; i < nblock; i++) {
if (write(f, buf, sizeof(buf)) <= 0)
perror("fstime: write");
}
time();
#if debug
printf("Effective write rate: ");
#endif
i = now.millitm - then.millitm;
t = (now.time - then.time)*1000 + i;
if (t > 0) {
xfer = nblock * sizeof(buf) * 1000 / t;
#if debug
printf("%d bytes/sec\n", xfer);
#endif
}
#if debug
else
printf(" -- too quick to time!\n");
#endif
#if awk
fprintf(stderr, "%.2f", t > 0 ? (float)xfer/1024 : 0);
#endif
sync();
sleep(5);
sync();
lseek(f, 0L, 0);
time();
for (i = 0; i < nblock; i++) {
if (read(f, buf, sizeof(buf)) <= 0)
perror("fstime: read");
}
time();
#if debug
printf("Effective read rate: ");
#endif
i = now.millitm - then.millitm;
t = (now.time - then.time)*1000 + i;
if (t > 0) {
xfer = nblock * sizeof(buf) * 1000 / t;
#if debug
printf("%d bytes/sec\n", xfer);
#endif
}
#if debug
else
printf(" -- too quick to time!\n");
#endif
#if awk
fprintf(stderr, " %.2f", t > 0 ? (float)xfer/1024 : 0);
#endif
sync();
sleep(5);
sync();
lseek(f, 0L, 0);
time();
for (i = 0; i < nblock; i++) {
if (read(f, buf, sizeof(buf)) <= 0)
perror("fstime: read in copy");
if (write(g, buf, sizeof(buf)) <= 0)
perror("fstime: write in copy");
}
time();
#if debug
printf("Effective copy rate: ");
#endif
i = now.millitm - then.millitm;
t = (now.time - then.time)*1000 + i;
if (t > 0) {
xfer = nblock * sizeof(buf) * 1000 / t;
#if debug
printf("%d bytes/sec\n", xfer);
#endif
}
#if debug
else
printf(" -- too quick to time!\n");
#endif
#if awk
fprintf(stderr, " %.2f\n", t > 0 ? (float)xfer/1024 : 0);
#endif
}

View File

@@ -0,0 +1,8 @@
99
k
2
v
p
q
[ calculate the sqrt(2) to 99 decimal places ... John Lions Test ]
[ $Header: dc.dat,v 1.1 87/06/22 14:28:28 kjmcdonell Beta $ ]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,362 @@
version="1.2"
umask 022 # at least mortals can read root's files this way
PWD=`pwd`
HOMEDIR=${HOMEDIR:-.}
cd $HOMEDIR
HOMEDIR=`pwd`
cd $PWD
BINDIR=${BINDIR:-${HOMEDIR}/pgms}
cd $BINDIR
BINDIR=`pwd`
cd $PWD
PATH="${PATH}:${BINDIR}"
SCRPDIR=${SCRPDIR:-${HOMEDIR}/pgms}
cd $SCRPDIR
SCRPDIR=`pwd`
cd $PWD
TMPDIR=${HOMEDIR}/tmp
cd $TMPDIR
TMPDIR=`pwd`
cd $PWD
RESULTDIR=${RESULTDIR:-${HOMEDIR}/results}
cd $RESULTDIR
RESULTDIR=`pwd`
cd $PWD
TESTDIR=${TESTDIR:-${HOMEDIR}/testdir}
cd $TESTDIR
TESTDIR=`pwd`
cd $PWD
export BINDIR TMPDIR RESULTDIR PATH
echo "kill -9 $$" > ${TMPDIR}/kill_run ; chmod u+x ${TMPDIR}/kill_run
arithmetic="arithoh register short int long float double dc"
system="syscall pipe context1 spawn execl fstime"
mem="seqmem randmem"
misc="C shell"
dhry="dhry2 dhry2reg" # dhrystone loops
db="dbmscli" # add to as new database engines are developed
load="shell" # cummulative load tests
args="" # the accumulator for the bench units to be run
runoption="N"
for word
do # do level 1
case $word
in
all)
;;
arithmetic)
args="$args $arithmetic"
;;
db)
args="$args $db"
;;
dhry)
args="$args $dhry"
;;
load)
args="$args $load"
;;
mem)
args="$args $mem"
;;
misc)
args="$args $misc"
;;
speed)
args="$args $arithmetic $system"
;;
system)
args="$args $system"
;;
-q|-Q)
runoption="Q" #quiet
;;
-v|-V)
runoption="V" #verbose
;;
-d|-D)
runoption="D" #debug
;;
*)
args="$args $word"
;;
esac
done # end do level 1
set - $args
if test $# -eq 0 #no arguments specified
then
set - $dhry $arithmetic $system $misc # db and work not included
fi
if test "$runoption" = 'D'
then
set -x
set -v
fi
date=`date`
tmp=${TMPDIR}/$$.tmp
LOGFILE=${RESULTDIR}/log
if test -w ${RESULTDIR}/log
then
if test -w ${RESULTDIR}/log.accum
then
cat ${RESULTDIR}/log >> ${RESULTDIR}/log.accum
rm ${RESULTDIR}/log
else
mv ${RESULTDIR}/log ${RESULTDIR}/log.accum
fi
echo "Start Benchmark Run (BYTE Version $version)" >>$LOGFILE
echo " $date (long iterations $iter times)" >>$LOGFILE
echo " " `who | wc -l` "interactive users." >>$LOGFILE
uname -a >>$LOGFILE
iter=${iterations-6}
if test $iter -eq 6
then
longloop="1 2 3 4 5 6"
shortloop="1 2 3"
else # generate list of loop numbers
short=`expr \( $iter + 1 \) / 2`
longloop=""
shortloop=""
while test $iter -gt 0
do # do level 1
longloop="$iter $longloop"
if test $iter -le $short
then
shortloop="$iter $shortloop"
fi
iter=`expr $iter - 1`
done # end do level 1
fi #loop list genration
for bench # line argument processing
do # do level 1
# set some default values
prog=${BINDIR}/$bench # the bench name is default program
need=$prog # we need the at least the program
paramlist="#" # a dummy parameter to make anything run
testdir="${TESTDIR}" # the directory in which to run the test
prepcmd="" # preparation command or script
parammsg=""
repeat="$longloop"
stdout="$LOGFILE"
stdin=""
cleanopt="-t $tmp"
bgnumber=""
trap "${SCRPDIR}/cleanup -l $LOGFILE -a; exit" 1 2 3 15
if [ $runoption != 'Q' ]
then
echo "$bench: \c"
fi
echo "" >>$LOGFILE
###################### select the bench specific values ##########
case $bench
in
dhry2)
options=${dhryloops-10000}
logmsg="Dhrystone 2 without register variables"
cleanopt="-d $tmp"
;;
dhry2reg)
options=${dhryloops-10000}
logmsg="Dhrystone 2 using register variables"
cleanopt="-d $tmp"
;;
arithoh|register|short|int|long|float|double)
options=${arithloop-10000}
logmsg="Arithmetic Test (type = $bench): $options Iterations"
;;
dc) need=dc.dat
prog=dc
options=""
stdin=dc.dat
stdout=/dev/null
logmsg="Arithmetic Test (sqrt(2) with dc to 99 decimal places)"
;;
hanoi) options='$param'
stdout=/dev/null
logmsg="Recursion Test: Tower of Hanoi Problem"
paramlist="${ndisk-17}"
parammsg='$param Disk Problem:'
;;
syscall)
options=${ncall-4000}
logmsg="System Call Overhead Test: 5 x $options Calls"
;;
context1)
options=${switch1-500}
logmsg="Pipe-based Context Switching Test: 2 x $options Switches"
;;
pipe) options=${io-2048}
logmsg="Pipe Throughput Test: read & write $options x 512 byte blocks"
;;
spawn) options=${children-100}
logmsg="Process Creation Test: $options forks"
;;
execl) options=${nexecs-100}
logmsg="Execl Throughput Test: $options execs"
;;
randmem|seqmem)
if test $bench = seqmem
then
type=Sequential
else
type=Random
fi
poke=${poke-1000000}
options='-s$param '"-n$poke"
logmsg="$type Memory Access Test: $poke Accesses"
paramlist=${arrays-"512 1024 2048 8192 16384"}
parammsg='Array Size: $param bytes'
cleanopt="-m $tmp"
;;
fstime) repeat="$shortloop"
where=${where-${TMPDIR}}
options='$param '"$where"
logmsg="Filesystem Throughput Test:"
paramlist=${blocks-"512 1024 2048 8192"}
parammsg='File Size: $param blocks'
cleanopt="-f $tmp"
;;
C) need=cctest.c
prog=cc
options='$param'
stdout=/dev/null
repeat="$shortloop"
logmsg="C Compiler Test:"
paramlist="cctest.c"
parammsg='cc $param'
rm -f a.out
;;
dbmscli)
repeat="$shortloop"
need="db.dat"
prepcmd='${BINDIR}/dbprep ${testdir}/db.dat 10000'
paramlist=${clients-"1 2 4 8"}
parammsg='$param client processes. (filesize `cat ${testdir}/db.dat|wc -c` bytes)'
logmsg="Client/Server Database Engine:"
options='${testdir}/db.dat $param 0 1000' # $param clients;
# 0 sleep; 1000 iterations
;;
shell)
prog="multi.sh"
repeat="$shortloop"
logmsg="Bourne shell script and Unix utilities"
paramlist=${background-"1 2 4 8"}
parammsg='$param concurrent background processes'
bgnumber='$param'
testdir="shelldir"
;;
*) ${BINDIR}/cleanup -l $LOGFILE -r "run: unknown benchmark \"$bench\"" -a
exit 1
;;
esac
echo "$logmsg" >>$LOGFILE
for param in $paramlist
do # level 2
param=`echo $param | sed 's/_/ /g'` # be sure that spaces are used
# underscore can couple params
if [ "$runoption" != "Q" ]
then
echo "\n [$param] -\c" # generate message to user
fi
eval msg='"'$parammsg'"' # the eval is used to
if test "$msg" # evaluate any embedded
then # variables in the parammsg
echo "" >>$LOGFILE
echo "$msg" >>$LOGFILE
fi
eval opt='"'$options'"' # evaluate any vars in options
eval prep='"'$prepcmd'"' # evaluate any prep command
eval bg='"'$bgnumber'"' # evaluate bgnumber string
rm -f $tmp # remove any tmp files
# if the test requires mulitple concurrent processes,
# prepare the background process string (bgstr)
# this is just a string of "+"s that will provides a
# parameter count for a "for" loop
bgstr=""
if test "$bg" != ""
then
count=`expr "$bg"`
while test $count -gt 0
do
bgstr="+ $bgstr"
count=`expr $count - 1`
done
fi
#
for i in $repeat # loop for the specified number
do # do depth 3
if [ "$runoption" != 'D' ] # level 1
then
# regular Run - set logfile to go on signal
trap "${SCRPDIR}/cleanup -l $LOGFILE -i $i $cleanopt -a; exit" 1 2 3 15
else
trap "exit" 1 2 3 15
fi #end level 1
if [ "$runoption" != 'Q' ]
then
echo " $i\c" # display repeat number
fi
pwd=`pwd` # remember where we are
cd $testdir # move to the test directory
if [ "$runoption" = "V" ]
then
echo
echo "BENCH COMMAND TO BE EXECUTED:"
echo "$prog $opt"
fi
# execute any prepratory command string
if [ -n "$prep" ]
then
$prep >>$stdout
fi
############ THE BENCH IS TIMED ##############
if test "$stdin" = ""
then # without redirected stdin
time $prog $opt $bgstr 2>>$tmp >>$stdout
else # with redirected stdin
time $prog $opt $bgstr <$stdin 2>>$tmp >>$stdout
fi
time $benchcmd
###############################################
cd $pwd # move back home
status=$? # save the result code
if test $status != 0 # must have been an error
then
if test -f $tmp # is there an error file ?
then
cp $tmp ${TMPDIR}/save.$bench.$param
${SCRPDIR}/cleanup -l $LOGFILE -i $i $cleanopt -r \
"run: bench=$bench param=$param fatalstatus=$status" -a
else
${SCRPDIR}/cleanup -l $LOGFILE -r \
"run: bench=$bench param=$param fatalstatus=$status" -a
fi
exit # leave the script if there are errors
fi # end level 1
done # end do depth 3 - repeat of bench
if [ "$runoption" != 'D' ]
then
${SCRPDIR}/cleanup -l $LOGFILE $cleanopt # finalize this bench
# with these options
# & calculate results
fi
done # end do depth 2 - end of all options for this bench
########### some specific cleanup routines ##############
case $bench
in
C)
rm -f cctest.o a.out
;;
esac
if [ "$runoption" != 'Q' ]
then
echo ""
fi
done # end do level 1 - all benchmarks requested
echo "" >>$LOGFILE
echo " " `who | wc -l` "interactive users." >>$LOGFILE
echo "End Benchmark Run ($date) ...." >>$LOGFILE
if [ "$runoption" != 'Q' ]
then
pg $LOGFILE
fi
exit

View File

@@ -1,8 +0,0 @@
# $NetBSD: Makefile,v 1.22 2007/12/31 15:31:24 ad Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
SUBDIR= cat chmod cp csh date dd df domainname echo ed expr hostname \
kill ksh ln ls mkdir mv pax pwd rcp rcmd rm rmdir sh \
sleep stty sync test
.include <bsd.subdir.mk>

View File

@@ -1,11 +0,0 @@
# $NetBSD: Makefile.inc,v 1.17 2012/03/21 05:47:53 matt Exp $
# @(#)Makefile.inc 8.1 (Berkeley) 5/31/93
.include <bsd.own.mk> # for MKDYNAMICROOT definition
WARNS?= 5
BINDIR?= /bin
.if (${MKDYNAMICROOT} == "no")
LDSTATIC?= -static
.endif

View File

@@ -1,6 +0,0 @@
# $NetBSD: Makefile,v 1.12 2003/05/18 07:57:31 lukem Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= cat
.include <bsd.prog.mk>

View File

@@ -1,218 +0,0 @@
.\" $NetBSD: cat.1,v 1.36 2012/11/10 18:41:10 wiz Exp $
.\"
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)cat.1 8.3 (Berkeley) 5/2/95
.\"
.Dd November 10, 2012
.Dt CAT 1
.Os
.Sh NAME
.Nm cat
.Nd concatenate and print files
.Sh SYNOPSIS
.Nm
.Op Fl beflnstuv
.Op Fl B Ar bsize
.Op Fl
.Op Ar
.Sh DESCRIPTION
The
.Nm
utility reads files sequentially, writing them to the standard output.
The
.Ar file
operands are processed in command line order.
A single dash represents the standard input,
and may appear multiple times in the
.Ar file
list.
If no
.Ar file
operands are given, standard input is read.
.Pp
The word
.Dq concatenate
is just a verbose synonym for
.Dq catenate .
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl B Ar bsize
Read with a buffer size of
.Ar bsize
bytes, instead of the default buffer size which is the blocksize of the
output file.
.It Fl b
Implies the
.Fl n
option, but doesn't number blank lines.
.It Fl e
Implies the
.Fl v
option, and displays a dollar sign
.Pq Ql \&$
at the end of each line
as well.
.It Fl f
Only attempt to display regular files.
.It Fl l
Set an exclusive advisory lock on the standard output file descriptor.
This lock is set using
.Xr fcntl 2
with the
.Dv F_SETLKW
command.
If the output file is already locked,
.Nm
will block until the lock is acquired.
.It Fl n
Number the output lines, starting at 1.
.It Fl s
Squeeze multiple adjacent empty lines, causing the output to be
single spaced.
.It Fl t
Implies the
.Fl v
option, and displays tab characters as
.Ql ^I
as well.
.It Fl u
The
.Fl u
option guarantees that the output is unbuffered.
.It Fl v
Displays non-printing characters so they are visible.
Control characters print as
.Ql ^X
for control-X; the delete
character (octal 0177) prints as
.Ql ^? .
Non-ascii characters (with the high bit set) are printed as
.Ql M-
(for meta) followed by the character for the low 7 bits.
.El
.Sh EXIT STATUS
The
.Nm
utility exits 0 on success, and \*[Gt]0 if an error occurs.
.Sh EXAMPLES
The command:
.Bd -literal -offset indent
.Ic cat file1
.Ed
.Pp
will print the contents of
.Ar file1
to the standard output.
.Pp
The command:
.Bd -literal -offset indent
.Ic cat file1 file2 \*[Gt] file3
.Ed
.Pp
will sequentially print the contents of
.Ar file1
and
.Ar file2
to the file
.Ar file3 ,
truncating
.Ar file3
if it already exists.
See the manual page for your shell (e.g.,
.Xr sh 1 )
for more information on redirection.
.Pp
The command:
.Bd -literal -offset indent
.Ic cat file1 - file2 - file3
.Ed
.Pp
will print the contents of
.Ar file1 ,
print data it receives from the standard input until it receives an
.Dv EOF
.Pq Sq ^D
character, print the contents of
.Ar file2 ,
read and output contents of the standard input again, then finally output
the contents of
.Ar file3 .
Note that if the standard input referred to a file, the second dash
on the command-line would have no effect, since the entire contents of the file
would have already been read and printed by
.Nm
when it encountered the first
.Ql \&-
operand.
.Sh SEE ALSO
.Xr head 1 ,
.Xr hexdump 1 ,
.Xr lpr 1 ,
.Xr more 1 ,
.Xr pr 1 ,
.Xr tail 1 ,
.Xr view 1 ,
.Xr vis 1 ,
.Xr fcntl 2
.Rs
.%A Rob Pike
.%T "UNIX Style, or cat -v Considered Harmful"
.%J "USENIX Summer Conference Proceedings"
.%D 1983
.Re
.Sh STANDARDS
The
.Nm
utility is expected to conform to the
.St -p1003.2-92
specification.
.Pp
The flags
.Op Fl belnstv
are extensions to the specification.
.Sh HISTORY
A
.Nm
utility appeared in
.At v1 .
Dennis Ritchie designed and wrote the first man page.
It appears to have been
.Xr cat 1 .
.Sh BUGS
Because of the shell language mechanism used to perform output
redirection, the command
.Dq Li cat file1 file2 \*[Gt] file1
will cause the original data in file1 to be destroyed!
This is performed by the shell before
.Nm
is run.

View File

@@ -1,323 +0,0 @@
/* $NetBSD: cat.c,v 1.52 2012/11/19 19:41:31 christos Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Kevin Fall.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if !defined(lint)
__COPYRIGHT(
"@(#) Copyright (c) 1989, 1993\
The Regents of the University of California. All rights reserved.");
#if 0
static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
#else
__RCSID("$NetBSD: cat.c,v 1.52 2012/11/19 19:41:31 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
static size_t bsize;
static int rval;
static const char *filename;
void cook_args(char *argv[]);
void cook_buf(FILE *);
void raw_args(char *argv[]);
void raw_cat(int);
int
main(int argc, char *argv[])
{
int ch;
struct flock stdout_lock;
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "B:beflnstuv")) != -1)
switch (ch) {
case 'B':
bsize = (size_t)strtol(optarg, NULL, 0);
break;
case 'b':
bflag = nflag = 1; /* -b implies -n */
break;
case 'e':
eflag = vflag = 1; /* -e implies -v */
break;
case 'f':
fflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'n':
nflag = 1;
break;
case 's':
sflag = 1;
break;
case 't':
tflag = vflag = 1; /* -t implies -v */
break;
case 'u':
setbuf(stdout, NULL);
break;
case 'v':
vflag = 1;
break;
default:
case '?':
(void)fprintf(stderr,
"Usage: %s [-beflnstuv] [-B bsize] [-] "
"[file ...]\n", getprogname());
return EXIT_FAILURE;
}
argv += optind;
if (lflag) {
stdout_lock.l_len = 0;
stdout_lock.l_start = 0;
stdout_lock.l_type = F_WRLCK;
stdout_lock.l_whence = SEEK_SET;
if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
err(EXIT_FAILURE, "stdout");
}
if (bflag || eflag || nflag || sflag || tflag || vflag)
cook_args(argv);
else
raw_args(argv);
if (fclose(stdout))
err(EXIT_FAILURE, "stdout");
return rval;
}
void
cook_args(char **argv)
{
FILE *fp;
fp = stdin;
filename = "stdin";
do {
if (*argv) {
if (!strcmp(*argv, "-"))
fp = stdin;
else if ((fp = fopen(*argv,
fflag ? "rf" : "r")) == NULL) {
warn("%s", *argv);
rval = EXIT_FAILURE;
++argv;
continue;
}
filename = *argv++;
}
cook_buf(fp);
if (fp != stdin)
(void)fclose(fp);
else
clearerr(fp);
} while (*argv);
}
void
cook_buf(FILE *fp)
{
int ch, gobble, line, prev;
line = gobble = 0;
for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (ch == '\n') {
if (sflag) {
if (!gobble && nflag && !bflag)
(void)fprintf(stdout,
"%6d\t\n", ++line);
else if (!gobble && putchar(ch) == EOF)
break;
gobble = 1;
continue;
}
if (nflag) {
if (!bflag) {
(void)fprintf(stdout,
"%6d\t", ++line);
if (ferror(stdout))
break;
} else if (eflag) {
(void)fprintf(stdout,
"%6s\t", "");
if (ferror(stdout))
break;
}
}
} else if (nflag) {
(void)fprintf(stdout, "%6d\t", ++line);
if (ferror(stdout))
break;
}
}
gobble = 0;
if (ch == '\n') {
if (eflag)
if (putchar('$') == EOF)
break;
} else if (ch == '\t') {
if (tflag) {
if (putchar('^') == EOF || putchar('I') == EOF)
break;
continue;
}
} else if (vflag) {
if (!isascii(ch)) {
if (putchar('M') == EOF || putchar('-') == EOF)
break;
ch = toascii(ch);
}
if (iscntrl(ch)) {
if (putchar('^') == EOF ||
putchar(ch == '\177' ? '?' :
ch | 0100) == EOF)
break;
continue;
}
}
if (putchar(ch) == EOF)
break;
}
if (ferror(fp)) {
warn("%s", filename);
rval = EXIT_FAILURE;
clearerr(fp);
}
if (ferror(stdout))
err(EXIT_FAILURE, "stdout");
}
void
raw_args(char **argv)
{
int fd;
fd = fileno(stdin);
filename = "stdin";
do {
if (*argv) {
if (!strcmp(*argv, "-"))
fd = fileno(stdin);
else if (fflag) {
struct stat st;
fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
if (fd < 0)
goto skip;
if (fstat(fd, &st) == -1) {
close(fd);
goto skip;
}
if (!S_ISREG(st.st_mode)) {
close(fd);
warnx("%s: not a regular file", *argv);
goto skipnomsg;
}
}
else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
skip:
warn("%s", *argv);
skipnomsg:
rval = EXIT_FAILURE;
++argv;
continue;
}
filename = *argv++;
}
raw_cat(fd);
if (fd != fileno(stdin))
(void)close(fd);
} while (*argv);
}
void
raw_cat(int rfd)
{
static char *buf;
static char fb_buf[BUFSIZ];
ssize_t nr, nw, off;
int wfd;
wfd = fileno(stdout);
if (buf == NULL) {
struct stat sbuf;
if (bsize == 0) {
if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > 0 &&
(size_t)sbuf.st_blksize > sizeof(fb_buf))
bsize = sbuf.st_blksize;
}
if (bsize > sizeof(fb_buf)) {
buf = malloc(bsize);
if (buf == NULL)
warnx("malloc, using %zu buffer", bsize);
}
if (buf == NULL) {
bsize = sizeof(fb_buf);
buf = fb_buf;
}
}
while ((nr = read(rfd, buf, bsize)) > 0)
for (off = 0; nr; nr -= nw, off += nw)
if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
err(EXIT_FAILURE, "stdout");
if (nr < 0) {
warn("%s", filename);
rval = EXIT_FAILURE;
}
}

View File

@@ -1,6 +0,0 @@
# $NetBSD: Makefile,v 1.8 1997/07/20 22:36:34 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= chmod
.include <bsd.prog.mk>

View File

@@ -1,307 +0,0 @@
.\" $NetBSD: chmod.1,v 1.24 2012/10/22 17:47:06 christos Exp $
.\"
.\" Copyright (c) 1989, 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)chmod.1 8.4 (Berkeley) 3/31/94
.\"
.Dd October 22, 2012
.Dt CHMOD 1
.Os
.Sh NAME
.Nm chmod
.Nd change file modes
.Sh SYNOPSIS
.Nm
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl fh
.Ar mode
.Ar
.Nm
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl fh
.Fl Fl reference=rfile
.Ar
.Sh DESCRIPTION
The
.Nm
utility modifies the file mode bits of the listed files
as specified by the
.Ar mode
operand, or
copied from a reference
.Ar rfile ,
as specified with the
.Fl Fl reference
argument.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl H
If the
.Fl R
option is specified, symbolic links on the command line are followed.
(Symbolic links encountered in the tree traversal are not followed.)
.It Fl L
If the
.Fl R
option is specified, all symbolic links are followed.
.It Fl P
If the
.Fl R
option is specified, no symbolic links are followed.
.It Fl R
Change the modes of the file hierarchies rooted in the files
instead of just the files themselves.
.It Fl f
Do not display a diagnostic message or modify the exit status if
.Nm
fails to change the mode of a file.
.It Fl h
If
.Ar file
is symbolic link, the mode of the link is changed.
.El
.Pp
The
.Fl H ,
.Fl L
and
.Fl P
options are ignored unless the
.Fl R
option is specified.
In addition, these options override each other and the
command's actions are determined by the last one specified.
.Pp
Only the owner of a file or the super-user is permitted to change
the mode of a file.
.Sh EXIT STATUS
The
.Nm
utility exits 0 on success, and \*[Gt]0 if an error occurs.
.Sh MODES
Modes may be absolute or symbolic.
An absolute mode is an octal number constructed by
.Em or Ap ing
the following values:
.Pp
.Bl -tag -width 6n -compact -offset indent
.It Li 4000
set-user-ID-on-execution
.It Li 2000
set-group-ID-on-execution
.It Li 1000
sticky bit, see
.Xr chmod 2
.It Li 0400
read by owner
.It Li 0200
write by owner
.It Li 0100
execute (or search for directories) by owner
.It Li 0070
read, write, execute/search by group
.It Li 0007
read, write, execute/search by others
.El
.Pp
The read, write, and execute/search values for group and others
are encoded as described for owner.
.Pp
The symbolic mode is described by the following grammar:
.Bd -literal -offset indent
mode ::= clause [, clause ...]
clause ::= [who ...] [action ...] last_action
action ::= op [perm ...]
last_action ::= op [perm ...]
who ::= a | u | g | o
op ::= + | \- | =
perm ::= r | s | t | w | x | X | u | g | o
.Ed
.Pp
The
.Ar who
symbols ``u'', ``g'', and ``o'' specify the user, group, and other parts
of the mode bits, respectively.
The
.Ar who
symbol ``a'' is equivalent to ``ugo''.
.Pp
The
.Ar perm
symbols represent the portions of the mode bits as follows:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It r
The read bits.
.It s
The set-user-ID-on-execution and set-group-ID-on-execution bits.
.It t
The sticky bit.
.It w
The write bits.
.It x
The execute/search bits.
.It X
The execute/search bits if the file is a directory or any of the
execute/search bits are set in the original (unmodified) mode.
Operations with the
.Ar perm
symbol ``X'' are only meaningful in conjunction with the
.Ar op
symbol ``+'', and are ignored in all other cases.
.It u
The user permission bits in the mode of the original file.
.It g
The group permission bits in the mode of the original file.
.It o
The other permission bits in the mode of the original file.
.El
.Pp
The
.Ar op
symbols represent the operation performed, as follows:
.Bl -tag -width 4n
.It +
If no value is supplied for
.Ar perm ,
the ``+'' operation has no effect.
If no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is set.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are set.
.It \&\-
If no value is supplied for
.Ar perm ,
the ``\-'' operation has no effect.
If no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is cleared.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are cleared.
.It =
The mode bits specified by the
.Ar who
value are cleared, or, if no who value is specified, the owner, group
and other mode bits are cleared.
Then, if no value is supplied for
.Ar who ,
each permission bit specified in
.Ar perm ,
for which the corresponding bit in the file mode creation mask
is clear, is set.
Otherwise, the mode bits represented by the specified
.Ar who
and
.Ar perm
values are set.
.El
.Pp
Each
.Ar clause
specifies one or more operations to be performed on the mode
bits, and each operation is applied to the mode bits in the
order specified.
.Pp
Operations upon the other permissions only (specified by the symbol
``o'' by itself), in combination with the
.Ar perm
symbols ``s'' or ``t'', are ignored.
.Sh EXAMPLES
.Bl -tag -width "u=rwx,go=u-w" -compact
.It Li 644
make a file readable by anyone and writable by the owner only.
.Pp
.It Li go-w
deny write permission to group and others.
.Pp
.It Li =rw,+X
set the read and write permissions to the usual defaults, but
retain any execute permissions that are currently set.
.Pp
.It Li +X
make a directory or file searchable/executable by everyone if it is
already searchable/executable by anyone.
.Pp
.It Li 755
.It Li u=rwx,go=rx
.It Li u=rwx,go=u-w
make a file readable/executable by everyone and writable by the owner only.
.Pp
.It Li go=
clear all mode bits for group and others.
.Pp
.It Li g=u-w
set the group bits equal to the user bits, but clear the group write bit.
.El
.Sh SEE ALSO
.Xr chflags 1 ,
.Xr install 1 ,
.Xr chmod 2 ,
.Xr stat 2 ,
.Xr umask 2 ,
.Xr fts 3 ,
.Xr setmode 3 ,
.Xr symlink 7 ,
.Xr chown 8
.Sh STANDARDS
The
.Nm
utility is expected to be
.St -p1003.2-92
compatible with the exception of the
.Ar perm
symbol
.Dq t
which is not included in that standard.
.Sh BUGS
There's no
.Ar perm
option for the naughty bits.

View File

@@ -1,239 +0,0 @@
/* $NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1989, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
__dead static void usage(void);
struct option chmod_longopts[] = {
{ "reference", required_argument, 0,
1 },
{ NULL, 0, 0,
0 },
};
int
main(int argc, char *argv[])
{
FTS *ftsp;
FTSENT *p;
void *set;
mode_t mval;
int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
char *mode, *reference;
int (*change_mode)(const char *, mode_t);
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
Hflag = Lflag = Rflag = fflag = hflag = 0;
reference = NULL;
while ((ch = getopt_long(argc, argv, "HLPRXfghorstuwx",
chmod_longopts, NULL)) != -1)
switch (ch) {
case 1:
reference = optarg;
break;
case 'H':
Hflag = 1;
Lflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = 0;
break;
case 'P':
Hflag = Lflag = 0;
break;
case 'R':
Rflag = 1;
break;
case 'f':
fflag = 1;
break;
case 'h':
/*
* In System V the -h option causes chmod to
* change the mode of the symbolic link.
* 4.4BSD's symbolic links didn't have modes,
* so it was an undocumented noop. In NetBSD
* 1.3, lchmod(2) is introduced and this
* option does real work.
*/
hflag = 1;
break;
/*
* XXX
* "-[rwx]" are valid mode commands. If they are the entire
* argument, getopt has moved past them, so decrement optind.
* Regardless, we're done argument processing.
*/
case 'g': case 'o': case 'r': case 's':
case 't': case 'u': case 'w': case 'X': case 'x':
if (argv[optind - 1][0] == '-' &&
argv[optind - 1][1] == ch &&
argv[optind - 1][2] == '\0')
--optind;
goto done;
case '?':
default:
usage();
}
done: argv += optind;
argc -= optind;
if (argc == 0 || (argc == 1 && reference == NULL))
usage();
fts_options = FTS_PHYSICAL;
if (Rflag) {
if (hflag) {
errx(EXIT_FAILURE,
"the -R and -h options may not be specified together.");
/* NOTREACHED */
}
if (Hflag)
fts_options |= FTS_COMFOLLOW;
if (Lflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
} else if (!hflag)
fts_options |= FTS_COMFOLLOW;
if (hflag)
change_mode = lchmod;
else
change_mode = chmod;
if (reference == NULL) {
mode = *argv++;
if ((set = setmode(mode)) == NULL) {
err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
/* NOTREACHED */
}
mval = 0;
} else {
struct stat st;
if (stat(reference, &st) == -1)
err(EXIT_FAILURE, "Cannot stat `%s'", reference);
mval = st.st_mode;
set = NULL;
}
if ((ftsp = fts_open(argv, fts_options, 0)) == NULL) {
err(EXIT_FAILURE, "fts_open");
/* NOTREACHED */
}
for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
switch (p->fts_info) {
case FTS_D:
if (!Rflag)
(void)fts_set(ftsp, p, FTS_SKIP);
break;
case FTS_DNR: /* Warn, chmod, continue. */
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
break;
case FTS_DP: /* Already changed at FTS_D. */
continue;
case FTS_ERR: /* Warn, continue. */
case FTS_NS:
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
continue;
case FTS_SL: /* Ignore. */
case FTS_SLNONE:
/*
* The only symlinks that end up here are ones that
* don't point to anything and ones that we found
* doing a physical walk.
*/
if (!hflag)
continue;
/* else */
/* FALLTHROUGH */
default:
break;
}
if ((*change_mode)(p->fts_accpath,
set ? getmode(set, p->fts_statp->st_mode) : mval)
&& !fflag) {
warn("%s", p->fts_path);
rval = 1;
}
}
if (errno) {
err(EXIT_FAILURE, "fts_read");
/* NOTREACHED */
}
exit(rval);
/* NOTREACHED */
}
static void
usage(void)
{
(void)fprintf(stderr,
"Usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n"
"\t%s [-R [-H | -L | -P]] [-fh] --reference=rfile file ...\n",
getprogname(), getprogname());
exit(1);
/* NOTREACHED */
}

View File

@@ -1,7 +0,0 @@
# $NetBSD: Makefile,v 1.9 1997/07/20 22:36:37 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= cp
SRCS= cp.c utils.c
.include <bsd.prog.mk>

View File

@@ -1,254 +0,0 @@
.\" $NetBSD: cp.1,v 1.42 2012/03/25 22:37:08 wiz Exp $
.\"
.\" Copyright (c) 1989, 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)cp.1 8.3 (Berkeley) 4/18/94
.\"
.Dd March 25, 2012
.Dt CP 1
.Os
.Sh NAME
.Nm cp
.Nd copy files
.Sh SYNOPSIS
.Nm
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl f | i
.Op Fl alNpv
.Ar source_file target_file
.Nm cp
.Oo
.Fl R
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl f | i
.Op Fl alNpv
.Ar source_file ... target_directory
.Sh DESCRIPTION
In the first synopsis form, the
.Nm
utility copies the contents of the
.Ar source_file
to the
.Ar target_file .
In the second synopsis form,
the contents of each named
.Ar source_file
is copied to the destination
.Ar target_directory .
The names of the files themselves are not changed.
If
.Nm
detects an attempt to copy a file to itself, the copy will fail.
.Pp
The following options are available:
.Bl -tag -width flag
.It Fl a
Archive mode.
Same as
.Fl RpP .
.It Fl f
For each existing destination pathname, attempt to overwrite it.
If permissions do not allow copy to succeed, remove it and create a new
file, without prompting for confirmation.
(The
.Fl i
option is ignored if the
.Fl f
option is specified.)
.It Fl H
If the
.Fl R
option is specified, symbolic links on the command line are followed.
(Symbolic links encountered in the tree traversal are not followed.)
.It Fl i
Causes
.Nm
to write a prompt to the standard error output before copying a file
that would overwrite an existing file.
If the response from the standard input begins with the character
.Sq Li y ,
the file copy is attempted.
.It Fl L
If the
.Fl R
option is specified, all symbolic links are followed.
.It Fl l
Create hard links to regular files in a hierarchy instead of copying.
.It Fl N
When used with
.Fl p ,
don't copy file flags.
.It Fl P
No symbolic links are followed.
This is the default.
.It Fl p
Causes
.Nm
to preserve in the copy as many of the modification time, access time,
file flags, file mode, user ID, group ID, and extended attributes,
as allowed by permissions.
.Pp
If the user ID and group ID cannot be preserved, no error message
is displayed and the exit value is not altered.
.Pp
If the source file has its set user ID bit on and the user ID cannot
be preserved, the set user ID bit is not preserved
in the copy's permissions.
If the source file has its set group ID bit on and the group ID cannot
be preserved, the set group ID bit is not preserved
in the copy's permissions.
If the source file has both its set user ID and set group ID bits on,
and either the user ID or group ID cannot be preserved, neither
the set user ID or set group ID bits are preserved in the copy's
permissions.
.Pp
Extended attributes from all accessible namespaces are copied;
others are ignored.
If an error occurs during this copy, a message is displayed and
.Nm
skips the other extended attributes for that file.
.It Fl R
If
.Ar source_file
designates a directory,
.Nm
copies the directory and the entire subtree connected at that point.
This option also causes symbolic links to be copied, rather than
followed, and for
.Nm
to create special files rather than copying them as normal files.
Created directories have the same mode as the corresponding source
directory, unmodified by the process's umask.
.Pp
Note that
.Nm
copies hard linked files as separate files.
If you need to preserve hard links, consider using a utility like
.Xr pax 1
instead.
.It Fl v
Causes
.Nm
to be verbose, showing files as they are copied.
.El
.Pp
For each destination file that already exists, its contents are
overwritten if permissions allow, but its mode, user ID, and group
ID are unchanged.
.Pp
In the second synopsis form,
.Ar target_directory
must exist unless there is only one named
.Ar source_file
which is a directory and the
.Fl R
flag is specified.
.Pp
If the destination file does not exist, the mode of the source file is
used as modified by the file mode creation mask
.Ic ( umask ,
see
.Xr csh 1 ) .
If the source file has its set user ID bit on, that bit is removed
unless both the source file and the destination file are owned by the
same user.
If the source file has its set group ID bit on, that bit is removed
unless both the source file and the destination file are in the same
group and the user is a member of that group.
If both the set user ID and set group ID bits are set, all of the above
conditions must be fulfilled or both bits are removed.
.Pp
Appropriate permissions are required for file creation or overwriting.
.Pp
Symbolic links are always followed unless the
.Fl R
flag is set, in which case symbolic links are not followed, by default.
The
.Fl H
or
.Fl L
flags (in conjunction with the
.Fl R
flag), as well as the
.Fl P
flag cause symbolic links to be followed as described above.
The
.Fl H
and
.Fl L
options are ignored unless the
.Fl R
option is specified.
In addition, these options override each other and the
command's actions are determined by the last one specified.
.Sh EXIT STATUS
.Ex -std cp
.Sh COMPATIBILITY
Historic versions of the
.Nm
utility had a
.Fl r
option.
This implementation supports that option, however, its use is strongly
discouraged, as it does not correctly copy special files, symbolic links
or fifo's.
.Sh SEE ALSO
.Xr mv 1 ,
.Xr pax 1 ,
.Xr rcp 1 ,
.Xr umask 2 ,
.Xr fts 3 ,
.Xr symlink 7
.Sh STANDARDS
The
.Nm
utility is expected to be
.St -p1003.2
compatible.
.Pp
The
.Fl a
and
.Fl l
flags are non-standard extensions.
They are intended to be compatible with the same options which
other implementations, namely GNU coreutils and
.Fx ,
of this utility have.
.Pp
The
.Fl v
option is an extension to
.St -p1003.2 .

View File

@@ -1,548 +0,0 @@
/* $NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $ */
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* David Hitz of Auspex Systems Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1988, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
#else
__RCSID("$NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $");
#endif
#endif /* not lint */
/*
* Cp copies source files to target files.
*
* The global PATH_T structure "to" always contains the path to the
* current target file. Since fts(3) does not change directories,
* this path can be either absolute or dot-relative.
*
* The basic algorithm is to initialize "to" and use fts(3) to traverse
* the file hierarchy rooted in the argument list. A trivial case is the
* case of 'cp file1 file2'. The more interesting case is the case of
* 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
* path (relative to the root of the traversal) is appended to dir (stored
* in "to") to form the final target path.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <locale.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#define STRIP_TRAILING_SLASH(p) { \
while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
*--(p).p_end = '\0'; \
}
static char empty[] = "";
PATH_T to = { .p_end = to.p_path, .target_end = empty };
uid_t myuid;
int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag, vflag, Nflag;
mode_t myumask;
sig_atomic_t pinfo;
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
static int copy(char *[], enum op, int);
static void
progress(int sig __unused)
{
pinfo++;
}
int
main(int argc, char *argv[])
{
struct stat to_stat, tmp_stat;
enum op type;
int ch, fts_options, r, have_trailing_slash;
char *target, **src;
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
Hflag = Lflag = Pflag = Rflag = 0;
while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
Lflag = Pflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = Pflag = 0;
break;
case 'N':
Nflag = 1;
break;
case 'P':
Pflag = 1;
Hflag = Lflag = 0;
break;
case 'R':
Rflag = 1;
break;
case 'a':
Pflag = 1;
pflag = 1;
Rflag = 1;
Hflag = Lflag = 0;
break;
case 'f':
fflag = 1;
iflag = 0;
break;
case 'i':
iflag = isatty(fileno(stdin));
fflag = 0;
break;
case 'l':
lflag = 1;
break;
case 'p':
pflag = 1;
break;
case 'r':
rflag = 1;
break;
case 'v':
vflag = 1;
break;
case '?':
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (argc < 2)
usage();
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
if (rflag) {
if (Rflag) {
errx(EXIT_FAILURE,
"the -R and -r options may not be specified together.");
/* NOTREACHED */
}
if (Hflag || Lflag || Pflag) {
errx(EXIT_FAILURE,
"the -H, -L, and -P options may not be specified with the -r option.");
/* NOTREACHED */
}
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
if (Rflag) {
if (Hflag)
fts_options |= FTS_COMFOLLOW;
if (Lflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
} else if (!Pflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
}
myuid = getuid();
/* Copy the umask for explicit mode setting. */
myumask = umask(0);
(void)umask(myumask);
/* Save the target base in "to". */
target = argv[--argc];
if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
errx(EXIT_FAILURE, "%s: name too long", target);
to.p_end = to.p_path + strlen(to.p_path);
have_trailing_slash = (to.p_end[-1] == '/');
if (have_trailing_slash)
STRIP_TRAILING_SLASH(to);
to.target_end = to.p_end;
/* Set end of argument list for fts(3). */
argv[argc] = NULL;
(void)signal(SIGINFO, progress);
/*
* Cp has two distinct cases:
*
* cp [-R] source target
* cp [-R] source1 ... sourceN directory
*
* In both cases, source can be either a file or a directory.
*
* In (1), the target becomes a copy of the source. That is, if the
* source is a file, the target will be a file, and likewise for
* directories.
*
* In (2), the real target is not directory, but "directory/source".
*/
if (Pflag)
r = lstat(to.p_path, &to_stat);
else
r = stat(to.p_path, &to_stat);
if (r == -1 && errno != ENOENT) {
err(EXIT_FAILURE, "%s", to.p_path);
/* NOTREACHED */
}
if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
/*
* Case (1). Target is not a directory.
*/
if (argc > 1)
usage();
/*
* Need to detect the case:
* cp -R dir foo
* Where dir is a directory and foo does not exist, where
* we want pathname concatenations turned on but not for
* the initial mkdir().
*/
if (r == -1) {
if (rflag || (Rflag && (Lflag || Hflag)))
r = stat(*argv, &tmp_stat);
else
r = lstat(*argv, &tmp_stat);
if (r == -1) {
err(EXIT_FAILURE, "%s", *argv);
/* NOTREACHED */
}
if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
type = DIR_TO_DNE;
else
type = FILE_TO_FILE;
} else
type = FILE_TO_FILE;
if (have_trailing_slash && type == FILE_TO_FILE) {
if (r == -1)
errx(1, "directory %s does not exist",
to.p_path);
else
errx(1, "%s is not a directory", to.p_path);
}
} else {
/*
* Case (2). Target is a directory.
*/
type = FILE_TO_DIR;
}
/*
* make "cp -rp src/ dst" behave like "cp -rp src dst" not
* like "cp -rp src/. dst"
*/
for (src = argv; *src; src++) {
size_t len = strlen(*src);
while (len-- > 1 && (*src)[len] == '/')
(*src)[len] = '\0';
}
exit(copy(argv, type, fts_options));
/* NOTREACHED */
}
static int dnestack[MAXPATHLEN]; /* unlikely we'll have more nested dirs */
static ssize_t dnesp;
static void
pushdne(int dne)
{
dnestack[dnesp++] = dne;
assert(dnesp < MAXPATHLEN);
}
static int
popdne(void)
{
int rv;
rv = dnestack[--dnesp];
assert(dnesp >= 0);
return rv;
}
static int
copy(char *argv[], enum op type, int fts_options)
{
struct stat to_stat;
FTS *ftsp;
FTSENT *curr;
int base, dne, sval;
int this_failed, any_failed;
size_t nlen;
char *p, *target_mid;
base = 0; /* XXX gcc -Wuninitialized (see comment below) */
if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
err(EXIT_FAILURE, "%s", argv[0]);
/* NOTREACHED */
for (any_failed = 0; (curr = fts_read(ftsp)) != NULL;) {
this_failed = 0;
switch (curr->fts_info) {
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
warnx("%s: %s", curr->fts_path,
strerror(curr->fts_errno));
this_failed = any_failed = 1;
continue;
case FTS_DC: /* Warn, continue. */
warnx("%s: directory causes a cycle", curr->fts_path);
this_failed = any_failed = 1;
continue;
}
/*
* If we are in case (2) or (3) above, we need to append the
* source name to the target name.
*/
if (type != FILE_TO_FILE) {
if ((curr->fts_namelen +
to.target_end - to.p_path + 1) > MAXPATHLEN) {
warnx("%s/%s: name too long (not copied)",
to.p_path, curr->fts_name);
this_failed = any_failed = 1;
continue;
}
/*
* Need to remember the roots of traversals to create
* correct pathnames. If there's a directory being
* copied to a non-existent directory, e.g.
* cp -R a/dir noexist
* the resulting path name should be noexist/foo, not
* noexist/dir/foo (where foo is a file in dir), which
* is the case where the target exists.
*
* Also, check for "..". This is for correct path
* concatentation for paths ending in "..", e.g.
* cp -R .. /tmp
* Paths ending in ".." are changed to ".". This is
* tricky, but seems the easiest way to fix the problem.
*
* XXX
* Since the first level MUST be FTS_ROOTLEVEL, base
* is always initialized.
*/
if (curr->fts_level == FTS_ROOTLEVEL) {
if (type != DIR_TO_DNE) {
p = strrchr(curr->fts_path, '/');
base = (p == NULL) ? 0 :
(int)(p - curr->fts_path + 1);
if (!strcmp(&curr->fts_path[base],
".."))
base += 1;
} else
base = curr->fts_pathlen;
}
p = &curr->fts_path[base];
nlen = curr->fts_pathlen - base;
target_mid = to.target_end;
if (*p != '/' && target_mid[-1] != '/')
*target_mid++ = '/';
*target_mid = 0;
if (target_mid - to.p_path + nlen >= PATH_MAX) {
warnx("%s%s: name too long (not copied)",
to.p_path, p);
this_failed = any_failed = 1;
continue;
}
(void)strncat(target_mid, p, nlen);
to.p_end = target_mid + nlen;
*to.p_end = 0;
STRIP_TRAILING_SLASH(to);
}
sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat);
/* Not an error but need to remember it happened */
if (sval == -1)
dne = 1;
else {
if (to_stat.st_dev == curr->fts_statp->st_dev &&
to_stat.st_ino == curr->fts_statp->st_ino) {
warnx("%s and %s are identical (not copied).",
to.p_path, curr->fts_path);
this_failed = any_failed = 1;
if (S_ISDIR(curr->fts_statp->st_mode))
(void)fts_set(ftsp, curr, FTS_SKIP);
continue;
}
if (!S_ISDIR(curr->fts_statp->st_mode) &&
S_ISDIR(to_stat.st_mode)) {
warnx("cannot overwrite directory %s with non-directory %s",
to.p_path, curr->fts_path);
this_failed = any_failed = 1;
continue;
}
dne = 0;
}
switch (curr->fts_statp->st_mode & S_IFMT) {
case S_IFLNK:
/* Catch special case of a non dangling symlink */
if((fts_options & FTS_LOGICAL) ||
((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
if (copy_file(curr, dne))
this_failed = any_failed = 1;
} else {
if (copy_link(curr, !dne))
this_failed = any_failed = 1;
}
break;
case S_IFDIR:
if (!Rflag && !rflag) {
if (curr->fts_info == FTS_D)
warnx("%s is a directory (not copied).",
curr->fts_path);
(void)fts_set(ftsp, curr, FTS_SKIP);
this_failed = any_failed = 1;
break;
}
/*
* Directories get noticed twice:
* In the first pass, create it if needed.
* In the second pass, after the children have been copied, set the permissions.
*/
if (curr->fts_info == FTS_D) /* First pass */
{
/*
* If the directory doesn't exist, create the new
* one with the from file mode plus owner RWX bits,
* modified by the umask. Trade-off between being
* able to write the directory (if from directory is
* 555) and not causing a permissions race. If the
* umask blocks owner writes, we fail..
*/
pushdne(dne);
if (dne) {
if (mkdir(to.p_path,
curr->fts_statp->st_mode | S_IRWXU) < 0)
err(EXIT_FAILURE, "%s",
to.p_path);
/* NOTREACHED */
} else if (!S_ISDIR(to_stat.st_mode)) {
errno = ENOTDIR;
err(EXIT_FAILURE, "%s",
to.p_path);
/* NOTREACHED */
}
}
else if (curr->fts_info == FTS_DP) /* Second pass */
{
/*
* If not -p and directory didn't exist, set it to be
* the same as the from directory, umodified by the
* umask; arguably wrong, but it's been that way
* forever.
*/
if (pflag && setfile(curr->fts_statp, 0))
this_failed = any_failed = 1;
else if ((dne = popdne()))
(void)chmod(to.p_path,
curr->fts_statp->st_mode);
}
else
{
warnx("directory %s encountered when not expected.",
curr->fts_path);
this_failed = any_failed = 1;
break;
}
break;
case S_IFBLK:
case S_IFCHR:
if (Rflag) {
if (copy_special(curr->fts_statp, !dne))
this_failed = any_failed = 1;
} else
if (copy_file(curr, dne))
this_failed = any_failed = 1;
break;
case S_IFIFO:
if (Rflag) {
if (copy_fifo(curr->fts_statp, !dne))
this_failed = any_failed = 1;
} else
if (copy_file(curr, dne))
this_failed = any_failed = 1;
break;
default:
if (copy_file(curr, dne))
this_failed = any_failed = 1;
break;
}
if (vflag && !this_failed)
(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
}
if (errno) {
err(EXIT_FAILURE, "fts_read");
/* NOTREACHED */
}
(void)fts_close(ftsp);
return (any_failed);
}

View File

@@ -1,61 +0,0 @@
/* $NetBSD: extern.h,v 1.17 2012/01/04 15:58:37 christos Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.2 (Berkeley) 4/1/94
*/
#ifndef _EXTERN_H_
#define _EXTERN_H_
typedef struct {
char *p_end; /* pointer to NULL at end of path */
char *target_end; /* pointer to end of target base */
char p_path[MAXPATHLEN + 1]; /* pointer to the start of a path */
} PATH_T;
extern PATH_T to;
extern uid_t myuid;
extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, pflag, Nflag;
extern mode_t myumask;
extern sig_atomic_t pinfo;
#include <sys/cdefs.h>
__BEGIN_DECLS
int copy_fifo(struct stat *, int);
int copy_file(FTSENT *, int);
int copy_link(FTSENT *, int);
int copy_special(struct stat *, int);
int set_utimes(const char *, struct stat *);
int setfile(struct stat *, int);
void usage(void) __attribute__((__noreturn__));
__END_DECLS
#endif /* !_EXTERN_H_ */

View File

@@ -1,429 +0,0 @@
/* $NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
#else
__RCSID("$NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $");
#endif
#endif /* not lint */
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/extattr.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#define MMAP_MAX_SIZE (8 * 1048576)
#define MMAP_MAX_WRITE (64 * 1024)
int
set_utimes(const char *file, struct stat *fs)
{
static struct timeval tv[2];
TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
if (lutimes(file, tv)) {
warn("lutimes: %s", file);
return (1);
}
return (0);
}
struct finfo {
const char *from;
const char *to;
size_t size;
};
static void
progress(const struct finfo *fi, size_t written)
{
int pcent = (int)((100.0 * written) / fi->size);
pinfo = 0;
(void)fprintf(stderr, "%s => %s %zu/%zu bytes %d%% written\n",
fi->from, fi->to, written, fi->size, pcent);
}
int
copy_file(FTSENT *entp, int dne)
{
static char buf[MAXBSIZE];
struct stat to_stat, *fs;
int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
char *p;
size_t ptotal = 0;
if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
warn("%s", entp->fts_path);
return (1);
}
to_fd = -1;
fs = entp->fts_statp;
tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
/*
* If the file exists and we're interactive, verify with the user.
* If the file DNE, set the mode to be the from file, minus setuid
* bits, modified by the umask; arguably wrong, but it makes copying
* executables work right and it's been that way forever. (The
* other choice is 666 or'ed with the execute bits on the from file
* modified by the umask.)
*/
if (!dne) {
struct stat sb;
int sval;
if (iflag) {
(void)fprintf(stderr, "overwrite %s? ", to.p_path);
checkch = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (checkch != 'y' && checkch != 'Y') {
(void)close(from_fd);
return (0);
}
}
sval = tolnk ?
lstat(to.p_path, &sb) : stat(to.p_path, &sb);
if (sval == -1) {
warn("stat: %s", to.p_path);
(void)close(from_fd);
return (1);
}
if (!(tolnk && S_ISLNK(sb.st_mode)))
to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
} else
to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
fs->st_mode & ~(S_ISUID | S_ISGID));
if (to_fd == -1 && (fflag || tolnk)) {
/*
* attempt to remove existing destination file name and
* create a new file
*/
(void)unlink(to.p_path);
to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
fs->st_mode & ~(S_ISUID | S_ISGID));
}
if (to_fd == -1) {
warn("%s", to.p_path);
(void)close(from_fd);
return (1);
}
rval = 0;
/* if hard linking then simply close the open fds, link and return */
if (lflag) {
(void)close(from_fd);
(void)close(to_fd);
(void)unlink(to.p_path);
if (link(entp->fts_path, to.p_path)) {
warn("%s", to.p_path);
return (1);
}
return (0);
}
/* NOTREACHED */
/*
* There's no reason to do anything other than close the file
* now if it's empty, so let's not bother.
*/
if (fs->st_size > 0) {
struct finfo fi;
fi.from = entp->fts_path;
fi.to = to.p_path;
fi.size = (size_t)fs->st_size;
/*
* Mmap and write if less than 8M (the limit is so
* we don't totally trash memory on big files).
* This is really a minor hack, but it wins some CPU back.
*/
bool use_read;
use_read = true;
if (fs->st_size <= MMAP_MAX_SIZE) {
size_t fsize = (size_t)fs->st_size;
p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
from_fd, (off_t)0);
if (p != MAP_FAILED) {
size_t remainder;
use_read = false;
#if !defined(__minix)
(void) madvise(p, (size_t)fs->st_size,
MADV_SEQUENTIAL);
#endif /* !defined(__minix) */
/*
* Write out the data in small chunks to
* avoid locking the output file for a
* long time if the reading the data from
* the source is slow.
*/
remainder = fsize;
do {
ssize_t chunk;
chunk = (remainder > MMAP_MAX_WRITE) ?
MMAP_MAX_WRITE : remainder;
if (write(to_fd, &p[fsize - remainder],
chunk) != chunk) {
warn("%s", to.p_path);
rval = 1;
break;
}
remainder -= chunk;
ptotal += chunk;
if (pinfo)
progress(&fi, ptotal);
} while (remainder > 0);
if (munmap(p, fsize) < 0) {
warn("%s", entp->fts_path);
rval = 1;
}
}
}
if (use_read) {
while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
wcount = write(to_fd, buf, (size_t)rcount);
if (rcount != wcount || wcount == -1) {
warn("%s", to.p_path);
rval = 1;
break;
}
ptotal += wcount;
if (pinfo)
progress(&fi, ptotal);
}
if (rcount < 0) {
warn("%s", entp->fts_path);
rval = 1;
}
}
}
#if !defined(__minix)
if (pflag && (fcpxattr(from_fd, to_fd) != 0))
warn("%s: error copying extended attributes", to.p_path);
#endif /* !defined(__minix) */
(void)close(from_fd);
if (rval == 1) {
(void)close(to_fd);
return (1);
}
if (pflag && setfile(fs, to_fd))
rval = 1;
/*
* If the source was setuid or setgid, lose the bits unless the
* copy is owned by the same user and group.
*/
#define RETAINBITS \
(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
if (!pflag && dne
&& fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
if (fstat(to_fd, &to_stat)) {
warn("%s", to.p_path);
rval = 1;
} else if (fs->st_gid == to_stat.st_gid &&
fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
warn("%s", to.p_path);
rval = 1;
}
}
if (close(to_fd)) {
warn("%s", to.p_path);
rval = 1;
}
/* set the mod/access times now after close of the fd */
if (pflag && set_utimes(to.p_path, fs)) {
rval = 1;
}
return (rval);
}
int
copy_link(FTSENT *p, int exists)
{
int len;
char target[MAXPATHLEN];
if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
warn("readlink: %s", p->fts_path);
return (1);
}
target[len] = '\0';
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (symlink(target, to.p_path)) {
warn("symlink: %s", target);
return (1);
}
return (pflag ? setfile(p->fts_statp, 0) : 0);
}
int
copy_fifo(struct stat *from_stat, int exists)
{
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (mkfifo(to.p_path, from_stat->st_mode)) {
warn("mkfifo: %s", to.p_path);
return (1);
}
return (pflag ? setfile(from_stat, 0) : 0);
}
int
copy_special(struct stat *from_stat, int exists)
{
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
}
if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
warn("mknod: %s", to.p_path);
return (1);
}
return (pflag ? setfile(from_stat, 0) : 0);
}
/*
* Function: setfile
*
* Purpose:
* Set the owner/group/permissions for the "to" file to the information
* in the stat structure. If fd is zero, also call set_utimes() to set
* the mod/access times. If fd is non-zero, the caller must do a utimes
* itself after close(fd).
*/
int
setfile(struct stat *fs, int fd)
{
int rval, islink;
rval = 0;
islink = S_ISLNK(fs->st_mode);
fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
/*
* Changing the ownership probably won't succeed, unless we're root
* or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
* the mode; current BSD behavior is to remove all setuid bits on
* chown. If chown fails, lose setuid/setgid bits.
*/
if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
lchown(to.p_path, fs->st_uid, fs->st_gid)) {
if (errno != EPERM) {
warn("chown: %s", to.p_path);
rval = 1;
}
fs->st_mode &= ~(S_ISUID | S_ISGID);
}
if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
warn("chmod: %s", to.p_path);
rval = 1;
}
#if !defined(__minix)
if (!islink && !Nflag) {
unsigned long fflags = fs->st_flags;
/*
* XXX
* NFS doesn't support chflags; ignore errors unless
* there's reason to believe we're losing bits.
* (Note, this still won't be right if the server
* supports flags and we were trying to *remove* flags
* on a file that we copied, i.e., that we didn't create.)
*/
errno = 0;
if ((fd ? fchflags(fd, fflags) :
chflags(to.p_path, fflags)) == -1)
if (errno != EOPNOTSUPP || fs->st_flags != 0) {
warn("chflags: %s", to.p_path);
rval = 1;
}
}
#endif /* !defined(__minix) */
/* if fd is non-zero, caller must call set_utimes() after close() */
if (fd == 0 && set_utimes(to.p_path, fs))
rval = 1;
return (rval);
}
void
usage(void)
{
(void)fprintf(stderr,
"usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
" %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
getprogname(), getprogname());
exit(1);
/* NOTREACHED */
}

View File

@@ -1,76 +0,0 @@
# $NetBSD: Makefile,v 1.39 2013/07/16 17:47:43 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
#
# C Shell with process control; VM/UNIX VAX Makefile
# Bill Joy UC Berkeley; Jim Kulp IIASA, Austria
#
# To profile, put -DPROF in DFLAGS and -pg in COPTS, and recompile.
.include <bsd.own.mk>
WARNS=6
PROG= csh
DFLAGS=-DBUILTIN -DFILEC -DNLS -DSHORT_STRINGS
# - Editor history not always aligned with shell history,
# should implement internally
# - Does not handle escaped prompts.
# - Does not do completion
.ifndef SMALLPROG
DFLAGS+=-DEDIT
.endif
CPPFLAGS+=-I${.CURDIR} -I. ${DFLAGS}
SRCS= alloc.c char.c const.c csh.c dir.c dol.c err.c exec.c exp.c file.c \
func.c glob.c hist.c init.c lex.c misc.c parse.c printf.c proc.c \
sem.c set.c str.c time.c
.PATH: ${NETBSDSRCDIR}/usr.bin/printf
MLINKS= csh.1 limit.1 csh.1 alias.1 csh.1 bg.1 csh.1 dirs.1 csh.1 fg.1 \
csh.1 foreach.1 csh.1 history.1 csh.1 jobs.1 csh.1 popd.1 \
csh.1 pushd.1 csh.1 rehash.1 csh.1 repeat.1 csh.1 suspend.1 \
csh.1 stop.1 csh.1 source.1
DPSRCS+= errnum.h const.h
CLEANFILES+= errnum.h const.h
errnum.h: err.c
${_MKTARGET_CREATE}
rm -f ${.TARGET}
(\
echo '/* Do not edit this file, make creates it. */' ;\
echo '#ifndef _h_sh_errnum' ;\
echo '#define _h_sh_errnum' ;\
egrep 'ERR_' ${.ALLSRC} | egrep '^#define' ;\
echo '#endif /* _h_sh_errnum */' ;\
) > ${.TARGET}
const.c: errnum.h
const.h: const.c
${_MKTARGET_CREATE}
rm -f ${.TARGET}
echo '/* Do not edit this file, make creates it. */' > ${.TARGET}
${CC} -E ${CPPFLAGS} ${.ALLSRC} | egrep 'Char STR' | \
${TOOL_SED} -e 's/Char \([a-zA-Z0-9_]*\)\(.*\)/extern Char \1[];/' | \
sort >> ${.TARGET}
.if make(install)
SUBDIR+=USD.doc
.endif
# XXX Only GCC 4.1 problem
.if defined(HAVE_GCC) && ${HAVE_GCC} == 4 && ${MACHINE_ARCH} == "vax"
COPTS.parse.c+= -O0
.endif
COPTS.err.c = -Wno-format-nonliteral
COPTS.printf.c = -Wno-format-nonliteral
COPTS.proc.c = -Wno-format-nonliteral
.if !empty(DFLAGS:M*EDIT)
LDADD+=-ledit -lterminfo -lutil
DPADD+=${LIBEDIT} ${LIBUTIL}
.else
LDADD+=-lutil
DPADD+=${LIBUTIL}
.endif
.include <bsd.prog.mk>
.include <bsd.subdir.mk>

View File

@@ -1,12 +0,0 @@
# $NetBSD: Makefile,v 1.7 2007/10/18 18:26:31 tls Exp $
# @(#)Makefile 8.1 (Berkeley) 8/14/93
DIR= usd/04.csh
SRCS= tabs csh.1 csh.2 csh.3 csh.4 csh.ap csh.g
MACROS= -ms
paper.ps: ${SRCS}
${TOOL_SOELIM} -I${.CURDIR} ${.ALLSRC} | \
${TOOL_ROFF_PS} ${MACROS} > ${.TARGET}
.include <bsd.doc.mk>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,648 +0,0 @@
.\" $NetBSD: csh.3,v 1.5 2003/08/07 09:05:08 agc Exp $
.\"
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.3 8.1 (Berkeley) 6/8/93
.\"
.nr H1 2
.NH
Shell control structures and command scripts
.NH 2
Introduction
.PP
It is possible to place commands in files and to cause shells to be
invoked to read and execute commands from these files,
which are called
.I "shell scripts."
We here detail those features of the shell useful to the writers of such
scripts.
.NH 2
Make
.PP
It is important to first note what shell scripts are
.I not
useful for.
There is a program called
.I make
which is very useful for maintaining a group of related files
or performing sets of operations on related files.
For instance a large program consisting of one or more files
can have its dependencies described in a
.I makefile
which contains definitions of the commands used to create these
different files when changes occur.
Definitions of the means for printing listings, cleaning up the directory
in which the files reside, and installing the resultant programs
are easily, and most appropriately placed in this
.I makefile.
This format is superior and preferable to maintaining a group of shell
procedures to maintain these files.
.PP
Similarly when working on a document a
.I makefile
may be created which defines how different versions of the document
are to be created and which options of
.I nroff
or
.I troff
are appropriate.
.NH 2
Invocation and the argv variable
.PP
A
.I csh
command script may be interpreted by saying
.DS
% csh script ...
.DE
where
.I script
is the name of the file containing a group of
.I csh
commands and
`\&...' is replaced by a sequence of arguments.
The shell places these arguments in the variable
.I argv
and then begins to read commands from the script.
These parameters are then available through the same mechanisms
which are used to reference any other shell variables.
.PP
If you make the file
`script'
executable by doing
.DS
chmod 755 script
.DE
and place a shell comment at the beginning of the shell script
(i.e. begin the file with a `#' character)
then a `/bin/csh' will automatically be invoked to execute `script' when
you type
.DS
script
.DE
If the file does not begin with a `#' then the standard shell
`/bin/sh' will be used to execute it.
This allows you to convert your older shell scripts to use
.I csh
at your convenience.
.NH 2
Variable substitution
.PP
After each input line is broken into words and history substitutions
are done on it, the input line is parsed into distinct commands.
Before each command is executed a mechanism know as
.I "variable substitution"
is done on these words.
Keyed by the character `$' this substitution replaces the names
of variables by their values.
Thus
.DS
echo $argv
.DE
when placed in a command script would cause the current value of the
variable
.I argv
to be echoed to the output of the shell script.
It is an error for
.I argv
to be unset at this point.
.PP
A number of notations are provided for accessing components and attributes
of variables.
The notation
.DS
$?name
.DE
expands to `1' if name is
.I set
or to `0'
if name is not
.I set.
It is the fundamental mechanism used for checking whether particular
variables have been assigned values.
All other forms of reference to undefined variables cause errors.
.PP
The notation
.DS
$#name
.DE
expands to the number of elements in the variable
.I name.
Thus
.DS
% set argv=(a b c)
% echo $?argv
1
% echo $#argv
3
% unset argv
% echo $?argv
0
% echo $argv
Undefined variable: argv.
%
.DE
.PP
It is also possible to access the components of a variable
which has several values.
Thus
.DS
$argv[1]
.DE
gives the first component of
.I argv
or in the example above `a'.
Similarly
.DS
$argv[$#argv]
.DE
would give `c',
and
.DS
$argv[1\-2]
.DE
would give `a b'. Other notations useful in shell scripts are
.DS
$\fIn\fR
.DE
where
.I n
is an integer as a shorthand for
.DS
$argv[\fIn\fR\|]
.DE
the
.I n\|th
parameter and
.DS
$*
.DE
which is a shorthand for
.DS
$argv
.DE
The form
.DS
$$
.DE
expands to the process number of the current shell.
Since this process number is unique in the system it can
be used in generation of unique temporary file names.
The form
.DS
$<
.DE
is quite special and is replaced by the next line of input read from
the shell's standard input (not the script it is reading). This is
useful for writing shell scripts that are interactive, reading
commands from the terminal, or even writing a shell script that
acts as a filter, reading lines from its input file. Thus the sequence
.DS
echo 'yes or no?\ec'
set a=($<)
.DE
would write out the prompt `yes or no?' without a newline and then
read the answer into the variable `a'. In this case `$#a' would be
`0' if either a blank line or end-of-file (^D) was typed.
.PP
One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'
should be noted here.
The form
`$argv[\fIn\fR\|]'
will yield an error if
.I n
is not in the range
`1\-$#argv'
while `$n'
will never yield an out of range subscript error.
This is for compatibility with the way older shells handled parameters.
.PP
Another important point is that it is never an error to give a subrange
of the form `n\-'; if there are less than
.I n
components of the given variable then no words are substituted.
A range of the form `m\-n' likewise returns an empty vector without giving
an error when \fIm\fR exceeds the number of elements of the given variable,
provided the subscript \fIn\fR is in range.
.NH 2
Expressions
.PP
In order for interesting shell scripts to be constructed it
must be possible to evaluate expressions in the shell based on the
values of variables.
In fact, all the arithmetic operations of the language C are available
in the shell
with the same precedence that they have in C.
In particular, the operations `==' and `!=' compare strings
and the operators `&&' and `|\|\||' implement the boolean and/or operations.
The special operators `=~' and `!~' are similar to `==' and `!=' except
that the string on the right side can have pattern matching characters
(like *, ? or []) and the test is whether the string on the left matches
the pattern on the right.
.PP
The shell also allows file enquiries of the form
.DS
\-? filename
.DE
where `?' is replace by a number of single characters.
For instance the expression primitive
.DS
\-e filename
.DE
tell whether the file
`filename'
exists.
Other primitives test for read, write and execute access to the file,
whether it is a directory, or has non-zero length.
.PP
It is possible to test whether a command terminates normally,
by a primitive of the
form `{ command }' which returns true, i.e. `1' if the command
succeeds exiting normally with exit status 0, or `0' if the command
terminates abnormally or with exit status non-zero.
If more detailed information about the execution status of a command
is required, it can be executed and the variable `$status' examined
in the next command.
Since `$status' is set by every command, it is very transient.
It can be saved if it is inconvenient to use it only in the single
immediately following command.
.PP
For a full list of expression components available see the manual
section for the shell.
.NH 2
Sample shell script
.PP
A sample shell script which makes use of the expression mechanism
of the shell and some of its control structure follows:
.DS
% cat copyc
#
# Copyc copies those C programs in the specified list
# to the directory ~/backup if they differ from the files
# already in ~/backup
#
set noglob
foreach i ($argv)
if ($i !~ *.c) continue # not a .c file so do nothing
if (! \-r ~/backup/$i:t) then
echo $i:t not in backup... not cp\e\'ed
continue
endif
cmp \-s $i ~/backup/$i:t # to set $status
if ($status != 0) then
echo new backup of $i
cp $i ~/backup/$i:t
endif
end
.DE
.PP
This script makes use of the
.I foreach
command, which causes the shell to execute the commands between the
.I foreach
and the matching
.I end
for each of the values given between `(' and `)' with the named
variable, in this case `i' set to successive values in the list.
Within this loop we may use the command
.I break
to stop executing the loop
and
.I continue
to prematurely terminate one iteration
and begin the next.
After the
.I foreach
loop the iteration variable
(\fIi\fR in this case)
has the value at the last iteration.
.PP
We set the variable
.I noglob
here to prevent filename expansion of the members of
.I argv.
This is a good idea, in general, if the arguments to a shell script
are filenames which have already been expanded or if the arguments
may contain filename expansion metacharacters.
It is also possible to quote each use of a `$' variable expansion,
but this is harder and less reliable.
.PP
The other control construct used here is a statement of the form
.DS
\fBif\fR ( expression ) \fBthen\fR
command
...
\fBendif\fR
.DE
The placement of the keywords here is
.B not
flexible due to the current implementation of the shell.\(dg
.FS
\(dgThe following two formats are not currently acceptable to the shell:
.sp
.in +5
.nf
\fBif\fR ( expression ) # \fBWon't work!\fR
\fBthen\fR
command
...
\fBendif\fR
.fi
.in -5
.sp
and
.sp
.in +5
.nf
\fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR
.in -5
.fi
.FE
.PP
The shell does have another form of the if statement of the form
.DS
\fBif\fR ( expression ) \fBcommand\fR
.DE
which can be written
.DS
\fBif\fR ( expression ) \e
command
.DE
Here we have escaped the newline for the sake of appearance.
The command must not involve `\||\|', `&' or `;'
and must not be another control command.
The second form requires the final `\e' to
.B immediately
precede the end-of-line.
.PP
The more general
.I if
statements above also admit a sequence of
.I else\-if
pairs followed by a single
.I else
and an
.I endif,
e.g.:
.DS
\fBif\fR ( expression ) \fBthen\fR
commands
\fBelse\fR \fBif\fR (expression ) \fBthen\fR
commands
\&...
\fBelse\fR
commands
\fBendif\fR
.DE
.PP
Another important mechanism used in shell scripts is the `:' modifier.
We can use the modifier `:r' here to extract a root of a filename or
`:e' to extract the
.I extension.
Thus if the variable
.I i
has the value
`/mnt/foo.bar'
then
.sp
.in +5
.nf
% echo $i $i:r $i:e
/mnt/foo.bar /mnt/foo bar
%
.sp
.in -5
.fi
shows how the `:r' modifier strips off the trailing `.bar' and the
the `:e' modifier leaves only the `bar'.
Other modifiers will take off the last component of a pathname leaving
the head `:h' or all but the last component of a pathname leaving the
tail `:t'.
These modifiers are fully described in the
.I csh
manual pages in the User's Reference Manual.
It is also possible to use the
.I "command substitution"
mechanism described in the next major section to perform modifications
on strings to then reenter the shell's environment.
Since each usage of this mechanism involves the creation of a new process,
it is much more expensive to use than the `:' modification mechanism.\(dd
.FS
\(dd It is also important to note that
the current implementation of the shell limits the number of `:' modifiers
on a `$' substitution to 1.
Thus
.sp
.nf
.in +5
% echo $i $i:h:t
/a/b/c /a/b:t
%
.in -5
.fi
.sp
does not do what one would expect.
.FE
Finally, we note that the character `#' lexically introduces a shell
comment in shell scripts (but not from the terminal).
All subsequent characters on the input line after a `#' are discarded
by the shell.
This character can be quoted using `\'' or `\e' to place it in
an argument word.
.NH 2
Other control structures
.PP
The shell also has control structures
.I while
and
.I switch
similar to those of C.
These take the forms
.DS
\fBwhile\fR ( expression )
commands
\fBend\fR
.DE
and
.DS
\fBswitch\fR ( word )
\fBcase\fR str1:
commands
\fBbreaksw\fR
\& ...
\fBcase\fR strn:
commands
\fBbreaksw\fR
\fBdefault:\fR
commands
\fBbreaksw\fR
\fBendsw\fR
.DE
For details see the manual section for
.I csh.
C programmers should note that we use
.I breaksw
to exit from a
.I switch
while
.I break
exits a
.I while
or
.I foreach
loop.
A common mistake to make in
.I csh
scripts is to use
.I break
rather than
.I breaksw
in switches.
.PP
Finally,
.I csh
allows a
.I goto
statement, with labels looking like they do in C, i.e.:
.DS
loop:
commands
\fBgoto\fR loop
.DE
.NH 2
Supplying input to commands
.PP
Commands run from shell scripts receive by default the standard
input of the shell which is running the script.
This is different from previous shells running
under \s-2UNIX\s0. It allows shell scripts to fully participate
in pipelines, but mandates extra notation for commands which are to take
inline data.
.PP
Thus we need a metanotation for supplying inline data to commands in
shell scripts.
As an example, consider this script which runs the editor to
delete leading blanks from the lines in each argument file:
.DS
% cat deblank
# deblank \-\- remove leading blanks
foreach i ($argv)
ed \- $i << \'EOF\'
1,$s/^[ ]*//
w
q
\&\'EOF\'
end
%
.DE
The notation `<< \'EOF\''
means that the standard input for the
.I ed
command is to come from the text in the shell script file
up to the next line consisting of exactly `\'EOF\''.
The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,
causes the shell to not perform variable substitution on the
intervening lines.
In general, if any part of the word following the `<<' which the
shell uses to terminate the text to be given to the command is quoted
then these substitutions will not be performed.
In this case since we used the form `1,$' in our editor script
we needed to insure that this `$' was not variable substituted.
We could also have insured this by preceding the `$' here with a `\e',
i.e.:
.DS
1,\e$s/^[ ]*//
.DE
but quoting the `EOF' terminator is a more reliable way of achieving the
same thing.
.NH 2
Catching interrupts
.PP
If our shell script creates temporary files, we may wish to catch
interruptions of the shell script so that we can clean up
these files.
We can then do
.DS
onintr label
.DE
where
.I label
is a label in our program.
If an interrupt is received the shell will do a
`goto label'
and we can remove the temporary files and then do an
.I exit
command (which is built in to the shell)
to exit from the shell script.
If we wish to exit with a non-zero status we can do
.DS
exit(1)
.DE
e.g. to exit with status `1'.
.NH 2
What else?
.PP
There are other features of the shell useful to writers of shell
procedures.
The
.I verbose
and
.I echo
options and the related
.I \-v
and
.I \-x
command line options can be used to help trace the actions of the shell.
The
.I \-n
option causes the shell only to read commands and not to execute
them and may sometimes be of use.
.PP
One other thing to note is that
.I csh
will not execute shell scripts which do not begin with the
character `#', that is shell scripts that do not begin with a comment.
Similarly, the `/bin/sh' on your system may well defer to `csh'
to interpret shell scripts which begin with `#'.
This allows shell scripts for both shells to live in harmony.
.PP
There is also another quotation mechanism using `"' which allows
only some of the expansion mechanisms we have so far discussed to occur
on the quoted string and serves to make this string into a single word
as `\'' does.
.bp

View File

@@ -1,176 +0,0 @@
.\" $NetBSD: csh.4,v 1.4 2003/08/07 09:05:08 agc Exp $
.\"
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.4 8.1 (Berkeley) 6/8/93
.\"
.nr H1 3
.NH
Other, less commonly used, shell features
.NH 2
Loops at the terminal; variables as vectors
.PP
It is occasionally useful to use the
.I foreach
control structure at the terminal to aid in performing a number
of similar commands.
For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
system at Cory Hall,
`/bin/sh',
`/bin/nsh',
and
`/bin/csh'.
To count the number of persons using each shell one could have issued
the commands
.DS
% grep \-c csh$ /etc/passwd
27
% grep \-c nsh$ /etc/passwd
128
% grep \-c \-v sh$ /etc/passwd
430
%
.DE
Since these commands are very similar we can use
.I foreach
to do this more easily.
.DS
% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
? grep \-c $i /etc/passwd
? end
27
128
430
%
.DE
Note here that the shell prompts for
input with `? ' when reading the body of the loop.
.PP
Very useful with loops are variables which contain lists of filenames
or other words.
You can, for example, do
.DS
% set a=(\`ls\`)
% echo $a
csh.n csh.rm
% ls
csh.n
csh.rm
% echo $#a
2
%
.DE
The
.I set
command here gave the variable
.I a
a list of all the filenames in the current directory as value.
We can then iterate over these names to perform any chosen function.
.PP
The output of a command within `\`' characters is converted by
the shell to a list of words.
You can also place the `\`' quoted string within `"' characters
to take each (non-empty) line as a component of the variable;
preventing the lines from being split into words at blanks and tabs.
A modifier `:x' exists which can be used later to expand each component
of the variable into another variable splitting it into separate words
at embedded blanks and tabs.
.NH 2
Braces { ... } in argument expansion
.PP
Another form of filename expansion, alluded
to before involves the characters `{' and `}'.
These characters specify that the contained strings, separated by `,'
are to be consecutively substituted into the containing characters
and the results expanded left to right.
Thus
.DS
A{str1,str2,...strn}B
.DE
expands to
.DS
Astr1B Astr2B ... AstrnB
.DE
This expansion occurs before the other filename expansions, and may
be applied recursively (i.e. nested).
The results of each expanded string are sorted separately, left
to right order being preserved.
The resulting filenames are not required to exist if no other expansion
mechanisms are used.
This means that this mechanism can be used to generate arguments which are
not filenames, but which have common parts.
.PP
A typical use of this would be
.DS
mkdir ~/{hdrs,retrofit,csh}
.DE
to make subdirectories `hdrs', `retrofit' and `csh'
in your home directory.
This mechanism is most useful when the common prefix is longer
than in this example, i.e.
.DS
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
.DE
.NH 2
Command substitution
.PP
A command enclosed in `\`' characters is replaced, just before
filenames are expanded, by the output from that command.
Thus it is possible to do
.DS
set pwd=\`pwd\`
.DE
to save the current directory in the variable
.I pwd
or to do
.DS
ex \`grep \-l TRACE *.c\`
.DE
to run the editor
.I ex
supplying as arguments those files whose names end in `.c'
which have the string `TRACE' in them.*
.FS
*Command expansion also occurs in input redirected with `<<'
and within `"' quotations.
Refer to the shell manual section for full details.
.FE
.NH 2
Other details not covered here
.PP
In particular circumstances it may be necessary to know the exact
nature and order of different substitutions performed by the shell.
The exact meaning of certain combinations of quotations is also
occasionally important.
These are detailed fully in its manual section.
.PP
The shell has a number of command line option flags mostly of use
in writing \s-2UNIX\s0 programs,
and debugging shell scripts.
See the csh(1) manual section for a list of these options.
.bp

View File

@@ -1,93 +0,0 @@
.\" $NetBSD: csh.ap,v 1.1 2007/10/18 18:26:31 tls Exp $
.\"
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)csh.a 8.1 (Berkeley) 6/8/93
.\"
.SH
Appendix \- Special characters
.LP
The following table lists the special characters of
.I csh
and the \s-2UNIX\s0 system, giving for each the section(s) in which it
is discussed.
A number of these characters also have special meaning in expressions.
See the
.I csh
manual section
for a complete list.
.ta .75i 1.5i 2.25i
.LP
Syntactic metacharacters
.DS
; 2.4 separates commands to be executed sequentially
| 1.5 separates commands in a pipeline
( ) 2.2,3.6 brackets expressions and variable values
& 2.5 follows commands to be executed without waiting for completion
.DE
.LP
Filename metacharacters
.DS
/ 1.6 separates components of a file's pathname
\&. 1.6 separates root parts of a file name from extensions
? 1.6 expansion character matching any single character
* 1.6 expansion character matching any sequence of characters
[ ] 1.6 expansion sequence matching any single character from a set
~ 1.6 used at the beginning of a filename to indicate home directories
{ } 4.2 used to specify groups of arguments with common parts
.DE
.LP
Quotation metacharacters
.DS
\e 1.7 prevents meta-meaning of following single character
\' 1.7 prevents meta-meaning of a group of characters
" 4.3 like \', but allows variable and command expansion
.DE
.LP
Input/output metacharacters
.DS
< 1.5 indicates redirected input
> 1.3 indicates redirected output
.DE
.LP
Expansion/substitution metacharacters
.DS
$ 3.4 indicates variable substitution
! 2.3 indicates history substitution
: 3.6 precedes substitution modifiers
^ 2.3 used in special forms of history substitution
\` 4.3 indicates command substitution
.DE
.LP
Other metacharacters
.DS
# 1.3,3.6 begins scratch file names; indicates shell comments
\- 1.2 prefixes option (flag) arguments to commands
% 2.6 prefixes job name specifications
.DE
.bp

File diff suppressed because it is too large Load Diff

View File

@@ -1,32 +0,0 @@
.\" $NetBSD: tabs,v 1.4 2003/08/07 09:05:09 agc Exp $
.\"
.\" Copyright (c) 1980, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)tabs 8.1 (Berkeley) 6/8/93
.\"
.ta 5n 10n 15n 20n 25n 30n 35n 40n 45n 50n 55n 60n 65n 70n 75n 80n

View File

@@ -1,91 +0,0 @@
/* $NetBSD: alloc.c,v 1.13 2013/01/22 19:28:00 christos Exp $ */
/*-
* Copyright (c) 1983, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)alloc.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: alloc.c,v 1.13 2013/01/22 19:28:00 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include "csh.h"
#include "extern.h"
ptr_t
Malloc(size_t n)
{
ptr_t ptr;
if ((ptr = malloc(n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
ptr_t
Realloc(ptr_t p, size_t n)
{
ptr_t ptr;
if ((ptr = realloc(p, n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
ptr_t
Calloc(size_t s, size_t n)
{
ptr_t ptr;
if ((ptr = calloc(s, n)) == (ptr_t) 0) {
child++;
stderror(ERR_NOMEM);
}
return (ptr);
}
void
Free(ptr_t p)
{
if (p)
free(p);
}

View File

@@ -1,315 +0,0 @@
/* $NetBSD: char.c,v 1.10 2012/01/19 02:42:53 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)char.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: char.c,v 1.10 2012/01/19 02:42:53 christos Exp $");
#endif
#endif /* not lint */
#include "char.h"
/* on default same as original map */
unsigned short _cmap[256] = {
/* 0 nul 1 soh 2 stx 3 etx */
_CTR, _CTR, _CTR, _CTR,
/* 4 eot 5 enq 6 ack 7 bel */
_CTR, _CTR, _CTR, _CTR,
/* 8 bs 9 ht 10 nl 11 vt */
_CTR, _CTR|_SP|_META, _CTR|_NL|_META, _CTR,
/* 12 np 13 cr 14 so 15 si */
_CTR, _CTR, _CTR, _CTR,
/* 16 dle 17 dc1 18 dc2 19 dc3 */
_CTR, _CTR, _CTR, _CTR,
/* 20 dc4 21 nak 22 syn 23 etb */
_CTR, _CTR, _CTR, _CTR,
/* 24 can 25 em 26 sub 27 esc */
_CTR, _CTR, _CTR, _CTR,
/* 28 fs 29 gs 30 rs 31 us */
_CTR, _CTR, _CTR, _CTR,
/* 32 sp 33 ! 34 " 35 # */
_SP|_META, _PUN, _QF|_PUN, _META|_PUN,
/* 36 $ 37 % 38 & 39 ' */
_DOL|_PUN, _PUN, _META|_CMD|_PUN,_QF|_PUN,
/* 40 ( 41 ) 42 * 43 + */
_META|_CMD|_PUN,_META|_PUN, _GLOB|_PUN, _PUN,
/* 44 , 45 - 46 . 47 / */
_PUN, _PUN, _PUN, _PUN,
/* 48 0 49 1 50 2 51 3 */
_DIG|_XD, _DIG|_XD, _DIG|_XD, _DIG|_XD,
/* 52 4 53 5 54 6 55 7 */
_DIG|_XD, _DIG|_XD, _DIG|_XD, _DIG|_XD,
/* 56 8 57 9 58 : 59 ; */
_DIG|_XD, _DIG|_XD, _PUN, _META|_CMD|_PUN,
/* 60 < 61 = 62 > 63 ? */
_META|_PUN, _PUN, _META|_PUN, _GLOB|_PUN,
/* 64 @ 65 A 66 B 67 C */
_PUN, _LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP|_XD,
/* 68 D 69 E 70 F 71 G */
_LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP|_XD, _LET|_UP,
/* 72 H 73 I 74 J 75 K */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 76 L 77 M 78 N 79 O */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 80 P 81 Q 82 R 83 S */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 84 T 85 U 86 V 87 W */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 88 X 89 Y 90 Z 91 [ */
_LET|_UP, _LET|_UP, _LET|_UP, _GLOB|_PUN,
/* 92 \ 93 ] 94 ^ 95 _ */
_ESC|_PUN, _PUN, _PUN, _PUN,
/* 96 ` 97 a 98 b 99 c */
_QB|_GLOB|_META|_PUN, _LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW|_XD,
/* 100 d 101 e 102 f 103 g */
_LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW|_XD, _LET|_LOW,
/* 104 h 105 i 106 j 107 k */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 108 l 109 m 110 n 111 o */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 112 p 113 q 114 r 115 s */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 116 t 117 u 118 v 119 w */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 120 x 121 y 122 z 123 { */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _GLOB|_PUN,
/* 124 | 125 } 126 ~ 127 del */
_META|_CMD|_PUN,_PUN, _PUN, _CTR,
#ifdef SHORT_STRINGS
/****************************************************************/
/* 128 - 255 The below is supposedly ISO 8859/1 */
/****************************************************************/
/* 128 (undef) 129 (undef) 130 (undef) 131 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 132 (undef) 133 (undef) 134 (undef) 135 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 136 (undef) 137 (undef) 138 (undef) 139 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 140 (undef) 141 (undef) 142 (undef) 143 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 144 (undef) 145 (undef) 146 (undef) 147 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 148 (undef) 149 (undef) 150 (undef) 151 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 152 (undef) 153 (undef) 154 (undef) 155 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 156 (undef) 157 (undef) 158 (undef) 159 (undef) */
_CTR, _CTR, _CTR, _CTR,
/* 160 nobreakspace 161 exclamdown 162 cent 163 sterling */
_PUN, /* XXX */ _PUN, _PUN, _PUN,
/* 164 currency 165 yen 166 brokenbar 167 section */
_PUN, _PUN, _PUN, _PUN,
/* 168 diaeresis 169 copyright 170 ordfeminine 171 guillemotleft*/
_PUN, _PUN, _PUN, _PUN,
/* 172 notsign 173 hyphen 174 registered 175 macron */
_PUN, _PUN, _PUN, _PUN,
/* 176 degree 177 plusminus 178 twosuperior 179 threesuperior*/
_PUN, _PUN, _PUN, _PUN,
/* 180 acute 181 mu 182 paragraph 183 periodcentered*/
_PUN, _PUN, /*XXX*/ _PUN, _PUN,
/* 184 cedilla 185 onesuperior 186 masculine 187 guillemotright*/
_PUN, _PUN, _PUN, _PUN,
/* 188 onequarter 189 onehalf 190 threequarters 191 questiondown*/
_PUN, _PUN, _PUN, _PUN,
/* 192 Agrave 193 Aacute 194 Acircumflex 195 Atilde */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 196 Adiaeresis 197 Aring 198 AE 199 Ccedilla */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 200 Egrave 201 Eacute 202 Ecircumflex 203 Ediaeresis */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 204 Igrave 205 Iacute 206 Icircumflex 207 Idiaeresis */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 208 ETH 209 Ntilde 210 Ograve 211 Oacute */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 212 Ocircumflex 213 Otilde 214 Odiaeresis 215 multiply */
_LET|_UP, _LET|_UP, _LET|_UP, _PUN,
/* 216 Ooblique 217 Ugrave 218 Uacute 219 Ucircumflex */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_UP,
/* 220 Udiaeresis 221 Yacute 222 THORN 223 ssharp */
_LET|_UP, _LET|_UP, _LET|_UP, _LET|_LOW,
/* 224 agrave 225 aacute 226 acircumflex 227 atilde */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 228 adiaeresis 229 aring 230 ae 231 ccedilla */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 232 egrave 233 eacute 234 ecircumflex 235 ediaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 236 igrave 237 iacute 238 icircumflex 239 idiaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 240 eth 241 ntilde 242 ograve 243 oacute */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 244 ocircumflex 245 otilde 246 odiaeresis 247 division */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _PUN,
/* 248 oslash 249 ugrave 250 uacute 251 ucircumflex */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
/* 252 udiaeresis 253 yacute 254 thorn 255 ydiaeresis */
_LET|_LOW, _LET|_LOW, _LET|_LOW, _LET|_LOW,
#endif /* SHORT_STRINGS */
};
#ifndef NLS
/* _cmap_lower, _cmap_upper for ISO 8859/1 */
unsigned char _cmap_lower[256] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0327,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
};
unsigned char _cmap_upper[256] = {
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0367,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0377,
};
#endif /* NLS */

View File

@@ -1,100 +0,0 @@
/* $NetBSD: char.h,v 1.9 2012/01/19 02:42:53 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)char.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _CHAR_H_
#define _CHAR_H_
#include <ctype.h>
extern unsigned short _cmap[];
#ifndef NLS
extern unsigned char _cmap_lower[], _cmap_upper[];
#endif
#define _QF 0x0001 /* '" (Forward quotes) */
#define _QB 0x0002 /* ` (Backquote) */
#define _SP 0x0004 /* space and tab */
#define _NL 0x0008 /* \n */
#define _META 0x0010 /* lex meta characters, sp #'`";&<>()|\t\n */
#define _GLOB 0x0020 /* glob characters, *?{[` */
#define _ESC 0x0040 /* \ */
#define _DOL 0x0080 /* $ */
#define _DIG 0x0100 /* 0-9 */
#define _LET 0x0200 /* a-z, A-Z, _ */
#define _UP 0x0400 /* A-Z */
#define _LOW 0x0800 /* a-z */
#define _XD 0x1000 /* 0-9, a-f, A-F */
#define _CMD 0x2000 /* lex end of command chars, ;&(|` */
#define _CTR 0x4000 /* control */
#define _PUN 0x8000 /* punctuation */
#define cmap(c, bits) \
(((c) & QUOTE) ? 0 : (_cmap[(unsigned char)(c)] & (bits)))
#define isglob(c) cmap(c, _GLOB)
#define isspc(c) cmap(c, _SP)
#define ismeta(c) cmap(c, _META)
#define iscmdmeta(c) cmap(c, _CMD)
#define letter(c) (((c) & QUOTE) ? 0 : \
(isalpha((unsigned char) (c)) || (c) == '_'))
#define alnum(c) (((c) & QUOTE) ? 0 : \
(isalnum((unsigned char) (c)) || (c) == '_'))
#ifdef NLS
#define Isspace(c) (((c) & QUOTE) ? 0 : isspace((unsigned char) (c)))
#define Isdigit(c) (((c) & QUOTE) ? 0 : isdigit((unsigned char) (c)))
#define Isalpha(c) (((c) & QUOTE) ? 0 : isalpha((unsigned char) (c)))
#define Islower(c) (((c) & QUOTE) ? 0 : islower((unsigned char) (c)))
#define Isupper(c) (((c) & QUOTE) ? 0 : isupper((unsigned char) (c)))
#define Tolower(c) (((c) & QUOTE) ? 0 : tolower((unsigned char) (c)))
#define Toupper(c) (((c) & QUOTE) ? 0 : toupper((unsigned char) (c)))
#define Isxdigit(c) (((c) & QUOTE) ? 0 : isxdigit((unsigned char) (c)))
#define Isalnum(c) (((c) & QUOTE) ? 0 : isalnum((unsigned char) (c)))
#define Iscntrl(c) (((c) & QUOTE) ? 0 : iscntrl((unsigned char) (c)))
#define Isprint(c) (((c) & QUOTE) ? 0 : isprint((unsigned char) (c)))
#else
#define Isspace(c) cmap(c, _SP|_NL)
#define Isdigit(c) cmap(c, _DIG)
#define Isalpha(c) (cmap(c,_LET) && !(((c) & META) && AsciiOnly))
#define Islower(c) (cmap(c,_LOW) && !(((c) & META) && AsciiOnly))
#define Isupper(c) (cmap(c, _UP) && !(((c) & META) && AsciiOnly))
#define Tolower(c) (_cmap_lower[(unsigned char)(c)])
#define Toupper(c) (_cmap_upper[(unsigned char)(c)])
#define Isxdigit(c) cmap(c, _XD)
#define Isalnum(c) (cmap(c, _DIG|_LET) && !(((c) & META) && AsciiOnly))
#define Iscntrl(c) (cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
#define Isprint(c) (!cmap(c,_CTR) && !(((c) & META) && AsciiOnly))
#endif
#endif /* !_CHAR_H_ */

View File

@@ -1,164 +0,0 @@
/* $NetBSD: const.c,v 1.10 2013/01/22 20:35:29 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)const.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: const.c,v 1.10 2013/01/22 20:35:29 christos Exp $");
#endif
#endif /* not lint */
/*
* tc.const.c: String constants for csh.
*/
#include "csh.h"
Char STR0[] = { '0', '\0' };
Char STR1[] = { '1', '\0' };
Char STRHOME[] = { 'H', 'O', 'M', 'E', '\0' };
Char STRLANG[] = { 'L', 'A', 'N', 'G', '\0' };
Char STRLC_CTYPE[] = { 'L', 'C', '_', 'C', 'T', 'Y', 'P', 'E' ,'\0' };
Char STRLOGNAME[] = { 'L', 'O', 'G', 'N', 'A', 'M', 'E', '\0' };
Char STRLbrace[] = { '{', '\0' };
Char STRLparen[] = { '(', '\0' };
Char STRLparensp[] = { '(', ' ', '\0' };
Char STRNULL[] = { '\0' };
Char STRPATH[] = { 'P', 'A', 'T', 'H', '\0' };
Char STRPWD[] = { 'P', 'W', 'D', '\0' };
Char STRQNULL[] = { '\0' | QUOTE, '\0' };
Char STRRbrace[] = { '}', '\0' };
Char STRspRparen[] = { ' ', ')', '\0' };
Char STRTERM[] = { 'T', 'E', 'R', 'M', '\0' };
Char STRUSER[] = { 'U', 'S', 'E', 'R', '\0' };
Char STRalias[] = { 'a', 'l', 'i', 'a', 's', '\0' };
Char STRand[] = { '&', '\0' };
Char STRand2[] = { '&', '&', '\0' };
Char STRaout[] = { 'a', '.', 'o', 'u', 't', '\0' };
Char STRargv[] = { 'a', 'r', 'g', 'v', '\0' };
Char STRbang[] = { '!', '\0' };
Char STRcaret[] = { '^', '\0' };
Char STRcdpath[] = { 'c', 'd', 'p', 'a', 't', 'h', '\0' };
Char STRcent2[] = { '%', '%', '\0' };
Char STRcenthash[] = { '%', '#', '\0' };
Char STRcentplus[] = { '%', '+', '\0' };
Char STRcentminus[] = { '%', '-', '\0' };
Char STRchase_symlinks[] = { 'c', 'h', 'a', 's', 'e', '_', 's', 'y', 'm', 'l',
'i', 'n', 'k', 's', '\0' };
Char STRchild[] = { 'c', 'h', 'i', 'l', 'd', '\0' };
Char STRcolon[] = { ':', '\0' };
Char STRcwd[] = { 'c', 'w', 'd', '\0' };
Char STRdefault[] = { 'd', 'e', 'f', 'a', 'u', 'l', 't', '\0' };
Char STRdot[] = { '.', '\0' };
Char STRdotdotsl[] = { '.', '.', '/', '\0' };
Char STRdotsl[] = { '.', '/', '\0' };
Char STRecho[] = { 'e', 'c', 'h', 'o', '\0' };
Char STRedit[] = { 'e', 'd', 'i', 't', '\0' };
Char STRequal[] = { '=', '\0' };
Char STRfakecom[] = { '{', ' ', '.', '.', '.', ' ', '}', '\0' };
Char STRfakecom1[] = { '`', ' ', '.', '.', '.', ' ', '`', '\0' };
Char STRfignore[] = { 'f', 'i', 'g', 'n', 'o', 'r', 'e', '\0' };
#ifdef FILEC
Char STRfilec[] = { 'f', 'i', 'l', 'e', 'c', '\0' };
#endif /* FILEC */
Char STRhistchars[] = { 'h', 'i', 's', 't', 'c', 'h', 'a', 'r', 's', '\0' };
Char STRtildothist[] = { '~', '/', '.', 'h', 'i', 's', 't', 'o', 'r',
'y', '\0' };
Char STRhistfile[] = { 'h', 'i', 's', 't', 'f', 'i', 'l', 'e', '\0' };
Char STRhistory[] = { 'h', 'i', 's', 't', 'o', 'r', 'y', '\0' };
Char STRhome[] = { 'h', 'o', 'm', 'e', '\0' };
Char STRignore_symlinks[] = { 'i', 'g', 'n', 'o', 'r', 'e', '_', 's', 'y', 'm',
'l', 'i', 'n', 'k', 's', '\0' };
Char STRignoreeof[] = { 'i', 'g', 'n', 'o', 'r', 'e', 'e', 'o', 'f', '\0' };
Char STRjobs[] = { 'j', 'o', 'b', 's', '\0' };
Char STRlistjobs[] = { 'l', 'i', 's', 't', 'j', 'o', 'b', 's', '\0' };
Char STRlogout[] = { 'l', 'o', 'g', 'o', 'u', 't', '\0' };
Char STRlong[] = { 'l', 'o', 'n', 'g', '\0' };
Char STRmail[] = { 'm', 'a', 'i', 'l', '\0' };
Char STRmh[] = { '-', 'h', '\0' };
Char STRminus[] = { '-', '\0' };
Char STRml[] = { '-', 'l', '\0' };
Char STRmn[] = { '-', 'n', '\0' };
Char STRmquestion[] = { '?' | QUOTE, ' ', '\0' };
Char STRnice[] = { 'n', 'i', 'c', 'e', '\0' };
Char STRnoambiguous[] = { 'n', 'o', 'a', 'm', 'b', 'i', 'g', 'u', 'o', 'u',
's', '\0' };
Char STRnobeep[] = { 'n', 'o', 'b', 'e', 'e', 'p', '\0' };
Char STRnoclobber[] = { 'n', 'o', 'c', 'l', 'o', 'b', 'b', 'e', 'r', '\0' };
Char STRnoglob[] = { 'n', 'o', 'g', 'l', 'o', 'b', '\0' };
Char STRnohup[] = { 'n', 'o', 'h', 'u', 'p', '\0' };
Char STRnonomatch[] = { 'n', 'o', 'n', 'o', 'm', 'a', 't', 'c', 'h', '\0' };
Char STRnormal[] = { 'n', 'o', 'r', 'm', 'a', 'l', '\0' };
Char STRnotify[] = { 'n', 'o', 't', 'i', 'f', 'y', '\0' };
Char STRor[] = { '|', '\0' };
Char STRor2[] = { '|', '|', '\0' };
Char STRpath[] = { 'p', 'a', 't', 'h', '\0' };
Char STRprintexitvalue[] = { 'p', 'r', 'i', 'n', 't', 'e', 'x', 'i', 't', 'v',
'a', 'l', 'u', 'e', '\0' };
Char STRprompt[] = { 'p', 'r', 'o', 'm', 'p', 't', '\0' };
Char STRprompt2[] = { 'p', 'r', 'o', 'm', 'p', 't', '2', '\0' };
Char STRpushdsilent[] = { 'p', 'u', 's', 'h', 'd', 's', 'i', 'l', 'e', 'n',
't', '\0' };
Char STRret[] = { '\n', '\0' };
Char STRsavehist[] = { 's', 'a', 'v', 'e', 'h', 'i', 's', 't', '\0' };
Char STRsemisp[] = { ';', ' ', '\0' };
Char STRshell[] = { 's', 'h', 'e', 'l', 'l', '\0' };
Char STRslash[] = { '/', '\0' };
Char STRsldotcshrc[] = { '/', '.', 'c', 's', 'h', 'r', 'c', '\0' };
Char STRsldotlogin[] = { '/', '.', 'l', 'o', 'g', 'i', 'n', '\0' };
Char STRsldthist[] = { '/', '.', 'h', 'i', 's', 't', 'o', 'r', 'y', '\0' };
Char STRsldtlogout[] = { '/', '.', 'l', 'o', 'g', 'o', 'u', 't', '\0' };
Char STRsource[] = { 's', 'o', 'u', 'r', 'c', 'e', '\0' };
Char STRsp3dots[] = { ' ', '.', '.', '.', '\0' };
Char STRspLarrow2sp[] = { ' ', '<', '<', ' ', '\0' };
Char STRspLarrowsp[] = { ' ', '<', ' ', '\0' };
Char STRspRarrow[] = { ' ', '>', '\0' };
Char STRspRarrow2[] = { ' ', '>', '>', '\0' };
Char STRRparen[] = { ')', '\0' };
Char STRspace[] = { ' ', '\0' };
Char STRspand2sp[] = { ' ', '&', '&', ' ', '\0' };
Char STRspor2sp[] = { ' ', '|', '|', ' ', '\0' };
Char STRsporsp[] = { ' ', '|', ' ', '\0' };
Char STRstar[] = { '*', '\0' };
Char STRstatus[] = { 's', 't', 'a', 't', 'u', 's', '\0' };
Char STRsymcent[] = { '%', ' ', '\0' };
Char STRsymhash[] = { '#', ' ', '\0' };
Char STRterm[] = { 't', 'e', 'r', 'm', '\0' };
Char STRthen[] = { 't', 'h', 'e', 'n', '\0' };
Char STRtilde[] = { '~', '\0' };
Char STRtime[] = { 't', 'i', 'm', 'e', '\0' };
Char STRtmpsh[] = { '/', 't', 'm', 'p', '/', 's', 'h', '\0' };
Char STRunalias[] = { 'u', 'n', 'a', 'l', 'i', 'a', 's', '\0' };
Char STRuser[] = { 'u', 's', 'e', 'r', '\0' };
Char STRverbose[] = { 'v', 'e', 'r', 'b', 'o', 's', 'e', '\0' };
Char STRwordchars[] = { 'w', 'o', 'r', 'd', 'c', 'h', 'a', 'r', 's', '\0' };

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,559 +0,0 @@
/* $NetBSD: csh.h,v 1.26 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)csh.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _CSH_H_
#define _CSH_H_
/*
* Fundamental definitions which may vary from system to system.
*
* BUFSIZE The i/o buffering size; also limits word size
* MAILINTVL How often to mailcheck; more often is more expensive
*/
#ifndef BUFSIZE
#define BUFSIZE 4096 /* default buffer size */
#endif /* BUFSIZE */
#define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
#define MAILINTVL 600 /* 10 minutes */
/*
* The shell moves std in/out/diag and the old std input away from units
* 0, 1, and 2 so that it is easy to set up these standards for invoked
* commands.
*/
#define FSHTTY 15 /* /dev/tty when manip pgrps */
#define FSHIN 16 /* Preferred desc for shell input */
#define FSHOUT 17 /* ... shell output */
#define FSHERR 18 /* ... shell diagnostics */
#define FOLDSTD 19 /* ... old std input */
#ifdef PROF
#define xexit(n) done(n)
#endif
#ifdef SHORT_STRINGS
typedef short Char;
#define SAVE(a) (Strsave(str2short(a)))
#else
typedef char Char;
#define SAVE(a) (strsave(a))
#endif
/*
* Make sure a variable is not stored in a register by taking its address
* This is used where variables might be clobbered by longjmp.
*/
#define UNREGISTER(a) (void) &a
typedef void *ioctl_t; /* Third arg of ioctl */
typedef void *ptr_t;
#include "const.h"
#include "char.h"
#include "errnum.h"
#define xmalloc(i) Malloc(i)
#define xrealloc(p, i) Realloc(p, i)
#define xcalloc(n, s) Calloc(n, s)
#define xfree(p) Free(p)
#include <stdio.h>
FILE *cshin, *cshout, *csherr;
#define isdir(d) (S_ISDIR(d.st_mode))
#define eq(a, b) (Strcmp(a, b) == 0)
/* globone() flags */
#define G_ERROR 0 /* default action: error if multiple words */
#define G_IGNORE 1 /* ignore the rest of the words */
#define G_APPEND 2 /* make a sentence by cat'ing the words */
/*
* Global flags
*/
int child; /* Child shell ... errors cause exit */
int chkstop; /* Warned of stopped jobs... allow exit */
int didfds; /* Have setup i/o fd's for child */
int doneinp; /* EOF indicator after reset from readc */
int exiterr; /* Exit if error or non-zero exit status */
int haderr; /* Reset was because of an error */
int havhash; /* path hashing is available */
int intact; /* We are interactive... therefore prompt */
int intty; /* Input is a tty */
int justpr; /* Just print because of :p hist mod */
int loginsh; /* We are a loginsh -> .login/.logout */
int neednote; /* Need to pnotify() */
int noexec; /* Don't execute, just syntax check */
int pjobs; /* want to print jobs if interrupted */
int setintr; /* Set interrupts on/off -> Wait intr... */
int timflg; /* Time the next waited for command */
#ifdef FILEC
extern int filec; /* doing filename expansion */
#endif
/*
* Global i/o info
*/
Char *arginp; /* Argument input for sh -c and internal `xx` */
Char *ffile; /* Name of shell file for $0 */
int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
extern char *seterr; /* Error message from scanner/parser */
Char *shtemp; /* Temp name for << shell files in /tmp */
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
struct timespec time0; /* Time at which the shell started */
struct rusage ru0;
/*
* Miscellany
*/
time_t chktim; /* Time mail last checked */
Char *doldol; /* Character pid for $$ */
pid_t backpid; /* Pid of the last background process */
gid_t egid, gid; /* Invokers gid */
uid_t euid, uid; /* Invokers uid */
int shpgrp; /* Pgrp of shell */
int tpgrp; /* Terminal process group */
/* If tpgrp is -1, leave tty alone! */
int opgrp; /* Initial pgrp and tty pgrp */
/*
* To be able to redirect i/o for builtins easily, the shell moves the i/o
* descriptors it uses away from 0,1,2.
* Ideally these should be in units which are closed across exec's
* (this saves work) but for version 6, this is not usually possible.
* The desired initial values for these descriptors are F{SHIN,...}.
*/
int SHIN; /* Current shell input (script) */
int SHOUT; /* Shell output */
int SHERR; /* Diagnostic output... shell errs go here */
int OLDSTD; /* Old standard input (def for cmds) */
/*
* Error control
*
* Errors in scanning and parsing set up an error message to be printed
* at the end and complete. Other errors always cause a reset.
* Because of source commands and .cshrc we need nested error catches.
*/
#include <setjmp.h>
jmp_buf reslab;
#define setexit() (setjmp(reslab))
#define reset() longjmp(reslab, 1)
/* Should use structure assignment here */
#define getexit(a) (void)memcpy((a), reslab, sizeof reslab)
#define resexit(a) (void)memcpy(reslab, (a), sizeof reslab)
Char *gointr; /* Label for an onintr transfer */
#include <signal.h>
sig_t parintr; /* Parents interrupt catch */
sig_t parterm; /* Parents terminate catch */
/*
* Lexical definitions.
*
* All lexical space is allocated dynamically.
* The eighth/sixteenth bit of characters is used to prevent recognition,
* and eventually stripped.
*/
#define META 0x80
#define ASCII 0x7f
#ifdef SHORT_STRINGS
#define CHAR ((Char)0xff)
#define QUOTE ((Char)0x8000) /* 16nth char bit used for 'ing */
#define TRIM ((Char)0x7fff) /* Mask to strip quote bit */
#else
#define CHAR ((Char)0x7f)
#define QUOTE ((Char)0x80) /* Eighth char bit used for 'ing */
#define TRIM ((Char)0x7f) /* Mask to strip quote bit */
#endif
int AsciiOnly; /* If set only 7 bits is expected in characters */
/*
* Each level of input has a buffered input structure.
* There are one or more blocks of buffered input for each level,
* exactly one if the input is seekable and tell is available.
* In other cases, the shell buffers enough blocks to keep all loops
* in the buffer.
*/
struct Bin {
off_t Bfseekp; /* Seek pointer */
off_t Bfbobp; /* Seekp of beginning of buffers */
off_t Bfeobp; /* Seekp of end of buffers */
int Bfblocks; /* Number of buffer blocks */
Char **Bfbuf; /* The array of buffer blocks */
} B;
/*
* This structure allows us to seek inside aliases
*/
struct Ain {
int type;
#define I_SEEK -1 /* Invalid seek */
#define A_SEEK 0 /* Alias seek */
#define F_SEEK 1 /* File seek */
#define E_SEEK 2 /* Eval seek */
union {
off_t _f_seek;
Char* _c_seek;
} fc;
#define f_seek fc._f_seek
#define c_seek fc._c_seek
Char **a_seek;
} ;
extern int aret; /* What was the last character returned */
#define SEEKEQ(a, b) ((a)->type == (b)->type && \
(a)->f_seek == (b)->f_seek && \
(a)->a_seek == (b)->a_seek)
#define fseekp B.Bfseekp
#define fbobp B.Bfbobp
#define feobp B.Bfeobp
#define fblocks B.Bfblocks
#define fbuf B.Bfbuf
/*
* The shell finds commands in loops by reseeking the input
* For whiles, in particular, it reseeks to the beginning of the
* line the while was on; hence the while placement restrictions.
*/
struct Ain lineloc;
int cantell; /* Is current source tellable ? */
/*
* Input lines are parsed into doubly linked circular
* lists of words of the following form.
*/
struct wordent {
Char *word;
struct wordent *prev;
struct wordent *next;
};
/*
* During word building, both in the initial lexical phase and
* when expanding $ variable substitutions, expansion by `!' and `$'
* must be inhibited when reading ahead in routines which are themselves
* processing `!' and `$' expansion or after characters such as `\' or in
* quotations. The following flags are passed to the getC routines
* telling them which of these substitutions are appropriate for the
* next character to be returned.
*/
#define DODOL 1
#define DOEXCL 2
#define DOALL DODOL|DOEXCL
/*
* Labuf implements a general buffer for lookahead during lexical operations.
* Text which is to be placed in the input stream can be stuck here.
* We stick parsed ahead $ constructs during initial input,
* process id's from `$$', and modified variable values (from qualifiers
* during expansion in sh.dol.c) here.
*/
Char *lap;
/*
* Parser structure
*
* Each command is parsed to a tree of command structures and
* flags are set bottom up during this process, to be propagated down
* as needed during the semantics/execution pass (sh.sem.c).
*/
struct command {
int t_dtyp; /* Type of node */
#define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
#define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
#define NODE_PIPE 3 /* t_dlef | t_drit */
#define NODE_LIST 4 /* t_dlef ; t_drit */
#define NODE_OR 5 /* t_dlef || t_drit */
#define NODE_AND 6 /* t_dlef && t_drit */
int t_dflg; /* Flags, e.g. F_AMPERSAND|... */
#define F_SAVE (F_NICE|F_TIME|F_NOHUP) /* save these when re-doing */
#define F_AMPERSAND (1<<0) /* executes in background */
#define F_APPEND (1<<1) /* output is redirected >> */
#define F_PIPEIN (1<<2) /* input is a pipe */
#define F_PIPEOUT (1<<3) /* output is a pipe */
#define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
#define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
/* spare */
#define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
#define F_OVERWRITE (1<<8) /* output was ! */
#define F_READ (1<<9) /* input redirection is << */
#define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
#define F_NICE (1<<11) /* t_nice is meaningful */
#define F_NOHUP (1<<12) /* nohup this command */
#define F_TIME (1<<13) /* time this command */
union {
Char *T_dlef; /* Input redirect word */
struct command *T_dcar; /* Left part of list/pipe */
} L;
union {
Char *T_drit; /* Output redirect word */
struct command *T_dcdr; /* Right part of list/pipe */
} R;
#define t_dlef L.T_dlef
#define t_dcar L.T_dcar
#define t_drit R.T_drit
#define t_dcdr R.T_dcdr
Char **t_dcom; /* Command/argument vector */
struct command *t_dspr; /* Pointer to ()'d subtree */
int t_nice;
};
/*
* These are declared here because they want to be
* initialized in sh.init.c (to allow them to be made readonly)
*/
extern struct biltins {
const char *bname;
void (*bfunct)(Char **, struct command *);
short minargs, maxargs;
} bfunc[];
extern int nbfunc;
extern int nsrchn;
extern struct srch {
const char *s_name;
short s_value;
} srchn[];
/*
* The keywords for the parser
*/
#define T_BREAK 0
#define T_BRKSW 1
#define T_CASE 2
#define T_DEFAULT 3
#define T_ELSE 4
#define T_END 5
#define T_ENDIF 6
#define T_ENDSW 7
#define T_EXIT 8
#define T_FOREACH 9
#define T_GOTO 10
#define T_IF 11
#define T_LABEL 12
#define T_LET 13
#define T_SET 14
#define T_SWITCH 15
#define T_TEST 16
#define T_THEN 17
#define T_WHILE 18
/*
* Structure defining the existing while/foreach loops at this
* source level. Loops are implemented by seeking back in the
* input. For foreach (fe), the word list is attached here.
*/
struct whyle {
struct Ain w_start; /* Point to restart loop */
struct Ain w_end; /* End of loop (0 if unknown) */
Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
Char *w_fename; /* Name for fe */
struct whyle *w_next; /* Next (more outer) loop */
} *whyles;
/*
* Variable structure
*
* Aliases and variables are stored in AVL balanced binary trees.
*/
struct varent {
Char **vec; /* Array of words which is the value */
Char *v_name; /* Name of variable/alias */
struct varent *v_link[3]; /* The links, see below */
int v_bal; /* Balance factor */
} shvhed, aliases;
#define v_left v_link[0]
#define v_right v_link[1]
#define v_parent v_link[2]
#define adrof(v) adrof1(v, &shvhed)
#define value(v) value1(v, &shvhed)
/*
* The following are for interfacing redo substitution in
* aliases to the lexical routines.
*/
struct wordent *alhistp; /* Argument list (first) */
struct wordent *alhistt; /* Node after last in arg list */
extern Char **alvec, *alvecp; /* The (remnants of) alias vector */
/*
* Filename/command name expansion variables
*/
int gflag; /* After tglob -> is globbing needed? */
#define MAXVARLEN 30 /* Maximum number of char in a variable name */
/*
* Variables for filename expansion
*/
extern long gargc; /* Number args in gargv */
extern Char **gargv; /* Pointer to the (stack) arglist */
/*
* Variables for command expansion.
*/
extern Char **pargv; /* Pointer to the argv list space */
extern long pargc; /* Count of arguments in pargv */
long pnleft; /* Number of chars left in pargs */
Char *pargs; /* Pointer to start current word */
Char *pargcp; /* Current index into pargs */
/*
* History list
*
* Each history list entry contains an embedded wordlist
* from the scanner, a number for the event, and a reference count
* to aid in discarding old entries.
*
* Essentially "invisible" entries are put on the history list
* when history substitution includes modifiers, and thrown away
* at the next discarding since their event numbers are very negative.
*/
struct Hist {
struct wordent Hlex;
int Hnum;
int Href;
struct Hist *Hnext;
} Histlist;
struct wordent paraml; /* Current lexical word list */
int eventno; /* Next events number */
int lastev; /* Last event reference (default) */
Char HIST; /* history invocation character */
Char HISTSUB; /* auto-substitute character */
/*
* strings.h:
*/
#ifndef SHORT_STRINGS
#define Strchr(a, b) strchr(a, b)
#define Strrchr(a, b) strrchr(a, b)
#define Strcat(a, b) strcat(a, b)
#define Strncat(a, b, c) strncat(a, b, c)
#define Strcpy(a, b) strcpy(a, b)
#define Strncpy(a, b, c) strncpy(a, b, c)
#define Strlen(a) strlen(a)
#define Strcmp(a, b) strcmp(a, b)
#define Strncmp(a, b, c) strncmp(a, b, c)
#define Strspl(a, b) strspl(a, b)
#define Strsave(a) strsave(a)
#define Strend(a) strend(a)
#define Strstr(a, b) strstr(a, b)
#define str2short(a) (a)
#define blk2short(a) saveblk(a)
#define short2blk(a) saveblk(a)
#define short2str(a) strip(a)
#else
#define Strchr(a, b) s_strchr(a, b)
#define Strrchr(a, b) s_strrchr(a, b)
#define Strcat(a, b) s_strcat(a, b)
#define Strncat(a, b, c) s_strncat(a, b, c)
#define Strcpy(a, b) s_strcpy(a, b)
#define Strncpy(a, b, c) s_strncpy(a, b, c)
#define Strlen(a) s_strlen(a)
#define Strcmp(a, b) s_strcmp(a, b)
#define Strncmp(a, b, c) s_strncmp(a, b, c)
#define Strspl(a, b) s_strspl(a, b)
#define Strsave(a) s_strsave(a)
#define Strend(a) s_strend(a)
#define Strstr(a, b) s_strstr(a, b)
#endif
/*
* setname is a macro to save space (see sh.err.c)
*/
const char *bname;
#define setname(a) (bname = (a))
Char *Vsav;
Char *Vdp;
Char *Vexpath;
char **Vt;
Char **evalvec;
Char *evalp;
/* word_chars is set by default to WORD_CHARS but can be overridden by
the worchars variable--if unset, reverts to WORD_CHARS */
Char *word_chars;
#define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
Char *STR_SHELLPATH;
#include <paths.h>
#ifdef _PATH_BSHELL
Char *STR_BSHELL;
#endif
Char *STR_WORD_CHARS;
Char **STR_environ;
#ifdef EDIT
#include <histedit.h>
EditLine *el;
History *hi;
#endif
int editing;
#endif /* !_CSH_H_ */

View File

@@ -1,901 +0,0 @@
/* $NetBSD: dir.c,v 1.30 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)dir.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: dir.c,v 1.30 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "csh.h"
#include "dir.h"
#include "extern.h"
/* Directory management. */
static struct directory *dfind(Char *);
static Char *dfollow(Char *);
static void printdirs(void);
static Char *dgoto(Char *);
static void skipargs(Char ***, const char *);
static void dnewcwd(struct directory *);
static void dset(Char *);
struct directory dhead; /* "head" of loop */
int printd; /* force name to be printed */
static int dirflag = 0;
/*
* dinit - initialize current working directory
*/
void
dinit(Char *hp)
{
static const char emsg[] = "csh: Trying to start from \"%s\"\n";
char path[MAXPATHLEN];
struct directory *dp;
const char *ecp;
Char *cp;
/* Don't believe the login shell home, because it may be a symlink */
ecp = getcwd(path, MAXPATHLEN);
if (ecp == NULL || *ecp == '\0') {
(void)fprintf(csherr, "csh: %s\n", strerror(errno));
if (hp && *hp) {
ecp = short2str(hp);
if (chdir(ecp) == -1)
cp = NULL;
else
cp = Strsave(hp);
(void)fprintf(csherr, emsg, vis_str(hp));
}
else
cp = NULL;
if (cp == NULL) {
(void)fprintf(csherr, emsg, "/");
if (chdir("/") == -1) {
/* I am not even try to print an error message! */
xexit(1);
}
cp = SAVE("/");
}
}
else {
struct stat swd, shp;
/*
* See if $HOME is the working directory we got and use that
*/
if (hp && *hp &&
stat(ecp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
cp = Strsave(hp);
else {
const char *cwd;
/*
* use PWD if we have it (for subshells)
*/
if ((cwd = getenv("PWD")) != NULL) {
if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
swd.st_ino == shp.st_ino)
ecp = cwd;
}
cp = dcanon(SAVE(ecp), STRNULL);
}
}
dp = (struct directory *)xcalloc(1, sizeof(struct directory));
dp->di_name = cp;
dp->di_count = 0;
dhead.di_next = dhead.di_prev = dp;
dp->di_next = dp->di_prev = &dhead;
printd = 0;
dnewcwd(dp);
}
static void
dset(Char *dp)
{
Char **vec;
/*
* Don't call set() directly cause if the directory contains ` or
* other junk characters glob will fail.
*/
vec = xmalloc((size_t)(2 * sizeof(Char **)));
vec[0] = Strsave(dp);
vec[1] = 0;
setq(STRcwd, vec, &shvhed);
Setenv(STRPWD, dp);
}
#define DIR_LONG 1
#define DIR_VERT 2
#define DIR_LINE 4
static void
skipargs(Char ***v, const char *str)
{
Char **n, *s;
n = *v;
dirflag = 0;
for (n++; *n != NULL && (*n)[0] == '-'; n++)
for (s = &((*n)[1]); *s; s++)
switch (*s) {
case 'l':
dirflag |= DIR_LONG;
break;
case 'n':
dirflag |= DIR_LINE;
break;
case 'v':
dirflag |= DIR_VERT;
break;
default:
stderror(ERR_DIRUS, vis_str(**v), str);
/* NOTREACHED */
}
*v = n;
}
/*
* dodirs - list all directories in directory loop
*/
void
/*ARGSUSED*/
dodirs(Char **v, struct command *t)
{
skipargs(&v, "");
if (*v != NULL)
stderror(ERR_DIRUS, "dirs", "");
printdirs();
}
static void
printdirs(void)
{
struct directory *dp;
Char *hp, *s;
size_t cur, idx, len;
hp = value(STRhome);
if (*hp == '\0')
hp = NULL;
dp = dcwd;
idx = 0;
cur = 0;
do {
if (dp == &dhead)
continue;
if (dirflag & DIR_VERT) {
(void)fprintf(cshout, "%zu\t", idx++);
cur = 0;
}
if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
(len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
(dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
len = Strlen(s = (dp->di_name + len)) + 2;
else
len = Strlen(s = dp->di_name) + 1;
cur += len;
if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
(void)fprintf(cshout, "\n");
cur = len;
}
(void) fprintf(cshout, "%s%s%c", (s != dp->di_name)? "~" : "",
vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
} while ((dp = dp->di_prev) != dcwd);
if (!(dirflag & DIR_VERT))
(void)fprintf(cshout, "\n");
}
void
dtildepr(Char *home, Char *dir)
{
if (!eq(home, STRslash) && prefix(home, dir))
(void)fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
else
(void)fprintf(cshout, "%s", vis_str(dir));
}
void
dtilde(void)
{
struct directory *d;
d = dcwd;
do {
if (d == &dhead)
continue;
d->di_name = dcanon(d->di_name, STRNULL);
} while ((d = d->di_prev) != dcwd);
dset(dcwd->di_name);
}
/* dnormalize():
* If the name starts with . or .. then we might need to normalize
* it depending on the symbolic link flags
*/
Char *
dnormalize(Char *cp)
{
#define UC (unsigned char)
#define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
#define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
if ((unsigned char) cp[0] == '/')
return (Strsave(cp));
if (adrof(STRignore_symlinks)) {
size_t dotdot = 0;
Char *dp, *cwd;
cwd = xmalloc((size_t)((Strlen(dcwd->di_name) + 3) *
sizeof(Char)));
(void)Strcpy(cwd, dcwd->di_name);
/*
* Ignore . and count ..'s
*/
while (*cp) {
if (ISDOT(cp)) {
if (*++cp)
cp++;
}
else if (ISDOTDOT(cp)) {
dotdot++;
cp += 2;
if (*cp)
cp++;
}
else
break;
}
while (dotdot > 0) {
dp = Strrchr(cwd, '/');
if (dp) {
*dp = '\0';
dotdot--;
}
else
break;
}
if (*cp) {
cwd[dotdot = Strlen(cwd)] = '/';
cwd[dotdot + 1] = '\0';
dp = Strspl(cwd, cp);
xfree((ptr_t) cwd);
return dp;
}
else {
if (!*cwd) {
cwd[0] = '/';
cwd[1] = '\0';
}
return cwd;
}
}
return Strsave(cp);
}
/*
* dochngd - implement chdir command.
*/
void
/*ARGSUSED*/
dochngd(Char **v, struct command *t)
{
struct directory *dp;
Char *cp;
skipargs(&v, " [<dir>]");
printd = 0;
if (*v == NULL) {
if ((cp = value(STRhome)) == NULL || *cp == 0)
stderror(ERR_NAME | ERR_NOHOMEDIR);
if (chdir(short2str(cp)) < 0)
stderror(ERR_NAME | ERR_CANTCHANGE);
cp = Strsave(cp);
}
else if (v[1] != NULL)
stderror(ERR_NAME | ERR_TOOMANY);
else if ((dp = dfind(*v)) != 0) {
char *tmp;
printd = 1;
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
dcwd->di_prev->di_next = dcwd->di_next;
dcwd->di_next->di_prev = dcwd->di_prev;
dfree(dcwd);
dnewcwd(dp);
return;
}
else
cp = dfollow(*v);
dp = (struct directory *)xcalloc(1, sizeof(struct directory));
dp->di_name = cp;
dp->di_count = 0;
dp->di_next = dcwd->di_next;
dp->di_prev = dcwd->di_prev;
dp->di_prev->di_next = dp;
dp->di_next->di_prev = dp;
dfree(dcwd);
dnewcwd(dp);
}
static Char *
dgoto(Char *cp)
{
Char *dp;
if (*cp != '/') {
Char *p, *q;
size_t cwdlen;
for (p = dcwd->di_name; *p++;)
continue;
if ((cwdlen = (size_t)(p - dcwd->di_name - 1)) == 1) /* root */
cwdlen = 0;
for (p = cp; *p++;)
continue;
dp = xmalloc((size_t)(cwdlen + (size_t)(p - cp) + 1) * sizeof(Char));
for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
continue;
if (cwdlen)
p[-1] = '/';
else
p--; /* don't add a / after root */
for (q = cp; (*p++ = *q++) != '\0';)
continue;
xfree((ptr_t) cp);
cp = dp;
dp += cwdlen;
}
else
dp = cp;
cp = dcanon(cp, dp);
return cp;
}
/*
* dfollow - change to arg directory; fall back on cdpath if not valid
*/
static Char *
dfollow(Char *cp)
{
char ebuf[MAXPATHLEN];
struct varent *c;
Char *dp;
int serrno;
cp = globone(cp, G_ERROR);
/*
* if we are ignoring symlinks, try to fix relatives now.
*/
dp = dnormalize(cp);
if (chdir(short2str(dp)) >= 0) {
xfree((ptr_t) cp);
return dgoto(dp);
}
else {
xfree((ptr_t) dp);
if (chdir(short2str(cp)) >= 0)
return dgoto(cp);
serrno = errno;
}
if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
&& (c = adrof(STRcdpath))) {
Char **cdp;
Char *p;
Char buf[MAXPATHLEN];
for (cdp = c->vec; *cdp; cdp++) {
for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
continue;
dp[-1] = '/';
for (p = cp; (*dp++ = *p++) != '\0';)
continue;
if (chdir(short2str(buf)) >= 0) {
printd = 1;
xfree((ptr_t) cp);
cp = Strsave(buf);
return dgoto(cp);
}
}
}
dp = value(cp);
if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
xfree((ptr_t) cp);
cp = Strsave(dp);
printd = 1;
return dgoto(cp);
}
(void)strcpy(ebuf, short2str(cp));
xfree((ptr_t) cp);
stderror(ERR_SYSTEM, ebuf, strerror(serrno));
/* NOTREACHED */
}
/*
* dopushd - push new directory onto directory stack.
* with no arguments exchange top and second.
* with numeric argument (+n) bring it to top.
*/
void
/*ARGSUSED*/
dopushd(Char **v, struct command *t)
{
struct directory *dp;
skipargs(&v, " [<dir>|+<n>]");
printd = 1;
if (*v == NULL) {
char *tmp;
if ((dp = dcwd->di_prev) == &dhead)
dp = dhead.di_prev;
if (dp == dcwd)
stderror(ERR_NAME | ERR_NODIR);
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
dp->di_prev->di_next = dp->di_next;
dp->di_next->di_prev = dp->di_prev;
dp->di_next = dcwd->di_next;
dp->di_prev = dcwd;
dcwd->di_next->di_prev = dp;
dcwd->di_next = dp;
}
else if (v[1] != NULL)
stderror(ERR_NAME | ERR_TOOMANY);
else if ((dp = dfind(*v)) != NULL) {
char *tmp;
if (chdir(tmp = short2str(dp->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
else {
Char *ccp;
ccp = dfollow(*v);
dp = (struct directory *)xcalloc(1, sizeof(struct directory));
dp->di_name = ccp;
dp->di_count = 0;
dp->di_prev = dcwd;
dp->di_next = dcwd->di_next;
dcwd->di_next = dp;
dp->di_next->di_prev = dp;
}
dnewcwd(dp);
}
/*
* dfind - find a directory if specified by numeric (+n) argument
*/
static struct directory *
dfind(Char *cp)
{
struct directory *dp;
Char *ep;
int i;
if (*cp++ != '+')
return (0);
for (ep = cp; Isdigit(*ep); ep++)
continue;
if (*ep)
return (0);
i = getn(cp);
if (i <= 0)
return (0);
for (dp = dcwd; i != 0; i--) {
if ((dp = dp->di_prev) == &dhead)
dp = dp->di_prev;
if (dp == dcwd)
stderror(ERR_NAME | ERR_DEEP);
}
return (dp);
}
/*
* dopopd - pop a directory out of the directory stack
* with a numeric argument just discard it.
*/
void
/*ARGSUSED*/
dopopd(Char **v, struct command *t)
{
struct directory *dp, *p = NULL;
skipargs(&v, " [+<n>]");
printd = 1;
if (*v == NULL)
dp = dcwd;
else if (v[1] != NULL)
stderror(ERR_NAME | ERR_TOOMANY);
else if ((dp = dfind(*v)) == 0)
stderror(ERR_NAME | ERR_BADDIR);
if (dp->di_prev == &dhead && dp->di_next == &dhead)
stderror(ERR_NAME | ERR_EMPTY);
if (dp == dcwd) {
char *tmp;
if ((p = dp->di_prev) == &dhead)
p = dhead.di_prev;
if (chdir(tmp = short2str(p->di_name)) < 0)
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
dp->di_prev->di_next = dp->di_next;
dp->di_next->di_prev = dp->di_prev;
if (dp == dcwd)
dnewcwd(p);
else {
printdirs();
}
dfree(dp);
}
/*
* dfree - free the directory (or keep it if it still has ref count)
*/
void
dfree(struct directory *dp)
{
if (dp->di_count != 0) {
dp->di_next = dp->di_prev = 0;
}
else {
xfree((char *) dp->di_name);
xfree((ptr_t) dp);
}
}
/*
* dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
* we are of course assuming that the file system is standardly
* constructed (always have ..'s, directories have links)
*/
Char *
dcanon(Char *cp, Char *p)
{
Char slink[MAXPATHLEN];
char tlink[MAXPATHLEN];
Char *newcp, *sp;
Char *p1, *p2; /* general purpose */
ssize_t cc;
size_t len;
int slash;
/*
* christos: if the path given does not start with a slash prepend cwd. If
* cwd does not start with a path or the result would be too long abort().
*/
if (*cp != '/') {
Char tmpdir[MAXPATHLEN];
p1 = value(STRcwd);
if (p1 == NULL || *p1 != '/')
abort();
if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
abort();
(void)Strcpy(tmpdir, p1);
(void)Strcat(tmpdir, STRslash);
(void)Strcat(tmpdir, cp);
xfree((ptr_t) cp);
cp = p = Strsave(tmpdir);
}
while (*p) { /* for each component */
sp = p; /* save slash address */
while (*++p == '/') /* flush extra slashes */
continue;
if (p != ++sp)
for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
p = sp; /* save start of component */
slash = 0;
while (*++p) /* find next slash or end of path */
if (*p == '/') {
slash = 1;
*p = 0;
break;
}
if (*sp == '\0') { /* if component is null */
if (--sp == cp) /* if path is one char (i.e. /) */
break;
else
*sp = '\0';
} else if (sp[0] == '.' && sp[1] == 0) {
if (slash) {
for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
continue;
p = --sp;
}
else if (--sp != cp)
*sp = '\0';
}
else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
/*
* We have something like "yyy/xxx/..", where "yyy" can be null or
* a path starting at /, and "xxx" is a single component. Before
* compressing "xxx/..", we want to expand "yyy/xxx", if it is a
* symbolic link.
*/
*--sp = 0; /* form the pathname for readlink */
if (sp != cp && !adrof(STRignore_symlinks) &&
(cc = readlink(short2str(cp), tlink,
sizeof(tlink) - 1)) >= 0) {
tlink[cc] = '\0';
(void)Strcpy(slink, str2short(tlink));
if (slash)
*p = '/';
/*
* Point p to the '/' in "/..", and restore the '/'.
*/
*(p = sp) = '/';
/*
* find length of p
*/
for (p1 = p; *p1++;)
continue;
if (*slink != '/') {
/*
* Relative path, expand it between the "yyy/" and the
* "/..". First, back sp up to the character past "yyy/".
*/
while (*--sp != '/')
continue;
sp++;
*sp = 0;
/*
* New length is "yyy/" + slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)((sp - cp) + cc + (p1 - p)) * sizeof(Char));
/*
* Copy new path into newcp
*/
for (p2 = cp; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = slink; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at expanded "/xxx".
*/
p = sp - cp - 1 + newcp;
}
else {
/*
* New length is slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)(cc + (p1 - p)) * sizeof(Char));
/*
* Copy new path into newcp
*/
for (p2 = slink; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at beginning
*/
p = newcp;
}
xfree((ptr_t) cp);
cp = newcp;
continue; /* canonicalize the link */
}
*sp = '/';
if (sp != cp)
while (*--sp != '/')
continue;
if (slash) {
for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
continue;
p = sp;
}
else if (cp == sp)
*++sp = '\0';
else
*sp = '\0';
}
else { /* normal dir name (not . or .. or nothing) */
if (sp != cp && adrof(STRchase_symlinks) &&
!adrof(STRignore_symlinks) &&
(cc = readlink(short2str(cp), tlink, sizeof(tlink)-1)) >= 0) {
tlink[cc] = '\0';
(void)Strcpy(slink, str2short(tlink));
/*
* restore the '/'.
*/
if (slash)
*p = '/';
/*
* point sp to p (rather than backing up).
*/
sp = p;
/*
* find length of p
*/
for (p1 = p; *p1++;)
continue;
if (*slink != '/') {
/*
* Relative path, expand it between the "yyy/" and the
* remainder. First, back sp up to the character past
* "yyy/".
*/
while (*--sp != '/')
continue;
sp++;
*sp = 0;
/*
* New length is "yyy/" + slink + "/.." and rest
*/
p1 = newcp = xmalloc(
(size_t)((sp - cp) + cc + (p1 - p)) * sizeof(Char));
/*
* Copy new path into newcp
*/
for (p2 = cp; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = slink; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at expanded "/xxx".
*/
p = sp - cp - 1 + newcp;
}
else {
/*
* New length is slink + the rest
*/
p1 = newcp = xmalloc(
(size_t)(cc + (p1 - p)) * sizeof(Char));
/*
* Copy new path into newcp
*/
for (p2 = slink; (*p1++ = *p2++) != '\0';)
continue;
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
continue;
/*
* Restart canonicalization at beginning
*/
p = newcp;
}
xfree((ptr_t) cp);
cp = newcp;
continue; /* canonicalize the link */
}
if (slash)
*p = '/';
}
}
/*
* fix home...
*/
p1 = value(STRhome);
len = Strlen(p1);
/*
* See if we're not in a subdir of STRhome
*/
if (p1 && *p1 == '/' &&
(Strncmp(p1, cp, len) != 0 || (cp[len] != '/' && cp[len] != '\0'))) {
static ino_t home_ino;
static dev_t home_dev = NODEV;
static Char *home_ptr = NULL;
struct stat statbuf;
/*
* Get dev and ino of STRhome
*/
if (home_ptr != p1 &&
stat(short2str(p1), &statbuf) != -1) {
home_dev = statbuf.st_dev;
home_ino = statbuf.st_ino;
home_ptr = p1;
}
/*
* Start comparing dev & ino backwards
*/
p2 = Strcpy(slink, cp);
for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
if (statbuf.st_dev == home_dev &&
statbuf.st_ino == home_ino) {
sp = (Char *) - 1;
break;
}
if ((sp = Strrchr(p2, '/')) != NULL)
*sp = '\0';
}
/*
* See if we found it
*/
if (*p2 && sp == (Char *) -1) {
/*
* Use STRhome to make '~' work
*/
newcp = Strspl(p1, cp + Strlen(p2));
xfree((ptr_t) cp);
cp = newcp;
}
}
return cp;
}
/*
* dnewcwd - make a new directory in the loop the current one
*/
static void
dnewcwd(struct directory *dp)
{
dcwd = dp;
dset(dcwd->di_name);
if (printd && !(adrof(STRpushdsilent)))
printdirs();
}

View File

@@ -1,48 +0,0 @@
/* $NetBSD: dir.h,v 1.8 2003/08/07 09:05:04 agc Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)dir.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _DIR_H_
#define _DIR_H_
/*
* Structure for entries in directory stack.
*/
struct directory {
struct directory *di_next; /* next in loop */
struct directory *di_prev; /* prev in loop */
unsigned short *di_count; /* refcount of processes */
Char *di_name; /* actual name */
};
struct directory *dcwd; /* the one we are in now */
#endif /* !_DIR_H_ */

View File

@@ -1,968 +0,0 @@
/* $NetBSD: dol.c,v 1.29 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)dol.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: dol.c,v 1.29 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "csh.h"
#include "extern.h"
/*
* These routines perform variable substitution and quoting via ' and ".
* To this point these constructs have been preserved in the divided
* input words. Here we expand variables and turn quoting via ' and " into
* QUOTE bits on characters (which prevent further interpretation).
* If the `:q' modifier was applied during history expansion, then
* some QUOTEing may have occurred already, so we dont "trim()" here.
*/
static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */
static Char *Dcp, **Dvp; /* Input vector for Dreadc */
#define DEOF -1
#define unDgetC(c) Dpeekc = c
#define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
/*
* The following variables give the information about the current
* $ expansion, recording the current word position, the remaining
* words within this expansion, the count of remaining words, and the
* information about any : modifier which is being applied.
*/
#define MAXWLEN (BUFSIZE - 4)
#define MAXMOD MAXWLEN /* This cannot overflow */
static Char dolmod[MAXMOD]; /* : modifier character */
static Char *dolp; /* Remaining chars from this word */
static Char **dolnxt; /* Further words */
static int dolcnt; /* Count of further words */
static int dolnmod; /* Number of modifiers */
static int dolmcnt; /* :gx -> 10000, else 1 */
static int dolwcnt; /* :wx -> 10000, else 1 */
static void Dfix2(Char **);
static Char *Dpack(Char *, Char *);
static int Dword(void);
__dead static void dolerror(Char *);
static int DgetC(int);
static void Dgetdol(void);
static void fixDolMod(void);
static void setDolp(Char *);
static void unDredc(int);
static int Dredc(void);
static void Dtestq(int);
/*
* Fix up the $ expansions and quotations in the
* argument list to command t.
*/
void
Dfix(struct command *t)
{
Char *p, **pp;
if (noexec)
return;
/* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
for (pp = t->t_dcom; (p = *pp++) != NULL;)
for (; *p; p++) {
if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
Dfix2(t->t_dcom); /* found one */
blkfree(t->t_dcom);
t->t_dcom = gargv;
gargv = 0;
return;
}
}
}
/*
* $ substitute one word, for i/o redirection
*/
Char *
Dfix1(Char *cp)
{
Char *Dv[2];
if (noexec)
return (0);
Dv[0] = cp;
Dv[1] = NULL;
Dfix2(Dv);
if (gargc != 1) {
setname(vis_str(cp));
stderror(ERR_NAME | ERR_AMBIG);
}
cp = Strsave(gargv[0]);
blkfree(gargv), gargv = 0;
return (cp);
}
/*
* Subroutine to do actual fixing after state initialization.
*/
static void
Dfix2(Char **v)
{
ginit(); /* Initialize glob's area pointers */
Dvp = v;
Dcp = STRNULL; /* Setup input vector for Dreadc */
unDgetC(0);
unDredc(0); /* Clear out any old peeks (at error) */
dolp = 0;
dolcnt = 0; /* Clear out residual $ expands (...) */
while (Dword())
continue;
}
/*
* Pack up more characters in this word
*/
static Char *
Dpack(Char *wbuf, Char *wp)
{
int c;
ptrdiff_t i;
i = MAXWLEN - (wp - wbuf);
for (;;) {
c = DgetC(DODOL);
if (c == '\\') {
c = DgetC(0);
if (c == DEOF) {
unDredc(c);
*wp = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (c == '\n')
c = ' ';
else
c |= QUOTE;
}
if (c == DEOF) {
unDredc(c);
*wp = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
unDgetC(c);
if (cmap(c, QUOTES))
return (wp);
*wp++ = 0;
Gcat(STRNULL, wbuf);
return (NULL);
}
if (--i <= 0)
stderror(ERR_WTOOLONG);
*wp++ = (Char)c;
}
}
/*
* Get a word. This routine is analogous to the routine
* word() in sh.lex.c for the main lexical input. One difference
* here is that we don't get a newline to terminate our expansion.
* Rather, DgetC will return a DEOF when we hit the end-of-input.
*/
static int
Dword(void)
{
Char wbuf[BUFSIZE], *wp;
int c, c1;
ptrdiff_t i;
int dolflg, done, sofar;
done = 0;
i = MAXWLEN;
sofar = 0;
wp = wbuf;
while (!done) {
done = 1;
c = DgetC(DODOL);
switch (c) {
case DEOF:
if (sofar == 0)
return (0);
/* finish this word and catch the code above the next time */
unDredc(c);
/* FALLTHROUGH */
case '\n':
*wp = 0;
Gcat(STRNULL, wbuf);
return (1);
case ' ':
case '\t':
done = 0;
break;
case '`':
/* We preserve ` quotations which are done yet later */
*wp++ = (Char)c, --i;
/* FALLTHROUGH */
case '\'':
case '"':
/*
* Note that DgetC never returns a QUOTES character from an
* expansion, so only true input quotes will get us here or out.
*/
c1 = c;
dolflg = c1 == '"' ? DODOL : 0;
for (;;) {
c = DgetC(dolflg);
if (c == c1)
break;
if (c == '\n' || c == DEOF)
stderror(ERR_UNMATCHED, c1);
if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE))
--wp, ++i;
if (--i <= 0)
stderror(ERR_WTOOLONG);
switch (c1) {
case '"':
/*
* Leave any `s alone for later. Other chars are all
* quoted, thus `...` can tell it was within "...".
*/
*wp++ = (Char)(c == '`' ? '`' : (c | QUOTE));
break;
case '\'':
/* Prevent all further interpretation */
*wp++ = (Char)(c | QUOTE);
break;
case '`':
/* Leave all text alone for later */
*wp++ = (Char)c;
break;
default:
break;
}
}
if (c1 == '`')
*wp++ = '`' /* i--; eliminated */;
sofar = 1;
if ((wp = Dpack(wbuf, wp)) == NULL)
return (1);
else {
i = MAXWLEN - (wp - wbuf);
done = 0;
}
break;
case '\\':
c = DgetC(0); /* No $ subst! */
if (c == '\n' || c == DEOF) {
done = 0;
break;
}
c |= QUOTE;
break;
default:
break;
}
if (done) {
unDgetC(c);
sofar = 1;
if ((wp = Dpack(wbuf, wp)) == NULL)
return (1);
else {
i = MAXWLEN - (wp - wbuf);
done = 0;
}
}
}
/* Really NOTREACHED */
return (0);
}
/*
* Get a character, performing $ substitution unless flag is 0.
* Any QUOTES character which is returned from a $ expansion is
* QUOTEd so that it will not be recognized above.
*/
static int
DgetC(int flag)
{
int c;
top:
if ((c = Dpeekc) != '\0') {
Dpeekc = 0;
return (c);
}
if (lap) {
c = *lap++ & (QUOTE | TRIM);
if (c == 0) {
lap = 0;
goto top;
}
quotspec:
if (cmap(c, QUOTES))
return (c | QUOTE);
return (c);
}
if (dolp) {
if ((c = *dolp++ & (QUOTE | TRIM)) != '\0')
goto quotspec;
if (dolcnt > 0) {
setDolp(*dolnxt++);
--dolcnt;
return (' ');
}
dolp = 0;
}
if (dolcnt > 0) {
setDolp(*dolnxt++);
--dolcnt;
goto top;
}
c = Dredc();
if (c == '$' && flag) {
Dgetdol();
goto top;
}
return (c);
}
static Char *nulvec[] = {0};
static struct varent nulargv = {nulvec, STRargv, { NULL, NULL, NULL }, 0};
static void
dolerror(Char *s)
{
setname(vis_str(s));
stderror(ERR_NAME | ERR_RANGE);
/* NOTREACHED */
}
/*
* Handle the multitudinous $ expansion forms.
* Ugh.
*/
static void
Dgetdol(void)
{
static Char *dolbang = NULL;
Char name[4*MAXVARLEN+1];
Char wbuf[BUFSIZE];
struct varent *vp;
Char *np;
int c, lwb, sc, subscr, upb;
int dimen, bitset;
char tnp;
bitset = 0;
dimen = 0;
lwb = 1;
upb = 0;
subscr = 0;
vp = NULL;
dolnmod = dolmcnt = dolwcnt = 0;
c = sc = DgetC(0);
if (c == '{')
c = DgetC(0); /* sc is { to take } later */
if ((c & TRIM) == '#')
dimen++, c = DgetC(0); /* $# takes dimension */
else if (c == '?')
bitset++, c = DgetC(0); /* $? tests existence */
switch (c) {
case '!':
if (dimen || bitset)
stderror(ERR_SYNTAX);
if (backpid != 0) {
if (dolbang)
xfree((ptr_t)dolbang);
setDolp(dolbang = putn(backpid));
}
goto eatbrac;
case '$':
if (dimen || bitset)
stderror(ERR_SYNTAX);
setDolp(doldol);
goto eatbrac;
case '<' | QUOTE:
if (bitset)
stderror(ERR_NOTALLOWED, "$?<");
if (dimen)
stderror(ERR_NOTALLOWED, "$?#");
for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) {
*np = (unsigned char)tnp;
if (np >= &wbuf[BUFSIZE - 1])
stderror(ERR_LTOOLONG);
if (tnp == '\n')
break;
}
*np = 0;
/*
* KLUDGE: dolmod is set here because it will cause setDolp to call
* domod and thus to copy wbuf. Otherwise setDolp would use it
* directly. If we saved it ourselves, no one would know when to free
* it. The actual function of the 'q' causes filename expansion not to
* be done on the interpolated value.
*/
dolmod[dolnmod++] = 'q';
dolmcnt = 10000;
setDolp(wbuf);
goto eatbrac;
case DEOF:
case '\n':
stderror(ERR_SYNTAX);
/* NOTREACHED */
case '*':
(void) Strcpy(name, STRargv);
vp = adrof(STRargv);
subscr = -1; /* Prevent eating [...] */
break;
default:
np = name;
if (Isdigit(c)) {
if (dimen)
stderror(ERR_NOTALLOWED, "$#<num>");
subscr = 0;
do {
subscr = subscr * 10 + c - '0';
c = DgetC(0);
} while (Isdigit(c));
unDredc(c);
if (subscr < 0)
stderror(ERR_RANGE);
if (subscr == 0) {
if (bitset) {
dolp = ffile ? STR1 : STR0;
goto eatbrac;
}
if (ffile == 0)
stderror(ERR_DOLZERO);
fixDolMod();
setDolp(ffile);
goto eatbrac;
}
if (bitset)
stderror(ERR_DOLQUEST);
vp = adrof(STRargv);
if (vp == 0) {
vp = &nulargv;
goto eatmod;
}
break;
}
if (!alnum(c))
stderror(ERR_VARALNUM);
for (;;) {
*np++ = (Char)c;
c = DgetC(0);
if (!alnum(c))
break;
if (np >= &name[MAXVARLEN])
stderror(ERR_VARTOOLONG);
}
*np++ = 0;
unDredc(c);
vp = adrof(name);
}
if (bitset) {
dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
goto eatbrac;
}
if (vp == 0) {
np = str2short(getenv(short2str(name)));
if (np) {
fixDolMod();
setDolp(np);
goto eatbrac;
}
udvar(name);
}
c = DgetC(0);
upb = blklen(vp->vec);
if (dimen == 0 && subscr == 0 && c == '[') {
np = name;
for (;;) {
c = DgetC(DODOL); /* Allow $ expand within [ ] */
if (c == ']')
break;
if (c == '\n' || c == DEOF)
stderror(ERR_INCBR);
if (np >= &name[sizeof(name) / sizeof(Char) - 2])
stderror(ERR_VARTOOLONG);
*np++ = (Char)c;
}
*np = 0, np = name;
if (dolp || dolcnt) /* $ exp must end before ] */
stderror(ERR_EXPORD);
if (!*np)
stderror(ERR_SYNTAX);
if (Isdigit(*np)) {
int i;
for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
continue;
if ((i < 0 || i > upb) && !any("-*", *np)) {
dolerror(vp->v_name);
return;
}
lwb = i;
if (!*np)
upb = lwb, np = STRstar;
}
if (*np == '*')
np++;
else if (*np != '-')
stderror(ERR_MISSING, '-');
else {
int i = upb;
np++;
if (Isdigit(*np)) {
i = 0;
while (Isdigit(*np))
i = i * 10 + *np++ - '0';
if (i < 0 || i > upb) {
dolerror(vp->v_name);
return;
}
}
if (i < lwb)
upb = lwb - 1;
else
upb = i;
}
if (lwb == 0) {
if (upb != 0) {
dolerror(vp->v_name);
return;
}
upb = -1;
}
if (*np)
stderror(ERR_SYNTAX);
}
else {
if (subscr > 0) {
if (subscr > upb)
lwb = 1, upb = 0;
else
lwb = upb = subscr;
}
unDredc(c);
}
if (dimen) {
Char *cp = putn(upb - lwb + 1);
addla(cp);
xfree((ptr_t) cp);
}
else {
eatmod:
fixDolMod();
dolnxt = &vp->vec[lwb - 1];
dolcnt = upb - lwb + 1;
}
eatbrac:
if (sc == '{') {
c = Dredc();
if (c != '}')
stderror(ERR_MISSING, '}');
}
}
static void
fixDolMod(void)
{
int c;
c = DgetC(0);
if (c == ':') {
do {
c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
if (c == 'g' || c == 'a') {
if (c == 'g')
dolmcnt = 10000;
else
dolwcnt = 10000;
c = DgetC(0);
}
if ((c == 'g' && dolmcnt != 10000) ||
(c == 'a' && dolwcnt != 10000)) {
if (c == 'g')
dolmcnt = 10000;
else
dolwcnt = 10000;
c = DgetC(0);
}
if (c == 's') { /* [eichin:19910926.0755EST] */
int delimcnt = 2;
int delim = DgetC(0);
dolmod[dolnmod++] = (Char)c;
dolmod[dolnmod++] = (Char)delim;
if (!delim || letter(delim)
|| Isdigit(delim) || any(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
while ((c = DgetC(0)) != (-1)) {
dolmod[dolnmod++] = (Char)c;
if(c == delim) delimcnt--;
if(!delimcnt) break;
}
if(delimcnt) {
seterror(ERR_BADSUBST);
break;
}
continue;
}
if (!any("htrqxes", c))
stderror(ERR_BADMOD, c);
dolmod[dolnmod++] = (Char)c;
if (c == 'q')
dolmcnt = 10000;
}
while ((c = DgetC(0)) == ':');
unDredc(c);
}
else
unDredc(c);
}
static void
setDolp(Char *cp)
{
Char *dp;
int i;
if (dolnmod == 0 || dolmcnt == 0) {
dolp = cp;
return;
}
dp = cp = Strsave(cp);
for (i = 0; i < dolnmod; i++) {
/* handle s// [eichin:19910926.0510EST] */
if(dolmod[i] == 's') {
int delim;
Char *lhsub, *rhsub, *np;
size_t lhlen = 0, rhlen = 0;
int didmod = 0;
delim = dolmod[++i];
if (!delim || letter(delim)
|| Isdigit(delim) || any(" \t\n", delim)) {
seterror(ERR_BADSUBST);
break;
}
lhsub = &dolmod[++i];
while(dolmod[i] != delim && dolmod[++i]) {
lhlen++;
}
dolmod[i] = 0;
rhsub = &dolmod[++i];
while(dolmod[i] != delim && dolmod[++i]) {
rhlen++;
}
dolmod[i] = 0;
do {
dp = Strstr(cp, lhsub);
if (dp) {
np = xmalloc(
(size_t)((Strlen(cp) + 1 - lhlen + rhlen) *
sizeof(*np)));
(void)Strncpy(np, cp, (size_t)(dp - cp));
(void)Strcpy(np + (dp - cp), rhsub);
(void)Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
xfree((ptr_t) cp);
dp = cp = np;
didmod = 1;
} else {
/* should this do a seterror? */
break;
}
}
while (dolwcnt == 10000);
/*
* restore dolmod for additional words
*/
dolmod[i] = rhsub[-1] = (Char)delim;
if (didmod)
dolmcnt--;
else
break;
} else {
int didmod = 0;
do {
if ((dp = domod(cp, dolmod[i]))) {
didmod = 1;
if (Strcmp(cp, dp) == 0) {
xfree((ptr_t) cp);
cp = dp;
break;
}
else {
xfree((ptr_t) cp);
cp = dp;
}
}
else
break;
}
while (dolwcnt == 10000);
dp = cp;
if (didmod)
dolmcnt--;
else
break;
}
}
if (dp) {
addla(dp);
xfree((ptr_t) dp);
}
else {
addla(cp);
xfree((ptr_t) cp);
}
dolp = STRNULL;
if (seterr)
stderror(ERR_OLD);
}
static void
unDredc(int c)
{
Dpeekrd = c;
}
static int
Dredc(void)
{
int c;
if ((c = Dpeekrd) != '\0') {
Dpeekrd = 0;
return (c);
}
if (Dcp && (c = *Dcp++))
return (c & (QUOTE | TRIM));
if (*Dvp == 0) {
Dcp = 0;
return (DEOF);
}
Dcp = *Dvp++;
return (' ');
}
static void
Dtestq(int c)
{
if (cmap(c, QUOTES))
gflag = 1;
}
/*
* Form a shell temporary file (in unit 0) from the words
* of the shell input up to EOF or a line the same as "term".
* Unit 0 should have been closed before this call.
*/
void
/*ARGSUSED*/
heredoc(Char *term)
{
Char obuf[BUFSIZE], lbuf[BUFSIZE], mbuf[BUFSIZE];
struct timespec tv;
Char *Dv[2], *lbp, *obp, *mbp, **vp;
char *tmp;
int c, ocnt, lcnt, mcnt;
int quoted;
again:
tmp = short2str(shtemp);
if (open(tmp, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600) < 0) {
if (errno == EEXIST) {
if (unlink(tmp) == -1) {
(void)clock_gettime(CLOCK_MONOTONIC, &tv);
mbp = putn((((int)tv.tv_sec) ^
((int)tv.tv_nsec) ^ ((int)getpid())) & 0x00ffffff);
shtemp = Strspl(STRtmpsh, mbp);
xfree((ptr_t)mbp);
}
goto again;
}
stderror(ERR_SYSTEM, tmp, strerror(errno));
}
(void)unlink(tmp); /* 0 0 inode! */
Dv[0] = term;
Dv[1] = NULL;
gflag = 0;
trim(Dv);
rscan(Dv, Dtestq);
quoted = gflag;
ocnt = BUFSIZE;
obp = obuf;
for (;;) {
/*
* Read up a line
*/
lbp = lbuf;
lcnt = BUFSIZE - 4;
for (;;) {
c = readc(1); /* 1 -> Want EOF returns */
if (c < 0 || c == '\n')
break;
if ((c &= TRIM) != '\0') {
*lbp++ = (Char)c;
if (--lcnt < 0) {
setname("<<");
stderror(ERR_NAME | ERR_OVERFLOW);
}
}
}
*lbp = 0;
/*
* Check for EOF or compare to terminator -- before expansion
*/
if (c < 0 || eq(lbuf, term)) {
(void)write(0, short2str(obuf), (size_t)(BUFSIZE - ocnt));
(void)lseek(0, (off_t)0, SEEK_SET);
return;
}
/*
* If term was quoted or -n just pass it on
*/
if (quoted || noexec) {
*lbp++ = '\n';
*lbp = 0;
for (lbp = lbuf; (c = *lbp++) != '\0';) {
*obp++ = (Char)c;
if (--ocnt == 0) {
(void) write(0, short2str(obuf), BUFSIZE);
obp = obuf;
ocnt = BUFSIZE;
}
}
continue;
}
/*
* Term wasn't quoted so variable and then command expand the input
* line
*/
Dcp = lbuf;
Dvp = Dv + 1;
mbp = mbuf;
mcnt = BUFSIZE - 4;
for (;;) {
c = DgetC(DODOL);
if (c == DEOF)
break;
if ((c &= TRIM) == 0)
continue;
/* \ quotes \ $ ` here */
if (c == '\\') {
c = DgetC(0);
if (!any("$\\`", c))
unDgetC(c | QUOTE), c = '\\';
else
c |= QUOTE;
}
*mbp++ = (Char)c;
if (--mcnt == 0) {
setname("<<");
stderror(ERR_NAME | ERR_OVERFLOW);
}
}
*mbp++ = 0;
/*
* If any ` in line do command substitution
*/
mbp = mbuf;
if (any(short2str(mbp), '`')) {
/*
* 1 arg to dobackp causes substitution to be literal. Words are
* broken only at newlines so that all blanks and tabs are
* preserved. Blank lines (null words) are not discarded.
*/
vp = dobackp(mbuf, 1);
}
else
/* Setup trivial vector similar to return of dobackp */
Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
/*
* Resurrect the words from the command substitution each separated by
* a newline. Note that the last newline of a command substitution
* will have been discarded, but we put a newline after the last word
* because this represents the newline after the last input line!
*/
for (; *vp; vp++) {
for (mbp = *vp; *mbp; mbp++) {
*obp++ = *mbp & TRIM;
if (--ocnt == 0) {
(void)write(0, short2str(obuf), BUFSIZE);
obp = obuf;
ocnt = BUFSIZE;
}
}
*obp++ = '\n';
if (--ocnt == 0) {
(void)write(0, short2str(obuf), BUFSIZE);
obp = obuf;
ocnt = BUFSIZE;
}
}
if (pargv)
blkfree(pargv), pargv = 0;
}
}

View File

@@ -1,388 +0,0 @@
/* $NetBSD: err.c,v 1.21 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: err.c,v 1.21 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include "csh.h"
#include "extern.h"
char *seterr = NULL; /* Holds last error if there was one */
#define ERR_FLAGS ((int)0xf0000000)
#define ERR_NAME 0x10000000
#define ERR_SILENT 0x20000000
#define ERR_OLD 0x40000000
static const char *errorlist[] =
{
#define ERR_SYNTAX 0
"Syntax Error",
#define ERR_NOTALLOWED 1
"%s is not allowed",
#define ERR_WTOOLONG 2
"Word too long",
#define ERR_LTOOLONG 3
"$< line too long",
#define ERR_DOLZERO 4
"No file for $0",
#define ERR_DOLQUEST 5
"$? not allowed here",
#define ERR_INCBR 6
"Incomplete [] modifier",
#define ERR_EXPORD 7
"$ expansion must end before ]",
#define ERR_BADMOD 8
"Bad : modifier in $ (%c)",
#define ERR_SUBSCRIPT 9
"Subscript error",
#define ERR_BADNUM 10
"Badly formed number",
#define ERR_NOMORE 11
"No more words",
#define ERR_FILENAME 12
"Missing file name",
#define ERR_GLOB 13
"Internal glob error",
#define ERR_COMMAND 14
"Command not found",
#define ERR_TOOFEW 15
"Too few arguments",
#define ERR_TOOMANY 16
"Too many arguments",
#define ERR_DANGER 17
"Too dangerous to alias that",
#define ERR_EMPTYIF 18
"Empty if",
#define ERR_IMPRTHEN 19
"Improper then",
#define ERR_NOPAREN 20
"Words not parenthesized",
#define ERR_NOTFOUND 21
"%s not found",
#define ERR_MASK 22
"Improper mask",
#define ERR_LIMIT 23
"No such limit",
#define ERR_TOOLARGE 24
"Argument too large",
#define ERR_SCALEF 25
"Improper or unknown scale factor",
#define ERR_UNDVAR 26
"Undefined variable",
#define ERR_DEEP 27
"Directory stack not that deep",
#define ERR_BADSIG 28
"Bad signal number",
#define ERR_UNKSIG 29
"Unknown signal; kill -l lists signals",
#define ERR_VARBEGIN 30
"Variable name must begin with a letter",
#define ERR_VARTOOLONG 31
"Variable name too long",
#define ERR_VARALNUM 32
"Variable name must contain alphanumeric characters",
#define ERR_JOBCONTROL 33
"No job control in this shell",
#define ERR_EXPRESSION 34
"Expression Syntax",
#define ERR_NOHOMEDIR 35
"No home directory",
#define ERR_CANTCHANGE 36
"Can't change to home directory",
#define ERR_NULLCOM 37
"Invalid null command",
#define ERR_ASSIGN 38
"Assignment missing expression",
#define ERR_UNKNOWNOP 39
"Unknown operator",
#define ERR_AMBIG 40
"Ambiguous",
#define ERR_EXISTS 41
"%s: File exists",
#define ERR_INTR 42
"Interrupted",
#define ERR_RANGE 43
"Subscript out of range",
#define ERR_OVERFLOW 44
"Line overflow",
#define ERR_VARMOD 45
"Unknown variable modifier",
#define ERR_NOSUCHJOB 46
"No such job",
#define ERR_TERMINAL 47
"Can't from terminal",
#define ERR_NOTWHILE 48
"Not in while/foreach",
#define ERR_NOPROC 49
"No more processes",
#define ERR_NOMATCH 50
"No match",
#define ERR_MISSING 51
"Missing %c",
#define ERR_UNMATCHED 52
"Unmatched %c",
#define ERR_NOMEM 53
"Out of memory",
#define ERR_PIPE 54
"Can't make pipe",
#define ERR_SYSTEM 55
"%s: %s",
#define ERR_STRING 56
"%s",
#define ERR_JOBS 57
"usage: jobs [ -l ]",
#define ERR_JOBARGS 58
"Arguments should be jobs or process id's",
#define ERR_JOBCUR 59
"No current job",
#define ERR_JOBPREV 60
"No previous job",
#define ERR_JOBPAT 61
"No job matches pattern",
#define ERR_NESTING 62
"Fork nesting > %d; maybe `...` loop",
#define ERR_JOBCTRLSUB 63
"No job control in subshells",
#define ERR_BADPLPS 64
"Badly placed ()'s",
#define ERR_STOPPED 65
"%sThere are suspended jobs",
#define ERR_NODIR 66
"No other directory",
#define ERR_EMPTY 67
"Directory stack empty",
#define ERR_BADDIR 68
"Bad directory",
#define ERR_DIRUS 69
"usage: %s [-lvn]%s",
#define ERR_HFLAG 70
"No operand for -h flag",
#define ERR_NOTLOGIN 71
"Not a login shell",
#define ERR_DIV0 72
"Division by 0",
#define ERR_MOD0 73
"Mod by 0",
#define ERR_BADSCALE 74
"Bad scaling; did you mean \"%s\"?",
#define ERR_SUSPLOG 75
"Can't suspend a login shell (yet)",
#define ERR_UNKUSER 76
"Unknown user: %s",
#define ERR_NOHOME 77
"No $home variable set",
#define ERR_HISTUS 78
"usage: history [-rh] [# number of events]",
#define ERR_SPDOLLT 79
"$, ! or < not allowed with $# or $?",
#define ERR_NEWLINE 80
"Newline in variable name",
#define ERR_SPSTAR 81
"* not allowed with $# or $?",
#define ERR_DIGIT 82
"$?<digit> or $#<digit> not allowed",
#define ERR_VARILL 83
"Illegal variable name",
#define ERR_NLINDEX 84
"Newline in variable index",
#define ERR_EXPOVFL 85
"Expansion buffer overflow",
#define ERR_VARSYN 86
"Variable syntax",
#define ERR_BADBANG 87
"Bad ! form",
#define ERR_NOSUBST 88
"No previous substitute",
#define ERR_BADSUBST 89
"Bad substitute",
#define ERR_LHS 90
"No previous left hand side",
#define ERR_RHSLONG 91
"Right hand side too long",
#define ERR_BADBANGMOD 92
"Bad ! modifier: %c",
#define ERR_MODFAIL 93
"Modifier failed",
#define ERR_SUBOVFL 94
"Substitution buffer overflow",
#define ERR_BADBANGARG 95
"Bad ! arg selector",
#define ERR_NOSEARCH 96
"No prev search",
#define ERR_NOEVENT 97
"%s: Event not found",
#define ERR_TOOMANYRP 98
"Too many )'s",
#define ERR_TOOMANYLP 99
"Too many ('s",
#define ERR_BADPLP 100
"Badly placed (",
#define ERR_MISRED 101
"Missing name for redirect",
#define ERR_OUTRED 102
"Ambiguous output redirect",
#define ERR_REDPAR 103
"Can't << within ()'s",
#define ERR_INRED 104
"Ambiguous input redirect",
#define ERR_ALIASLOOP 105
"Alias loop",
#define ERR_HISTLOOP 106
"!# History loop",
#define ERR_ARCH 107
"%s: %s. Wrong Architecture",
#define ERR_FILEINQ 108
"Malformed file inquiry",
#define ERR_SELOVFL 109
"Selector overflow",
#define ERR_INVALID 110
"Invalid Error"
};
/*
* The parser and scanner set up errors for later by calling seterr,
* which sets the variable err as a side effect; later to be tested,
* e.g. in process.
*/
void
seterror(int id, ...)
{
if (seterr == 0) {
char berr[BUFSIZE];
va_list va;
va_start(va, id);
if (id < 0 || id >= (int)(sizeof(errorlist) / sizeof(errorlist[0])) - 1)
id = ERR_INVALID;
(void)vsnprintf(berr, sizeof(berr), errorlist[id], va);
va_end(va);
seterr = strsave(berr);
}
}
/*
* Print the error with the given id.
*
* Special ids:
* ERR_SILENT: Print nothing.
* ERR_OLD: Print the previously set error if one was there.
* otherwise return.
* ERR_NAME: If this bit is set, print the name of the function
* in bname
*
* This routine always resets or exits. The flag haderr
* is set so the routine who catches the unwind can propogate
* it if they want.
*
* Note that any open files at the point of error will eventually
* be closed in the routine process in sh.c which is the only
* place error unwinds are ever caught.
*/
void
stderror(int id, ...)
{
va_list va;
Char **v;
int flags;
flags = id & ERR_FLAGS;
id &= ~ERR_FLAGS;
if ((flags & ERR_OLD) && seterr == NULL)
abort();
if (id < 0 || id > (int)(sizeof(errorlist) / sizeof(errorlist[0])))
id = ERR_INVALID;
(void)fflush(cshout);
(void)fflush(csherr);
haderr = 1; /* Now to diagnostic output */
timflg = 0; /* This isn't otherwise reset */
if (!(flags & ERR_SILENT)) {
if (flags & ERR_NAME)
(void)fprintf(csherr, "%s: ", bname);
if ((flags & ERR_OLD))
/* Old error. */
(void)fprintf(csherr, "%s.\n", seterr);
else {
va_start(va, id);
(void)vfprintf(csherr, errorlist[id], va);
va_end(va);
(void)fprintf(csherr, ".\n");
}
}
if (seterr) {
xfree((ptr_t) seterr);
seterr = NULL;
}
if ((v = pargv) != NULL)
pargv = 0, blkfree(v);
if ((v = gargv) != NULL)
gargv = 0, blkfree(v);
(void)fflush(cshout);
(void)fflush(csherr);
didfds = 0; /* Forget about 0,1,2 */
/*
* Go away if -e or we are a child shell
*/
if (exiterr || child)
xexit(1);
/*
* Reset the state of the input. This buffered seek to end of file will
* also clear the while/foreach stack.
*/
btoeof();
set(STRstatus, Strsave(STR1));
if (tpgrp > 0)
(void)tcsetpgrp(FSHTTY, tpgrp);
reset(); /* Unwind */
}

View File

@@ -1,746 +0,0 @@
/* $NetBSD: exec.c,v 1.29 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)exec.c 8.3 (Berkeley) 5/23/95";
#else
__RCSID("$NetBSD: exec.c,v 1.29 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "csh.h"
#include "extern.h"
/*
* System level search and execute of a command. We look in each directory
* for the specified command name. If the name contains a '/' then we
* execute only the full path name. If there is no search path then we
* execute only full path names.
*/
extern char **environ;
/*
* As we search for the command we note the first non-trivial error
* message for presentation to the user. This allows us often
* to show that a file has the wrong mode/no access when the file
* is not in the last component of the search path, so we must
* go on after first detecting the error.
*/
static const char *exerr; /* Execution error message */
static Char *expath; /* Path for exerr */
/*
* Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
* to hash execs. If it is allocated (havhash true), then to tell
* whether ``name'' is (possibly) present in the i'th component
* of the variable path, you look at the bit in xhash indexed by
* hash(hashname("name"), i). This is setup automatically
* after .login is executed, and recomputed whenever ``path'' is
* changed.
* The two part hash function is designed to let texec() call the
* more expensive hashname() only once and the simple hash() several
* times (once for each path component checked).
* Byte size is assumed to be 8.
*/
#define HSHSIZ 8192 /* 1k bytes */
#define HSHMASK (HSHSIZ - 1)
#define HSHMUL 243
static unsigned char xhash[HSHSIZ / 8];
#define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
#define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
#define bis(h, b) ((h)[(b) >> 3] |= (unsigned char)(1 << ((b) & 7))) /* bit set */
static int hits, misses;
/* Dummy search path for just absolute search when no path */
static Char *justabs[] = {STRNULL, 0};
static void pexerr(void) __dead;
static void texec(Char *, Char **);
static int hashname(Char *);
static int tellmewhat(struct wordent *, Char *);
static int executable(Char *, Char *, int);
static int iscommand(Char *);
void
/*ARGSUSED*/
doexec(Char **v, struct command *t)
{
struct varent *pathv;
Char *blk[2], **av, *dp, **pv, *sav;
int i, hashval, hashval1;
sigset_t nsigset;
int slash;
hashval = 0;
/*
* Glob the command name. We will search $path even if this does something,
* as in sh but not in csh. One special case: if there is no PATH, then we
* execute only commands which start with '/'.
*/
blk[0] = t->t_dcom[0];
blk[1] = 0;
gflag = 0, tglob(blk);
if (gflag) {
pv = globall(blk);
if (pv == 0) {
setname(vis_str(blk[0]));
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = 0;
}
else
pv = saveblk(blk);
trim(pv);
exerr = 0;
expath = Strsave(pv[0]);
Vexpath = expath;
pathv = adrof(STRpath);
if (pathv == 0 && expath[0] != '/') {
blkfree(pv);
pexerr();
}
slash = any(short2str(expath), '/');
/*
* Glob the argument list, if necessary. Otherwise trim off the quote bits.
*/
gflag = 0;
av = &t->t_dcom[1];
tglob(av);
if (gflag) {
av = globall(av);
if (av == 0) {
blkfree(pv);
setname(vis_str(expath));
stderror(ERR_NAME | ERR_NOMATCH);
}
gargv = 0;
}
else
av = saveblk(av);
blkfree(t->t_dcom);
t->t_dcom = blkspl(pv, av);
xfree((ptr_t) pv);
xfree((ptr_t) av);
av = t->t_dcom;
trim(av);
if (*av == NULL || **av == '\0')
pexerr();
xechoit(av); /* Echo command if -x */
/*
* Since all internal file descriptors are set to close on exec, we don't
* need to close them explicitly here. Just reorient ourselves for error
* messages.
*/
SHIN = 0;
SHOUT = 1;
SHERR = 2;
OLDSTD = 0;
/*
* We must do this AFTER any possible forking (like `foo` in glob) so that
* this shell can still do subprocesses.
*/
sigemptyset(&nsigset);
(void)sigprocmask(SIG_SETMASK, &nsigset, NULL);
/*
* If no path, no words in path, or a / in the filename then restrict the
* command search.
*/
if (pathv == 0 || pathv->vec[0] == 0 || slash)
pv = justabs;
else
pv = pathv->vec;
sav = Strspl(STRslash, *av); /* / command name for postpending */
Vsav = sav;
if (havhash)
hashval = hashname(*av);
i = 0;
hits++;
do {
/*
* Try to save time by looking at the hash table for where this command
* could be. If we are doing delayed hashing, then we put the names in
* one at a time, as the user enters them. This is kinda like Korn
* Shell's "tracked aliases".
*/
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
texec(*av, av);
else {
dp = Strspl(*pv, sav);
Vdp = dp;
texec(dp, av);
Vdp = 0;
xfree((ptr_t)dp);
}
misses++;
cont:
pv++;
i++;
} while (*pv);
hits--;
Vsav = 0;
xfree((ptr_t)sav);
pexerr();
/* NOTREACHED */
}
static void
pexerr(void)
{
/* Couldn't find the damn thing */
if (expath) {
setname(vis_str(expath));
Vexpath = 0;
xfree((ptr_t)expath);
expath = 0;
}
else
setname("");
if (exerr)
stderror(ERR_NAME | ERR_STRING, exerr);
else
stderror(ERR_NAME | ERR_COMMAND);
/* NOTREACHED */
}
/*
* Execute command f, arg list t.
* Record error message if not found.
* Also do shell scripts here.
*/
static void
texec(Char *sf, Char **st)
{
struct varent *v;
Char *lastsh[2], **vp, *st0, **ost;
char *f, **t;
int fd;
unsigned char c = '\0';
/* The order for the conversions is significant */
t = short2blk(st);
f = short2str(sf);
Vt = t;
errno = 0; /* don't use a previous error */
(void)execve(f, t, environ);
Vt = 0;
blkfree((Char **)t);
switch (errno) {
case ENOEXEC:
/*
* From: casper@fwi.uva.nl (Casper H.S. Dik) If we could not execute
* it, don't feed it to the shell if it looks like a binary!
*/
if ((fd = open(f, O_RDONLY)) != -1) {
if (read(fd, (char *)&c, 1) == 1) {
if (!Isprint(c) && (c != '\n' && c != '\t')) {
(void)close(fd);
/*
* We *know* what ENOEXEC means.
*/
stderror(ERR_ARCH, f, strerror(errno));
}
}
#ifdef _PATH_BSHELL
else
c = '#';
#endif
(void)close(fd);
}
/*
* If there is an alias for shell, then put the words of the alias in
* front of the argument list replacing the command name. Note no
* interpretation of the words at this point.
*/
v = adrof1(STRshell, &aliases);
if (v == 0) {
vp = lastsh;
vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
vp[1] = NULL;
#ifdef _PATH_BSHELL
if (fd != -1 && c != '#')
vp[0] = STR_BSHELL;
#endif
}
else
vp = v->vec;
st0 = st[0];
st[0] = sf;
ost = st;
st = blkspl(vp, st); /* Splice up the new arglst */
ost[0] = st0;
sf = *st;
/* The order for the conversions is significant */
t = short2blk(st);
f = short2str(sf);
xfree((ptr_t) st);
Vt = t;
(void)execve(f, t, environ);
Vt = 0;
blkfree((Char **) t);
/* FALLTHROUGH */
case ENOMEM:
stderror(ERR_SYSTEM, f, strerror(errno));
/* NOTREACHED */
case ENOENT:
break;
default:
if (exerr == 0) {
exerr = strerror(errno);
if (expath)
xfree((ptr_t) expath);
expath = Strsave(sf);
Vexpath = expath;
}
}
}
/*ARGSUSED*/
void
execash(Char **t, struct command *kp)
{
jmp_buf osetexit;
sig_t osigint, osigquit, osigterm;
int my_reenter, odidfds, oOLDSTD, oSHERR, oSHIN, oSHOUT;
int saveDIAG, saveIN, saveOUT, saveSTD;
if (chkstop == 0 && setintr)
panystop(0);
/*
* Hmm, we don't really want to do that now because we might
* fail, but what is the choice
*/
rechist();
osigint = signal(SIGINT, parintr);
osigquit = signal(SIGQUIT, parintr);
osigterm = signal(SIGTERM, parterm);
odidfds = didfds;
oSHIN = SHIN;
oSHOUT = SHOUT;
oSHERR = SHERR;
oOLDSTD = OLDSTD;
saveIN = dcopy(SHIN, -1);
saveOUT = dcopy(SHOUT, -1);
saveDIAG = dcopy(SHERR, -1);
saveSTD = dcopy(OLDSTD, -1);
lshift(kp->t_dcom, 1);
getexit(osetexit);
if ((my_reenter = setexit()) == 0) {
SHIN = dcopy(0, -1);
SHOUT = dcopy(1, -1);
SHERR = dcopy(2, -1);
didfds = 0;
doexec(t, kp);
}
(void)signal(SIGINT, osigint);
(void)signal(SIGQUIT, osigquit);
(void)signal(SIGTERM, osigterm);
doneinp = 0;
didfds = odidfds;
(void)close(SHIN);
(void)close(SHOUT);
(void)close(SHERR);
(void)close(OLDSTD);
SHIN = dmove(saveIN, oSHIN);
SHOUT = dmove(saveOUT, oSHOUT);
SHERR = dmove(saveDIAG, oSHERR);
OLDSTD = dmove(saveSTD, oOLDSTD);
resexit(osetexit);
if (my_reenter)
stderror(ERR_SILENT);
}
void
xechoit(Char **t)
{
if (adrof(STRecho)) {
int odidfds = didfds;
(void)fflush(csherr);
odidfds = didfds;
didfds = 0;
blkpr(csherr, t);
(void)fputc('\n', csherr);
(void)fflush(csherr);
didfds = odidfds;
}
}
void
/*ARGSUSED*/
dohash(Char **v, struct command *t)
{
struct dirent *dp;
struct varent *pathv;
DIR *dirp;
Char **pv;
size_t cnt;
int hashval, i;
i = 0;
havhash = 1;
pathv = adrof(STRpath);
for (cnt = 0; cnt < sizeof xhash; cnt++)
xhash[cnt] = 0;
if (pathv == 0)
return;
for (pv = pathv->vec; *pv; pv++, i++) {
if (pv[0][0] != '/')
continue;
dirp = opendir(short2str(*pv));
if (dirp == NULL)
continue;
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_ino == 0)
continue;
if (dp->d_name[0] == '.' &&
(dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
continue;
hashval = hash(hashname(str2short(dp->d_name)), i);
bis(xhash, hashval);
/* tw_add_comm_name (dp->d_name); */
}
(void) closedir(dirp);
}
}
void
/*ARGSUSED*/
dounhash(Char **v, struct command *t)
{
havhash = 0;
}
void
/*ARGSUSED*/
hashstat(Char **v, struct command *t)
{
if (hits + misses)
(void)fprintf(cshout, "%d hits, %d misses, %d%%\n",
hits, misses, 100 * hits / (hits + misses));
}
/*
* Hash a command name.
*/
static int
hashname(Char *cp)
{
long h = 0;
while (*cp)
h = hash(h, *cp++);
return ((int) h);
}
static int
iscommand(Char *name)
{
struct varent *v;
Char **pv, *sav;
int hashval, hashval1, i;
int slash;
hashval = 0;
slash = any(short2str(name), '/');
v = adrof(STRpath);
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
sav = Strspl(STRslash, name); /* / command name for postpending */
if (havhash)
hashval = hashname(name);
i = 0;
do {
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
if (executable(NULL, name, 0)) {
xfree((ptr_t) sav);
return i + 1;
}
}
else {
if (executable(*pv, sav, 0)) {
xfree((ptr_t) sav);
return i + 1;
}
}
cont:
pv++;
i++;
} while (*pv);
xfree((ptr_t) sav);
return 0;
}
/* Also by:
* Andreas Luik <luik@isaak.isa.de>
* I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
* Azenberstr. 35
* D-7000 Stuttgart 1
* West-Germany
* is the executable() routine below and changes to iscommand().
* Thanks again!!
*/
/*
* executable() examines the pathname obtained by concatenating dir and name
* (dir may be NULL), and returns 1 either if it is executable by us, or
* if dir_ok is set and the pathname refers to a directory.
* This is a bit kludgy, but in the name of optimization...
*/
static int
executable(Char *dir, Char *name, int dir_ok)
{
struct stat stbuf;
Char path[MAXPATHLEN + 1], *dp, *sp;
char *strname;
if (dir && *dir) {
for (dp = path, sp = dir; *sp; *dp++ = *sp++)
if (dp == &path[MAXPATHLEN + 1]) {
*--dp = '\0';
break;
}
for (sp = name; *sp; *dp++ = *sp++)
if (dp == &path[MAXPATHLEN + 1]) {
*--dp = '\0';
break;
}
*dp = '\0';
strname = short2str(path);
}
else
strname = short2str(name);
return (stat(strname, &stbuf) != -1 && ((S_ISREG(stbuf.st_mode) &&
/* save time by not calling access() in the hopeless case */
(stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
access(strname, X_OK) == 0) || (dir_ok && S_ISDIR(stbuf.st_mode))));
}
/* The dowhich() is by:
* Andreas Luik <luik@isaak.isa.de>
* I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
* Azenberstr. 35
* D-7000 Stuttgart 1
* West-Germany
* Thanks!!
*/
/*ARGSUSED*/
void
dowhich(Char **v, struct command *c)
{
struct wordent lexw[3];
struct varent *vp;
lexw[0].next = &lexw[1];
lexw[1].next = &lexw[2];
lexw[2].next = &lexw[0];
lexw[0].prev = &lexw[2];
lexw[1].prev = &lexw[0];
lexw[2].prev = &lexw[1];
lexw[0].word = STRNULL;
lexw[2].word = STRret;
while (*++v) {
if ((vp = adrof1(*v, &aliases)) != NULL) {
(void)fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
blkpr(cshout, vp->vec);
(void)fputc('\n', cshout);
set(STRstatus, Strsave(STR0));
}
else {
lexw[1].word = *v;
set(STRstatus, Strsave(tellmewhat(lexw, NULL) ? STR0 : STR1));
}
}
}
static int
tellmewhat(struct wordent *lexp, Char *str)
{
struct biltins *bptr;
struct wordent *sp;
Char *cmd, *s0, *s1, *s2;
int i;
int aliased, found;
Char qc;
aliased = 0;
sp = lexp->next;
if (adrof1(sp->word, &aliases)) {
alias(lexp);
sp = lexp->next;
aliased = 1;
}
s0 = sp->word; /* to get the memory freeing right... */
/* handle quoted alias hack */
if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
(sp->word)++;
/* do quoting, if it hasn't been done */
s1 = s2 = sp->word;
while (*s2)
switch (*s2) {
case '\'':
case '"':
qc = *s2++;
while (*s2 && *s2 != qc)
*s1++ = (Char)(*s2++ | QUOTE);
if (*s2)
s2++;
break;
case '\\':
if (*++s2)
*s1++ = (Char)(*s2++ | QUOTE);
break;
default:
*s1++ = *s2++;
}
*s1 = '\0';
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
if (eq(sp->word, str2short(bptr->bname))) {
if (str == NULL) {
if (aliased)
prlex(cshout, lexp);
(void)fprintf(cshout, "%s: shell built-in command.\n",
vis_str(sp->word));
}
else
(void)Strcpy(str, sp->word);
sp->word = s0; /* we save and then restore this */
return 1;
}
}
sp->word = cmd = globone(sp->word, G_IGNORE);
if ((i = iscommand(sp->word)) != 0) {
Char **pv;
struct varent *v;
int slash = any(short2str(sp->word), '/');
v = adrof(STRpath);
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
while (--i)
pv++;
if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
if (!slash) {
sp->word = Strspl(STRdotsl, sp->word);
prlex(cshout, lexp);
xfree((ptr_t) sp->word);
}
else
prlex(cshout, lexp);
}
else {
s1 = Strspl(*pv, STRslash);
sp->word = Strspl(s1, sp->word);
xfree((ptr_t) s1);
if (str == NULL)
prlex(cshout, lexp);
else
(void)Strcpy(str, sp->word);
xfree((ptr_t) sp->word);
}
found = 1;
}
else {
if (str == NULL) {
if (aliased)
prlex(cshout, lexp);
(void)fprintf(csherr,
"%s: Command not found.\n", vis_str(sp->word));
}
else
(void)Strcpy(str, sp->word);
found = 0;
}
sp->word = s0; /* we save and then restore this */
xfree((ptr_t) cmd);
return found;
}

View File

@@ -1,661 +0,0 @@
/* $NetBSD: exp.c,v 1.20 2009/02/14 07:12:29 lukem Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)exp.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: exp.c,v 1.20 2009/02/14 07:12:29 lukem Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#include "csh.h"
#include "extern.h"
#define IGNORE 1 /* in ignore, it means to ignore value, just parse */
#define NOGLOB 2 /* in ignore, it means not to globone */
#define ADDOP 1
#define MULOP 2
#define EQOP 4
#define RELOP 8
#define RESTOP 16
#define ANYOP 31
#define EQEQ 1
#define GTR 2
#define LSS 4
#define NOTEQ 6
#define EQMATCH 7
#define NOTEQMATCH 8
static int exp1(Char ***, int);
static int csh_exp2(Char ***, int);
static int exp2a(Char ***, int);
static int exp2b(Char ***, int);
static int exp2c(Char ***, int);
static Char *exp3(Char ***, int);
static Char *exp3a(Char ***, int);
static Char *exp4(Char ***, int);
static Char *exp5(Char ***, int);
static Char *exp6(Char ***, int);
static void evalav(Char **);
static int isa(Char *, int);
static int egetn(Char *);
#ifdef EDEBUG
static void etracc(char *, Char *, Char ***);
static void etraci(char *, int, Char ***);
#endif
int
expr(Char ***vp)
{
return (exp0(vp, 0));
}
int
exp0(Char ***vp, int ignore)
{
int p1;
p1 = exp1(vp, ignore);
#ifdef EDEBUG
etraci("exp0 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRor2)) {
int p2;
(*vp)++;
p2 = exp0(vp, (ignore & IGNORE) || p1);
#ifdef EDEBUG
etraci("exp0 p2", p2, vp);
#endif
return (p1 || p2);
}
return (p1);
}
static int
exp1(Char ***vp, int ignore)
{
int p1;
p1 = csh_exp2(vp, ignore);
#ifdef EDEBUG
etraci("exp1 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRand2)) {
int p2;
(*vp)++;
p2 = exp1(vp, (ignore & IGNORE) || !p1);
#ifdef EDEBUG
etraci("exp1 p2", p2, vp);
#endif
return (p1 && p2);
}
return (p1);
}
static int
csh_exp2(Char ***vp, int ignore)
{
int p1;
p1 = exp2a(vp, ignore);
#ifdef EDEBUG
etraci("exp3 p1", p1, vp);
#endif
if (**vp && eq(**vp, STRor)) {
int p2;
(*vp)++;
p2 = csh_exp2(vp, ignore);
#ifdef EDEBUG
etraci("exp3 p2", p2, vp);
#endif
return (p1 | p2);
}
return (p1);
}
static int
exp2a(Char ***vp, int ignore)
{
int p1;
p1 = exp2b(vp, ignore);
#ifdef EDEBUG
etraci("exp2a p1", p1, vp);
#endif
if (**vp && eq(**vp, STRcaret)) {
int p2;
(*vp)++;
p2 = exp2a(vp, ignore);
#ifdef EDEBUG
etraci("exp2a p2", p2, vp);
#endif
return (p1 ^ p2);
}
return (p1);
}
static int
exp2b(Char ***vp, int ignore)
{
int p1;
p1 = exp2c(vp, ignore);
#ifdef EDEBUG
etraci("exp2b p1", p1, vp);
#endif
if (**vp && eq(**vp, STRand)) {
int p2;
(*vp)++;
p2 = exp2b(vp, ignore);
#ifdef EDEBUG
etraci("exp2b p2", p2, vp);
#endif
return (p1 & p2);
}
return (p1);
}
static int
exp2c(Char ***vp, int ignore)
{
Char *p1, *p2;
int i;
p1 = exp3(vp, ignore);
#ifdef EDEBUG
etracc("exp2c p1", p1, vp);
#endif
if ((i = isa(**vp, EQOP)) != 0) {
(*vp)++;
if (i == EQMATCH || i == NOTEQMATCH)
ignore |= NOGLOB;
p2 = exp3(vp, ignore);
#ifdef EDEBUG
etracc("exp2c p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (i) {
case EQEQ:
i = eq(p1, p2);
break;
case EQMATCH:
i = Gmatch(p1, p2);
break;
case NOTEQ:
i = !eq(p1, p2);
break;
case NOTEQMATCH:
i = !Gmatch(p1, p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (i);
}
i = egetn(p1);
xfree((ptr_t) p1);
return (i);
}
static Char *
exp3(Char ***vp, int ignore)
{
Char *p1, *p2;
int i;
p1 = exp3a(vp, ignore);
#ifdef EDEBUG
etracc("exp3 p1", p1, vp);
#endif
if ((i = isa(**vp, RELOP)) != 0) {
(*vp)++;
if (**vp && eq(**vp, STRequal))
i |= 1, (*vp)++;
p2 = exp3(vp, ignore);
#ifdef EDEBUG
etracc("exp3 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (i) {
case GTR:
i = egetn(p1) > egetn(p2);
break;
case GTR | 1:
i = egetn(p1) >= egetn(p2);
break;
case LSS:
i = egetn(p1) < egetn(p2);
break;
case LSS | 1:
i = egetn(p1) <= egetn(p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp3a(Char ***vp, int ignore)
{
Char *op, *p1, *p2;
int i;
p1 = exp4(vp, ignore);
#ifdef EDEBUG
etracc("exp3a p1", p1, vp);
#endif
op = **vp;
if (op && any("<>", op[0]) && op[0] == op[1]) {
(*vp)++;
p2 = exp3a(vp, ignore);
#ifdef EDEBUG
etracc("exp3a p2", p2, vp);
#endif
if (op[0] == '<')
i = egetn(p1) << egetn(p2);
else
i = egetn(p1) >> egetn(p2);
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp4(Char ***vp, int ignore)
{
Char *p1, *p2;
int i;
i = 0;
p1 = exp5(vp, ignore);
#ifdef EDEBUG
etracc("exp4 p1", p1, vp);
#endif
if (isa(**vp, ADDOP)) {
Char *op;
op = *(*vp)++;
p2 = exp4(vp, ignore);
#ifdef EDEBUG
etracc("exp4 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (op[0]) {
case '+':
i = egetn(p1) + egetn(p2);
break;
case '-':
i = egetn(p1) - egetn(p2);
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp5(Char ***vp, int ignore)
{
Char *p1, *p2;
int i;
i = 0;
p1 = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp5 p1", p1, vp);
#endif
if (isa(**vp, MULOP)) {
Char *op;
op = *(*vp)++;
p2 = exp5(vp, ignore);
#ifdef EDEBUG
etracc("exp5 p2", p2, vp);
#endif
if (!(ignore & IGNORE))
switch (op[0]) {
case '*':
i = egetn(p1) * egetn(p2);
break;
case '/':
i = egetn(p2);
if (i == 0)
stderror(ERR_DIV0);
i = egetn(p1) / i;
break;
case '%':
i = egetn(p2);
if (i == 0)
stderror(ERR_MOD0);
i = egetn(p1) % i;
break;
}
xfree((ptr_t) p1);
xfree((ptr_t) p2);
return (putn(i));
}
return (p1);
}
static Char *
exp6(Char ***vp, int ignore)
{
Char *cp, *dp, *ep;
int ccode, i;
i = 0;
if (**vp == 0)
stderror(ERR_NAME | ERR_EXPRESSION);
if (eq(**vp, STRbang)) {
(*vp)++;
cp = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp6 ! cp", cp, vp);
#endif
i = egetn(cp);
xfree((ptr_t) cp);
return (putn(!i));
}
if (eq(**vp, STRtilde)) {
(*vp)++;
cp = exp6(vp, ignore);
#ifdef EDEBUG
etracc("exp6 ~ cp", cp, vp);
#endif
i = egetn(cp);
xfree((ptr_t) cp);
return (putn(~i));
}
if (eq(**vp, STRLparen)) {
(*vp)++;
ccode = exp0(vp, ignore);
#ifdef EDEBUG
etraci("exp6 () ccode", ccode, vp);
#endif
if (**vp == 0 || ***vp != ')')
stderror(ERR_NAME | ERR_EXPRESSION);
(*vp)++;
return (putn(ccode));
}
if (eq(**vp, STRLbrace)) {
struct command faket;
Char *fakecom[2];
Char **v;
faket.t_dtyp = NODE_COMMAND;
faket.t_dflg = 0;
faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
faket.t_dcom = fakecom;
fakecom[0] = STRfakecom;
fakecom[1] = NULL;
(*vp)++;
v = *vp;
for (;;) {
if (!**vp)
stderror(ERR_NAME | ERR_MISSING, '}');
if (eq(*(*vp)++, STRRbrace))
break;
}
if (ignore & IGNORE)
return (Strsave(STRNULL));
psavejob();
if (pfork(&faket, -1) == 0) {
*--(*vp) = 0;
evalav(v);
exitstat();
}
pwait();
prestjob();
#ifdef EDEBUG
etraci("exp6 {} status", egetn(value(STRstatus)), vp);
#endif
return (putn(egetn(value(STRstatus)) == 0));
}
if (isa(**vp, ANYOP))
return (Strsave(STRNULL));
cp = *(*vp)++;
if (*cp == '-' && any("erwxfdzopls", cp[1])) {
struct stat stb;
if (cp[2] != '\0')
stderror(ERR_NAME | ERR_FILEINQ);
/*
* Detect missing file names by checking for operator in the file name
* position. However, if an operator name appears there, we must make
* sure that there's no file by that name (e.g., "/") before announcing
* an error. Even this check isn't quite right, since it doesn't take
* globbing into account.
*/
if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
stderror(ERR_NAME | ERR_FILENAME);
dp = *(*vp)++;
if (ignore & IGNORE)
return (Strsave(STRNULL));
ep = globone(dp, G_ERROR);
switch (cp[1]) {
case 'r':
i = !access(short2str(ep), R_OK);
break;
case 'w':
i = !access(short2str(ep), W_OK);
break;
case 'x':
i = !access(short2str(ep), X_OK);
break;
default:
if (cp[1] == 'l' ?
lstat(short2str(ep), &stb) : stat(short2str(ep), &stb)) {
xfree((ptr_t) ep);
return (Strsave(STR0));
}
switch (cp[1]) {
case 'd':
i = S_ISDIR(stb.st_mode);
break;
case 'e':
i = 1;
break;
case 'f':
i = S_ISREG(stb.st_mode);
break;
case 'l':
#ifdef S_ISLNK
i = S_ISLNK(stb.st_mode);
#else
i = 0;
#endif
break;
case 'o':
i = stb.st_uid == (uid_t)uid;
break;
case 'p':
#ifdef S_ISFIFO
i = S_ISFIFO(stb.st_mode);
#else
i = 0;
#endif
break;
case 's':
#ifdef S_ISSOCK
i = S_ISSOCK(stb.st_mode);
#else
i = 0;
#endif
break;
case 'z':
i = stb.st_size == 0;
break;
}
}
#ifdef EDEBUG
etraci("exp6 -? i", i, vp);
#endif
xfree((ptr_t) ep);
return (putn(i));
}
#ifdef EDEBUG
etracc("exp6 default", cp, vp);
#endif
return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
}
static void
evalav(Char **v)
{
struct wordent *hp, paraml1, *wdp;
struct command *t;
hp = &paraml1;
wdp = hp;
set(STRstatus, Strsave(STR0));
hp->prev = hp->next = hp;
hp->word = STRNULL;
while (*v) {
struct wordent *new;
new = (struct wordent *)xcalloc(1, sizeof *wdp);
new->prev = wdp;
new->next = hp;
wdp->next = new;
wdp = new;
wdp->word = Strsave(*v++);
}
hp->prev = wdp;
alias(&paraml1);
t = syntax(paraml1.next, &paraml1, 0);
if (seterr)
stderror(ERR_OLD);
execute(t, -1, NULL, NULL);
freelex(&paraml1), freesyn(t);
}
static int
isa(Char *cp, int what)
{
if (cp == 0)
return ((what & RESTOP) != 0);
if (cp[1] == 0) {
if (what & ADDOP && (*cp == '+' || *cp == '-'))
return (1);
if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
return (1);
if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
*cp == '~' || *cp == '^' || *cp == '"'))
return (1);
}
else if (cp[2] == 0) {
if (what & RESTOP) {
if (cp[0] == '|' && cp[1] == '&')
return (1);
if (cp[0] == '<' && cp[1] == '<')
return (1);
if (cp[0] == '>' && cp[1] == '>')
return (1);
}
if (what & EQOP) {
if (cp[0] == '=') {
if (cp[1] == '=')
return (EQEQ);
if (cp[1] == '~')
return (EQMATCH);
}
else if (cp[0] == '!') {
if (cp[1] == '=')
return (NOTEQ);
if (cp[1] == '~')
return (NOTEQMATCH);
}
}
}
if (what & RELOP) {
if (*cp == '<')
return (LSS);
if (*cp == '>')
return (GTR);
}
return (0);
}
static int
egetn(Char *cp)
{
if (*cp && *cp != '-' && !Isdigit(*cp))
stderror(ERR_NAME | ERR_EXPRESSION);
return (getn(cp));
}
/* Phew! */
#ifdef EDEBUG
static void
etraci(char *str, int i, Char ***vp)
{
(void)fprintf(csherr, "%s=%d\t", str, i);
blkpr(csherr, *vp);
(void)fprintf(csherr, "\n");
}
static void
etracc(char *str, Char *cp, Char ***vp)
{
(void)fprintf(csherr, "%s=%s\t", str, vis_str(cp));
blkpr(csherr, *vp);
(void)fprintf(csherr, "\n");
}
#endif

View File

@@ -1,341 +0,0 @@
/* $NetBSD: extern.h,v 1.29 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _EXTERN_H_
#define _EXTERN_H_
#include <sys/cdefs.h>
/*
* csh.c
*/
int gethdir(Char *);
void dosource(Char **, struct command *);
__dead void exitstat(void);
__dead void goodbye(void);
void importpath(Char *);
void initdesc(void);
__dead void pintr(int);
__dead void pintr1(int);
void printprompt(void);
#ifdef EDIT
char *printpromptstr(EditLine *);
#endif
void process(int);
void rechist(void);
void untty(void);
int vis_fputc(int, FILE *);
#ifdef PROF
__dead void done(int);
#else
__dead void xexit(int);
#endif
/*
* dir.c
*/
void dinit(Char *);
void dodirs(Char **, struct command *);
Char *dcanon(Char *, Char *);
void dtildepr(Char *, Char *);
void dtilde(void);
void dochngd(Char **, struct command *);
Char *dnormalize(Char *);
void dopushd(Char **, struct command *);
void dopopd(Char **, struct command *);
struct directory;
void dfree(struct directory *);
/*
* dol.c
*/
void Dfix(struct command *);
Char *Dfix1(Char *);
void heredoc(Char *);
/*
* err.c
*/
void seterror(int, ...);
__dead void stderror(int, ...);
/*
* exec.c
*/
__dead void doexec(Char **, struct command *);
void dohash(Char **, struct command *);
void dounhash(Char **, struct command *);
void dowhich(Char **, struct command *);
void execash(Char **, struct command *);
void hashstat(Char **, struct command *);
void xechoit(Char **);
/*
* exp.c
*/
int expr(Char ***);
int exp0(Char ***, int);
/*
* file.c
*/
#ifdef FILEC
ssize_t tenex(Char *, size_t);
#endif
/*
* func.c
*/
void Setenv(Char *, Char *);
void doalias(Char **, struct command *);
void dobreak(Char **, struct command *);
void docontin(Char **, struct command *);
void doecho(Char **, struct command *);
void doelse(Char **, struct command *);
void doend(Char **, struct command *);
void doeval(Char **, struct command *);
void doexit(Char **, struct command *);
void doforeach(Char **, struct command *);
void doglob(Char **, struct command *);
void dogoto(Char **, struct command *);
void doif(Char **, struct command *);
void dolimit(Char **, struct command *);
__dead void dologin(Char **, struct command *);
__dead void dologout(Char **, struct command *);
void donohup(Char **, struct command *);
void doonintr(Char **, struct command *);
void doprintf(Char **, struct command *);
void dorepeat(Char **, struct command *);
void dosetenv(Char **, struct command *);
void dosuspend(Char **, struct command *);
void doswbrk(Char **, struct command *);
void doswitch(Char **, struct command *);
void doumask(Char **, struct command *);
void dounlimit(Char **, struct command *);
void dounsetenv(Char **, struct command *);
void dowhile(Char **, struct command *);
void dozip(Char **, struct command *);
void func(struct command *, struct biltins *);
struct biltins *isbfunc(struct command *);
void prvars(void);
void gotolab(Char *);
int srchx(Char *);
void unalias(Char **, struct command *);
void wfree(void);
/*
* glob.c
*/
Char **dobackp(Char *, int);
void Gcat(Char *, Char *);
Char *globone(Char *, int);
int Gmatch(Char *, Char *);
void ginit(void);
Char **globall(Char **);
void rscan(Char **, void (*)(int));
void tglob(Char **);
void trim(Char **);
#ifdef FILEC
int sortscmp(const ptr_t, const ptr_t);
#endif /* FILEC */
/*
* hist.c
*/
void dohist(Char **, struct command *);
struct Hist *enthist(int, struct wordent *, int);
#ifdef EDIT
void loadhist(struct Hist *);
#endif
void savehist(struct wordent *);
/*
* lex.c
*/
void addla(Char *);
void bseek(struct Ain *);
void btell(struct Ain *);
void btoeof(void);
void copylex(struct wordent *, struct wordent *);
Char *domod(Char *, int);
void freelex(struct wordent *);
int lex(struct wordent *);
void prlex(FILE *, struct wordent *);
#ifdef EDIT
int sprlex(char **, struct wordent *);
#endif
int readc(int);
void settell(void);
void unreadc(int);
/*
* misc.c
*/
int any(const char *, int);
Char **blkcat(Char **, Char **);
Char **blkcpy(Char **, Char **);
Char **blkend(Char **);
void blkfree(Char **);
int blklen(Char **);
void blkpr(FILE *, Char **);
Char **blkspl(Char **, Char **);
void closem(void);
Char **copyblk(Char **);
int dcopy(int, int);
int dmove(int, int);
void donefds(void);
Char lastchr(Char *);
void lshift(Char **, size_t);
int number(Char *);
int prefix(Char *, Char *);
Char **saveblk(Char **);
Char *strip(Char *);
Char *quote(Char *);
char *strsave(const char *);
char *strspl(char *, char *);
__dead void udvar(Char *);
#ifndef SHORT_STRINGS
# ifdef NOTUSED
char *strstr(const char *, const char *);
# endif /* NOTUSED */
char *strend(char *);
#endif
/*
* parse.c
*/
void alias(struct wordent *);
void freesyn(struct command *);
struct command *syntax(struct wordent *, struct wordent *, int);
/*
* proc.c
*/
void dobg(Char **, struct command *);
void dobg1(Char **, struct command *);
void dofg(Char **, struct command *);
void dofg1(Char **, struct command *);
void dojobs(Char **, struct command *);
void dokill(Char **, struct command *);
void donotify(Char **, struct command *);
void dostop(Char **, struct command *);
void dowait(Char **, struct command *);
void palloc(int, struct command *);
void panystop(int);
void pchild(int);
void pendjob(void);
struct process *pfind(Char *);
int pfork(struct command *, int);
void pgetty(int, int);
void pjwait(struct process *);
void pnote(void);
void prestjob(void);
void psavejob(void);
void pstart(struct process *, int);
void pwait(void);
/*
* sem.c
*/
void execute(struct command *, int, int *, int *);
void mypipe(int *);
/*
* set.c
*/
struct varent*adrof1(Char *, struct varent *);
void doset(Char **, struct command *);
void dolet(Char **, struct command *);
Char *putn(int);
int getn(Char *);
Char *value1(Char *, struct varent *);
void set(Char *, Char *);
void set1(Char *, Char **, struct varent *);
void setq(Char *, Char **, struct varent *);
void unset(Char **, struct command *);
void unset1(Char *[], struct varent *);
void unsetv(Char *);
void setNS(Char *);
void shift(Char **, struct command *);
void plist(struct varent *);
/*
* time.c
*/
void donice(Char **, struct command *);
void dotime(Char **, struct command *);
void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *,
struct timespec *);
void ruadd(struct rusage *, struct rusage *);
void settimes(void);
void psecs(long);
/*
* alloc.c
*/
void Free(ptr_t);
ptr_t Malloc(size_t);
ptr_t Realloc(ptr_t, size_t);
ptr_t Calloc(size_t, size_t);
/*
* str.c:
*/
#ifdef SHORT_STRINGS
Char *s_strchr(const Char *, int);
Char *s_strrchr(const Char *, int);
Char *s_strcat(Char *, const Char *);
#ifdef NOTUSED
Char *s_strncat(Char *, const Char *, size_t);
#endif
Char *s_strcpy(Char *, const Char *);
Char *s_strncpy(Char *, const Char *, size_t);
Char *s_strspl(const Char *, const Char *);
size_t s_strlen(const Char *);
int s_strcmp(const Char *, const Char *);
int s_strncmp(const Char *, const Char *, size_t);
Char *s_strsave(const Char *);
Char *s_strend(const Char *);
Char *s_strstr(const Char *, const Char *);
Char *str2short(const char *);
Char **blk2short(char **);
char *short2str(const Char *);
char **short2blk(Char * const *);
#endif
char *short2qstr(const Char *);
char *vis_str(const Char *);
#endif /* !_EXTERN_H_ */

View File

@@ -1,729 +0,0 @@
/* $NetBSD: file.c,v 1.30 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)file.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: file.c,v 1.30 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#ifdef FILEC
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/tty.h>
#include <dirent.h>
#include <pwd.h>
#include <termios.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#include "csh.h"
#include "extern.h"
/*
* Tenex style file name recognition, .. and more.
* History:
* Author: Ken Greer, Sept. 1975, CMU.
* Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
*/
#define ON 1
#define OFF 0
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define ESC '\033'
typedef enum {
LIST, RECOGNIZE
} COMMAND;
static void setup_tty(int);
static void back_to_col_1(void);
static int pushback(Char *);
static void catn(Char *, Char *, size_t);
static void copyn(Char *, Char *, size_t);
static Char filetype(Char *, Char *);
static void print_by_column(Char *, Char *[], size_t);
static Char *tilde(Char *, Char *);
static void retype(void);
static void beep(void);
static void print_recognized_stuff(Char *);
static void extract_dir_and_name(Char *, Char *, Char *);
static Char *getentry(DIR *, int);
static void free_items(Char **, size_t);
static size_t tsearch(Char *, COMMAND, size_t);
static int recognize(Char *, Char *, size_t, size_t);
static int is_prefix(Char *, Char *);
static int is_suffix(Char *, Char *);
static int ignored(Char *);
/*
* Put this here so the binary can be patched with adb to enable file
* completion by default. Filec controls completion, nobeep controls
* ringing the terminal bell on incomplete expansions.
*/
int filec = 0;
static void
setup_tty(int on)
{
struct termios tchars;
(void)tcgetattr(SHIN, &tchars);
if (on) {
tchars.c_cc[VEOL] = ESC;
if (tchars.c_lflag & ICANON)
on = TCSADRAIN;
else {
tchars.c_lflag |= ICANON;
on = TCSAFLUSH;
}
}
else {
tchars.c_cc[VEOL] = _POSIX_VDISABLE;
on = TCSADRAIN;
}
(void)tcsetattr(SHIN, on, &tchars);
}
/*
* Move back to beginning of current line
*/
static void
back_to_col_1(void)
{
struct termios tty, tty_normal;
sigset_t nsigset, osigset;
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
(void)tcgetattr(SHOUT, &tty);
tty_normal = tty;
tty.c_iflag &= ~INLCR;
tty.c_oflag &= ~ONLCR;
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
(void)write(SHOUT, "\r", 1);
(void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
}
/*
* Push string contents back into tty queue
*/
static int
pushback(Char *string)
{
struct termios tty, tty_normal;
char buf[64], svchars[sizeof(buf)];
sigset_t nsigset, osigset;
Char *p;
size_t bufidx, i, len_str, nbuf, nsv, onsv, retrycnt;
char c;
nsv = 0;
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
(void)tcgetattr(SHOUT, &tty);
tty_normal = tty;
tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
/* FIONREAD works only in noncanonical mode. */
tty.c_lflag &= ~ICANON;
tty.c_cc[VMIN] = 0;
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
for (retrycnt = 5; ; retrycnt--) {
/*
* Push back characters.
*/
for (p = string; (c = (char)*p) != '\0'; p++)
(void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &c);
for (i = 0; i < nsv; i++)
(void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &svchars[i]);
if (retrycnt == 0)
break; /* give up salvaging characters */
len_str = (size_t)(p - string);
if (ioctl(SHOUT, FIONREAD, (ioctl_t) &nbuf) ||
nbuf <= len_str + nsv || /* The string fit. */
nbuf > sizeof(buf)) /* For future binary compatibility
(and safety). */
break;
/*
* User has typed characters before the pushback finished.
* Salvage the characters.
*/
/* This read() should be in noncanonical mode. */
if (read(SHOUT, &buf, nbuf) != (ssize_t)nbuf)
continue; /* hangup? */
onsv = nsv;
for (bufidx = 0, i = 0; bufidx < nbuf; bufidx++, i++) {
c = buf[bufidx];
if ((i < len_str) ? c != (char)string[i] :
(i < len_str + onsv) ? c != svchars[i - len_str] : 1) {
/* Salvage a character. */
if (nsv < (int)(sizeof svchars / sizeof svchars[0])) {
svchars[nsv++] = c;
i--; /* try this comparison with the next char */
} else
break; /* too many */
}
}
}
#if 1
/*
* XXX Is this a bug or a feature of kernel tty driver?
*
* FIONREAD in canonical mode does not return correct byte count
* in tty input queue, but this is required to avoid unwanted echo.
*/
tty.c_lflag |= ICANON;
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
(void)ioctl(SHOUT, FIONREAD, (ioctl_t) &i);
#endif
(void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
return (int)nsv;
}
/*
* Concatenate src onto tail of des.
* Des is a string whose maximum length is count.
* Always null terminate.
*/
static void
catn(Char *des, Char *src, size_t count)
{
while (count-- > 0 && *des)
des++;
while (count-- > 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
/*
* Like strncpy but always leave room for trailing \0
* and always null terminate.
*/
static void
copyn(Char *des, Char *src, size_t count)
{
while (count-- > 0)
if ((*des++ = *src++) == 0)
return;
*des = '\0';
}
static Char
filetype(Char *dir, Char *file)
{
struct stat statb;
Char path[MAXPATHLEN];
catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
if (lstat(short2str(path), &statb) == 0) {
switch (statb.st_mode & S_IFMT) {
case S_IFDIR:
return ('/');
case S_IFLNK:
if (stat(short2str(path), &statb) == 0 && /* follow it out */
S_ISDIR(statb.st_mode))
return ('>');
else
return ('@');
case S_IFSOCK:
return ('=');
default:
if (statb.st_mode & 0111)
return ('*');
}
}
return (' ');
}
static struct winsize win;
/*
* Print sorted down columns
*/
static void
print_by_column(Char *dir, Char *items[], size_t count)
{
size_t c, columns, i, maxwidth, r, rows;
maxwidth = 0;
if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
win.ws_col = 80;
for (i = 0; i < count; i++)
maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
maxwidth += 2; /* for the file tag and space */
columns = win.ws_col / maxwidth;
if (columns == 0)
columns = 1;
rows = (count + (columns - 1)) / columns;
for (r = 0; r < rows; r++) {
for (c = 0; c < columns; c++) {
i = c * rows + r;
if (i < count) {
size_t w;
(void)fprintf(cshout, "%s", vis_str(items[i]));
(void)fputc(dir ? filetype(dir, items[i]) : ' ', cshout);
if (c < columns - 1) { /* last column? */
w = Strlen(items[i]) + 1;
for (; w < maxwidth; w++)
(void) fputc(' ', cshout);
}
}
}
(void)fputc('\r', cshout);
(void)fputc('\n', cshout);
}
}
/*
* Expand file name with possible tilde usage
* ~person/mumble
* expands to
* home_directory_of_person/mumble
*/
static Char *
tilde(Char *new, Char *old)
{
static Char person[40];
struct passwd *pw;
Char *o, *p;
if (old[0] != '~')
return (Strcpy(new, old));
for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++)
continue;
*p = '\0';
if (person[0] == '\0')
(void)Strcpy(new, value(STRhome));
else {
pw = getpwnam(short2str(person));
if (pw == NULL)
return (NULL);
(void)Strcpy(new, str2short(pw->pw_dir));
}
(void)Strcat(new, o);
return (new);
}
/*
* Cause pending line to be printed
*/
static void
retype(void)
{
struct termios tty;
(void)tcgetattr(SHOUT, &tty);
tty.c_lflag |= PENDIN;
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
}
static void
beep(void)
{
if (adrof(STRnobeep) == 0)
(void)write(SHOUT, "\007", 1);
}
/*
* Erase that silly ^[ and
* print the recognized part of the string
*/
static void
print_recognized_stuff(Char *recognized_part)
{
/* An optimized erasing of that silly ^[ */
(void)fputc('\b', cshout);
(void)fputc('\b', cshout);
switch (Strlen(recognized_part)) {
case 0: /* erase two Characters: ^[ */
(void)fputc(' ', cshout);
(void)fputc(' ', cshout);
(void)fputc('\b', cshout);
(void)fputc('\b', cshout);
break;
case 1: /* overstrike the ^, erase the [ */
(void)fprintf(cshout, "%s", vis_str(recognized_part));
(void)fputc(' ', cshout);
(void)fputc('\b', cshout);
break;
default: /* overstrike both Characters ^[ */
(void)fprintf(cshout, "%s", vis_str(recognized_part));
break;
}
(void)fflush(cshout);
}
/*
* Parse full path in file into 2 parts: directory and file names
* Should leave final slash (/) at end of dir.
*/
static void
extract_dir_and_name(Char *path, Char *dir, Char *name)
{
Char *p;
p = Strrchr(path, '/');
if (p == NULL) {
copyn(name, path, MAXNAMLEN);
dir[0] = '\0';
}
else {
copyn(name, ++p, MAXNAMLEN);
copyn(dir, path, (size_t)(p - path));
}
}
static Char *
getentry(DIR *dir_fd, int looking_for_lognames)
{
struct dirent *dirp;
struct passwd *pw;
if (looking_for_lognames) {
if ((pw = getpwent()) == NULL)
return (NULL);
return (str2short(pw->pw_name));
}
if ((dirp = readdir(dir_fd)) != NULL)
return (str2short(dirp->d_name));
return (NULL);
}
static void
free_items(Char **items, size_t numitems)
{
size_t i;
for (i = 0; i < numitems; i++)
xfree((ptr_t) items[i]);
xfree((ptr_t) items);
}
#define FREE_ITEMS(items, numitems) { \
sigset_t nsigset, osigset;\
\
sigemptyset(&nsigset);\
(void) sigaddset(&nsigset, SIGINT);\
(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);\
free_items(items, numitems);\
(void) sigprocmask(SIG_SETMASK, &osigset, NULL);\
}
/*
* Perform a RECOGNIZE or LIST command on string "word".
*/
static size_t
tsearch(Char *word, COMMAND command, size_t max_word_length)
{
Char dir[MAXPATHLEN + 1], extended_name[MAXNAMLEN + 1];
Char name[MAXNAMLEN + 1], tilded_dir[MAXPATHLEN + 1];
DIR *dir_fd;
Char *entry;
int ignoring, looking_for_lognames;
size_t name_length, nignored, numitems;
Char **items = NULL;
size_t maxitems = 0;
numitems = 0;
ignoring = TRUE;
nignored = 0;
looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
if (looking_for_lognames) {
(void)setpwent();
copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */
dir_fd = NULL;
}
else {
extract_dir_and_name(word, dir, name);
if (tilde(tilded_dir, dir) == 0)
return (0);
dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
if (dir_fd == NULL)
return (0);
}
again: /* search for matches */
name_length = Strlen(name);
for (numitems = 0; (entry = getentry(dir_fd, looking_for_lognames)) != NULL;) {
if (!is_prefix(name, entry))
continue;
/* Don't match . files on null prefix match */
if (name_length == 0 && entry[0] == '.' &&
!looking_for_lognames)
continue;
if (command == LIST) {
if ((size_t)numitems >= maxitems) {
maxitems += 1024;
if (items == NULL)
items = xmalloc(sizeof(*items) * maxitems);
else
items = xrealloc((ptr_t) items,
sizeof(*items) * maxitems);
}
items[numitems] = xmalloc((size_t) (Strlen(entry) + 1) *
sizeof(Char));
copyn(items[numitems], entry, MAXNAMLEN);
numitems++;
}
else { /* RECOGNIZE command */
if (ignoring && ignored(entry))
nignored++;
else if (recognize(extended_name,
entry, name_length, ++numitems))
break;
}
}
if (ignoring && numitems == 0 && nignored > 0) {
ignoring = FALSE;
nignored = 0;
if (looking_for_lognames)
(void)setpwent();
else
rewinddir(dir_fd);
goto again;
}
if (looking_for_lognames)
(void)endpwent();
else
(void)closedir(dir_fd);
if (numitems == 0)
return (0);
if (command == RECOGNIZE) {
if (looking_for_lognames)
copyn(word, STRtilde, 1);
else
/* put back dir part */
copyn(word, dir, max_word_length);
/* add extended name */
catn(word, extended_name, max_word_length);
return (numitems);
}
else { /* LIST */
qsort((ptr_t) items, numitems, sizeof(items[0]),
(int (*) (const void *, const void *)) sortscmp);
print_by_column(looking_for_lognames ? NULL : tilded_dir,
items, numitems);
if (items != NULL)
FREE_ITEMS(items, numitems);
}
return (0);
}
/*
* Object: extend what user typed up to an ambiguity.
* Algorithm:
* On first match, copy full entry (assume it'll be the only match)
* On subsequent matches, shorten extended_name to the first
* Character mismatch between extended_name and entry.
* If we shorten it back to the prefix length, stop searching.
*/
static int
recognize(Char *extended_name, Char *entry, size_t name_length, size_t numitems)
{
if (numitems == 1) /* 1st match */
copyn(extended_name, entry, MAXNAMLEN);
else { /* 2nd & subsequent matches */
Char *ent, *x;
size_t len = 0;
x = extended_name;
for (ent = entry; *x && *x == *ent++; x++, len++)
continue;
*x = '\0'; /* Shorten at 1st Char diff */
if (len == name_length) /* Ambiguous to prefix? */
return (-1); /* So stop now and save time */
}
return (0);
}
/*
* Return true if check matches initial Chars in template.
* This differs from PWB imatch in that if check is null
* it matches anything.
*/
static int
is_prefix(Char *check, Char *template)
{
do
if (*check == 0)
return (TRUE);
while (*check++ == *template++);
return (FALSE);
}
/*
* Return true if the Chars in template appear at the
* end of check, I.e., are its suffix.
*/
static int
is_suffix(Char *check, Char *template)
{
Char *c, *t;
for (c = check; *c++;)
continue;
for (t = template; *t++;)
continue;
for (;;) {
if (t == template)
return 1;
if (c == check || *--t != *--c)
return 0;
}
}
ssize_t
tenex(Char *inputline, size_t inputline_size)
{
char tinputline[BUFSIZE];
ssize_t num_read;
size_t numitems;
setup_tty(ON);
while ((num_read = read(SHIN, tinputline, BUFSIZE)) > 0) {
size_t i, nr = (size_t) num_read;
static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
'>', '(', ')', '|', '^', '%', '\0'};
Char *str_end, *word_start, last_Char, should_retype;
size_t space_left;
COMMAND command;
for (i = 0; i < nr; i++)
inputline[i] = (unsigned char) tinputline[i];
last_Char = inputline[nr - 1] & ASCII;
if (last_Char == '\n' || nr == inputline_size)
break;
command = (last_Char == ESC) ? RECOGNIZE : LIST;
if (command == LIST)
(void)fputc('\n', cshout);
str_end = &inputline[nr];
if (last_Char == ESC)
--str_end; /* wipeout trailing cmd Char */
*str_end = '\0';
/*
* Find LAST occurence of a delimiter in the inputline. The word start
* is one Character past it.
*/
for (word_start = str_end; word_start > inputline; --word_start)
if (Strchr(delims, word_start[-1]))
break;
space_left = inputline_size - (size_t)(word_start - inputline) - 1;
numitems = tsearch(word_start, command, space_left);
if (command == RECOGNIZE) {
/* print from str_end on */
print_recognized_stuff(str_end);
if (numitems != 1) /* Beep = No match/ambiguous */
beep();
}
/*
* Tabs in the input line cause trouble after a pushback. tty driver
* won't backspace over them because column positions are now
* incorrect. This is solved by retyping over current line.
*/
should_retype = FALSE;
if (Strchr(inputline, '\t')) { /* tab Char in input line? */
back_to_col_1();
should_retype = TRUE;
}
if (command == LIST) /* Always retype after a LIST */
should_retype = TRUE;
if (pushback(inputline))
should_retype = TRUE;
if (should_retype) {
if (command == RECOGNIZE)
(void) fputc('\n', cshout);
printprompt();
}
if (should_retype)
retype();
}
setup_tty(OFF);
return num_read;
}
static int
ignored(Char *entry)
{
struct varent *vp;
Char **cp;
if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
return (FALSE);
for (; *cp != NULL; cp++)
if (is_suffix(entry, *cp))
return (TRUE);
return (FALSE);
}
#endif /* FILEC */

File diff suppressed because it is too large Load Diff

View File

@@ -1,923 +0,0 @@
/* $NetBSD: glob.c,v 1.27 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: glob.c,v 1.27 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <errno.h>
#include <glob.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "csh.h"
#include "extern.h"
static int noglob;
static int gargsiz, pargsiz;
/*
* Values for gflag
*/
#define G_NONE 0 /* No globbing needed */
#define G_GLOB 1 /* string contains *?[] characters */
#define G_CSH 2 /* string contains ~`{ characters */
#define GLOBSPACE 100 /* Alloc increment */
#define LBRC '{'
#define RBRC '}'
#define LBRK '['
#define RBRK ']'
#define EOS '\0'
Char **gargv = NULL;
Char **pargv = NULL;
long gargc = 0;
long pargc = 0;
/*
* globbing is now done in two stages. In the first pass we expand
* csh globbing idioms ~`{ and then we proceed doing the normal
* globbing if needed ?*[
*
* Csh type globbing is handled in globexpand() and the rest is
* handled in glob() which is part of the 4.4BSD libc.
*
*/
static Char *globtilde(Char **, Char *);
static Char *handleone(Char *, Char **, int);
static Char **libglob(Char **);
static Char **globexpand(Char **);
static int globbrace(Char *, Char *, Char ***);
static void expbrace(Char ***, Char ***, size_t);
static int pmatch(Char *, Char *);
static void pword(void);
static void psave(int);
static void backeval(Char *, int);
static Char *
globtilde(Char **nv, Char *s)
{
Char gbuf[MAXPATHLEN], *b, *e, *gstart, *u;
gstart = gbuf;
*gstart++ = *s++;
u = s;
for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
*s && *s != '/' && *s != ':' && b < e;
*b++ = *s++)
continue;
*b = EOS;
if (gethdir(gstart)) {
blkfree(nv);
if (*gstart)
stderror(ERR_UNKUSER, vis_str(gstart));
else
stderror(ERR_NOHOME);
}
b = &gstart[Strlen(gstart)];
while (*s)
*b++ = *s++;
*b = EOS;
--u;
xfree((ptr_t) u);
return (Strsave(gstart));
}
static int
globbrace(Char *s, Char *p, Char ***bl)
{
Char gbuf[MAXPATHLEN];
Char *lm, *pe, *pl, *pm, **nv, **vl;
int i, len, size;
size = GLOBSPACE;
nv = vl = xmalloc(sizeof(Char *) * (size_t)size);
*vl = NULL;
len = 0;
/* copy part up to the brace */
for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
continue;
/* check for balanced braces */
for (i = 0, pe = ++p; *pe; pe++)
if (*pe == LBRK) {
/* Ignore everything between [] */
for (++pe; *pe != RBRK && *pe != EOS; pe++)
continue;
if (*pe == EOS) {
blkfree(nv);
return (-RBRK);
}
}
else if (*pe == LBRC)
i++;
else if (*pe == RBRC) {
if (i == 0)
break;
i--;
}
if (i != 0 || *pe == '\0') {
blkfree(nv);
return (-RBRC);
}
for (i = 0, pl = pm = p; pm <= pe; pm++)
switch (*pm) {
case LBRK:
for (++pm; *pm != RBRK && *pm != EOS; pm++)
continue;
if (*pm == EOS) {
*vl = NULL;
blkfree(nv);
return (-RBRK);
}
break;
case LBRC:
i++;
break;
case RBRC:
if (i) {
i--;
break;
}
/* FALLTHROUGH */
case ',':
if (i && *pm == ',')
break;
else {
Char savec = *pm;
*pm = EOS;
(void)Strcpy(lm, pl);
(void)Strcat(gbuf, pe + 1);
*pm = savec;
*vl++ = Strsave(gbuf);
len++;
pl = pm + 1;
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t) nv,
(size_t)size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
break;
default:
break;
}
*vl = NULL;
*bl = nv;
return (len);
}
static void
expbrace(Char ***nvp, Char ***elp, size_t size)
{
Char **ex, **nv, *s, **vl;
vl = nv = *nvp;
if (elp != NULL)
ex = *elp;
else
for (ex = vl; *ex; ex++)
continue;
for (s = *vl; s; s = *++vl) {
Char *b, **bp, **vp;
/* leave {} untouched for find */
if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
continue;
if ((b = Strchr(s, '{')) != NULL) {
Char **bl;
int len;
if ((len = globbrace(s, b, &bl)) < 0) {
xfree((ptr_t)nv);
stderror(ERR_MISSING, -len);
}
xfree((ptr_t) s);
if (len == 1) {
*vl-- = *bl;
xfree((ptr_t) bl);
continue;
}
len = blklen(bl);
if (&ex[len] >= &nv[size]) {
ptrdiff_t l, e;
l = &ex[len] - &nv[size];
size += (size_t)(GLOBSPACE > l ? GLOBSPACE : l);
l = vl - nv;
e = ex - nv;
nv = (Char **)xrealloc((ptr_t)nv,
(size_t)size * sizeof(Char *));
vl = nv + l;
ex = nv + e;
}
vp = vl--;
*vp = *bl;
len--;
for (bp = ex; bp != vp; bp--)
bp[len] = *bp;
ex += len;
vp++;
for (bp = bl + 1; *bp; *vp++ = *bp++)
continue;
xfree((ptr_t)bl);
}
}
if (elp != NULL)
*elp = ex;
*nvp = nv;
}
static Char **
globexpand(Char **v)
{
Char **ex, **nv, *s, **vl;
size_t size;
size = GLOBSPACE;
nv = vl = xmalloc(sizeof(Char *) * size);
*vl = NULL;
/*
* Step 1: expand backquotes.
*/
while ((s = *v++) != NULL) {
if (Strchr(s, '`')) {
int i;
(void) dobackp(s, 0);
for (i = 0; i < pargc; i++) {
*vl++ = pargv[i];
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t) nv,
(size_t)size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
xfree((ptr_t)pargv);
pargv = NULL;
}
else {
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t)nv,
size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
}
*vl = NULL;
if (noglob)
return (nv);
/*
* Step 2: expand braces
*/
ex = vl;
expbrace(&nv, &ex, size);
/*
* Step 3: expand ~
*/
vl = nv;
for (s = *vl; s; s = *++vl)
if (*s == '~')
*vl = globtilde(nv, s);
vl = nv;
return (vl);
}
static Char *
handleone(Char *str, Char **vl, int action)
{
Char *cp, **vlp;
vlp = vl;
switch (action) {
case G_ERROR:
setname(vis_str(str));
blkfree(vl);
stderror(ERR_NAME | ERR_AMBIG);
/* NOTREACHED */
case G_APPEND:
trim(vlp);
str = Strsave(*vlp++);
do {
cp = Strspl(str, STRspace);
xfree((ptr_t)str);
str = Strspl(cp, *vlp);
xfree((ptr_t)cp);
}
while (*++vlp);
blkfree(vl);
break;
case G_IGNORE:
str = Strsave(strip(*vlp));
blkfree(vl);
break;
default:
break;
}
return (str);
}
static Char **
libglob(Char **vl)
{
glob_t globv;
char *ptr;
int gflgs, magic, match, nonomatch;
gflgs = GLOB_NOMAGIC;
magic = 0;
match = 0;
nonomatch = adrof(STRnonomatch) != 0;
if (!vl || !vl[0])
return (vl);
globv.gl_offs = 0;
globv.gl_pathv = 0;
globv.gl_pathc = 0;
if (nonomatch)
gflgs |= GLOB_NOCHECK;
do {
ptr = short2qstr(*vl);
switch (glob(ptr, gflgs, 0, &globv)) {
case GLOB_ABORTED:
setname(vis_str(*vl));
stderror(ERR_NAME | ERR_GLOB);
/* NOTREACHED */
case GLOB_NOSPACE:
stderror(ERR_NOMEM);
/* NOTREACHED */
default:
break;
}
if (globv.gl_flags & GLOB_MAGCHAR) {
match |= (globv.gl_matchc != 0);
magic = 1;
}
gflgs |= GLOB_APPEND;
}
while (*++vl);
vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
NULL : blk2short(globv.gl_pathv);
globfree(&globv);
return (vl);
}
Char *
globone(Char *str, int action)
{
Char *v[2], **vl, **vo;
int gflg;
noglob = adrof(STRnoglob) != 0;
gflag = 0;
v[0] = str;
v[1] = 0;
tglob(v);
gflg = gflag;
if (gflg == G_NONE)
return (strip(Strsave(str)));
if (gflg & G_CSH) {
/*
* Expand back-quote, tilde and brace
*/
vo = globexpand(v);
if (noglob || (gflg & G_GLOB) == 0) {
if (vo[0] == NULL) {
xfree((ptr_t)vo);
return (Strsave(STRNULL));
}
if (vo[1] != NULL)
return (handleone(str, vo, action));
else {
str = strip(vo[0]);
xfree((ptr_t) vo);
return (str);
}
}
}
else if (noglob || (gflg & G_GLOB) == 0)
return (strip(Strsave(str)));
else
vo = v;
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
if (vl == NULL) {
setname(vis_str(str));
stderror(ERR_NAME | ERR_NOMATCH);
}
if (vl[0] == NULL) {
xfree((ptr_t)vl);
return (Strsave(STRNULL));
}
if (vl[1] != NULL)
return (handleone(str, vl, action));
else {
str = strip(*vl);
xfree((ptr_t)vl);
return (str);
}
}
Char **
globall(Char **v)
{
Char **vl, **vo;
int gflg;
gflg = gflag;
if (!v || !v[0]) {
gargv = saveblk(v);
gargc = blklen(gargv);
return (gargv);
}
noglob = adrof(STRnoglob) != 0;
if (gflg & G_CSH)
/*
* Expand back-quote, tilde and brace
*/
vl = vo = globexpand(v);
else
vl = vo = saveblk(v);
if (!noglob && (gflg & G_GLOB)) {
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
}
else
trim(vl);
gargc = vl ? blklen(vl) : 0;
return (gargv = vl);
}
void
ginit(void)
{
gargsiz = GLOBSPACE;
gargv = xmalloc(sizeof(Char *) * (size_t)gargsiz);
gargv[0] = 0;
gargc = 0;
}
void
rscan(Char **t, void (*f)(int))
{
Char *p;
while ((p = *t++) != NULL)
while (*p)
(*f) (*p++);
}
void
trim(Char **t)
{
Char *p;
while ((p = *t++) != NULL)
while (*p)
*p++ &= TRIM;
}
void
tglob(Char **t)
{
Char *p, c;
while ((p = *t++) != NULL) {
if (*p == '~' || *p == '=')
gflag |= G_CSH;
else if (*p == '{' &&
(p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
continue;
while ((c = *p++) != '\0') {
/*
* eat everything inside the matching backquotes
*/
if (c == '`') {
gflag |= G_CSH;
while (*p && *p != '`')
if (*p++ == '\\') {
if (*p) /* Quoted chars */
p++;
else
break;
}
if (*p) /* The matching ` */
p++;
else
break;
}
else if (c == '{')
gflag |= G_CSH;
else if (isglob(c))
gflag |= G_GLOB;
}
}
}
/*
* Command substitute cp. If literal, then this is a substitution from a
* << redirection, and so we should not crunch blanks and tabs, separating
* words only at newlines.
*/
Char **
dobackp(Char *cp, int literal)
{
Char word[MAXPATHLEN], *ep, *lp, *rp;
if (pargv) {
#ifdef notdef
abort();
#endif
blkfree(pargv);
}
pargsiz = GLOBSPACE;
pargv = xmalloc(sizeof(Char *) * (size_t)pargsiz);
pargv[0] = NULL;
pargcp = pargs = word;
pargc = 0;
pnleft = MAXPATHLEN - 4;
for (;;) {
for (lp = cp; *lp != '`'; lp++) {
if (*lp == 0) {
if (pargcp != pargs)
pword();
return (pargv);
}
psave(*lp);
}
lp++;
for (rp = lp; *rp && *rp != '`'; rp++)
if (*rp == '\\') {
rp++;
if (!*rp)
goto oops;
}
if (!*rp) {
oops:
stderror(ERR_UNMATCHED, '`');
}
ep = Strsave(lp);
ep[rp - lp] = 0;
backeval(ep, literal);
cp = rp + 1;
}
}
static void
backeval(Char *cp, int literal)
{
struct command faket;
char tibuf[BUFSIZE];
Char ibuf[BUFSIZE], *fakecom[2], *ip;
int pvec[2], c, quoted;
ssize_t icnt;
int hadnl;
hadnl = 0;
icnt = 0;
quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
faket.t_dtyp = NODE_COMMAND;
faket.t_dflg = 0;
faket.t_dlef = 0;
faket.t_drit = 0;
faket.t_dspr = 0;
faket.t_dcom = fakecom;
fakecom[0] = STRfakecom1;
fakecom[1] = 0;
/*
* We do the psave job to temporarily change the current job so that the
* following fork is considered a separate job. This is so that when
* backquotes are used in a builtin function that calls glob the "current
* job" is not corrupted. We only need one level of pushed jobs as long as
* we are sure to fork here.
*/
psavejob();
/*
* It would be nicer if we could integrate this redirection more with the
* routines in sh.sem.c by doing a fake execute on a builtin function that
* was piped out.
*/
mypipe(pvec);
if (pfork(&faket, -1) == 0) {
struct wordent fparaml;
struct command *t;
(void)close(pvec[0]);
(void)dmove(pvec[1], 1);
(void)dmove(SHERR, 2);
initdesc();
/*
* Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
* posted to comp.bugs.4bsd 12 Sep. 1989.
*/
if (pargv) /* mg, 21.dec.88 */
blkfree(pargv), pargv = 0, pargsiz = 0;
/* mg, 21.dec.88 */
arginp = cp;
for (arginp = cp; *cp; cp++) {
*cp &= TRIM;
if (*cp == '\n' || *cp == '\r')
*cp = ';';
}
/*
* In the child ``forget'' everything about current aliases or
* eval vectors.
*/
alvec = NULL;
evalvec = NULL;
alvecp = NULL;
evalp = NULL;
(void) lex(&fparaml);
if (seterr)
stderror(ERR_OLD);
alias(&fparaml);
t = syntax(fparaml.next, &fparaml, 0);
if (seterr)
stderror(ERR_OLD);
if (t)
t->t_dflg |= F_NOFORK;
(void)signal(SIGTSTP, SIG_IGN);
(void)signal(SIGTTIN, SIG_IGN);
(void)signal(SIGTTOU, SIG_IGN);
execute(t, -1, NULL, NULL);
exitstat();
}
xfree((ptr_t)cp);
(void)close(pvec[1]);
c = 0;
ip = NULL;
do {
int cnt;
cnt = 0;
for (;;) {
if (icnt == 0) {
int i;
ip = ibuf;
do
icnt = read(pvec[0], tibuf, BUFSIZE);
while (icnt == -1 && errno == EINTR);
if (icnt <= 0) {
c = -1;
break;
}
for (i = 0; i < icnt; i++)
ip[i] = (unsigned char) tibuf[i];
}
if (hadnl)
break;
--icnt;
c = (*ip++ & TRIM);
if (c == 0)
break;
if (c == '\n') {
/*
* Continue around the loop one more time, so that we can eat
* the last newline without terminating this word.
*/
hadnl = 1;
continue;
}
if (!quoted && (c == ' ' || c == '\t'))
break;
cnt++;
psave(c | quoted);
}
/*
* Unless at end-of-file, we will form a new word here if there were
* characters in the word, or in any case when we take text literally.
* If we didn't make empty words here when literal was set then we
* would lose blank lines.
*/
if (c != -1 && (cnt || literal))
pword();
hadnl = 0;
} while (c >= 0);
(void)close(pvec[0]);
pwait();
prestjob();
}
static void
psave(int c)
{
if (--pnleft <= 0)
stderror(ERR_WTOOLONG);
*pargcp++ = (Char)c;
}
static void
pword(void)
{
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
pargv = (Char **)xrealloc((ptr_t)pargv,
(size_t)pargsiz * sizeof(Char *));
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
pargcp = pargs;
pnleft = MAXPATHLEN - 4;
}
int
Gmatch(Char *string, Char *pattern)
{
Char **blk, **p;
int gpol, gres;
gpol = 1;
gres = 0;
if (*pattern == '^') {
gpol = 0;
pattern++;
}
blk = xmalloc(GLOBSPACE * sizeof(Char *));
blk[0] = Strsave(pattern);
blk[1] = NULL;
expbrace(&blk, NULL, GLOBSPACE);
for (p = blk; *p; p++)
gres |= pmatch(string, *p);
blkfree(blk);
return(gres == gpol);
}
static int
pmatch(Char *string, Char *pattern)
{
int match, negate_range;
Char patternc, rangec, stringc;
for (;; ++string) {
stringc = *string & TRIM;
patternc = *pattern++;
switch (patternc) {
case 0:
return (stringc == 0);
case '?':
if (stringc == 0)
return (0);
break;
case '*':
if (!*pattern)
return (1);
while (*string)
if (Gmatch(string++, pattern))
return (1);
return (0);
case '[':
match = 0;
if ((negate_range = (*pattern == '^')) != 0)
pattern++;
while ((rangec = *pattern++) != '\0') {
if (rangec == ']')
break;
if (match)
continue;
if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
match = (stringc <= (*pattern & TRIM) &&
(*(pattern-2) & TRIM) <= stringc);
pattern++;
}
else
match = (stringc == (rangec & TRIM));
}
if (rangec == 0)
stderror(ERR_NAME | ERR_MISSING, ']');
if (match == negate_range)
return (0);
break;
default:
if ((patternc & TRIM) != stringc)
return (0);
break;
}
}
}
void
Gcat(Char *s1, Char *s2)
{
Char *p, *q;
ptrdiff_t n;
for (p = s1; *p++;)
continue;
for (q = s2; *q++;)
continue;
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
gargv = (Char **)xrealloc((ptr_t)gargv,
(size_t)gargsiz * sizeof(Char *));
}
gargv[gargc] = 0;
p = gargv[gargc - 1] = xmalloc((size_t)n * sizeof(Char));
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)
continue;
}
#ifdef FILEC
int
sortscmp(const ptr_t a, const ptr_t b)
{
#if defined(NLS) && !defined(NOSTRCOLL)
char buf[2048];
#endif
if (!a) /* check for NULL */
return (b ? 1 : 0);
if (!b)
return (-1);
if (!*(Char **)a) /* check for NULL */
return (*(Char **)b ? 1 : 0);
if (!*(Char **)b)
return (-1);
#if defined(NLS) && !defined(NOSTRCOLL)
(void)strcpy(buf, short2str(*(Char **)a));
return ((int)strcoll(buf, short2str(*(Char **)b)));
#else
return ((int)Strcmp(*(Char **)a, *(Char **)b));
#endif
}
#endif /* FILEC */

View File

@@ -1,207 +0,0 @@
/* $NetBSD: hist.c,v 1.20 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: hist.c,v 1.20 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#include "csh.h"
#include "extern.h"
static void hfree(struct Hist *);
static void dohist1(struct Hist *, int *, int, int);
static void phist(struct Hist *, int);
void
savehist(struct wordent *sp)
{
struct Hist *hp, *np;
Char *cp;
int histlen;
histlen = 0;
/* throw away null lines */
if (sp->next->word[0] == '\n')
return;
cp = value(STRhistory);
if (*cp) {
Char *p = cp;
while (*p) {
if (!Isdigit(*p)) {
histlen = 0;
break;
}
histlen = histlen * 10 + *p++ - '0';
}
}
for (hp = &Histlist; (np = hp->Hnext) != NULL;)
if (eventno - np->Href >= histlen || histlen == 0)
hp->Hnext = np->Hnext, hfree(np);
else
hp = np;
(void) enthist(++eventno, sp, 1);
}
#ifdef EDIT
void
loadhist(struct Hist *hp) {
char *h = NULL;
if (hi == NULL || hp == NULL)
return;
loadhist(hp->Hnext);
if (sprlex(&h, &hp->Hlex) != -1) {
HistEvent ev;
history(hi, &ev, H_ENTER, h);
}
}
#endif
struct Hist *
enthist(int event, struct wordent *lp, int docopy)
{
struct Hist *np;
#ifdef EDIT
if (hi) {
char *h = NULL;
if (sprlex(&h, lp) != -1) {
HistEvent ev;
history(hi, &ev, H_ENTER, h);
}
}
#endif
np = xmalloc(sizeof(*np));
np->Hnum = np->Href = event;
if (docopy) {
copylex(&np->Hlex, lp);
}
else {
np->Hlex.next = lp->next;
lp->next->prev = &np->Hlex;
np->Hlex.prev = lp->prev;
lp->prev->next = &np->Hlex;
}
np->Hnext = Histlist.Hnext;
Histlist.Hnext = np;
return (np);
}
static void
hfree(struct Hist *hp)
{
freelex(&hp->Hlex);
xfree((ptr_t) hp);
}
void
/*ARGSUSED*/
dohist(Char **v, struct command *t)
{
sigset_t nsigset;
int hflg, n, rflg;
hflg = 0;
rflg = 0;
if (getn(value(STRhistory)) == 0)
return;
if (setintr) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
}
while (*++v && **v == '-') {
Char *vp = *v;
while (*++vp)
switch (*vp) {
case 'h':
hflg++;
break;
case 'r':
rflg++;
break;
case '-': /* ignore multiple '-'s */
break;
default:
stderror(ERR_HISTUS);
/* NOTREACHED */
}
}
if (*v)
n = getn(*v);
else {
n = getn(value(STRhistory));
}
dohist1(Histlist.Hnext, &n, rflg, hflg);
}
static void
dohist1(struct Hist *hp, int *np, int rflg, int hflg)
{
int print;
print = (*np) > 0;
for (; hp != 0; hp = hp->Hnext) {
(*np)--;
hp->Href++;
if (rflg == 0) {
dohist1(hp->Hnext, np, rflg, hflg);
if (print)
phist(hp, hflg);
return;
}
if (*np >= 0)
phist(hp, hflg);
}
}
static void
phist(struct Hist *hp, int hflg)
{
if (hflg == 0)
(void)fprintf(cshout, "%6d\t", hp->Hnum);
prlex(cshout, &hp->Hlex);
}

View File

@@ -1,131 +0,0 @@
/* $NetBSD: init.c,v 1.11 2013/01/22 19:28:00 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: init.c,v 1.11 2013/01/22 19:28:00 christos Exp $");
#endif
#endif /* not lint */
#include <stdarg.h>
#include "csh.h"
#include "extern.h"
#define INF 1000
struct biltins bfunc[] =
{
{ "@", dolet, 0, INF },
{ "alias", doalias, 0, INF },
{ "bg", dobg, 0, INF },
{ "break", dobreak, 0, 0 },
{ "breaksw", doswbrk, 0, 0 },
{ "case", dozip, 0, 1 },
{ "cd", dochngd, 0, INF },
{ "chdir", dochngd, 0, INF },
{ "continue", docontin, 0, 0 },
{ "default", dozip, 0, 0 },
{ "dirs", dodirs, 0, INF },
{ "echo", doecho, 0, INF },
{ "else", doelse, 0, INF },
{ "end", doend, 0, 0 },
{ "endif", dozip, 0, 0 },
{ "endsw", dozip, 0, 0 },
{ "eval", doeval, 0, INF },
{ "exec", execash, 1, INF },
{ "exit", doexit, 0, INF },
{ "fg", dofg, 0, INF },
{ "foreach", doforeach, 3, INF },
{ "glob", doglob, 0, INF },
{ "goto", dogoto, 1, 1 },
{ "hashstat", hashstat, 0, 0 },
{ "history", dohist, 0, 2 },
{ "if", doif, 1, INF },
{ "jobs", dojobs, 0, 1 },
{ "kill", dokill, 1, INF },
{ "limit", dolimit, 0, 3 },
{ "linedit", doecho, 0, INF },
{ "login", dologin, 0, 1 },
{ "logout", dologout, 0, 0 },
{ "nice", donice, 0, INF },
{ "nohup", donohup, 0, INF },
{ "notify", donotify, 0, INF },
{ "onintr", doonintr, 0, 2 },
{ "popd", dopopd, 0, INF },
{ "printf", doprintf, 1, INF },
{ "pushd", dopushd, 0, INF },
{ "rehash", dohash, 0, 0 },
{ "repeat", dorepeat, 2, INF },
{ "set", doset, 0, INF },
{ "setenv", dosetenv, 0, 2 },
{ "shift", shift, 0, 1 },
{ "source", dosource, 1, 2 },
{ "stop", dostop, 1, INF },
{ "suspend", dosuspend, 0, 0 },
{ "switch", doswitch, 1, INF },
{ "time", dotime, 0, INF },
{ "umask", doumask, 0, 1 },
{ "unalias", unalias, 1, INF },
{ "unhash", dounhash, 0, 0 },
{ "unlimit", dounlimit, 0, INF },
{ "unset", unset, 1, INF },
{ "unsetenv", dounsetenv, 1, INF },
{ "wait", dowait, 0, 0 },
{ "which", dowhich, 1, INF },
{ "while", dowhile, 1, INF }
};
int nbfunc = sizeof(bfunc) / sizeof(*bfunc);
struct srch srchn[] =
{
{ "@", T_LET },
{ "break", T_BREAK },
{ "breaksw", T_BRKSW },
{ "case", T_CASE },
{ "default", T_DEFAULT },
{ "else", T_ELSE },
{ "end", T_END },
{ "endif", T_ENDIF },
{ "endsw", T_ENDSW },
{ "exit", T_EXIT },
{ "foreach", T_FOREACH },
{ "goto", T_GOTO },
{ "if", T_IF },
{ "label", T_LABEL },
{ "set", T_SET },
{ "switch", T_SWITCH },
{ "while", T_WHILE }
};
int nsrchn = sizeof(srchn) / sizeof(*srchn);

File diff suppressed because it is too large Load Diff

View File

@@ -1,407 +0,0 @@
/* $NetBSD: misc.c,v 1.20 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: misc.c,v 1.20 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "csh.h"
#include "extern.h"
static int renum(int, int);
int
any(const char *s, int c)
{
if (!s)
return (0); /* Check for nil pointer */
while (*s)
if (*s++ == c)
return (1);
return (0);
}
char *
strsave(const char *s)
{
const char *n;
char *p, *r;
if (s == NULL)
s = "";
for (n = s; *n++;)
continue;
r = p = xmalloc((size_t)(n - s) * sizeof(*p));
while ((*p++ = *s++) != '\0')
continue;
return (r);
}
Char **
blkend(Char **up)
{
while (*up)
up++;
return (up);
}
void
blkpr(FILE *fp, Char **av)
{
for (; *av; av++) {
(void)fprintf(fp, "%s", vis_str(*av));
if (av[1])
(void)fprintf(fp, " ");
}
}
int
blklen(Char **av)
{
int i;
i = 0;
while (*av++)
i++;
return (i);
}
Char **
blkcpy(Char **oav, Char **bv)
{
Char **av;
av = oav;
while ((*av++ = *bv++) != NULL)
continue;
return (oav);
}
Char **
blkcat(Char **up, Char **vp)
{
(void)blkcpy(blkend(up), vp);
return (up);
}
void
blkfree(Char **av0)
{
Char **av;
av = av0;
if (!av0)
return;
for (; *av; av++)
xfree((ptr_t) * av);
xfree((ptr_t) av0);
}
Char **
saveblk(Char **v)
{
Char **newv, **onewv;
if (v == NULL)
return NULL;
newv = xcalloc((size_t)(blklen(v) + 1), sizeof(*newv));
onewv = newv;
while (*v)
*newv++ = Strsave(*v++);
return (onewv);
}
#ifdef NOTUSED
char *
strstr(char *s, char *t)
{
do {
char *ss;
char *tt;
ss = s;
tt = t;
do
if (*tt == '\0')
return (s);
while (*ss++ == *tt++);
} while (*s++ != '\0');
return (NULL);
}
#endif /* NOTUSED */
#ifndef SHORT_STRINGS
char *
strspl(char *cp, char *dp)
{
char *ep, *p, *q;
if (!cp)
cp = "";
if (!dp)
dp = "";
for (p = cp; *p++;)
continue;
for (q = dp; *q++;)
continue;
ep = xmalloc((size_t)(((p - cp) + (q - dp) - 1) * sizeof(*ep)));
for (p = ep, q = cp; *p++ = *q++;)
continue;
for (p--, q = dp; *p++ = *q++;)
continue;
return (ep);
}
#endif
Char **
blkspl(Char **up, Char **vp)
{
Char **wp;
wp = xcalloc((size_t)(blklen(up) + blklen(vp) + 1), sizeof(*wp));
(void)blkcpy(wp, up);
return (blkcat(wp, vp));
}
Char
lastchr(Char *cp)
{
if (!cp)
return (0);
if (!*cp)
return (0);
while (cp[1])
cp++;
return (*cp);
}
/*
* This routine is called after an error to close up
* any units which may have been left open accidentally.
*/
void
closem(void)
{
int f;
int nofile;
#ifdef F_CLOSEM
nofile = FOLDSTD + 1;
if (fcntl(nofile, F_CLOSEM, 0) == -1)
#endif
nofile = NOFILE;
for (f = 0; f < nofile; f++)
if (f != SHIN && f != SHOUT && f != SHERR && f != OLDSTD &&
f != FSHTTY)
(void) close(f);
}
void
donefds(void)
{
(void)close(0);
(void)close(1);
(void)close(2);
didfds = 0;
}
/*
* Move descriptor i to j.
* If j is -1 then we just want to get i to a safe place,
* i.e. to a unit > 2. This also happens in dcopy.
*/
int
dmove(int i, int j)
{
if (i == j || i < 0)
return (i);
if (j >= 0) {
(void)dup2(i, j);
if (j != i)
(void)close(i);
return (j);
}
j = dcopy(i, j);
if (j != i)
(void)close(i);
return (j);
}
int
dcopy(int i, int j)
{
if (i == j || i < 0 || (j < 0 && i > 2))
return (i);
if (j >= 0) {
(void)dup2(i, j);
return (j);
}
return (renum(i, j));
}
static int
renum(int i, int j)
{
int k;
k = dup(i);
if (k < 0)
return (-1);
if (j == -1 && k > 2)
return (k);
if (k != j) {
j = renum(k, j);
(void)close(k);
return (j);
}
return (k);
}
/*
* Left shift a command argument list, discarding
* the first c arguments. Used in "shift" commands
* as well as by commands like "repeat".
*/
void
lshift(Char **v, size_t c)
{
Char **u;
for (u = v; *u && c-- > 0; u++)
xfree((ptr_t) *u);
(void)blkcpy(v, u);
}
int
number(Char *cp)
{
if (!cp)
return(0);
if (*cp == '-') {
cp++;
if (!Isdigit(*cp))
return (0);
cp++;
}
while (*cp && Isdigit(*cp))
cp++;
return (*cp == 0);
}
Char **
copyblk(Char **v)
{
Char **nv;
nv = xcalloc((size_t)(blklen(v) + 1), sizeof(*nv));
return (blkcpy(nv, v));
}
#ifndef SHORT_STRINGS
char *
strend(char *cp)
{
if (!cp)
return (cp);
while (*cp)
cp++;
return (cp);
}
#endif /* SHORT_STRINGS */
Char *
strip(Char *cp)
{
Char *dp;
dp = cp;
if (!cp)
return (cp);
while ((*dp++ &= TRIM) != '\0')
continue;
return (cp);
}
Char *
quote(Char *cp)
{
Char *dp;
dp = cp;
if (!cp)
return (cp);
while (*dp != '\0')
*dp++ |= QUOTE;
return (cp);
}
void
udvar(Char *name)
{
setname(vis_str(name));
stderror(ERR_NAME | ERR_UNDVAR);
/* NOTREACHED */
}
int
prefix(Char *sub, Char *str)
{
for (;;) {
if (*sub == 0)
return (1);
if (*str == 0)
return (0);
if (*sub++ != *str++)
return (0);
}
}

View File

@@ -1,640 +0,0 @@
/* $NetBSD: parse.c,v 1.17 2007/07/16 18:26:10 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: parse.c,v 1.17 2007/07/16 18:26:10 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "csh.h"
#include "extern.h"
static void asyntax(struct wordent *, struct wordent *);
static void asyn0(struct wordent *, struct wordent *);
static void asyn3(struct wordent *, struct wordent *);
static struct wordent *freenod(struct wordent *, struct wordent *);
static struct command *syn0(struct wordent *, struct wordent *, int);
static struct command *syn1(struct wordent *, struct wordent *, int);
static struct command *syn1a(struct wordent *, struct wordent *, int);
static struct command *syn1b(struct wordent *, struct wordent *, int);
static struct command *syn2(struct wordent *, struct wordent *, int);
static struct command *syn3(struct wordent *, struct wordent *, int);
#define ALEFT 21 /* max of 20 alias expansions */
#define HLEFT 11 /* max of 10 history expansions */
/*
* Perform aliasing on the word list lex
* Do a (very rudimentary) parse to separate into commands.
* If word 0 of a command has an alias, do it.
* Repeat a maximum of 20 times.
*/
static int aleft;
extern int hleft;
void
alias(struct wordent *lexp)
{
jmp_buf osetexit;
aleft = ALEFT;
hleft = HLEFT;
getexit(osetexit);
(void)setexit();
if (haderr) {
resexit(osetexit);
reset();
}
if (--aleft == 0)
stderror(ERR_ALIASLOOP);
asyntax(lexp->next, lexp);
resexit(osetexit);
}
static void
asyntax(struct wordent *p1, struct wordent *p2)
{
while (p1 != p2)
if (any(";&\n", p1->word[0]))
p1 = p1->next;
else {
asyn0(p1, p2);
return;
}
}
static void
asyn0(struct wordent *p1, struct wordent *p2)
{
struct wordent *p;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
if (l < 0)
stderror(ERR_TOOMANYRP);
continue;
case '>':
if (p->next != p2 && eq(p->next->word, STRand))
p = p->next;
continue;
case '&':
case '|':
case ';':
case '\n':
if (l != 0)
continue;
asyn3(p1, p);
asyntax(p->next, p2);
return;
}
if (l == 0)
asyn3(p1, p2);
}
static void
asyn3(struct wordent *p1, struct wordent *p2)
{
struct varent *ap;
struct wordent alout;
int redid;
if (p1 == p2)
return;
if (p1->word[0] == '(') {
for (p2 = p2->prev; p2->word[0] != ')'; p2 = p2->prev)
if (p2 == p1)
return;
if (p2 == p1->next)
return;
asyn0(p1->next, p2);
return;
}
ap = adrof1(p1->word, &aliases);
if (ap == 0)
return;
alhistp = p1->prev;
alhistt = p2;
alvec = ap->vec;
redid = lex(&alout);
alhistp = alhistt = 0;
alvec = 0;
if (seterr) {
freelex(&alout);
stderror(ERR_OLD);
}
if (p1->word[0] && eq(p1->word, alout.next->word)) {
Char *cp;
cp = alout.next->word;
alout.next->word = Strspl(STRQNULL, cp);
xfree((ptr_t) cp);
}
p1 = freenod(p1, redid ? p2 : p1->next);
if (alout.next != &alout) {
p1->next->prev = alout.prev->prev;
alout.prev->prev->next = p1->next;
alout.next->prev = p1;
p1->next = alout.next;
xfree((ptr_t)alout.prev->word);
xfree((ptr_t)(alout.prev));
}
reset(); /* throw! */
}
static struct wordent *
freenod(struct wordent *p1, struct wordent *p2)
{
struct wordent *retp;
retp = p1->prev;
while (p1 != p2) {
xfree((ptr_t)p1->word);
p1 = p1->next;
xfree((ptr_t) (p1->prev));
}
retp->next = p2;
p2->prev = retp;
return (retp);
}
#define PHERE 1
#define PIN 2
#define POUT 4
#define PERR 8
/*
* syntax
* empty
* syn0
*/
struct command *
syntax(struct wordent *p1, struct wordent *p2, int flags)
{
while (p1 != p2)
if (any(";&\n", p1->word[0]))
p1 = p1->next;
else
return (syn0(p1, p2, flags));
return (0);
}
/*
* syn0
* syn1
* syn1 & syntax
*/
static struct command *
syn0(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *p;
struct command *t, *t1;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
if (l < 0)
seterror(ERR_TOOMANYRP);
continue;
case '|':
if (p->word[1] == '|')
continue;
/* FALLTHROUGH */
case '>':
if (p->next != p2 && eq(p->next->word, STRand))
p = p->next;
continue;
case '&':
if (l != 0)
break;
if (p->word[1] == '&')
continue;
t1 = syn1(p1, p, flags);
if (t1->t_dtyp == NODE_LIST ||
t1->t_dtyp == NODE_AND ||
t1->t_dtyp == NODE_OR) {
t = (struct command *)xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_PAREN;
t->t_dflg = F_AMPERSAND | F_NOINTERRUPT;
t->t_dspr = t1;
t1 = t;
}
else
t1->t_dflg |= F_AMPERSAND | F_NOINTERRUPT;
t = (struct command *)xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_LIST;
t->t_dflg = 0;
t->t_dcar = t1;
t->t_dcdr = syntax(p, p2, flags);
return (t);
}
if (l == 0)
return (syn1(p1, p2, flags));
seterror(ERR_TOOMANYLP);
return (0);
}
/*
* syn1
* syn1a
* syn1a ; syntax
*/
static struct command *
syn1(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *p;
struct command *t;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case ';':
case '\n':
if (l != 0)
break;
t = (struct command *) xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_LIST;
t->t_dcar = syn1a(p1, p, flags);
t->t_dcdr = syntax(p->next, p2, flags);
if (t->t_dcdr == 0)
t->t_dcdr = t->t_dcar, t->t_dcar = 0;
return (t);
}
return (syn1a(p1, p2, flags));
}
/*
* syn1a
* syn1b
* syn1b || syn1a
*/
static struct command *
syn1a(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *p;
struct command *t;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '|':
if (p->word[1] != '|')
continue;
if (l == 0) {
t = (struct command *)xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_OR;
t->t_dcar = syn1b(p1, p, flags);
t->t_dcdr = syn1a(p->next, p2, flags);
t->t_dflg = 0;
return (t);
}
continue;
}
return (syn1b(p1, p2, flags));
}
/*
* syn1b
* syn2
* syn2 && syn1b
*/
static struct command *
syn1b(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *p;
struct command *t;
int l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '&':
if (p->word[1] == '&' && l == 0) {
t = (struct command *)xcalloc(1, sizeof(*t));
t->t_dtyp = NODE_AND;
t->t_dcar = syn2(p1, p, flags);
t->t_dcdr = syn1b(p->next, p2, flags);
t->t_dflg = 0;
return (t);
}
continue;
}
return (syn2(p1, p2, flags));
}
/*
* syn2
* syn3
* syn3 | syn2
* syn3 |& syn2
*/
static struct command *
syn2(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *p, *pn;
struct command *t;
int f, l;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
l++;
continue;
case ')':
l--;
continue;
case '|':
if (l != 0)
continue;
t = (struct command *)xcalloc(1, sizeof(*t));
f = flags | POUT;
pn = p->next;
if (pn != p2 && pn->word[0] == '&') {
f |= PERR;
t->t_dflg |= F_STDERR;
}
t->t_dtyp = NODE_PIPE;
t->t_dcar = syn3(p1, p, f);
if (pn != p2 && pn->word[0] == '&')
p = pn;
t->t_dcdr = syn2(p->next, p2, flags | PIN);
return (t);
}
return (syn3(p1, p2, flags));
}
static char RELPAR[] = {'<', '>', '(', ')', '\0'};
/*
* syn3
* ( syn0 ) [ < in ] [ > out ]
* word word* [ < in ] [ > out ]
* KEYWORD ( word* ) word* [ < in ] [ > out ]
*
* KEYWORD = (@ exit foreach if set switch test while)
*/
static struct command *
syn3(struct wordent *p1, struct wordent *p2, int flags)
{
struct wordent *lp, *p, *rp;
struct command *t;
Char **av;
int c, l, n;
int specp;
specp = 0;
if (p1 != p2) {
p = p1;
again:
switch (srchx(p->word)) {
case T_ELSE:
p = p->next;
if (p != p2)
goto again;
break;
case T_EXIT:
case T_FOREACH:
case T_IF:
case T_LET:
case T_SET:
case T_SWITCH:
case T_WHILE:
specp = 1;
break;
}
}
n = 0;
l = 0;
for (p = p1; p != p2; p = p->next)
switch (p->word[0]) {
case '(':
if (specp)
n++;
l++;
continue;
case ')':
if (specp)
n++;
l--;
continue;
case '>':
case '<':
if (l != 0) {
if (specp)
n++;
continue;
}
if (p->next == p2)
continue;
if (any(RELPAR, p->next->word[0]))
continue;
n--;
continue;
default:
if (!specp && l != 0)
continue;
n++;
continue;
}
if (n < 0)
n = 0;
t = (struct command *)xcalloc(1, sizeof(*t));
av = (Char **)xcalloc((size_t)(n + 1), sizeof(Char **));
t->t_dcom = av;
n = 0;
if (p2->word[0] == ')')
t->t_dflg = F_NOFORK;
lp = 0;
rp = 0;
l = 0;
for (p = p1; p != p2; p = p->next) {
c = p->word[0];
switch (c) {
case '(':
if (l == 0) {
if (lp != 0 && !specp)
seterror(ERR_BADPLP);
lp = p->next;
}
l++;
goto savep;
case ')':
l--;
if (l == 0)
rp = p;
goto savep;
case '>':
if (l != 0)
goto savep;
if (p->word[1] == '>')
t->t_dflg |= F_APPEND;
if (p->next != p2 && eq(p->next->word, STRand)) {
t->t_dflg |= F_STDERR, p = p->next;
if (flags & (POUT | PERR)) {
seterror(ERR_OUTRED);
continue;
}
}
if (p->next != p2 && eq(p->next->word, STRbang))
t->t_dflg |= F_OVERWRITE, p = p->next;
if (p->next == p2) {
seterror(ERR_MISRED);
continue;
}
p = p->next;
if (any(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
if ((flags & POUT) && ((flags & PERR) == 0 || t->t_drit))
seterror(ERR_OUTRED);
else
t->t_drit = Strsave(p->word);
continue;
case '<':
if (l != 0)
goto savep;
if (p->word[1] == '<')
t->t_dflg |= F_READ;
if (p->next == p2) {
seterror(ERR_MISRED);
continue;
}
p = p->next;
if (any(RELPAR, p->word[0])) {
seterror(ERR_MISRED);
continue;
}
if ((flags & PHERE) && (t->t_dflg & F_READ))
seterror(ERR_REDPAR);
else if ((flags & PIN) || t->t_dlef)
seterror(ERR_INRED);
else
t->t_dlef = Strsave(p->word);
continue;
savep:
if (!specp)
continue;
/* FALLTHROUGH */
default:
if (l != 0 && !specp)
continue;
if (seterr == 0)
av[n] = Strsave(p->word);
n++;
continue;
}
}
if (lp != 0 && !specp) {
if (n != 0)
seterror(ERR_BADPLPS);
t->t_dtyp = NODE_PAREN;
t->t_dspr = syn0(lp, rp, PHERE);
}
else {
if (n == 0)
seterror(ERR_NULLCOM);
t->t_dtyp = NODE_COMMAND;
}
return (t);
}
void
freesyn(struct command *t)
{
Char **v;
if (t == 0)
return;
switch (t->t_dtyp) {
case NODE_COMMAND:
for (v = t->t_dcom; *v; v++)
xfree((ptr_t) * v);
xfree((ptr_t)(t->t_dcom));
xfree((ptr_t)t->t_dlef);
xfree((ptr_t)t->t_drit);
break;
case NODE_PAREN:
freesyn(t->t_dspr);
xfree((ptr_t)t->t_dlef);
xfree((ptr_t)t->t_drit);
break;
case NODE_AND:
case NODE_OR:
case NODE_PIPE:
case NODE_LIST:
freesyn(t->t_dcar), freesyn(t->t_dcdr);
break;
}
xfree((ptr_t)t);
}

View File

@@ -1,44 +0,0 @@
/* $NetBSD: pathnames.h,v 1.8 2003/08/07 09:05:06 agc Exp $ */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _PATHNAMES_H_
#define _PATHNAMES_H_
#define _PATH_BIN "/bin"
#define _PATH_DOTCSHRC "/etc/csh.cshrc"
#define _PATH_DOTLOGIN "/etc/csh.login"
#define _PATH_DOTLOGOUT "/etc/csh.logout"
#define _PATH_LOGIN "/usr/bin/login"
#define _PATH_USRBIN "/usr/bin"
#endif /* !_PATHNAMES_H_ */

File diff suppressed because it is too large Load Diff

View File

@@ -1,104 +0,0 @@
/* $NetBSD: proc.h,v 1.14 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)proc.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _PROC_H_
#define _PROC_H_
/*
* Structure for each process the shell knows about:
* allocated and filled by pcreate.
* flushed by pflush; freeing always happens at top level
* so the interrupt level has less to worry about.
* processes are related to "friends" when in a pipeline;
* p_friends links makes a circular list of such jobs
*/
struct process {
struct process *p_next; /* next in global "proclist" */
struct process *p_friends; /* next in job list (or self) */
struct directory *p_cwd; /* cwd of the job (only in head) */
int p_flags; /* various job status flags */
int p_reason; /* reason for entering this state */
int p_index; /* shorthand job index */
pid_t p_pid;
pid_t p_jobid; /* pid of job leader */
/* if a job is stopped/background p_jobid gives its pgrp */
struct timespec p_btime; /* begin time */
struct timespec p_etime; /* end time */
struct rusage p_rusage;
Char *p_command; /* first PMAXLEN chars of command */
};
/* flag values for p_flags */
#define PRUNNING (1<<0) /* running */
#define PSTOPPED (1<<1) /* stopped */
#define PNEXITED (1<<2) /* normally exited */
#define PAEXITED (1<<3) /* abnormally exited */
#define PSIGNALED (1<<4) /* terminated by a signal != SIGINT */
#define PNOTIFY (1<<5) /* notify async when done */
#define PTIME (1<<6) /* job times should be printed */
#define PAWAITED (1<<7) /* top level is waiting for it */
#define PFOREGND (1<<8) /* started in shells pgrp */
#define PDUMPED (1<<9) /* process dumped core */
#define PERR (1<<10) /* diagnostic output also piped out */
#define PPOU (1<<11) /* piped output */
#define PREPORTED (1<<12) /* status has been reported */
#define PINTERRUPTED (1<<13) /* job stopped via interrupt signal */
#define PPTIME (1<<14) /* time individual process */
#define PNEEDNOTE (1<<15) /* notify as soon as practical */
#define PALLSTATES (PRUNNING|PSTOPPED|PNEXITED|PAEXITED|PSIGNALED|PINTERRUPTED)
#define PMAXLEN 80
/* defines for arguments to pprint */
#define NUMBER 01
#define NAME 02
#define REASON 04
#define AMPERSAND 010
#define FANCY 020
#define SHELLDIR 040 /* print shell's dir if not the same */
#define JOBDIR 0100 /* print job's dir if not the same */
#define AREASON 0200
struct process proclist; /* list head of all processes */
int pnoprocesses; /* pchild found nothing to wait for */
struct process *pholdjob; /* one level stack of current jobs */
struct process *pcurrjob; /* current job */
struct process *pcurrent; /* current job in table */
struct process *pprevious; /* previous job in table */
int pmaxindex; /* current maximum job index */
#endif /* !_PROC_H_ */

View File

@@ -1,646 +0,0 @@
/* $NetBSD: sem.c,v 1.29 2011/08/29 14:51:17 joerg Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)sem.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: sem.c,v 1.29 2011/08/29 14:51:17 joerg Exp $");
#endif
#endif /* not lint */
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "csh.h"
#include "extern.h"
#include "proc.h"
__dead static void vffree(int);
static Char *splicepipe(struct command *t, Char *);
static void doio(struct command *t, int *, int *);
static void chkclob(char *);
void
execute(struct command *t, int wtty, int *pipein, int *pipeout)
{
static sigset_t csigset, ocsigset;
static int nosigchld = 0, onosigchld = 0;
volatile int wanttty = wtty;
struct biltins * volatile bifunc;
int pv[2], pid;
sigset_t nsigset;
int forked;
UNREGISTER(forked);
UNREGISTER(bifunc);
UNREGISTER(wanttty);
forked = 0;
pid = 0;
if (t == 0)
return;
if (t->t_dflg & F_AMPERSAND)
wanttty = 0;
switch (t->t_dtyp) {
case NODE_COMMAND:
if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
(void)Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
if ((t->t_dflg & F_REPEAT) == 0)
Dfix(t); /* $ " ' \ */
if (t->t_dcom[0] == 0)
return;
/* FALLTHROUGH */
case NODE_PAREN:
if (t->t_dflg & F_PIPEOUT)
mypipe(pipeout);
/*
* Must do << early so parent will know where input pointer should be.
* If noexec then this is all we do.
*/
if (t->t_dflg & F_READ) {
(void)close(0);
heredoc(t->t_dlef);
if (noexec)
(void)close(0);
}
set(STRstatus, Strsave(STR0));
/*
* This mess is the necessary kludge to handle the prefix builtins:
* nice, nohup, time. These commands can also be used by themselves,
* and this is not handled here. This will also work when loops are
* parsed.
*/
while (t->t_dtyp == NODE_COMMAND)
if (eq(t->t_dcom[0], STRnice)) {
if (t->t_dcom[1]) {
if (strchr("+-", t->t_dcom[1][0])) {
if (t->t_dcom[2]) {
setname("nice");
t->t_nice =
getn(t->t_dcom[1]);
lshift(t->t_dcom, 2);
t->t_dflg |= F_NICE;
}
else
break;
} else {
t->t_nice = 4;
lshift(t->t_dcom, 1);
t->t_dflg |= F_NICE;
}
} else
break;
} else if (eq(t->t_dcom[0], STRnohup)) {
if (t->t_dcom[1]) {
t->t_dflg |= F_NOHUP;
lshift(t->t_dcom, 1);
}
else
break;
} else if (eq(t->t_dcom[0], STRtime)) {
if (t->t_dcom[1]) {
t->t_dflg |= F_TIME;
lshift(t->t_dcom, 1);
}
else
break;
} else
break;
/* is it a command */
if (t->t_dtyp == NODE_COMMAND) {
/*
* Check if we have a builtin function and remember which one.
*/
bifunc = isbfunc(t);
if (noexec && bifunc != NULL) {
/*
* Continue for builtins that are part of the scripting language
*/
if (bifunc->bfunct != dobreak && bifunc->bfunct != docontin &&
bifunc->bfunct != doelse && bifunc->bfunct != doend &&
bifunc->bfunct != doforeach && bifunc->bfunct != dogoto &&
bifunc->bfunct != doif && bifunc->bfunct != dorepeat &&
bifunc->bfunct != doswbrk && bifunc->bfunct != doswitch &&
bifunc->bfunct != dowhile && bifunc->bfunct != dozip)
break;
}
}
else { /* not a command */
bifunc = NULL;
if (noexec)
break;
}
/*
* We fork only if we are timed, or are not the end of a parenthesized
* list and not a simple builtin function. Simple meaning one that is
* not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
* fork in some of these cases.
*/
/*
* Prevent forking cd, pushd, popd, chdir cause this will cause the
* shell not to change dir!
*/
if (bifunc && (bifunc->bfunct == dochngd ||
bifunc->bfunct == dopushd ||
bifunc->bfunct == dopopd))
t->t_dflg &= ~(F_NICE);
if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
(!bifunc || t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
/*
* We have to fork for eval too.
*/
(bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
bifunc->bfunct == doeval)) {
if (t->t_dtyp == NODE_PAREN ||
t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
forked++;
/*
* We need to block SIGCHLD here, so that if the process does
* not die before we can set the process group
*/
if (wanttty >= 0 && !nosigchld) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGCHLD);
(void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
nosigchld = 1;
}
pid = pfork(t, wanttty);
if (pid == 0 && nosigchld) {
(void)sigprocmask(SIG_SETMASK, &csigset, NULL);
nosigchld = 0;
}
else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
backpid = pid;
}
else {
int ochild, osetintr, ohaderr, odidfds;
int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
sigset_t osigset;
/*
* Prepare for the vfork by saving everything that the child
* corrupts before it exec's. Note that in some signal
* implementations which keep the signal info in user space
* (e.g. Sun's) it will also be necessary to save and restore
* the current sigaction's for the signals the child touches
* before it exec's.
*/
if (wanttty >= 0 && !nosigchld && !noexec) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGCHLD);
(void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
nosigchld = 1;
}
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGCHLD);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
ochild = child;
osetintr = setintr;
ohaderr = haderr;
odidfds = didfds;
oSHIN = SHIN;
oSHOUT = SHOUT;
oSHERR = SHERR;
oOLDSTD = OLDSTD;
otpgrp = tpgrp;
ocsigset = csigset;
onosigchld = nosigchld;
Vsav = Vdp = 0;
Vexpath = 0;
Vt = 0;
pid = vfork();
if (pid < 0) {
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
stderror(ERR_NOPROC);
}
forked++;
if (pid) { /* parent */
child = ochild;
setintr = osetintr;
haderr = ohaderr;
didfds = odidfds;
SHIN = oSHIN;
SHOUT = oSHOUT;
SHERR = oSHERR;
OLDSTD = oOLDSTD;
tpgrp = otpgrp;
csigset = ocsigset;
nosigchld = onosigchld;
xfree((ptr_t) Vsav);
Vsav = 0;
xfree((ptr_t) Vdp);
Vdp = 0;
xfree((ptr_t) Vexpath);
Vexpath = 0;
blkfree((Char **) Vt);
Vt = 0;
/* this is from pfork() */
palloc(pid, t);
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
}
else { /* child */
/* this is from pfork() */
int pgrp;
int ignint = 0;
if (nosigchld) {
(void)sigprocmask(SIG_SETMASK, &csigset, NULL);
nosigchld = 0;
}
if (setintr)
ignint =
(tpgrp == -1 &&
(t->t_dflg & F_NOINTERRUPT))
|| (gointr && eq(gointr, STRminus));
pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
child++;
if (setintr) {
setintr = 0;
if (ignint) {
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGQUIT, SIG_IGN);
}
else {
(void)signal(SIGINT, vffree);
(void)signal(SIGQUIT, SIG_DFL);
}
if (wanttty >= 0) {
(void)signal(SIGTSTP, SIG_DFL);
(void)signal(SIGTTIN, SIG_DFL);
(void)signal(SIGTTOU, SIG_DFL);
}
(void)signal(SIGTERM, parterm);
}
else if (tpgrp == -1 &&
(t->t_dflg & F_NOINTERRUPT)) {
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGQUIT, SIG_IGN);
}
pgetty(wanttty, pgrp);
if (t->t_dflg & F_NOHUP)
(void)signal(SIGHUP, SIG_IGN);
if (t->t_dflg & F_NICE)
(void)setpriority(PRIO_PROCESS, 0, t->t_nice);
}
}
}
if (pid != 0) {
/*
* It would be better if we could wait for the whole job when we
* knew the last process had been started. Pwait, in fact, does
* wait for the whole job anyway, but this test doesn't really
* express our intentions.
*/
if (didfds == 0 && t->t_dflg & F_PIPEIN) {
(void)close(pipein[0]);
(void)close(pipein[1]);
}
if ((t->t_dflg & F_PIPEOUT) == 0) {
if (nosigchld) {
(void)sigprocmask(SIG_SETMASK, &csigset, NULL);
nosigchld = 0;
}
if ((t->t_dflg & F_AMPERSAND) == 0)
pwait();
}
break;
}
doio(t, pipein, pipeout);
if (t->t_dflg & F_PIPEOUT) {
(void)close(pipeout[0]);
(void)close(pipeout[1]);
}
/*
* Perform a builtin function. If we are not forked, arrange for
* possible stopping
*/
if (bifunc) {
func(t, bifunc);
if (forked)
exitstat();
break;
}
if (t->t_dtyp != NODE_PAREN)
doexec(NULL, t);
/*
* For () commands must put new 0,1,2 in FSH* and recurse
*/
(void) ioctl(OLDSTD = dcopy(0, FOLDSTD), FIOCLEX, NULL);
(void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
(void) ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
(void) close(SHIN);
SHIN = -1;
didfds = 0;
wanttty = -1;
t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dspr, wanttty, NULL, NULL);
exitstat();
/* NOTREACHED */
case NODE_PIPE:
t->t_dcar->t_dflg |= F_PIPEOUT |
(t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
execute(t->t_dcar, wanttty, pipein, pv);
t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
if (wanttty > 0)
wanttty = 0; /* got tty already */
execute(t->t_dcdr, wanttty, pv, pipeout);
break;
case NODE_LIST:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
/*
* In strange case of A&B make a new job after A
*/
if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
pendjob();
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
case NODE_OR:
case NODE_AND:
if (t->t_dcar) {
t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
execute(t->t_dcar, wanttty, NULL, NULL);
if ((getn(value(STRstatus)) == 0) !=
(t->t_dtyp == NODE_AND))
return;
}
if (t->t_dcdr) {
t->t_dcdr->t_dflg |= t->t_dflg &
(F_NOFORK | F_NOINTERRUPT);
execute(t->t_dcdr, wanttty, NULL, NULL);
}
break;
}
/*
* Fall through for all breaks from switch
*
* If there will be no more executions of this command, flush all file
* descriptors. Places that turn on the F_REPEAT bit are responsible for
* doing donefds after the last re-execution
*/
if (didfds && !(t->t_dflg & F_REPEAT))
donefds();
}
static void
vffree(int i)
{
Char **v;
if ((v = gargv) != NULL) {
gargv = 0;
xfree((ptr_t) v);
}
if ((v = pargv) != NULL) {
pargv = 0;
xfree((ptr_t) v);
}
_exit(i);
/* NOTREACHED */
}
/*
* Expand and glob the words after an i/o redirection.
* If more than one word is generated, then update the command vector.
*
* This is done differently in all the shells:
* 1. in the bourne shell and ksh globbing is not performed
* 2. Bash/csh say ambiguous
* 3. zsh does i/o to/from all the files
* 4. itcsh concatenates the words.
*
* I don't know what is best to do. I think that Ambiguous is better
* than restructuring the command vector, because the user can get
* unexpected results. In any case, the command vector restructuring
* code is present and the user can choose it by setting noambiguous
*/
static Char *
splicepipe(struct command *t, Char *cp /* word after < or > */)
{
Char *blk[2];
if (adrof(STRnoambiguous)) {
Char **pv;
blk[0] = Dfix1(cp); /* expand $ */
blk[1] = NULL;
gflag = 0, tglob(blk);
if (gflag) {
pv = globall(blk);
if (pv == NULL) {
setname(vis_str(blk[0]));
xfree((ptr_t) blk[0]);
stderror(ERR_NAME | ERR_NOMATCH);
/* NOTREACHED */
}
gargv = NULL;
if (pv[1] != NULL) { /* we need to fix the command vector */
Char **av = blkspl(t->t_dcom, &pv[1]);
xfree((ptr_t) t->t_dcom);
t->t_dcom = av;
}
xfree((ptr_t) blk[0]);
blk[0] = pv[0];
xfree((ptr_t) pv);
}
}
else {
blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR);
xfree((ptr_t) blk[1]);
}
return(blk[0]);
}
/*
* Perform io redirection.
* We may or maynot be forked here.
*/
static void
doio(struct command *t, int *pipein, int *pipeout)
{
Char *cp;
int fd, flags;
flags = t->t_dflg;
if (didfds || (flags & F_REPEAT))
return;
if ((flags & F_READ) == 0) {/* F_READ already done */
if (t->t_dlef) {
char tmp[MAXPATHLEN+1];
/*
* so < /dev/std{in,out,err} work
*/
(void)dcopy(SHIN, 0);
(void)dcopy(SHOUT, 1);
(void)dcopy(SHERR, 2);
cp = splicepipe(t, t->t_dlef);
(void)strlcpy(tmp, short2str(cp), sizeof(tmp));
xfree((ptr_t) cp);
if ((fd = open(tmp, O_RDONLY)) < 0) {
stderror(ERR_SYSTEM, tmp, strerror(errno));
/* NOTREACHED */
}
(void)dmove(fd, 0);
}
else if (flags & F_PIPEIN) {
(void)close(0);
(void)dup(pipein[0]);
(void)close(pipein[0]);
(void)close(pipein[1]);
}
else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
(void)close(0);
(void)open(_PATH_DEVNULL, O_RDONLY);
}
else {
(void)close(0);
(void)dup(OLDSTD);
(void)ioctl(0, FIONCLEX, NULL);
}
}
if (t->t_drit) {
char tmp[MAXPATHLEN+1];
cp = splicepipe(t, t->t_drit);
(void)strlcpy(tmp, short2str(cp), sizeof(tmp));
xfree((ptr_t) cp);
/*
* so > /dev/std{out,err} work
*/
(void)dcopy(SHOUT, 1);
(void)dcopy(SHERR, 2);
if ((flags & F_APPEND) &&
#ifdef O_APPEND
(fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
#else
(fd = open(tmp, O_WRONLY)) >= 0)
(void)lseek(1, (off_t) 0, SEEK_END);
#endif
else {
if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
if (flags & F_APPEND) {
stderror(ERR_SYSTEM, tmp, strerror(errno));
/* NOTREACHED */
}
chkclob(tmp);
}
if ((fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
stderror(ERR_SYSTEM, tmp, strerror(errno));
/* NOTREACHED */
}
}
(void)dmove(fd, 1);
}
else if (flags & F_PIPEOUT) {
(void)close(1);
(void)dup(pipeout[1]);
}
else {
(void)close(1);
(void)dup(SHOUT);
(void)ioctl(1, FIONCLEX, NULL);
}
(void)close(2);
if (flags & F_STDERR) {
(void)dup(1);
}
else {
(void)dup(SHERR);
(void)ioctl(2, FIONCLEX, NULL);
}
didfds = 1;
}
void
mypipe(int *pv)
{
if (pipe(pv) < 0)
goto oops;
pv[0] = dmove(pv[0], -1);
pv[1] = dmove(pv[1], -1);
if (pv[0] >= 0 && pv[1] >= 0)
return;
oops:
stderror(ERR_PIPE);
/* NOTREACHED */
}
static void
chkclob(char *cp)
{
struct stat stb;
if (stat(cp, &stb) < 0)
return;
if (S_ISCHR(stb.st_mode))
return;
stderror(ERR_EXISTS, cp);
/* NOTREACHED */
}

View File

@@ -1,820 +0,0 @@
/* $NetBSD: set.c,v 1.33 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)set.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: set.c,v 1.33 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#ifndef SHORT_STRINGS
#include <string.h>
#endif /* SHORT_STRINGS */
#include "csh.h"
#include "extern.h"
static Char *getinx(Char *, int *);
static void asx(Char *, int, Char *);
static struct varent *getvx(Char *, int);
static Char *xset(Char *, Char ***);
static Char *operate(int, Char *, Char *);
static void putn1(int);
static struct varent *madrof(Char *, struct varent *);
static void unsetv1(struct varent *);
static void exportpath(Char **);
static void balance(struct varent *, int, int);
/*
* C Shell
*/
static void
update_vars(Char *vp)
{
if (eq(vp, STRpath)) {
struct varent *pt = adrof(STRpath);
if (pt == NULL)
stderror(ERR_NAME | ERR_UNDVAR);
else {
exportpath(pt->vec);
dohash(NULL, NULL);
}
}
else if (eq(vp, STRhistchars)) {
Char *pn = value(STRhistchars);
HIST = *pn++;
HISTSUB = *pn;
}
else if (eq(vp, STRuser)) {
Setenv(STRUSER, value(vp));
Setenv(STRLOGNAME, value(vp));
}
else if (eq(vp, STRwordchars)) {
word_chars = value(vp);
}
else if (eq(vp, STRterm))
Setenv(STRTERM, value(vp));
else if (eq(vp, STRhome)) {
Char *cp;
cp = Strsave(value(vp)); /* get the old value back */
/*
* convert to canonical pathname (possibly resolving symlinks)
*/
cp = dcanon(cp, cp);
set(vp, Strsave(cp)); /* have to save the new val */
/* and now mirror home with HOME */
Setenv(STRHOME, cp);
/* fix directory stack for new tilde home */
dtilde();
xfree((ptr_t)cp);
}
#ifdef FILEC
else if (eq(vp, STRfilec))
filec = 1;
#endif
#ifdef EDIT
else if (eq(vp, STRedit)) {
HistEvent ev;
editing = 1;
el = el_init_fd(getprogname(), cshin, cshout, csherr,
SHIN, SHOUT, SHERR);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, printpromptstr);
hi = history_init();
history(hi, &ev, H_SETSIZE, getn(value(STRhistory)));
loadhist(Histlist.Hnext);
el_set(el, EL_HIST, history, hi);
}
#endif
}
void
/*ARGSUSED*/
doset(Char **v, struct command *t)
{
Char op, *p, **vecp, *vp;
int subscr = 0; /* XXX: GCC */
int hadsub;
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
vp = p;
if (letter(*p))
for (; alnum(*p); p++)
continue;
if (vp == p || !letter(*vp))
stderror(ERR_NAME | ERR_VARBEGIN);
if ((p - vp) > MAXVARLEN)
stderror(ERR_NAME | ERR_VARTOOLONG);
if (*p == '[') {
hadsub++;
p = getinx(p, &subscr);
}
if ((op = *p) != '\0') {
*p++ = 0;
if (*p == 0 && *v && **v == '(')
p = *v++;
}
else if (*v && eq(*v, STRequal)) {
op = '=', v++;
if (*v)
p = *v++;
}
if (op && op != '=')
stderror(ERR_NAME | ERR_SYNTAX);
if (eq(p, STRLparen)) {
Char **e = v;
if (hadsub)
stderror(ERR_NAME | ERR_SYNTAX);
for (;;) {
if (!*e)
stderror(ERR_NAME | ERR_MISSING, ')');
if (**e == ')')
break;
e++;
}
p = *e;
*e = 0;
vecp = saveblk(v);
set1(vp, vecp, &shvhed);
*e = p;
v = e + 1;
}
else if (hadsub)
asx(vp, subscr, Strsave(p));
else
set(vp, Strsave(p));
update_vars(vp);
} while ((p = *v++) != NULL);
}
static Char *
getinx(Char *cp, int *ip)
{
*ip = 0;
*cp++ = 0;
while (*cp && Isdigit(*cp))
*ip = *ip * 10 + *cp++ - '0';
if (*cp++ != ']')
stderror(ERR_NAME | ERR_SUBSCRIPT);
return (cp);
}
static void
asx(Char *vp, int subscr, Char *p)
{
struct varent *v;
v = getvx(vp, subscr);
xfree((ptr_t) v->vec[subscr - 1]);
v->vec[subscr - 1] = globone(p, G_APPEND);
}
static struct varent *
getvx(Char *vp, int subscr)
{
struct varent *v;
v = adrof(vp);
if (v == 0)
udvar(vp);
if (subscr < 1 || subscr > blklen(v->vec))
stderror(ERR_NAME | ERR_RANGE);
return (v);
}
void
/*ARGSUSED*/
dolet(Char **v, struct command *t)
{
Char c, op, *p, *vp;
int subscr = 0; /* XXX: GCC */
int hadsub;
v++;
p = *v++;
if (p == 0) {
prvars();
return;
}
do {
hadsub = 0;
vp = p;
if (letter(*p))
for (; alnum(*p); p++)
continue;
if (vp == p || !letter(*vp))
stderror(ERR_NAME | ERR_VARBEGIN);
if ((p - vp) > MAXVARLEN)
stderror(ERR_NAME | ERR_VARTOOLONG);
if (*p == '[') {
hadsub++;
p = getinx(p, &subscr);
}
if (*p == 0 && *v)
p = *v++;
if ((op = *p) != '\0')
*p++ = 0;
else
stderror(ERR_NAME | ERR_ASSIGN);
if (*p == '\0' && *v == NULL)
stderror(ERR_NAME | ERR_ASSIGN);
vp = Strsave(vp);
if (op == '=') {
c = '=';
p = xset(p, &v);
}
else {
c = *p++;
if (any("+-", c)) {
if (c != op || *p)
stderror(ERR_NAME | ERR_UNKNOWNOP);
p = Strsave(STR1);
}
else {
if (any("<>", op)) {
if (c != op)
stderror(ERR_NAME | ERR_UNKNOWNOP);
c = *p++;
stderror(ERR_NAME | ERR_SYNTAX);
}
if (c != '=')
stderror(ERR_NAME | ERR_UNKNOWNOP);
p = xset(p, &v);
}
}
if (op == '=') {
if (hadsub)
asx(vp, subscr, p);
else
set(vp, p);
} else if (hadsub) {
struct varent *gv = getvx(vp, subscr);
asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
}
else
set(vp, operate(op, value(vp), p));
if (eq(vp, STRpath)) {
struct varent *pt = adrof(STRpath);
if (pt == NULL)
stderror(ERR_NAME | ERR_UNDVAR);
else {
exportpath(pt->vec);
dohash(NULL, NULL);
}
}
xfree((ptr_t) vp);
if (c != '=')
xfree((ptr_t) p);
} while ((p = *v++) != NULL);
}
static Char *
xset(Char *cp, Char ***vp)
{
Char *dp;
if (*cp) {
dp = Strsave(cp);
--(*vp);
xfree((ptr_t) ** vp);
**vp = dp;
}
return (putn(expr(vp)));
}
static Char *
operate(int op, Char *vp, Char *p)
{
Char opr[2], **v, *vec[5], **vecp;
int i;
v = vec;
vecp = v;
if (op != '=') {
if (*vp)
*v++ = vp;
opr[0] = (Char)op;
opr[1] = 0;
*v++ = opr;
if (op == '<' || op == '>')
*v++ = opr;
}
*v++ = p;
*v++ = 0;
i = expr(&vecp);
if (*vecp)
stderror(ERR_NAME | ERR_EXPRESSION);
return (putn(i));
}
static Char *putp;
Char *
putn(int n)
{
static Char numbers[15];
putp = numbers;
if (n < 0) {
n = -n;
*putp++ = '-';
}
if ((unsigned int)n == 0x80000000U) {
*putp++ = '2';
n = 147483648;
}
putn1(n);
*putp = 0;
return (Strsave(numbers));
}
static void
putn1(int n)
{
if (n > 9)
putn1(n / 10);
*putp++ = (Char)(n % 10 + '0');
}
int
getn(Char *cp)
{
int n, sign;
sign = 0;
if (cp[0] == '+' && cp[1])
cp++;
if (*cp == '-') {
sign++;
cp++;
if (!Isdigit(*cp))
stderror(ERR_NAME | ERR_BADNUM);
}
n = 0;
while (Isdigit(*cp))
n = n * 10 + *cp++ - '0';
if (*cp)
stderror(ERR_NAME | ERR_BADNUM);
return (sign ? -n : n);
}
Char *
value1(Char *var, struct varent *head)
{
struct varent *vp;
vp = adrof1(var, head);
return (vp == 0 || vp->vec[0] == 0 ? STRNULL : vp->vec[0]);
}
static struct varent *
madrof(Char *pat, struct varent *vp)
{
struct varent *vp1;
for (; vp; vp = vp->v_right) {
if (vp->v_left && (vp1 = madrof(pat, vp->v_left)))
return vp1;
if (Gmatch(vp->v_name, pat))
return vp;
}
return vp;
}
struct varent *
adrof1(Char *name, struct varent *v)
{
int cmp;
v = v->v_left;
while (v && ((cmp = *name - *v->v_name) ||
(cmp = Strcmp(name, v->v_name))))
if (cmp < 0)
v = v->v_left;
else
v = v->v_right;
return v;
}
/*
* The caller is responsible for putting value in a safe place
*/
void
set(Char *var, Char *val)
{
Char **vec;
vec = xmalloc(2 * sizeof(*vec));
vec[0] = val;
vec[1] = 0;
set1(var, vec, &shvhed);
}
void
set1(Char *var, Char **vec, struct varent *head)
{
Char **oldv;
oldv = vec;
gflag = 0;
tglob(oldv);
if (gflag) {
vec = globall(oldv);
if (vec == 0) {
blkfree(oldv);
stderror(ERR_NAME | ERR_NOMATCH);
}
blkfree(oldv);
gargv = 0;
}
setq(var, vec, head);
}
void
setq(Char *name, Char **vec, struct varent *p)
{
struct varent *c;
int f;
f = 0; /* tree hangs off the header's left link */
while ((c = p->v_link[f]) != NULL) {
if ((f = *name - *c->v_name) == 0 &&
(f = Strcmp(name, c->v_name)) == 0) {
blkfree(c->vec);
goto found;
}
p = c;
f = f > 0;
}
p->v_link[f] = c = xmalloc(sizeof(*c));
c->v_name = Strsave(name);
c->v_bal = 0;
c->v_left = c->v_right = 0;
c->v_parent = p;
balance(p, f, 0);
found:
trim(c->vec = vec);
}
void
/*ARGSUSED*/
unset(Char **v, struct command *t)
{
unset1(v, &shvhed);
if (adrof(STRhistchars) == 0) {
HIST = '!';
HISTSUB = '^';
}
else if (adrof(STRwordchars) == 0)
word_chars = STR_WORD_CHARS;
#ifdef FILEC
else if (adrof(STRfilec) == 0)
filec = 0;
#endif
#ifdef EDIT
else if (adrof(STRedit) == 0) {
el_end(el);
history_end(hi);
el = NULL;
hi = NULL;
editing = 0;
}
#endif
}
void
unset1(Char *v[], struct varent *head)
{
struct varent *vp;
int cnt;
while (*++v) {
cnt = 0;
while ((vp = madrof(*v, head->v_left)) != NULL)
unsetv1(vp), cnt++;
if (cnt == 0)
setname(vis_str(*v));
}
}
void
unsetv(Char *var)
{
struct varent *vp;
if ((vp = adrof1(var, &shvhed)) == 0)
udvar(var);
unsetv1(vp);
}
static void
unsetv1(struct varent *p)
{
struct varent *c, *pp;
int f;
/*
* Free associated memory first to avoid complications.
*/
blkfree(p->vec);
xfree((ptr_t) p->v_name);
/*
* If p is missing one child, then we can move the other into where p is.
* Otherwise, we find the predecessor of p, which is guaranteed to have no
* right child, copy it into p, and move its left child into it.
*/
if (p->v_right == 0)
c = p->v_left;
else if (p->v_left == 0)
c = p->v_right;
else {
for (c = p->v_left; c->v_right; c = c->v_right)
continue;
p->v_name = c->v_name;
p->vec = c->vec;
p = c;
c = p->v_left;
}
/*
* Move c into where p is.
*/
pp = p->v_parent;
f = pp->v_right == p;
if ((pp->v_link[f] = c) != NULL)
c->v_parent = pp;
/*
* Free the deleted node, and rebalance.
*/
xfree((ptr_t) p);
balance(pp, f, 1);
}
void
setNS(Char *cp)
{
set(cp, Strsave(STRNULL));
}
void
/*ARGSUSED*/
shift(Char **v, struct command *t)
{
struct varent *argv;
Char *name;
v++;
name = *v;
if (name == 0)
name = STRargv;
else
(void) strip(name);
argv = adrof(name);
if (argv == 0)
udvar(name);
if (argv->vec[0] == 0)
stderror(ERR_NAME | ERR_NOMORE);
lshift(argv->vec, 1);
update_vars(name);
}
static void
exportpath(Char **val)
{
Char exppath[BUFSIZE];
exppath[0] = 0;
if (val)
while (*val) {
if (Strlen(*val) + Strlen(exppath) + 2 > BUFSIZE) {
(void)fprintf(csherr,
"Warning: ridiculously long PATH truncated\n");
break;
}
(void)Strcat(exppath, *val++);
if (*val == 0 || eq(*val, STRRparen))
break;
(void)Strcat(exppath, STRcolon);
}
Setenv(STRPATH, exppath);
}
#ifndef lint
/*
* Lint thinks these have null effect
*/
/* macros to do single rotations on node p */
#define rright(p) (\
t = (p)->v_left,\
(t)->v_parent = (p)->v_parent,\
((p)->v_left = t->v_right) ? (t->v_right->v_parent = (p)) : 0,\
(t->v_right = (p))->v_parent = t,\
(p) = t)
#define rleft(p) (\
t = (p)->v_right,\
(t)->v_parent = (p)->v_parent,\
((p)->v_right = t->v_left) ? (t->v_left->v_parent = (p)) : 0,\
(t->v_left = (p))->v_parent = t,\
(p) = t)
#else
struct varent *
rleft(struct varent *p)
{
return (p);
}
struct varent *
rright(struct varent *p)
{
return (p);
}
#endif /* ! lint */
/*
* Rebalance a tree, starting at p and up.
* F == 0 means we've come from p's left child.
* D == 1 means we've just done a delete, otherwise an insert.
*/
static void
balance(struct varent *p, int f, int d)
{
struct varent *pp;
#ifndef lint
struct varent *t; /* used by the rotate macros */
#endif
int ff;
/*
* Ok, from here on, p is the node we're operating on; pp is its parent; f
* is the branch of p from which we have come; ff is the branch of pp which
* is p.
*/
for (; (pp = p->v_parent) != NULL; p = pp, f = ff) {
ff = pp->v_right == p;
if (f ^ d) { /* right heavy */
switch (p->v_bal) {
case -1: /* was left heavy */
p->v_bal = 0;
break;
case 0: /* was balanced */
p->v_bal = 1;
break;
case 1: /* was already right heavy */
switch (p->v_right->v_bal) {
case 1: /* single rotate */
pp->v_link[ff] = rleft(p);
p->v_left->v_bal = 0;
p->v_bal = 0;
break;
case 0: /* single rotate */
pp->v_link[ff] = rleft(p);
p->v_left->v_bal = 1;
p->v_bal = -1;
break;
case -1: /* double rotate */
(void) rright(p->v_right);
pp->v_link[ff] = rleft(p);
p->v_left->v_bal =
p->v_bal < 1 ? 0 : -1;
p->v_right->v_bal =
p->v_bal > -1 ? 0 : 1;
p->v_bal = 0;
break;
}
break;
}
}
else { /* left heavy */
switch (p->v_bal) {
case 1: /* was right heavy */
p->v_bal = 0;
break;
case 0: /* was balanced */
p->v_bal = -1;
break;
case -1: /* was already left heavy */
switch (p->v_left->v_bal) {
case -1: /* single rotate */
pp->v_link[ff] = rright(p);
p->v_right->v_bal = 0;
p->v_bal = 0;
break;
case 0: /* single rotate */
pp->v_link[ff] = rright(p);
p->v_right->v_bal = -1;
p->v_bal = 1;
break;
case 1: /* double rotate */
(void) rleft(p->v_left);
pp->v_link[ff] = rright(p);
p->v_left->v_bal =
p->v_bal < 1 ? 0 : -1;
p->v_right->v_bal =
p->v_bal > -1 ? 0 : 1;
p->v_bal = 0;
break;
}
break;
}
}
/*
* If from insert, then we terminate when p is balanced. If from
* delete, then we terminate when p is unbalanced.
*/
if ((p->v_bal == 0) ^ d)
break;
}
}
void
plist(struct varent *p)
{
struct varent *c;
sigset_t nsigset;
int len;
if (setintr) {
sigemptyset(&nsigset);
(void)sigaddset(&nsigset, SIGINT);
(void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
}
for (;;) {
while (p->v_left)
p = p->v_left;
x:
if (p->v_parent == 0) /* is it the header? */
return;
len = blklen(p->vec);
(void)fprintf(cshout, "%s\t", short2str(p->v_name));
if (len != 1)
(void)fputc('(', cshout);
blkpr(cshout, p->vec);
if (len != 1)
(void)fputc(')', cshout);
(void)fputc('\n', cshout);
if (p->v_right) {
p = p->v_right;
continue;
}
do {
c = p;
p = p->v_parent;
} while (p->v_right == c);
goto x;
}
}

View File

@@ -1,441 +0,0 @@
/* $NetBSD: str.c,v 1.15 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)str.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: str.c,v 1.15 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#define MALLOC_INCR 128
/*
* tc.str.c: Short string package
* This has been a lesson of how to write buggy code!
*/
#include <sys/types.h>
#include <stdarg.h>
#include <vis.h>
#include "csh.h"
#include "extern.h"
#ifdef SHORT_STRINGS
Char **
blk2short(char **src)
{
Char **dst, **sdst;
size_t n;
/*
* Count
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
for (; *src != NULL; src++)
*dst++ = SAVE(*src);
*dst = NULL;
return (sdst);
}
char **
short2blk(Char *const *src)
{
char **dst, **sdst;
size_t n;
/*
* Count
*/
for (n = 0; src[n] != NULL; n++)
continue;
sdst = dst = xmalloc((size_t)((n + 1) * sizeof(*dst)));
for (; *src != NULL; src++)
*dst++ = strsave(short2str(*src));
*dst = NULL;
return (sdst);
}
Char *
str2short(const char *src)
{
static Char *sdst;
Char *dst, *edst;
static size_t dstsize = 0;
if (src == NULL)
return (NULL);
if (sdst == (NULL)) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
*dst++ = (Char) ((unsigned char) *src++);
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc((ptr_t)sdst,
(size_t)dstsize * sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
char *
short2str(const Char *src)
{
static char *sdst = NULL;
static size_t dstsize = 0;
char *dst, *edst;
if (src == NULL)
return (NULL);
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc((ptr_t)sdst,
(size_t)dstsize * sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
Char *
s_strcpy(Char *dst, const Char *src)
{
Char *sdst;
sdst = dst;
while ((*dst++ = *src++) != '\0')
continue;
return (sdst);
}
Char *
s_strncpy(Char *dst, const Char *src, size_t n)
{
Char *sdst;
if (n == 0)
return(dst);
sdst = dst;
do
if ((*dst++ = *src++) == '\0') {
while (--n != 0)
*dst++ = '\0';
return(sdst);
}
while (--n != 0);
return (sdst);
}
Char *
s_strcat(Char *dst, const Char *src)
{
short *sdst;
sdst = dst;
while (*dst++)
continue;
--dst;
while ((*dst++ = *src++) != '\0')
continue;
return (sdst);
}
#ifdef NOTUSED
Char *
s_strncat(Char *dst, Char *src, size_t n)
{
Char *sdst;
if (n == 0)
return (dst);
sdst = dst;
while (*dst++)
continue;
--dst;
do
if ((*dst++ = *src++) == '\0')
return(sdst);
while (--n != 0)
continue;
*dst = '\0';
return (sdst);
}
#endif
Char *
s_strchr(const Char *str, int ch)
{
do
if (*str == ch)
return __UNCONST(str);
while (*str++);
return (NULL);
}
Char *
s_strrchr(const Char *str, int ch)
{
const Char *rstr;
rstr = NULL;
do
if (*str == ch)
rstr = str;
while (*str++);
return __UNCONST(rstr);
}
size_t
s_strlen(const Char *str)
{
size_t n;
for (n = 0; *str++; n++)
continue;
return (n);
}
int
s_strcmp(const Char *str1, const Char *str2)
{
for (; *str1 && *str1 == *str2; str1++, str2++)
continue;
/*
* The following case analysis is necessary so that characters which look
* negative collate low against normal characters but high against the
* end-of-string NUL.
*/
if (*str1 == '\0' && *str2 == '\0')
return (0);
else if (*str1 == '\0')
return (-1);
else if (*str2 == '\0')
return (1);
else
return (*str1 - *str2);
}
int
s_strncmp(const Char *str1, const Char *str2, size_t n)
{
if (n == 0)
return (0);
do {
if (*str1 != *str2) {
/*
* The following case analysis is necessary so that characters
* which look negative collate low against normal characters
* but high against the end-of-string NUL.
*/
if (*str1 == '\0')
return (-1);
else if (*str2 == '\0')
return (1);
else
return (*str1 - *str2);
}
if (*str1 == '\0')
return(0);
str1++, str2++;
} while (--n != 0);
return(0);
}
Char *
s_strsave(const Char *s)
{
const Char *p;
Char *n;
if (s == 0)
s = STRNULL;
for (p = s; *p++;)
continue;
p = n = xmalloc((size_t)(p - s) * sizeof(*n));
while ((*n++ = *s++) != '\0')
continue;
return __UNCONST(p);
}
Char *
s_strspl(const Char *cp, const Char *dp)
{
Char *ep, *d;
const Char *p, *q;
if (!cp)
cp = STRNULL;
if (!dp)
dp = STRNULL;
for (p = cp; *p++;)
continue;
for (q = dp; *q++;)
continue;
ep = xmalloc((size_t)((p - cp) + (q - dp) - 1) * sizeof(*ep));
for (d = ep, q = cp; (*d++ = *q++) != '\0';)
continue;
for (d--, q = dp; (*d++ = *q++) != '\0';)
continue;
return (ep);
}
Char *
s_strend(const Char *cp)
{
if (!cp)
return __UNCONST(cp);
while (*cp)
cp++;
return __UNCONST(cp);
}
Char *
s_strstr(const Char *s, const Char *t)
{
do {
const Char *ss = s;
const Char *tt = t;
do
if (*tt == '\0')
return __UNCONST(s);
while (*ss++ == *tt++);
} while (*s++ != '\0');
return (NULL);
}
#endif /* SHORT_STRINGS */
char *
short2qstr(const Char *src)
{
static char *sdst = NULL;
static size_t dstsize = 0;
char *dst, *edst;
if (src == NULL)
return (NULL);
if (sdst == NULL) {
dstsize = MALLOC_INCR;
sdst = xmalloc((size_t)dstsize * sizeof(*sdst));
}
dst = sdst;
edst = &dst[dstsize];
while (*src) {
if (*src & QUOTE) {
*dst++ = '\\';
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc((ptr_t) sdst,
(size_t)dstsize * sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst++ = (char) *src++;
if (dst == edst) {
dstsize += MALLOC_INCR;
sdst = xrealloc((ptr_t) sdst,
(size_t)dstsize * sizeof(*sdst));
edst = &sdst[dstsize];
dst = &edst[-MALLOC_INCR];
}
}
*dst = 0;
return (sdst);
}
/*
* XXX: Should we worry about QUOTE'd chars?
*/
char *
vis_str(const Char *cp)
{
static char *sdst = NULL;
static size_t dstsize = 0;
const Char *dp;
size_t n;
if (cp == NULL)
return (NULL);
for (dp = cp; *dp++;)
continue;
n = ((size_t)(dp - cp) << 2) + 1; /* 4 times + NULL */
if (dstsize < n) {
sdst = (dstsize ?
xrealloc(sdst, (size_t)n * sizeof(*sdst)) :
xmalloc((size_t)n * sizeof(*sdst)));
dstsize = n;
}
/*
* XXX: When we are in AsciiOnly we want all characters >= 0200 to
* be encoded, but currently there is no way in vis to do that.
*/
(void)strvis(sdst, short2str(cp), VIS_NOSLASH);
return (sdst);
}

View File

@@ -1,285 +0,0 @@
/* $NetBSD: time.c,v 1.20 2013/07/16 17:47:43 christos Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: time.c,v 1.20 2013/07/16 17:47:43 christos Exp $");
#endif
#endif /* not lint */
#ifndef NOT_CSH
#include <sys/types.h>
#include <stdarg.h>
#include "csh.h"
#include "extern.h"
#endif
#include <util.h>
/*
* C Shell - routines handling process timing and niceing
*/
static void pdeltat(FILE *, struct timeval *, struct timeval *);
static void pcsecs(FILE *, long);
#ifndef NOT_CSH
void
settimes(void)
{
struct rusage ruch;
(void)clock_gettime(CLOCK_MONOTONIC, &time0);
(void)getrusage(RUSAGE_SELF, &ru0);
(void)getrusage(RUSAGE_CHILDREN, &ruch);
ruadd(&ru0, &ruch);
}
/*
* dotime is only called if it is truly a builtin function and not a
* prefix to another command
*/
void
/*ARGSUSED*/
dotime(Char **v, struct command *t)
{
struct rusage ru1, ruch;
struct timespec timedol;
(void)getrusage(RUSAGE_SELF, &ru1);
(void)getrusage(RUSAGE_CHILDREN, &ruch);
ruadd(&ru1, &ruch);
(void)clock_gettime(CLOCK_MONOTONIC, &timedol);
prusage(cshout, &ru0, &ru1, &timedol, &time0);
}
/*
* donice is only called when it on the line by itself or with a +- value
*/
void
/*ARGSUSED*/
donice(Char **v, struct command *t)
{
Char *cp;
int nval;
nval = 0;
v++;
cp = *v++;
if (cp == 0)
nval = 4;
else if (*v == 0 && any("+-", cp[0]))
nval = getn(cp);
(void)setpriority(PRIO_PROCESS, 0, nval);
}
void
ruadd(struct rusage *ru, struct rusage *ru2)
{
timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
if (ru2->ru_maxrss > ru->ru_maxrss)
ru->ru_maxrss = ru2->ru_maxrss;
ru->ru_ixrss += ru2->ru_ixrss;
ru->ru_idrss += ru2->ru_idrss;
ru->ru_isrss += ru2->ru_isrss;
ru->ru_minflt += ru2->ru_minflt;
ru->ru_majflt += ru2->ru_majflt;
ru->ru_nswap += ru2->ru_nswap;
ru->ru_inblock += ru2->ru_inblock;
ru->ru_oublock += ru2->ru_oublock;
ru->ru_msgsnd += ru2->ru_msgsnd;
ru->ru_msgrcv += ru2->ru_msgrcv;
ru->ru_nsignals += ru2->ru_nsignals;
ru->ru_nvcsw += ru2->ru_nvcsw;
ru->ru_nivcsw += ru2->ru_nivcsw;
}
#endif /* NOT_CSH */
void
prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timespec *e,
struct timespec *b)
{
#ifndef NOT_CSH
struct varent *vp;
#endif
const char *cp;
long i;
time_t t;
time_t ms;
cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
(r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
(r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
(r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
#ifndef NOT_CSH
vp = adrof(STRtime);
if (vp && vp->vec[0] && vp->vec[1])
cp = short2str(vp->vec[1]);
#endif
for (; *cp; cp++)
if (*cp != '%')
(void) fputc(*cp, fp);
else if (cp[1])
switch (*++cp) {
case 'D': /* (average) unshared data size */
(void)fprintf(fp, "%ld", t == 0 ? 0L :
(long)((r1->ru_idrss + r1->ru_isrss -
(r0->ru_idrss + r0->ru_isrss)) / t));
break;
case 'E': /* elapsed (wall-clock) time */
pcsecs(fp, (long) ms);
break;
case 'F': /* page faults */
(void)fprintf(fp, "%ld", r1->ru_majflt - r0->ru_majflt);
break;
case 'I': /* FS blocks in */
(void)fprintf(fp, "%ld", r1->ru_inblock - r0->ru_inblock);
break;
case 'K': /* (average) total data memory used */
(void)fprintf(fp, "%ld", t == 0 ? 0L :
(long)(((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
(r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t));
break;
case 'M': /* max. Resident Set Size */
(void)fprintf(fp, "%ld", r1->ru_maxrss / 2L);
break;
case 'O': /* FS blocks out */
(void)fprintf(fp, "%ld", r1->ru_oublock - r0->ru_oublock);
break;
case 'P': /* percent time spent running */
/* check if it did not run at all */
if (ms == 0) {
(void)fputs("0.0%", fp);
} else {
char pb[32];
(void)fputs(strpct(pb, sizeof(pb),
(uintmax_t)t, (uintmax_t)ms, 1), fp);
(void)fputc('%', fp);
}
break;
case 'R': /* page reclaims */
(void)fprintf(fp, "%ld", r1->ru_minflt - r0->ru_minflt);
break;
case 'S': /* system CPU time used */
pdeltat(fp, &r1->ru_stime, &r0->ru_stime);
break;
case 'U': /* user CPU time used */
pdeltat(fp, &r1->ru_utime, &r0->ru_utime);
break;
case 'W': /* number of swaps */
i = r1->ru_nswap - r0->ru_nswap;
(void)fprintf(fp, "%ld", i);
break;
case 'X': /* (average) shared text size */
(void)fprintf(fp, "%ld", t == 0 ? 0L :
(long)((r1->ru_ixrss - r0->ru_ixrss) / t));
break;
case 'c': /* num. involuntary context switches */
(void)fprintf(fp, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
break;
case 'k': /* number of signals received */
(void)fprintf(fp, "%ld", r1->ru_nsignals-r0->ru_nsignals);
break;
case 'r': /* socket messages received */
(void)fprintf(fp, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
break;
case 's': /* socket messages sent */
(void)fprintf(fp, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
break;
case 'w': /* num. voluntary context switches (waits) */
(void)fprintf(fp, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
break;
}
(void)fputc('\n', fp);
}
static void
pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0)
{
struct timeval td;
timersub(t1, t0, &td);
(void)fprintf(fp, "%ld.%01ld", (long)td.tv_sec,
(long)(td.tv_usec / 100000));
}
#define P2DIG(fp, i) (void)fprintf(fp, "%ld%ld", (i) / 10, (i) % 10)
#ifndef NOT_CSH
void
psecs(long l)
{
long i;
i = l / 3600;
if (i) {
(void)fprintf(cshout, "%ld:", i);
i = l % 3600;
P2DIG(cshout, i / 60);
goto minsec;
}
i = l;
(void)fprintf(cshout, "%ld", i / 60);
minsec:
i %= 60;
(void)fputc(':', cshout);
P2DIG(cshout, i);
}
#endif
static void
pcsecs(FILE *fp, long l) /* PWP: print mm:ss.dd, l is in sec*100 */
{
long i;
i = l / 360000;
if (i) {
(void)fprintf(fp, "%ld:", i);
i = (l % 360000) / 100;
P2DIG(fp, i / 60);
goto minsec;
}
i = l / 100;
(void)fprintf(fp, "%ld", i / 60);
minsec:
i %= 60;
(void)fputc(':', fp);
P2DIG(fp, i);
(void)fputc('.', fp);
P2DIG(fp, (l % 100));
}

View File

@@ -1,12 +0,0 @@
# $NetBSD: Makefile,v 1.15 2011/08/14 10:53:16 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= date
SRCS= date.c netdate.c
DPADD+= ${LIBUTIL}
LDADD+= -lutil
CPPFLAGS+=-I${.CURDIR}
COPTS.date.c = -Wno-format-nonliteral
.include <bsd.prog.mk>

View File

@@ -1,246 +0,0 @@
.\" $NetBSD: date.1,v 1.42 2012/04/06 11:36:56 wiz Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)date.1 8.3 (Berkeley) 4/28/95
.\"
.Dd November 15, 2006
.Dt DATE 1
.Os
.Sh NAME
.Nm date
.Nd display or set date and time
.Sh SYNOPSIS
.Nm
.Op Fl ajnu
.Op Fl d Ar date
.Op Fl r Ar seconds
.Op Cm + Ns Ar format
.Sm off
.Oo Oo Oo Oo Oo Oo
.Ar CC Oc
.Ar yy Oc
.Ar mm Oc
.Ar dd Oc
.Ar HH Oc Ar MM Oo
.Li \&. Ar SS Oc Oc
.Sm on
.Sh DESCRIPTION
.Nm
displays the current date and time when invoked without arguments.
Providing arguments will format the date and time in a user-defined
way or set the date.
Only the superuser may set the date.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl a
Use
.Xr adjtime 2
to change the local system time slowly,
maintaining it as a monotonically increasing function.
.Fl a
implies
.Fl n .
.It Fl d Ar date
Parse the provided human-described date and time and display the result without
actually changing the system clock.
(See
.Xr parsedate 3
for examples.)
.It Fl j
Parse the provided canonical representation of date and time (described below)
and display the result without actually changing the system clock.
.It Fl n
The utility
.Xr timed 8
is used to synchronize the clocks on groups of machines.
By default, if
.Xr timed 8
is running,
.Nm
will set the time on all of the machines in the local group.
The
.Fl n
option stops
.Nm
from setting the time for other than the current machine.
.It Fl r Ar seconds
Print out the date and time that is
.Ar seconds
from the Epoch.
.It Fl u
Display or set the date in
.Tn UTC
(universal) time.
.El
.Pp
An operand with a leading plus
.Pq Cm +
sign signals a user-defined format
string which specifies the format in which to display the date and time.
The format string may contain any of the conversion specifications described
in the
.Xr strftime 3
manual page, as well as any arbitrary text.
A \*[Lt]newline\*[Gt] character is always output after the characters
specified by the format string.
The format string for the default display is:
.Bd -literal -offset indent
%a %b %e %H:%M:%S %Z %Y
.Ed
.Pp
If an operand does not have a leading plus sign, it is interpreted as
a value for setting the system's notion of the current date and time.
The canonical representation for setting the date and time is:
.Pp
.Bl -tag -width Ds -compact -offset indent
.It Ar CC
The first two digits of the year (the century).
.It Ar yy
The second two digits of the year.
If
.Ar yy
is specified, but
.Ar CC
is not, a value for
.Ar yy
between 69 and 99 results in a
.Ar CC
value of 19.
Otherwise, a
.Ar CC
value of 20 is used.
.It Ar mm
The month of the year, from 01 to 12.
.It Ar dd
The day of the month, from 01 to 31.
.It Ar HH
The hour of the day, from 00 to 23.
.It Ar MM
The minute of the hour, from 00 to 59.
.It Ar SS
The second of the minute, from 00 to 61.
.El
.Pp
Everything but the minutes is optional.
.Pp
Time changes for Daylight Saving and Standard Time and leap seconds
and years are handled automatically.
.Sh ENVIRONMENT
The following environment variables affect the execution of
.Nm :
.Bl -tag -width iTZ
.It Ev TZ
The timezone to use when displaying dates.
See
.Xr environ 7
for more information.
.El
.Sh FILES
.Bl -tag -width /var/log/messages -compact
.It Pa /etc/localtime
Symlink pointing to system's default timezone information file in
.Pa /usr/share/zoneinfo
directory.
.It Pa /var/log/wtmp
A record of date resets and time changes.
.It Pa /var/log/messages
A record of the user setting the time.
.El
.Sh EXAMPLES
The command:
.Bd -literal -offset indent
date '+DATE: %m/%d/%y%nTIME: %H:%M:%S'
.Ed
.Pp
will display:
.Bd -literal -offset indent
DATE: 11/21/87
TIME: 13:36:16
.Ed
.Pp
The command:
.Bd -literal -offset indent
date 8506131627
.Ed
.Pp
sets the date to
.Dq Li "June 13, 1985, 4:27 PM" .
.Pp
The command:
.Bd -literal -offset indent
date 1432
.Ed
.Pp
sets the time to
.Li "2:32 PM" ,
without modifying the date.
.Sh DIAGNOSTICS
Exit status is 0 on success, 1 if unable to set the date, and 2
if able to set the local date, but unable to set it globally.
.Pp
Occasionally, when
.Xr timed 8
synchronizes the time on many hosts, the setting of a new time value may
require more than a few seconds.
On these occasions,
.Nm
prints:
.Ql Network time being set .
The message
.Ql Communication error with
.Xr timed 8
occurs when the communication
between
.Nm
and
.Xr timed 8
fails.
.Sh SEE ALSO
.Xr adjtime 2 ,
.Xr gettimeofday 2 ,
.Xr settimeofday 2 ,
.Xr parsedate 3 ,
.Xr strftime 3 ,
.Xr utmp 5 ,
.Xr environ 7 ,
.Xr timed 8
.Rs
.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD"
.%A R. Gusella
.%A S. Zatti
.Re
.Sh STANDARDS
The
.Nm
utility is expected to be compatible with
.St -p1003.2 .

View File

@@ -1,343 +0,0 @@
/* $NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg Exp $ */
/*
* Copyright (c) 1985, 1987, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1985, 1987, 1988, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
#include <util.h>
#include "extern.h"
static time_t tval;
static int aflag, jflag, rflag, nflag;
__dead static void badformat(void);
__dead static void badtime(void);
__dead static void badvalue(const char *);
static void setthetime(const char *);
__dead static void usage(void);
int
main(int argc, char *argv[])
{
char *buf;
size_t bufsiz;
const char *format;
int ch;
long long val;
struct tm *tm;
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) {
switch (ch) {
case 'a': /* adjust time slowly */
aflag = 1;
nflag = 1;
break;
case 'd':
#if !defined(__minix)
rflag = 1;
tval = parsedate(optarg, NULL, NULL);
if (tval == -1)
#endif /* !defined(__minix) */
badarg: errx(EXIT_FAILURE,
"Cannot parse `%s'", optarg);
break;
case 'j': /* don't set time */
jflag = 1;
break;
case 'n': /* don't set network */
nflag = 1;
break;
case 'r': /* user specified seconds */
errno = 0;
val = strtoll(optarg, &buf, 0);
if (optarg[0] == '\0' || *buf != '\0')
goto badarg;
if (errno == ERANGE && (val == LLONG_MAX ||
val == LLONG_MIN))
err(EXIT_FAILURE, "Bad number `%s'", optarg);
rflag = 1;
tval = (time_t)val;
break;
case 'u': /* do everything in UTC */
(void)setenv("TZ", "UTC0", 1);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (!rflag && time(&tval) == -1)
err(EXIT_FAILURE, "time");
/* allow the operands in any order */
if (*argv && **argv == '+') {
format = *argv;
++argv;
} else
format = "+%a %b %e %H:%M:%S %Z %Y";
if (*argv) {
setthetime(*argv);
++argv;
}
if (*argv && **argv == '+')
format = *argv;
if ((buf = malloc(bufsiz = 1024)) == NULL)
goto bad;
if ((tm = localtime(&tval)) == NULL)
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
while (strftime(buf, bufsiz, format, tm) == 0)
if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
goto bad;
(void)printf("%s\n", buf + 1);
free(buf);
return 0;
bad:
err(EXIT_FAILURE, "Cannot allocate format buffer");
}
static void
badformat(void)
{
warnx("illegal time format");
usage();
}
static void
badtime(void)
{
errx(EXIT_FAILURE, "illegal time");
/* NOTREACHED */
}
static void
badvalue(const char *param)
{
warnx("invalid %s supplied", param);
usage();
}
#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
static void
setthetime(const char *p)
{
struct timeval tv;
time_t new_time;
struct tm *lt;
const char *dot, *t;
size_t len;
int yearset;
for (t = p, dot = NULL; *t; ++t) {
if (isdigit((unsigned char)*t))
continue;
if (*t == '.' && dot == NULL) {
dot = t;
continue;
}
badformat();
}
if ((lt = localtime(&tval)) == NULL)
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
lt->tm_isdst = -1; /* Divine correct DST */
if (dot != NULL) { /* .ss */
len = strlen(dot);
if (len != 3)
badformat();
++dot;
lt->tm_sec = ATOI2(dot);
if (lt->tm_sec > 61)
badvalue("seconds");
} else {
len = 0;
lt->tm_sec = 0;
}
yearset = 0;
switch (strlen(p) - len) {
case 12: /* cc */
lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
if (lt->tm_year < 0)
badtime();
yearset = 1;
/* FALLTHROUGH */
case 10: /* yy */
if (yearset) {
lt->tm_year += ATOI2(p);
} else {
yearset = ATOI2(p);
if (yearset < 69)
lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
else
lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
}
/* FALLTHROUGH */
case 8: /* mm */
lt->tm_mon = ATOI2(p);
if (lt->tm_mon > 12 || lt->tm_mon == 0)
badvalue("month");
--lt->tm_mon; /* time struct is 0 - 11 */
/* FALLTHROUGH */
case 6: /* dd */
lt->tm_mday = ATOI2(p);
switch (lt->tm_mon) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
if (lt->tm_mday > 31 || lt->tm_mday == 0)
badvalue("day of month");
break;
case 3:
case 5:
case 8:
case 10:
if (lt->tm_mday > 30 || lt->tm_mday == 0)
badvalue("day of month");
break;
case 1:
if (lt->tm_mday > 29 || lt->tm_mday == 0 ||
(lt->tm_mday == 29 &&
!isleap(lt->tm_year + TM_YEAR_BASE)))
badvalue("day of month");
break;
default:
badvalue("month");
break;
}
/* FALLTHROUGH */
case 4: /* hh */
lt->tm_hour = ATOI2(p);
if (lt->tm_hour > 23)
badvalue("hour");
/* FALLTHROUGH */
case 2: /* mm */
lt->tm_min = ATOI2(p);
if (lt->tm_min > 59)
badvalue("minute");
break;
case 0: /* was just .sss */
if (len != 0)
break;
/* FALLTHROUGH */
default:
badformat();
}
/* convert broken-down time to UTC clock time */
if ((new_time = mktime(lt)) == -1)
badtime();
/* if jflag is set, don't actually change the time, just return */
if (jflag) {
tval = new_time;
return;
}
/* set the time */
if (nflag || netsettime(new_time)) {
logwtmp("|", "date", "");
if (aflag) {
tv.tv_sec = new_time - tval;
tv.tv_usec = 0;
if (adjtime(&tv, NULL))
err(EXIT_FAILURE, "adjtime");
} else {
tval = new_time;
tv.tv_sec = tval;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL))
err(EXIT_FAILURE, "settimeofday");
}
logwtmp("{", "date", "");
}
if ((p = getlogin()) == NULL)
p = "???";
syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
}
static void
usage(void)
{
(void)fprintf(stderr,
"Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
getprogname());
(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}

View File

@@ -1,39 +0,0 @@
/* $NetBSD: extern.h,v 1.8 2006/11/17 22:11:28 christos Exp $ */
/*-
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)extern.h 8.1 (Berkeley) 5/31/93
*/
#ifndef _EXTERN_H_
#define _EXTERN_H_
int netsettime(time_t);
#endif /* !_EXTERN_H_ */

View File

@@ -1,200 +0,0 @@
/* $NetBSD: netdate.c,v 1.30 2011/01/29 02:16:52 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: netdate.c,v 1.30 2011/01/29 02:16:52 christos Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define TSPTYPES
#include <protocols/timed.h>
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#define WAITACK 2000 /* milliseconds */
#define WAITDATEACK 5000 /* milliseconds */
static const char *
tsp_type_to_string(const struct tsp *msg)
{
unsigned i;
i = msg->tsp_type;
return i < TSPTYPENUMBER ? tsptype[i] : "unknown";
}
/*
* Set the date in the machines controlled by timedaemons by communicating the
* new date to the local timedaemon. If the timedaemon is in the master state,
* it performs the correction on all slaves. If it is in the slave state, it
* notifies the master that a correction is needed.
* Returns 0 on success. Returns > 0 on failure.
*/
int
netsettime(time_t tval)
{
struct sockaddr_in dest;
struct tsp msg;
char hostname[MAXHOSTNAMELEN];
struct servent *sp;
struct pollfd ready;
int found, s, timed_ack, waittime;
if ((sp = getservbyname("timed", "udp")) == NULL) {
warnx("udp/timed: unknown service");
return 2;
}
(void)memset(&dest, 0, sizeof(dest));
#ifdef BSD4_4
dest.sin_len = sizeof(dest);
#endif
dest.sin_family = AF_INET;
dest.sin_port = sp->s_port;
dest.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
if (errno != EAFNOSUPPORT)
warn("timed");
return 2;
}
#ifdef IP_PORTRANGE
{
static const int on = IP_PORTRANGE_LOW;
if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on,
sizeof(on)) == -1) {
warn("setsockopt");
goto bad;
}
}
#endif
msg.tsp_type = TSP_SETDATE;
msg.tsp_vers = TSPVERSION;
if (gethostname(hostname, sizeof(hostname)) == -1) {
warn("gethostname");
goto bad;
}
(void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
msg.tsp_seq = htons((in_port_t)0);
msg.tsp_time.tv_sec = htonl((in_addr_t)tval); /* XXX: y2038 */
msg.tsp_time.tv_usec = htonl((in_addr_t)0);
if (connect(s, (const void *)&dest, sizeof(dest)) == -1) {
warn("connect");
goto bad;
}
if (send(s, &msg, sizeof(msg), 0) == -1) {
if (errno != ECONNREFUSED)
warn("send");
goto bad;
}
timed_ack = -1;
waittime = WAITACK;
ready.fd = s;
ready.events = POLLIN;
loop:
found = poll(&ready, 1, waittime);
{
socklen_t len;
int error;
len = sizeof(error);
if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) == -1) {
warn("getsockopt");
goto bad;
}
if (error) {
if (error != ECONNREFUSED) {
errno = error;
warn("send (delayed error)");
}
goto bad;
}
}
if (found > 0 && ready.revents & POLLIN) {
ssize_t ret;
if ((ret = recv(s, &msg, sizeof(msg), 0)) == -1) {
if (errno != ECONNREFUSED)
warn("recv");
goto bad;
} else if ((size_t)ret < sizeof(msg)) {
warnx("recv: incomplete packet");
goto bad;
}
msg.tsp_seq = ntohs(msg.tsp_seq);
msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
switch (msg.tsp_type) {
case TSP_ACK:
timed_ack = TSP_ACK;
waittime = WAITDATEACK;
goto loop;
case TSP_DATEACK:
(void)close(s);
return 0;
default:
warnx("wrong ack received from timed: %s",
tsp_type_to_string(&msg));
timed_ack = -1;
break;
}
}
if (timed_ack == -1)
warnx("can't reach time daemon, time set locally");
bad:
(void)close(s);
return 2;
}

View File

@@ -1,21 +0,0 @@
# $NetBSD: Makefile,v 1.17 2012/08/08 14:09:14 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
.include <bsd.own.mk>
RUMPPRG=dd
SRCS= args.c conv.c dd.c misc.c position.c
DPADD+= ${LIBUTIL}
LDADD+= -lutil
.ifdef SMALLPROG
CPPFLAGS+= -DNO_CONV -DNO_MSGFMT -DSMALL
.else
SRCS+= conv_tab.c
.ifdef CRUNCHEDPROG
CPPFLAGS+= -DSMALL
.endif
.endif
.include <bsd.prog.mk>

View File

@@ -1,391 +0,0 @@
/* $NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dd.h"
#include "extern.h"
static int c_arg(const void *, const void *);
#ifdef NO_MSGFMT
static void f_msgfmt(char *) __dead;
#else
static void f_msgfmt(char *);
#endif /* NO_MSGFMT */
#ifdef NO_CONV
static void f_conv(char *) __dead;
#else
static void f_conv(char *);
static int c_conv(const void *, const void *);
#endif /* NO_CONV */
static void f_bs(char *);
static void f_cbs(char *);
static void f_count(char *);
static void f_files(char *);
static void f_ibs(char *);
static void f_if(char *);
static void f_obs(char *);
static void f_of(char *);
static void f_seek(char *);
static void f_skip(char *);
static void f_progress(char *);
static const struct arg {
const char *name;
void (*f)(char *);
u_int set, noset;
} args[] = {
/* the array needs to be sorted by the first column so
bsearch() can be used to find commands quickly */
{ "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
{ "cbs", f_cbs, C_CBS, C_CBS },
{ "conv", f_conv, 0, 0 },
{ "count", f_count, C_COUNT, C_COUNT },
{ "files", f_files, C_FILES, C_FILES },
{ "ibs", f_ibs, C_IBS, C_BS|C_IBS },
{ "if", f_if, C_IF, C_IF },
{ "iseek", f_skip, C_SKIP, C_SKIP },
{ "msgfmt", f_msgfmt, 0, 0 },
{ "obs", f_obs, C_OBS, C_BS|C_OBS },
{ "of", f_of, C_OF, C_OF },
{ "oseek", f_seek, C_SEEK, C_SEEK },
{ "progress", f_progress, 0, 0 },
{ "seek", f_seek, C_SEEK, C_SEEK },
{ "skip", f_skip, C_SKIP, C_SKIP },
};
/*
* args -- parse JCL syntax of dd.
*/
void
jcl(char **argv)
{
struct arg *ap, tmp;
char *oper, *arg;
in.dbsz = out.dbsz = 512;
while ((oper = *++argv) != NULL) {
if ((oper = strdup(oper)) == NULL) {
errx(EXIT_FAILURE,
"unable to allocate space for the argument %s",
*argv);
/* NOTREACHED */
}
if ((arg = strchr(oper, '=')) == NULL) {
errx(EXIT_FAILURE, "unknown operand %s", oper);
/* NOTREACHED */
}
*arg++ = '\0';
if (!*arg) {
errx(EXIT_FAILURE, "no value specified for %s", oper);
/* NOTREACHED */
}
tmp.name = oper;
if (!(ap = bsearch(&tmp, args,
__arraycount(args), sizeof(*args), c_arg))) {
errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
/* NOTREACHED */
}
if (ddflags & ap->noset) {
errx(EXIT_FAILURE,
"%s: illegal argument combination or already set",
tmp.name);
/* NOTREACHED */
}
ddflags |= ap->set;
ap->f(arg);
}
/* Final sanity checks. */
if (ddflags & C_BS) {
/*
* Bs is turned off by any conversion -- we assume the user
* just wanted to set both the input and output block sizes
* and didn't want the bs semantics, so we don't warn.
*/
if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
ddflags &= ~C_BS;
ddflags |= C_IBS|C_OBS;
}
/* Bs supersedes ibs and obs. */
if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
warnx("bs supersedes ibs and obs");
}
/*
* Ascii/ebcdic and cbs implies block/unblock.
* Block/unblock requires cbs and vice-versa.
*/
if (ddflags & (C_BLOCK|C_UNBLOCK)) {
if (!(ddflags & C_CBS)) {
errx(EXIT_FAILURE, "record operations require cbs");
/* NOTREACHED */
}
cfunc = ddflags & C_BLOCK ? block : unblock;
} else if (ddflags & C_CBS) {
if (ddflags & (C_ASCII|C_EBCDIC)) {
if (ddflags & C_ASCII) {
ddflags |= C_UNBLOCK;
cfunc = unblock;
} else {
ddflags |= C_BLOCK;
cfunc = block;
}
} else {
errx(EXIT_FAILURE,
"cbs meaningless if not doing record operations");
/* NOTREACHED */
}
} else
cfunc = def;
/* Read, write and seek calls take off_t as arguments.
*
* The following check is not done because an off_t is a quad
* for current NetBSD implementations.
*
* if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
* errx(1, "seek offsets cannot be larger than %d", INT_MAX);
*/
}
static int
c_arg(const void *a, const void *b)
{
return (strcmp(((const struct arg *)a)->name,
((const struct arg *)b)->name));
}
static void
f_bs(char *arg)
{
in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
}
static void
f_cbs(char *arg)
{
cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
}
static void
f_count(char *arg)
{
cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
if (!cpy_cnt)
terminate(0);
}
static void
f_files(char *arg)
{
files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
if (!files_cnt)
terminate(0);
}
static void
f_ibs(char *arg)
{
if (!(ddflags & C_BS))
in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
}
static void
f_if(char *arg)
{
in.name = arg;
}
#ifdef NO_MSGFMT
/* Build a small version (i.e. for a ramdisk root) */
static void
f_msgfmt(char *arg)
{
errx(EXIT_FAILURE, "msgfmt option disabled");
/* NOTREACHED */
}
#else /* NO_MSGFMT */
static void
f_msgfmt(char *arg)
{
/*
* If the format string is not valid, dd_write_msg() will print
* an error and exit.
*/
dd_write_msg(arg, 0);
msgfmt = arg;
}
#endif /* NO_MSGFMT */
static void
f_obs(char *arg)
{
if (!(ddflags & C_BS))
out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
}
static void
f_of(char *arg)
{
out.name = arg;
}
static void
f_seek(char *arg)
{
out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
}
static void
f_skip(char *arg)
{
in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
}
static void
f_progress(char *arg)
{
progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
}
#ifdef NO_CONV
/* Build a small version (i.e. for a ramdisk root) */
static void
f_conv(char *arg)
{
errx(EXIT_FAILURE, "conv option disabled");
/* NOTREACHED */
}
#else /* NO_CONV */
static const struct conv {
const char *name;
u_int set, noset;
const u_char *ctab;
} clist[] = {
{ "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
{ "block", C_BLOCK, C_UNBLOCK, NULL },
{ "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
{ "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
{ "lcase", C_LCASE, C_UCASE, NULL },
{ "noerror", C_NOERROR, 0, NULL },
{ "notrunc", C_NOTRUNC, 0, NULL },
{ "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
{ "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
{ "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
{ "osync", C_OSYNC, C_BS, NULL },
{ "sparse", C_SPARSE, 0, NULL },
{ "swab", C_SWAB, 0, NULL },
{ "sync", C_SYNC, 0, NULL },
{ "ucase", C_UCASE, C_LCASE, NULL },
{ "unblock", C_UNBLOCK, C_BLOCK, NULL },
/* If you add items to this table, be sure to add the
* conversions to the C_BS check in the jcl routine above.
*/
};
static void
f_conv(char *arg)
{
struct conv *cp, tmp;
while (arg != NULL) {
tmp.name = strsep(&arg, ",");
if (!(cp = bsearch(&tmp, clist,
__arraycount(clist), sizeof(*clist), c_conv))) {
errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
/* NOTREACHED */
}
if (ddflags & cp->noset) {
errx(EXIT_FAILURE,
"%s: illegal conversion combination", tmp.name);
/* NOTREACHED */
}
ddflags |= cp->set;
if (cp->ctab)
ctab = cp->ctab;
}
}
static int
c_conv(const void *a, const void *b)
{
return (strcmp(((const struct conv *)a)->name,
((const struct conv *)b)->name));
}
#endif /* NO_CONV */

View File

@@ -1,283 +0,0 @@
/* $NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <err.h>
#include <string.h>
#include <stdlib.h>
#include "dd.h"
#include "extern.h"
/*
* def --
* Copy input to output. Input is buffered until reaches obs, and then
* output until less than obs remains. Only a single buffer is used.
* Worst case buffer calculation is (ibs + obs - 1).
*/
void
def(void)
{
uint64_t cnt;
u_char *inp;
const u_char *t;
if ((t = ctab) != NULL)
for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
*inp = t[*inp];
/* Make the output buffer look right. */
out.dbp = in.dbp;
out.dbcnt = in.dbcnt;
if (in.dbcnt >= out.dbsz) {
/* If the output buffer is full, write it. */
dd_out(0);
/*
* Ddout copies the leftover output to the beginning of
* the buffer and resets the output buffer. Reset the
* input buffer to match it.
*/
in.dbp = out.dbp;
in.dbcnt = out.dbcnt;
}
}
void
def_close(void)
{
/* Just update the count, everything is already in the buffer. */
if (in.dbcnt)
out.dbcnt = in.dbcnt;
}
#ifdef NO_CONV
/* Build a smaller version (i.e. for a miniroot) */
/* These can not be called, but just in case... */
static const char no_block[] = "unblock and -DNO_CONV?";
void block(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
void block_close(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
void unblock(void) { errx(EXIT_FAILURE, "%s", no_block); }
void unblock_close(void) { errx(EXIT_FAILURE, "%s", no_block); }
#else /* NO_CONV */
/*
* Copy variable length newline terminated records with a max size cbsz
* bytes to output. Records less than cbs are padded with spaces.
*
* max in buffer: MAX(ibs, cbsz)
* max out buffer: obs + cbsz
*/
void
block(void)
{
static int intrunc;
int ch = 0; /* pacify gcc */
uint64_t cnt, maxlen;
u_char *inp, *outp;
const u_char *t;
/*
* Record truncation can cross block boundaries. If currently in a
* truncation state, keep tossing characters until reach a newline.
* Start at the beginning of the buffer, as the input buffer is always
* left empty.
*/
if (intrunc) {
for (inp = in.db, cnt = in.dbrcnt;
cnt && *inp++ != '\n'; --cnt);
if (!cnt) {
in.dbcnt = 0;
in.dbp = in.db;
return;
}
intrunc = 0;
/* Adjust the input buffer numbers. */
in.dbcnt = cnt - 1;
in.dbp = inp + cnt - 1;
}
/*
* Copy records (max cbsz size chunks) into the output buffer. The
* translation is done as we copy into the output buffer.
*/
for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
maxlen = MIN(cbsz, in.dbcnt);
if ((t = ctab) != NULL)
for (cnt = 0;
cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
*outp++ = t[ch];
else
for (cnt = 0;
cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
*outp++ = ch;
/*
* Check for short record without a newline. Reassemble the
* input block.
*/
if (ch != '\n' && in.dbcnt < cbsz) {
(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
break;
}
/* Adjust the input buffer numbers. */
in.dbcnt -= cnt;
if (ch == '\n')
--in.dbcnt;
/* Pad short records with spaces. */
if (cnt < cbsz)
(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
else {
/*
* If the next character wouldn't have ended the
* block, it's a truncation.
*/
if (!in.dbcnt || *inp != '\n')
++st.trunc;
/* Toss characters to a newline. */
for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
if (!in.dbcnt)
intrunc = 1;
else
--in.dbcnt;
}
/* Adjust output buffer numbers. */
out.dbp += cbsz;
if ((out.dbcnt += cbsz) >= out.dbsz)
dd_out(0);
outp = out.dbp;
}
in.dbp = in.db + in.dbcnt;
}
void
block_close(void)
{
/*
* Copy any remaining data into the output buffer and pad to a record.
* Don't worry about truncation or translation, the input buffer is
* always empty when truncating, and no characters have been added for
* translation. The bottom line is that anything left in the input
* buffer is a truncated record. Anything left in the output buffer
* just wasn't big enough.
*/
if (in.dbcnt) {
++st.trunc;
(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
(void)memset(out.dbp + in.dbcnt,
ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
out.dbcnt += cbsz;
}
}
/*
* Convert fixed length (cbsz) records to variable length. Deletes any
* trailing blanks and appends a newline.
*
* max in buffer: MAX(ibs, cbsz) + cbsz
* max out buffer: obs + cbsz
*/
void
unblock(void)
{
uint64_t cnt;
u_char *inp;
const u_char *t;
/* Translation and case conversion. */
if ((t = ctab) != NULL)
for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
*inp = t[*inp];
/*
* Copy records (max cbsz size chunks) into the output buffer. The
* translation has to already be done or we might not recognize the
* spaces.
*/
for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
if (t >= inp) {
cnt = t - inp + 1;
(void)memmove(out.dbp, inp, cnt);
out.dbp += cnt;
out.dbcnt += cnt;
}
++out.dbcnt;
*out.dbp++ = '\n';
if (out.dbcnt >= out.dbsz)
dd_out(0);
}
if (in.dbcnt)
(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
in.dbp = in.db + in.dbcnt;
}
void
unblock_close(void)
{
uint64_t cnt;
u_char *t;
if (in.dbcnt) {
warnx("%s: short input record", in.name);
for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
if (t >= in.db) {
cnt = t - in.db + 1;
(void)memmove(out.dbp, in.db, cnt);
out.dbp += cnt;
out.dbcnt += cnt;
}
++out.dbcnt;
*out.dbp++ = '\n';
}
}
#endif /* NO_CONV */

View File

@@ -1,287 +0,0 @@
/* $NetBSD: conv_tab.c,v 1.9 2003/08/07 09:05:10 agc Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego and Lance
* Visser of Convex Computer Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)conv_tab.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: conv_tab.c,v 1.9 2003/08/07 09:05:10 agc Exp $");
#endif
#endif /* not lint */
#include <sys/types.h>
/*
* There are currently six tables:
*
* ebcdic -> ascii 32V conv=oldascii
* ascii -> ebcdic 32V conv=oldebcdic
* ascii -> ibm ebcdic 32V conv=oldibm
*
* ebcdic -> ascii POSIX/S5 conv=ascii
* ascii -> ebcdic POSIX/S5 conv=ebcdic
* ascii -> ibm ebcdic POSIX/S5 conv=ibm
*
* Other tables are built from these if multiple conversions are being
* done.
*
* Tables used for conversions to/from IBM and EBCDIC to support an extension
* to POSIX P1003.2/D11. The tables referencing POSIX contain data extracted
* from tables 4-3 and 4-4 in P1003.2/Draft 11. The historic tables were
* constructed by running against a file with all possible byte values.
*
* More information can be obtained in "Correspondences of 8-Bit and Hollerith
* Codes for Computer Environments-A USASI Tutorial", Communications of the
* ACM, Volume 11, Number 11, November 1968, pp. 783-789.
*/
u_char casetab[256];
/* EBCDIC to ASCII -- 32V compatible. */
const u_char e2a_32V[] = {
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177, /* 0000 */
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207, /* 0020 */
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037, /* 0030 */
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033, /* 0040 */
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007, /* 0050 */
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004, /* 0060 */
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032, /* 0070 */
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, /* 0100 */
0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041, /* 0110 */
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0120 */
0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136, /* 0130 */
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, /* 0140 */
0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077, /* 0150 */
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, /* 0160 */
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, /* 0170 */
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0200 */
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, /* 0210 */
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, /* 0220 */
0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320, /* 0230 */
0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170, /* 0240 */
0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327, /* 0250 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0260 */
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, /* 0270 */
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0300 */
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, /* 0310 */
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, /* 0320 */
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, /* 0330 */
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, /* 0340 */
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, /* 0350 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0360 */
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to EBCDIC -- 32V compatible. */
const u_char a2e_32V[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to IBM EBCDIC -- 32V compatible. */
const u_char a2ibm_32V[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* EBCDIC to ASCII -- POSIX and System V compatible. */
const u_char e2a_POSIX[] = {
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177, /* 0000 */
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207, /* 0020 */
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037, /* 0030 */
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033, /* 0040 */
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007, /* 0050 */
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004, /* 0060 */
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032, /* 0070 */
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246, /* 0100 */
0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174, /* 0110 */
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257, /* 0120 */
0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176, /* 0130 */
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267, /* 0140 */
0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077, /* 0150 */
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301, /* 0160 */
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042, /* 0170 */
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147, /* 0200 */
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311, /* 0210 */
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160, /* 0220 */
0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320, /* 0230 */
0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170, /* 0240 */
0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327, /* 0250 */
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, /* 0260 */
0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347, /* 0270 */
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107, /* 0300 */
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355, /* 0310 */
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120, /* 0320 */
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363, /* 0330 */
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130, /* 0340 */
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371, /* 0350 */
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, /* 0360 */
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to EBCDIC -- POSIX and System V compatible. */
const u_char a2e_POSIX[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};
/* ASCII to IBM EBCDIC -- POSIX and System V compatible. */
const u_char a2ibm_POSIX[] = {
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057, /* 0000 */
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017, /* 0010 */
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046, /* 0020 */
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037, /* 0030 */
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175, /* 0040 */
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141, /* 0050 */
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, /* 0060 */
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157, /* 0070 */
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307, /* 0100 */
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326, /* 0110 */
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346, /* 0120 */
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155, /* 0130 */
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207, /* 0140 */
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226, /* 0150 */
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246, /* 0160 */
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007, /* 0170 */
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027, /* 0200 */
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033, /* 0210 */
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010, /* 0220 */
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341, /* 0230 */
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, /* 0240 */
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127, /* 0250 */
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147, /* 0260 */
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165, /* 0270 */
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215, /* 0300 */
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236, /* 0310 */
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257, /* 0320 */
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, /* 0330 */
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, /* 0340 */
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333, /* 0350 */
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355, /* 0360 */
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377, /* 0370 */
};

View File

@@ -1,477 +0,0 @@
.\" $NetBSD: dd.1,v 1.25 2012/06/20 17:54:16 wiz Exp $
.\"
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Keith Muller of the University of California, San Diego.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)dd.1 8.2 (Berkeley) 1/13/94
.\"
.Dd November 6, 2011
.Dt DD 1
.Os
.Sh NAME
.Nm dd
.Nd convert and copy a file
.Sh SYNOPSIS
.Nm
.Op operand ...
.Sh DESCRIPTION
The
.Nm
utility copies the standard input to the standard output.
Input data is read and written in 512-byte blocks.
If input reads are short, input from multiple reads are aggregated
to form the output block.
When finished,
.Nm
displays the number of complete and partial input and output blocks
and truncated input records to the standard error output.
.Pp
The following operands are available:
.Bl -tag -width of=file
.It Cm bs= Ns Ar n
Set both input and output block size, superseding the
.Cm ibs
and
.Cm obs
operands.
If no conversion values other than
.Cm noerror ,
.Cm notrunc
or
.Cm sync
are specified, then each input block is copied to the output as a
single block without any aggregation of short blocks.
.It Cm cbs= Ns Ar n
Set the conversion record size to
.Va n
bytes.
The conversion record size is required by the record oriented conversion
values.
.It Cm count= Ns Ar n
Copy only
.Va n
input blocks.
.It Cm files= Ns Ar n
Copy
.Va n
input files before terminating.
This operand is only applicable when the input device is a tape.
.It Cm ibs= Ns Ar n
Set the input block size to
.Va n
bytes instead of the default 512.
.It Cm if= Ns Ar file
Read input from
.Ar file
instead of the standard input.
.It Cm iseek= Ns Ar n
Seek on the input file
.Ar n
blocks.
This is synonymous with
.Cm skip= Ns Ar n .
.It Cm msgfmt= Ns Ar fmt
Specify the message format
.Ar fmt
to be used when writing information to standard output.
Possible values are:
.Bl -tag -width xxxxx -offset indent -compact
.It quiet
turns off information summary report except for errors and
.Cm progress .
.It posix
default information summary report as specified by POSIX.
.It human
default information summary report extended with human-readable
values.
.El
.Pp
When
.Ar fmt
does not correspond to any value given above,
it contains a string that will be used as format specifier
for the information summary output.
Each conversion specification is introduced by the character
.Cm % .
The following ones are available:
.Bl -tag -width xx -offset indent -compact
.It b
total number of bytes transferred
.It B
total number of bytes transferred in
.Xr humanize_number 3
format
.It e
speed transfer
.It E
speed transfer in
.Xr humanize_number 3
format
.It i
number of partial input block(s)
.It I
number of full input block(s)
.It o
number of partial output block(s)
.It O
number of full output block(s)
.It s
time elapsed since the beginning in
.Do seconds.ms Dc
format
.It p
number of sparse output blocks
.It t
number of truncated blocks
.It w
number of odd-length swab blocks
.It P
singular/plural of
.Do block Dc
depending on number of sparse blocks
.It T
singular/plural of
.Do block Dc
depending on number of truncated blocks
.It W
singular/plural of
.Do block Dc
depending on number of swab blocks
.El
.It Cm obs= Ns Ar n
Set the output block size to
.Va n
bytes instead of the default 512.
.It Cm of= Ns Ar file
Write output to
.Ar file
instead of the standard output.
Any regular output file is truncated unless the
.Cm notrunc
conversion value is specified.
If an initial portion of the output file is skipped (see the
.Cm seek
operand)
the output file is truncated at that point.
.It Cm oseek= Ns Ar n
Seek on the output file
.Ar n
blocks.
This is synonymous with
.Cm seek= Ns Ar n .
.It Cm seek= Ns Ar n
Seek
.Va n
blocks from the beginning of the output before copying.
On non-tape devices, an
.Xr lseek 2
operation is used.
Otherwise, existing blocks are read and the data discarded.
If the user does not have read permission for the tape, it is positioned
using the tape
.Xr ioctl 2
function calls.
If the seek operation is past the end of file, space from the current
end of file to the specified offset is filled with blocks of
.Tn NUL
bytes.
.It Cm skip= Ns Ar n
Skip
.Va n
blocks from the beginning of the input before copying.
On input which supports seeks, an
.Xr lseek 2
operation is used.
Otherwise, input data is read and discarded.
For pipes, the correct number of bytes is read.
For all other devices, the correct number of blocks is read without
distinguishing between a partial or complete block being read.
.It Cm progress= Ns Ar n
Switch on display of progress if
.Va n
is set to any non-zero value.
This will cause a
.Dq \&.
to be printed (to the standard error output) for every
.Va n
full or partial blocks written to the output file.
.Sm off
.It Cm conv= Cm value Op \&, Cm value \&...
.Sm on
Where
.Cm value
is one of the symbols from the following list.
.Bl -tag -width unblock
.It Cm ascii , oldascii
The same as the
.Cm unblock
value except that characters are translated from
.Tn EBCDIC
to
.Tn ASCII
before the
records are converted.
(These values imply
.Cm unblock
if the operand
.Cm cbs
is also specified.)
There are two conversion maps for
.Tn ASCII .
The value
.Cm ascii
specifies the recommended one which is compatible with
.At V .
The value
.Cm oldascii
specifies the one used in historic
.Tn AT\*[Am]T
and pre-
.Bx 4.3 Reno
systems.
.It Cm block
Treats the input as a sequence of newline or end-of-file terminated variable
length records independent of input and output block boundaries.
Any trailing newline character is discarded.
Each input record is converted to a fixed length output record where the
length is specified by the
.Cm cbs
operand.
Input records shorter than the conversion record size are padded with spaces.
Input records longer than the conversion record size are truncated.
The number of truncated input records, if any, are reported to the standard
error output at the completion of the copy.
.It Cm ebcdic , ibm , oldebcdic , oldibm
The same as the
.Cm block
value except that characters are translated from
.Tn ASCII
to
.Tn EBCDIC
after the
records are converted.
(These values imply
.Cm block
if the operand
.Cm cbs
is also specified.)
There are four conversion maps for
.Tn EBCDIC .
The value
.Cm ebcdic
specifies the recommended one which is compatible with
.At V .
The value
.Cm ibm
is a slightly different mapping, which is compatible with the
.At V
.Cm ibm
value.
The values
.Cm oldebcdic
and
.Cm oldibm
are maps used in historic
.Tn AT\*[Am]T
and pre
.Bx 4.3 Reno
systems.
.It Cm lcase
Transform uppercase characters into lowercase characters.
.It Cm noerror
Do not stop processing on an input error.
When an input error occurs, a diagnostic message followed by the current
input and output block counts will be written to the standard error output
in the same format as the standard completion message.
If the
.Cm sync
conversion is also specified, any missing input data will be replaced
with
.Tn NUL
bytes (or with spaces if a block oriented conversion value was
specified) and processed as a normal input buffer.
If the
.Cm sync
conversion is not specified, the input block is omitted from the output.
On input files which are not tapes or pipes, the file offset
will be positioned past the block in which the error occurred using
.Xr lseek 2 .
.It Cm notrunc
Do not truncate the output file.
This will preserve any blocks in the output file not explicitly written
by
.Nm .
The
.Cm notrunc
value is not supported for tapes.
.It Cm osync
Pad the final output block to the full output block size.
If the input file is not a multiple of the output block size
after conversion, this conversion forces the final output block
to be the same size as preceding blocks for use on devices that require
regularly sized blocks to be written.
This option is incompatible with use of the
.Cm bs= Ns Ar n
block size specification.
.It Cm sparse
If one or more non-final output blocks would consist solely of
.Dv NUL
bytes, try to seek the output file by the required space instead of
filling them with
.Dv NUL Ns s .
This results in a sparse file on some file systems.
.It Cm swab
Swap every pair of input bytes.
If an input buffer has an odd number of bytes, the last byte will be
ignored during swapping.
.It Cm sync
Pad every input block to the input buffer size.
Spaces are used for pad bytes if a block oriented conversion value is
specified, otherwise
.Tn NUL
bytes are used.
.It Cm ucase
Transform lowercase characters into uppercase characters.
.It Cm unblock
Treats the input as a sequence of fixed length records independent of input
and output block boundaries.
The length of the input records is specified by the
.Cm cbs
operand.
Any trailing space characters are discarded and a newline character is
appended.
.El
.El
.Pp
Where sizes are specified, a decimal number of bytes is expected.
Two or more numbers may be separated by an
.Dq x
to indicate a product.
Each number may have one of the following optional suffixes:
.Bl -tag -width 3n -offset indent -compact
.It b
Block; multiply by 512
.It k
Kibi; multiply by 1024 (1 KiB)
.It m
Mebi; multiply by 1048576 (1 MiB)
.It g
Gibi; multiply by 1073741824 (1 GiB)
.It t
Tebi; multiply by 1099511627776 (1 TiB)
.It w
Word; multiply by the number of bytes in an integer
.El
.Pp
When finished,
.Nm
displays the number of complete and partial input and output blocks,
truncated input records and odd-length byte-swapping blocks to the
standard error output.
A partial input block is one where less than the input block size
was read.
A partial output block is one where less than the output block size
was written.
Partial output blocks to tape devices are considered fatal errors.
Otherwise, the rest of the block will be written.
Partial output blocks to character devices will produce a warning message.
A truncated input block is one where a variable length record oriented
conversion value was specified and the input line was too long to
fit in the conversion record or was not newline terminated.
.Pp
Normally, data resulting from input or conversion or both are aggregated
into output blocks of the specified size.
After the end of input is reached, any remaining output is written as
a block.
This means that the final output block may be shorter than the output
block size.
.Pp
If
.Nm
receives a
.Dv SIGINFO
signal
(see the
.Ic status
argument for
.Xr stty 1 ) ,
the current input and output block counts will
be written to the standard error output
in the same format as the standard completion message.
If
.Nm
receives a
.Dv SIGINT
signal, the current input and output block counts will
be written to the standard error output
in the same format as the standard completion message and
.Nm
will exit.
.Sh EXIT STATUS
The
.Nm
utility exits 0 on success and \*[Gt]0 if an error occurred.
.Sh EXAMPLES
To print summary information in human-readable form:
.Pp
.Dl dd if=/dev/zero of=/dev/null count=1 msgfmt=human
.Pp
To customize the information summary output and print it through
.Xr unvis 3 :
.Pp
.Bd -literal -offset indent
dd if=/dev/zero of=/dev/null count=1 \e
msgfmt='speed:%E, in %s seconds\en' 2\*[Gt]\*[Am]1 | unvis
.Ed
.Sh SEE ALSO
.Xr cp 1 ,
.Xr mt 1 ,
.Xr tr 1
.Sh STANDARDS
The
.Nm
utility is expected to be a superset of the
.St -p1003.2
standard.
The
.Cm files
and
.Cm msgfmt
operands and the
.Cm ascii ,
.Cm ebcdic ,
.Cm ibm ,
.Cm oldascii ,
.Cm oldebcdic
and
.Cm oldibm
values are extensions to the
.Tn POSIX
standard.

Some files were not shown because too many files have changed in this diff Show More