summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>1998-04-17 19:52:44 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:51 +0000
commitcce855bc5b117cb7ae70064131120687bc69fac0 (patch)
tree39c7a4ec8f6d22ef03df74f2684e6a04fef10399 /examples
parente8ce775db824de329b81293b4e5d8fbd65624528 (diff)
downloadbash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.gz
Imported from ../bash-2.02.tar.gz.
Diffstat (limited to 'examples')
-rw-r--r--examples/functions/basename243
-rw-r--r--examples/functions/getoptx.bash302
-rw-r--r--examples/functions/inetaddr44
-rw-r--r--examples/functions/inpath5
-rw-r--r--examples/functions/isnum.bash23
-rw-r--r--examples/functions/isnum222
-rw-r--r--examples/functions/jdate.bash78
-rw-r--r--examples/functions/pathfuncs48
-rw-r--r--examples/functions/shcat219
-rw-r--r--examples/loadables/Makefile.in (renamed from examples/loadables/Makefile)91
-rw-r--r--examples/loadables/getconf.c402
-rw-r--r--examples/loadables/print.c416
-rw-r--r--examples/loadables/printf.c460
-rwxr-xr-xexamples/misc/aliasconv.bash (renamed from examples/misc/alias-conv.bash)0
-rwxr-xr-xexamples/misc/aliasconv.sh (renamed from examples/misc/alias-conv.sh)0
-rw-r--r--examples/scripts.v2/cal2day.bash49
-rw-r--r--examples/scripts/fixfiles.bash92
-rw-r--r--examples/scripts/hanoi.bash21
-rwxr-xr-xexamples/scripts/krand.bash74
-rw-r--r--examples/scripts/randomcard.bash18
-rwxr-xr-xexamples/scripts/scrollbar10
-rwxr-xr-xexamples/scripts/scrollbar224
-rw-r--r--examples/scripts/showperm.bash53
-rw-r--r--examples/scripts/timeout53
-rw-r--r--examples/startup-files/Bashrc.bfox (renamed from examples/startup-files/Bashrc)0
25 files changed, 1391 insertions, 956 deletions
diff --git a/examples/functions/basename2 b/examples/functions/basename2
new file mode 100644
index 00000000..a42231cf
--- /dev/null
+++ b/examples/functions/basename2
@@ -0,0 +1,43 @@
+#From: "Grigoriy Strokin" <grg@philol.msu.ru>
+#Newsgroups: comp.unix.shell
+#Subject: fast basename and dirname functions for BASH/SH
+#Date: Sat, 27 Dec 1997 21:18:40 +0300
+#
+#Please send your comments to grg@philol.msu.ru
+
+function basename()
+{
+ local name="${1##*/}"
+ echo "${name%$2}"
+}
+
+function dirname()
+{
+ local dir="${1%${1##*/}}"
+ [ "${dir:=./}" != "/" ] && dir="${dir%?}"
+ echo "$dir"
+}
+
+# Two additional functions:
+# 1) namename prints the basename without extension
+# 2) ext prints extension of a file, including "."
+
+function namename()
+{
+ local name=${1##*/}
+ local name0="${name%.*}"
+ echo "${name0:-$name}"
+}
+function ext()
+{
+ local name=${1##*/}
+ local name0="${name%.*}"
+ local ext=${name0:+${name#$name0}}
+ echo "${ext:-.}"
+}
+
+
+
+
+
+
diff --git a/examples/functions/getoptx.bash b/examples/functions/getoptx.bash
new file mode 100644
index 00000000..23e5de52
--- /dev/null
+++ b/examples/functions/getoptx.bash
@@ -0,0 +1,302 @@
+#From: "Grigoriy Strokin" <grg@philol.msu.ru>
+#Newsgroups: comp.unix.shell
+#Subject: BASH: getopt function that parses long-named options
+#Date: Mon, 22 Dec 1997 20:35:18 +0300
+
+#Hi, I have written a BASH function named getoptex, that is like bash builtin
+#"getopts", but does parse long-named options and optional arguments. It only
+#uses builtin bash commands, so it is very fast. In order to use it in your
+#bash scripts, include a command ". getopt.sh" (<dot> getopt.sh) to the file
+#containing your script, and that will define functions getopt, getoptex, and
+#optlistex (the file getopt.sh with its detailed description is listed
+#below).
+
+#*** file getopt.sh ***
+
+#! /bin/bash
+#
+# getopt.sh:
+# functions like getopts but do long-named options parsing
+# and support optional arguments
+#
+# Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain
+# Date created: December 21, 1997
+# Date modified: December 21, 1997
+#
+# IMPORTANT FEATURES
+#
+# 1) Parses both short and long-named options
+# 2) Supports optional arguments
+# 3) Only uses bash builtins, thus no calls to external
+# utilities such as expr or sed is done. Therefore,
+# parsing speed is high enough
+#
+#
+# DESCRIPTION
+#
+# FUNCTION getopt
+# Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse options with both required and optional arguments,
+# Options with optional arguments must have "." instead of ":" after them.
+# Furthemore, a variable name to place option name cannot be specified
+# and is always placed in OPTOPT variable
+#
+# This function is provided for compatibility with getopts()
+# OPTLIST style, and it actually calls getoptex (see bellow)
+#
+# NOTE that a list of parameters is required and must be either "$@",
+# if processing command line arguments, or some alternative parameters.
+#
+# FUNCTION getoptex
+# Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse long-named options.
+#
+# Both getopt and getoptex return 0 if an option has been parsed,
+# and 1 if all options are already parsed or an error occured
+#
+# Both getopt and getoptex set or test the following variables:
+#
+# OPTERR -- tested for whether error messages must be given for invalid
+options
+#
+# OPTOPT -- set to the name of an option parsed,
+# or to "?" if no more options or error
+# OPTARG -- set to the option argument, if any;
+# unset if ther is no argument;
+# on error, set to the erroneous option name
+#
+# OPTIND -- Initialized to 1.
+# Then set to the number of the next parameter to be parsed
+# when getopt or getoptex will be called next time.
+# When all options are parsed, contains a number of
+# the first non-option argument.
+#
+#
+# OPTOFS -- If a parameter number $OPTIND containg an option parsed
+# does not contain any more options, OPTOFS is unset;
+# otherwise, OPTOFS is set to such a number of "?" signs
+# which is equal to the number of options parsed
+#
+# You might not set variables OPTIND and OPTOFS yourself
+# unless you want to parse a list of parameters more than once.
+# Otherwise, you whould unset OPTIND (or set it to 1)
+# and unset OPTOFS each time you want to parse a new parameters
+list
+#
+# Option list format is DIFFERENT from one for getopts or getopt.
+getopts-style
+# option list can be converted to getoptex-style using a function optlistex
+# (see bellow)
+#
+# DESCRIPTION of option list used with getoptex:
+# Option names are separated by whitespace. Options consiting of
+# more than one character are treated as long-named (--option)
+#
+# Special characters can appear at the and of option names specifying
+# whether an argument is required (default is ";"):
+# ";" (default) -- no argument
+# ":" -- required argument
+# "," -- optional argument
+#
+# For example, an option list "a b c help version f: file: separator."
+# defines the following options:
+# -a, -b, -c, --help, --version -- no argument
+# -f, --file -- argument required
+# --separator -- optional argument
+#
+# FUNCTION optlistex
+# Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST`
+#
+# Converts getopts-style option list in a format suitable for use with getoptex
+# Namely, it inserts spaces after each option name.
+#
+#
+# HOW TO USE
+#
+# In order o use in your bash scripts the functions described,
+# include a command ". getopt.sh" to the file containing the script,
+# which will define functions getopt, getoptex, and optlistex
+#
+# EXAMPLES
+#
+# See files 'getopt1' and 'getopt2' that contain sample scripts that use
+# getopt and getoptex functions respectively
+#
+#
+# Please send your comments to grg@philol.msu.ru
+
+function getoptex()
+{
+ let $# || return 1
+ local optlist="${1#;}"
+ let OPTIND || OPTIND=1
+ [ $OPTIND -lt $# ] || return 1
+ shift $OPTIND
+ if [ "$1" != "-" -a "$1" != "${1#-}" ]
+ then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ]
+ then
+ local o
+ o="-${1#-$OPTOFS}"
+ for opt in ${optlist#;}
+ do
+ OPTOPT="${opt%[;.:]}"
+ unset OPTARG
+ local opttype="${opt##*[^;:.]}"
+ [ -z "$opttype" ] && opttype=";"
+ if [ ${#OPTOPT} -gt 1 ]
+ then # long-named option
+ case $o in
+ "--$OPTOPT")
+ if [ "$opttype" != ":" ]; then return 0; fi
+ OPTARG="$2"
+ if [ -z "$OPTARG" ];
+ then # error: must have an agrument
+ let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT";
+ OPTOPT="?"
+ return 1;
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "--$OPTOPT="*)
+ if [ "$opttype" = ";" ];
+ then # error: must not have arguments
+ let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTARG=${o#"--$OPTOPT="}
+ return 0
+ ;;
+ esac
+ else # short-named option
+ case "$o" in
+ "-$OPTOPT")
+ unset OPTOFS
+ [ "$opttype" != ":" ] && return 0
+ OPTARG="$2"
+ if [ -z "$OPTARG" ]
+ then
+ echo "$0: error: -$OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "-$OPTOPT"*)
+ if [ $opttype = ";" ]
+ then # an option with no argument is in a chain of options
+ OPTOFS="$OPTOFS?" # move to the next option in the chain
+ OPTIND=$[OPTIND-1] # the chain still has other options
+ return 0
+ else
+ unset OPTOFS
+ OPTARG="${o#-$OPTOPT}"
+ return 0
+ fi
+ ;;
+ esac
+ fi
+ done
+ echo "$0: error: invalid option: $o"
+ fi; fi
+ OPTOPT="?"
+ unset OPTARG
+ return 1
+}
+function optlistex
+{
+ local l="$1"
+ local m # mask
+ local r # to store result
+ while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask
+ while [ -n "$l" ]
+ do
+ r="${r:+"$r "}${l%$m}" # append the first character of $l to $r
+ l="${l#?}" # cut the first charecter from $l
+ m="${m#?}" # cut one "?" sign from m
+ if [ -n "${l%%[^:.;]*}" ]
+ then # a special character (";", ".", or ":") was found
+ r="$r${l%$m}" # append it to $r
+ l="${l#?}" # cut the special character from l
+ m="${m#?}" # cut one more "?" sign
+ fi
+ done
+ echo $r
+}
+function getopt()
+{
+ local optlist=`optlistex "$1"`
+ shift
+ getoptex "$optlist" "$@"
+ return $?
+}
+
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt.sh) ***
+
+
+#*** file getopt1 ***
+
+#! /bin/bash
+# getopt1:
+# Sample script using the function getopt
+#
+# Type something like "getopt1 -ab -d 10 -e20 text1 text2"
+# on the command line to see how it works
+#
+# See getopt.sh for more information
+#. getopt.sh
+#echo Using getopt to parse arguments:
+#while getopt "abcd:e." "$@"
+#do
+# echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
+#done
+#shift $[OPTIND-1]
+#for arg in "$@"
+#do
+# echo "Non option argument <$arg>"
+#done
+#
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt1) ***
+#
+#
+#*** file getopt2 ***
+#
+#! /bin/bash
+# getopt2:
+# Sample script using the function getoptex
+#
+# Type something like "getopt2 -ab -d 10 -e20 --opt1 --opt4=100 text1 text2"
+# to see how it works
+#
+# See getopt.sh for more information
+. getopt.sh
+#echo Using getoptex to parse arguments:
+#while getoptex "a; b; c; d: e. opt1 opt2 opt3 opt4: opt5." "$@"
+#do
+# echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
+#done
+#shift $[OPTIND-1]
+#for arg in "$@"
+#do
+# echo "Non option argument <$arg>"
+#done
+#
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt2) ***
+
+
diff --git a/examples/functions/inetaddr b/examples/functions/inetaddr
new file mode 100644
index 00000000..776b204a
--- /dev/null
+++ b/examples/functions/inetaddr
@@ -0,0 +1,44 @@
+#
+# inet2hex - Internet address conversion, dotted-decimal to hex
+#
+inet2hex ()
+{
+ local IFS
+
+ IFS=.
+ set -- $1
+
+ if (( $# != 4 )); then
+ echo "inet2hex: incorrect input format: $1" >&2
+ echo "inet2hex: usage: inet2hex XX.XX.XX.XX" >&2
+ return 2
+ fi
+
+ printf "0x%02x%02x%02x%02x\n" $1 $2 $3 $4
+}
+
+#
+# hex2inet - Internet address conversion, hex to dotted-decimal
+#
+hex2inet ()
+{
+ local x1 x2 x3 x4
+
+ case "$1" in
+ 0x*) h=${1#??} ;;
+ *) h=$1 ;;
+ esac
+
+ if (( ${#h} != 8 )); then
+ echo "hex2inet: $h not in inet format" >&2
+ echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2
+ return 2
+ fi
+
+ x1=$(( 0x${h:0:2} ))
+ x2=$(( 0x${h:2:2} ))
+ x3=$(( 0x${h:4:2} ))
+ x4=$(( 0x${h:6:2} ))
+
+ printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
+}
diff --git a/examples/functions/inpath b/examples/functions/inpath
index 7755b33b..cb4c93da 100644
--- a/examples/functions/inpath
+++ b/examples/functions/inpath
@@ -1,5 +1,6 @@
inpath()
{
+ local PROG
path=$(echo $PATH | sed 's/^:/.:/
s/::/:.:/g
s/:$/:./
@@ -9,7 +10,5 @@ inpath()
do
[ -x $x/$1 ] && { PROG=$x/$1; break; }
done
- [ -z "$PROG" ]
- return
+ [ -n "$PROG" ]
}
-
diff --git a/examples/functions/isnum.bash b/examples/functions/isnum.bash
new file mode 100644
index 00000000..1eff13fb
--- /dev/null
+++ b/examples/functions/isnum.bash
@@ -0,0 +1,23 @@
+#From: jrmartin@rainey.blueneptune.com (James R. Martin)
+#Newsgroups: comp.unix.shell
+#Subject: Re: testing user input on numeric or character value
+#Date: 26 Nov 1997 01:28:43 GMT
+
+# isnum returns True if its argument is a valid number,
+# and False (retval=1) if it is any other string.
+# The first pattern requires a digit before the decimal
+# point, and the second after the decimal point.
+
+# BASH NOTE: make sure you have executed `shopt -s extglob' before
+# trying to use this function, or it will not work
+
+function isnum # string
+{
+ case $1 in
+ ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
+ return 0;;
+ ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
+ return 0;;
+ *) return 1;;
+ esac
+}
diff --git a/examples/functions/isnum2 b/examples/functions/isnum2
new file mode 100644
index 00000000..e2e7a5f5
--- /dev/null
+++ b/examples/functions/isnum2
@@ -0,0 +1,22 @@
+isnum2()
+{
+ case "$1" in
+ '[-+]' | '') return 1;; # empty or bare `-' or `+'
+ [-+]*[!0-9]*) return 1;; # non-digit with leading sign
+ [-+]*) return 0;; # OK
+ *[!0-9]*) return 1;; # non-digit
+ *) return 0;; # OK
+ esac
+}
+
+# this one handles floating point
+isnum3()
+{
+ case "$1" in
+ '') return 1;; # empty
+ *[!0-9.+-]*) return 1;; # non-digit, +, -, or .
+ *?[-+]*) return 1;; # sign as second or later char
+ *.*.*) return 1;; # multiple decimal points
+ *) return 0;; # OK
+ esac
+}
diff --git a/examples/functions/jdate.bash b/examples/functions/jdate.bash
new file mode 100644
index 00000000..9488ed90
--- /dev/null
+++ b/examples/functions/jdate.bash
@@ -0,0 +1,78 @@
+#From: damatex@CAM.ORG (Mario Boudreault)
+#Newsgroups: comp.unix.shell
+#Subject: JULIAN DATE CONVERSION SUB
+#Date: 4 Aug 1995 10:23:28 -0400
+#Message-ID: <3vtah0$jb3@ocean.CAM.ORG>
+
+#For those using shells and who want to convert dates to a julian number
+#here is a shell script (wihtout validation) that can be used as a base
+#program for your shell scripts.
+
+#Special thanks to Ed Ferguson@ti.com who sent me the algorithm to compute
+#that date.
+
+#
+# MODIFIED BY CHET RAMEY TO CONVERT TO bash v2 SYNTAX
+#
+
+# cnvdate - Conversion de dates en julienne et vice et versa...
+#
+# Par : Mario Boudreault Damatex Inc Montreal, Canada
+# Date: 2 Aout 1995
+# Rev.: 2 Aout 1995
+#
+# Usage:
+# cvdate [-j] YYYMMDD pour convertir en nbre de jours
+# cvdate -d {julian number} pour convertir en AAAAMMJJ
+#
+
+jul_date()
+{
+ #
+ # Separe ANNEE, MOIS et JOUR...
+ #
+ YEAR=`echo $DATE | awk ' { print substr($0,1,4) } '`
+ MONTH=`echo $DATE | awk ' { print substr($0,5,2) } '`
+ DAY=`echo $DATE | awk ' { print substr($0,7,2) } '`
+ #
+ # Execute la formule magique...
+ #
+ A=$(( $DAY - 32075 + 1461 * ( $YEAR + 4800 - ( 14 - $MONTH ) / 12 ) \
+ / 4 + 367 * ( $MONTH - 2 + ( 14 - $MONTH ) / 12 * 12 ) / 12 - \
+ 3 * ( ( $YEAR + 4900 - ( 14 - $MONTH ) / 12 ) / 100 ) / 4 ))
+ echo $A
+}
+
+day_date()
+{
+ TEMP1=$(( $DATE + 68569 ))
+ TEMP2=$(( 4 * $TEMP1 / 146097 ))
+ TEMP1=$(( $TEMP1 - ( 146097 * $TEMP2 + 3 ) / 4 ))
+ Y=$(( 4000 * ( $TEMP1 + 1 ) / 1461001 ))
+ TEMP1=$(( $TEMP1 - 1461 * $Y / 4 + 31 ))
+ M=$(( 80 * $TEMP1 / 2447 ))
+ D=$(( $TEMP1 - 2447 * $M / 80 ))
+ TEMP1=$(( $M / 11 ))
+ M=$(( $M + 2 - 12 * $TEMP1 ))
+ Y=$(( 100 * ( $TEMP2 - 49 ) + $Y + $TEMP1 ))
+ M=`echo $M | awk ' { M=$0 ; if ( length($0) == 1 ) M="0"$0 } END { print M } '`
+ D=`echo $D | awk ' { D=$0 ; if ( length($0) == 1 ) D="0"$0 } END { print D } '`
+ echo $Y$M$D
+}
+
+# main()
+
+if [ $# -eq 1 ]; then
+ DATE=$1
+ jul_date
+elif [ "$1" = '-j' ]; then
+ DATE=$2
+ jul_date
+elif [ "$1" = '-d' ]; then
+ DATE=$2
+ day_date
+fi
+#
+# Termine
+#
+exit 0
diff --git a/examples/functions/pathfuncs b/examples/functions/pathfuncs
new file mode 100644
index 00000000..47896bf5
--- /dev/null
+++ b/examples/functions/pathfuncs
@@ -0,0 +1,48 @@
+#From: "Simon J. Gerraty" <sjg@zen.void.oz.au>
+#Message-Id: <199510091130.VAA01188@zen.void.oz.au>
+#Subject: Re: a shell idea?
+#Date: Mon, 09 Oct 1995 21:30:20 +1000
+
+
+# NAME:
+# add_path.sh - add dir to path
+#
+# DESCRIPTION:
+# These functions originated in /etc/profile and ksh.kshrc, but
+# are more useful in a separate file.
+#
+# SEE ALSO:
+# /etc/profile
+#
+# AUTHOR:
+# Simon J. Gerraty <sjg@zen.void.oz.au>
+
+# RCSid:
+# $Id: add_path.sh,v 1.1 1995/09/30 12:45:23 sjg Exp $
+#
+# @(#)Copyright (c) 1991 Simon J. Gerraty
+#
+# This file is provided in the hope that it will
+# be of use. There is absolutely NO WARRANTY.
+# Permission to copy, redistribute or otherwise
+# use this file is hereby granted provided that
+# the above copyright notice and this notice are
+# left intact.
+
+# is $1 missing from $2 (or PATH) ?
+no_path() {
+ eval "case :\$${2-PATH}: in *:$1:*) return 1;; *) return 0;; esac"
+}
+# if $1 exists and is not in path, append it
+add_path () {
+ [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="\$${2:-PATH}:$1"
+}
+# if $1 exists and is not in path, prepend it
+pre_path () {
+ [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="$1:\$${2:-PATH}"
+}
+# if $1 is in path, remove it
+del_path () {
+ no_path $* || eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: |
+ sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`
+}
diff --git a/examples/functions/shcat2 b/examples/functions/shcat2
new file mode 100644
index 00000000..6fe90f40
--- /dev/null
+++ b/examples/functions/shcat2
@@ -0,0 +1,19 @@
+shcat()
+{
+ while read -r line
+ do
+ echo "$line"
+ done
+}
+
+shcat2()
+{
+ while [ $# -ge 1 ]; do
+ case "$1" in
+ -) shcat ;;
+ *) shcat < "$1" ;;
+ esac
+ shift
+ done
+ exit 0
+}
diff --git a/examples/loadables/Makefile b/examples/loadables/Makefile.in
index 9f93bca7..9773144d 100644
--- a/examples/loadables/Makefile
+++ b/examples/loadables/Makefile.in
@@ -1,41 +1,64 @@
#
# Simple makefile for the sample loadable builtins
#
-CC = cc
+# This includes some boilerplate definitions added by configure, but will
+# still need hand-editing
+#
+# Include some boilerplate Gnu makefile definitions.
+prefix = @prefix@
+
+exec_prefix = @exec_prefix@
+bindir = @bindir@
+libdir = @libdir@
+infodir = @infodir@
+includedir = @includedir@
+
+topdir = @top_srcdir@
+BUILD_DIR = @BUILD_DIR@
+srcdir = @srcdir@
+VPATH = .:@srcdir@
+
+@SET_MAKE@
+CC = @CC@
+RM = rm -f
+
+SHELL = /bin/sh
# SunOS 4
-PICFLAG = -pic
+#PICFLAG = -pic
# Some versions of gcc, esp. on NetBSD and FreeBSD
-#PICFLAG = -fpic
+PICFLAG = -fpic
# Linux -- could also be -fpic
#PICFLAG = -fPIC
# SunOS 5
#PICFLAG = -K pic
# SVR4, SVR4.2, Irix
#PICFLAG = -K PIC
-# BSD/OS 2.1
+# BSD/OS 2.1, BSD/OS 3.x
#PICFLAG =
# AIX 4.2
#PICFLAG = -K
-# SunOS 4, BSD/OS 2.1, SVR4.2, SVR4, Linux, AIX 4.2, etc.
+# SunOS 4, BSD/OS 2.1, BSD/OS 3.x, SVR4.2, SVR4, Linux, AIX 4.2, etc.
LD = ld
# SunOS 5, Linux
-#LD = cc
+#LD = ${CC}
# SunOS 4
-LDOPT = -assert pure-text
+#LDOPT = -assert pure-text
# OSF/1, Digital UNIX
#LDOPT = -shared -soname $@ -expect_unresolved '*'
-# SunOS 5
+# SunOS 5 using sun cc
#LDOPT = -dy -z text -G -i -h $@
+# SunOS 5 using gcc with Sun ld
+#LDOPT = -shared -Wl,-dy -Wl,-G -Wl,-i
# SVR4, SVR4.2
#LDOPT = -dy -z text -G -h $@
# NetBSD, FreeBSD -- might also need -r
-#LDOPT = -x -Bshareable
+LDOPT = -x -Bshareable
# Linux
#LDOPT = -shared
-# BSD/OS 2.1
+# BSD/OS 2.1, BSD/OS 3.x
#LDOPT = -r
# AIX 4.2
#LDOPT = -bdynamic -bnoentry -bexpall -G
@@ -43,19 +66,25 @@ LDOPT = -assert pure-text
# other libraries to link the shared object against
# BSD/OS 2.1
#LDLIBS = -lc_s.2.1.0
+# BSD/OS 3.0, BSD/OS 3.1
+#LDLIBS = -lc_s.3.0.0
+
-srcdir = ../..
-INC= -I$(srcdir) -I$(srcdir)/builtins -I$(srcdir)/lib
+INC = -I. -I.. -I$(topdir) -I$(topdir)/lib -I$(topdir)/builtins \
+ -I$(BUILD_DIR) -I$(BUILD_DIR)/lib -I$(BUILD_DIR)/builtins
.c.o:
$(CC) $(PICFLAG) $(CFLAGS) $(INC) -c -o $@ $<
-all: printf print truefalse sleep pushd finfo logname basename dirname \
- tty pathchk tee head rmdir sprintf
-others: necho getconf hello cat
-printf: printf.o
- $(LD) $(LDOPT) -o $@ printf.o $(LDLIBS)
+ALLPROG = print truefalse sleep pushd finfo logname basename dirname \
+ tty pathchk tee head rmdir sprintf
+OTHERPROG = necho getconf hello cat
+
+all: $(ALLPROG)
+others: $(OTHERPROG)
+
+everything: all others
sprintf: sprintf.o
$(LD) $(LDOPT) -o $@ sprintf.o $(LDLIBS)
@@ -110,3 +139,31 @@ rmdir: rmdir.o
head: head.o
$(LD) $(LDOPT) -o $@ head.o $(LDLIBS)
+
+clean:
+ $(RM) $(ALLPROG) $(OTHERPROG) *.o
+
+mostlyclean: clean
+
+distclean maintainer-clean: clean
+ $(RM) Makefile
+
+print.o: print.c
+truefalse.o: truefalse.c
+sleep.o: sleep.c
+pushd.o: pushd.c
+finfo.o: finfo.c
+logname.o: logname.c
+basename.o: basename.c
+dirname.o: dirname.c
+tty.o: tty.c
+pathchk.o: pathchk.c
+tee.o: tee.c
+head.o: head.c
+rmdir.o: rmdir.c
+sprintf.o: sprintf.c
+necho.o: necho.c
+getconf.o: getconf.c
+hello.o: hello.c
+cat.o: cat.c
+
diff --git a/examples/loadables/getconf.c b/examples/loadables/getconf.c
index 3b53331d..0f264edb 100644
--- a/examples/loadables/getconf.c
+++ b/examples/loadables/getconf.c
@@ -1,4 +1,6 @@
/*
+ * ORIGINAL COPYRIGHT STATEMENT:
+ *
* Copyright (c) 1994 Winning Strategies, Inc.
* All rights reserved.
*
@@ -31,14 +33,13 @@
/*
* POSIX.2 getconf utility
*
- * Written by:
+ * Originally Written by:
* J.T. Conklin (jtc@wimsey.com), Winning Strategies, Inc.
+ *
+ * Heavily modified for inclusion in bash by
+ * Chet Ramey <chet@po.cwru.edu>
*/
-#ifndef lint
-static char rcsid[] = "$Id: getconf.c,v 1.2 1994/05/10 00:04:12 jtc Exp $";
-#endif /* not lint */
-
#include <stdio.h>
#include <limits.h>
#include <locale.h>
@@ -48,7 +49,8 @@ static char rcsid[] = "$Id: getconf.c,v 1.2 1994/05/10 00:04:12 jtc Exp $";
#include "shell.h"
#include "builtins.h"
#include "stdc.h"
-
+#include "common.h"
+#include "bashgetopt.h"
struct conf_variable
{
@@ -57,25 +59,66 @@ struct conf_variable
long value;
};
-/* BSD/OS does not define this; use Posix.2 recommended minimum value. */
+/* Some systems do not define these; use POSIX.2 minimum recommended values. */
#ifndef _POSIX2_COLL_WEIGHTS_MAX
-#define _POSIX2_COLL_WEIGHTS_MAX 2
+# define _POSIX2_COLL_WEIGHTS_MAX 2
#endif
static const struct conf_variable conf_table[] =
{
+ /* POSIX.2 Configurable Variable Values */
{ "PATH", CONFSTR, _CS_PATH },
+ { "CS_PATH", CONFSTR, _CS_PATH },
+
+ /* POSIX.1 Configurable Variable Values (only Solaris?) */
+#if defined (_CS_LFS_CFLAGS)
+ { "LFS_CFLAGS", CONFSTR, _CS_LFS_CFLAGS },
+ { "LFS_LDFLAGS", CONFSTR, _CS_LFS_LDFLAGS },
+ { "LFS_LIBS", CONFSTR, _CS_LFS_LIBS },
+ { "LFS_LINTFLAGS", CONFSTR, _CS_LFS_LINTFLAGS },
+#endif
+#if defined (_CS_LFS64_CFLAGS)
+ { "LFS64_CFLAGS", CONFSTR, _CS_LFS64_CFLAGS },
+ { "LFS64_LDFLAGS", CONFSTR, _CS_LFS64_LDFLAGS },
+ { "LFS64_LIBS", CONFSTR, _CS_LFS64_LIBS },
+ { "LFS64_LINTFLAGS", CONFSTR, _CS_LFS64_LINTFLAGS },
+#endif
- /* Utility Limit Minimum Values */
+ /* Single UNIX Specification version 2 Configurable Variable Values */
+#if defined (_CS_XBS5_ILP32_OFF32_CFLAGS)
+ { "XBS5_ILP32_OFF32_CFLAGS", CONFSTR, _CS_XBS5_ILP32_OFF32_CFLAGS },
+ { "XBS5_ILP32_OFF32_LDFLAGS", CONFSTR, _CS_XBS5_ILP32_OFF32_LDFLAGS },
+ { "XBS5_ILP32_OFF32_LIBS", CONFSTR, _CS_XBS5_ILP32_OFF32_LIBS },
+ { "XBS5_ILP32_OFF32_LINTFLAGS", CONFSTR, _CS_XBS5_ILP32_OFF32_LINTFLAGS },
+ { "XBS5_ILP32_OFFBIG_CFLAGS", CONFSTR, _CS_XBS5_ILP32_OFFBIG_CFLAGS },
+ { "XBS5_ILP32_OFFBIG_LDFLAGS", CONFSTR, _CS_XBS5_ILP32_OFFBIG_LDFLAGS },
+ { "XBS5_ILP32_OFFBIG_LIBS", CONFSTR, _CS_XBS5_ILP32_OFFBIG_LIBS },
+ { "XBS5_ILP32_OFFBIG_LINTFLAGS", CONFSTR, _CS_XBS5_ILP32_OFFBIG_LINTFLAGS },
+ { "XBS5_LP64_OFF64_CFLAGS", CONFSTR, _CS_XBS5_LP64_OFF64_CFLAGS },
+ { "XBS5_LP64_OFF64_LDFLAGS", CONFSTR, _CS_XBS5_LP64_OFF64_LDFLAGS },
+ { "XBS5_LP64_OFF64_LIBS", CONFSTR, _CS_XBS5_LP64_OFF64_LIBS },
+ { "XBS5_LP64_OFF64_LINTFLAGS", CONFSTR, _CS_XBS5_LP64_OFF64_LINTFLAGS },
+ { "XBS5_LPBIG_OFFBIG_CFLAGS", CONFSTR, _CS_XBS5_LPBIG_OFFBIG_CFLAGS },
+ { "XBS5_LPBIG_OFFBIG_LDFLAGS", CONFSTR, _CS_XBS5_LPBIG_OFFBIG_LDFLAGS },
+ { "XBS5_LPBIG_OFFBIG_LIBS", CONFSTR, _CS_XBS5_LPBIG_OFFBIG_LIBS },
+ { "XBS5_LPBIG_OFFBIG_LINTFLAGS", CONFSTR, _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS },
+#endif /* _CS_XBS5_ILP32_OFF32_CFLAGS */
+
+ /* POSIX.2 Utility Limit Minimum Values */
{ "POSIX2_BC_BASE_MAX", CONSTANT, _POSIX2_BC_BASE_MAX },
{ "POSIX2_BC_DIM_MAX", CONSTANT, _POSIX2_BC_DIM_MAX },
{ "POSIX2_BC_SCALE_MAX", CONSTANT, _POSIX2_BC_SCALE_MAX },
{ "POSIX2_BC_STRING_MAX", CONSTANT, _POSIX2_BC_STRING_MAX },
{ "POSIX2_COLL_WEIGHTS_MAX", CONSTANT, _POSIX2_COLL_WEIGHTS_MAX },
+#if defined (_POSIX2_EQUIV_CLASS_MAX)
+ { "POSIX2_EQUIV_CLASS_MAX", CONSTANT, _POSIX2_EQUIV_CLASS_MAX },
+#endif
{ "POSIX2_EXPR_NEST_MAX", CONSTANT, _POSIX2_EXPR_NEST_MAX },
{ "POSIX2_LINE_MAX", CONSTANT, _POSIX2_LINE_MAX },
{ "POSIX2_RE_DUP_MAX", CONSTANT, _POSIX2_RE_DUP_MAX },
+#if defined (_POSIX2_VERSION)
{ "POSIX2_VERSION", CONSTANT, _POSIX2_VERSION },
+#endif
/* POSIX.1 Minimum Values */
{ "_POSIX_ARG_MAX", CONSTANT, _POSIX_ARG_MAX },
@@ -92,7 +135,7 @@ static const struct conf_variable conf_table[] =
{ "_POSIX_STREAM_MAX", CONSTANT, _POSIX_STREAM_MAX },
{ "_POSIX_TZNAME_MAX", CONSTANT, _POSIX_TZNAME_MAX },
- /* Symbolic Utility Limits */
+ /* POSIX.2 Symbolic Utility Limits */
{ "BC_BASE_MAX", SYSCONF, _SC_BC_BASE_MAX },
{ "BC_DIM_MAX", SYSCONF, _SC_BC_DIM_MAX },
{ "BC_SCALE_MAX", SYSCONF, _SC_BC_SCALE_MAX },
@@ -102,15 +145,25 @@ static const struct conf_variable conf_table[] =
{ "LINE_MAX", SYSCONF, _SC_LINE_MAX },
{ "RE_DUP_MAX", SYSCONF, _SC_RE_DUP_MAX },
- /* Optional Facility Configuration Values */
+ /* POSIX.2 Optional Facility Configuration Values */
{ "POSIX2_C_BIND", SYSCONF, _SC_2_C_BIND },
{ "POSIX2_C_DEV", SYSCONF, _SC_2_C_DEV },
+#if defined (_SC_2_C_VERSION)
+ { "POSIX2_C_VERSION", SYSCONF, _SC_2_C_VERSION },
+#endif
+#if defined (_SC_2_CHAR_TERM)
{ "POSIX2_CHAR_TERM", SYSCONF, _SC_2_CHAR_TERM },
+#endif
{ "POSIX2_FORT_DEV", SYSCONF, _SC_2_FORT_DEV },
{ "POSIX2_FORT_RUN", SYSCONF, _SC_2_FORT_RUN },
{ "POSIX2_LOCALEDEF", SYSCONF, _SC_2_LOCALEDEF },
{ "POSIX2_SW_DEV", SYSCONF, _SC_2_SW_DEV },
+#if defined (_SC2_UPE)
{ "POSIX2_UPE", SYSCONF, _SC_2_UPE },
+#endif
+#if !defined (_POSIX2_VERSION) && defined (_SC_2_VERSION)
+ { "POSIX2_VERSION" SYSCONF, _SC_2_VERSION },
+#endif
/* POSIX.1 Configurable System Variables */
{ "ARG_MAX", SYSCONF, _SC_ARG_MAX },
@@ -124,6 +177,140 @@ static const struct conf_variable conf_table[] =
{ "_POSIX_SAVED_IDS", SYSCONF, _SC_SAVED_IDS },
{ "_POSIX_VERSION", SYSCONF, _SC_VERSION },
+ /* POSIX.1 Optional Facility Configuration Values */
+#if defined (_SC_ASYNCHRONOUS_IO)
+ { "_POSIX_ASYNCHRONOUS_IO", SYSCONF, _SC_ASYNCHRONOUS_IO },
+#endif
+#if defined (_SC_FSYNC)
+ { "_POSIX_FSYNC", SYSCONF, _SC_FSYNC },
+#endif
+#if defined (_SC_MAPPED_FILES)
+ { "_POSIX_MAPPED_FILES", SYSCONF, _SC_MAPPED_FILES },
+#endif
+#if defined (_SC_MEMLOCK)
+ { "_POSIX_MEMLOCK", SYSCONF, _SC_MEMLOCK },
+#endif
+#if defined (_SC_MEMLOCK_RANGE)
+ { "_POSIX_MEMLOCK_RANGE", SYSCONF, _SC_MEMLOCK_RANGE },
+#endif
+#if defined (_SC_MEMORY_PROTECTION)
+ { "_POSIX_MEMORY_PROTECTION", SYSCONF, _SC_MEMORY_PROTECTION },
+#endif
+#if defined (_SC_MESSAGE_PASSING)
+ { "_POSIX_MESSAGE_PASSING", SYSCONF, _SC_MESSAGE_PASSING },
+#endif
+#if defined (SC_PRIORITIZED_IO)
+ { "_POSIX_PRIORITIZED_IO", SYSCONF, _SC_PRIORITIZED_IO },
+#endif
+#if defined (_SC_PRIORITY_SCHEDULING)
+ { "_POSIX_PRIORITY_SCHEDULING", SYSCONF, _SC_PRIORITY_SCHEDULING },
+#endif
+#if defined (_SC_REALTIME_SIGNALS)
+ { "_POSIX_REALTIME_SIGNALS", SYSCONF, _SC_REALTIME_SIGNALS },
+#endif
+#if defined (_SC_SEMAPHORES)
+ { "_POSIX_SEMAPHORES", SYSCONF, _SC_SEMAPHORES },
+#endif
+#if defined (_SC_SHARED_MEMORY_OBJECTS)
+ { "_POSIX_SHARED_MEMORY_OBJECTS", SYSCONF, _SC_SHARED_MEMORY_OBJECTS },
+#endif
+#if defined (_SC_SYNCHRONIZED_IO)
+ { "_POSIX_SYNCHRONIZED_IO", SYSCONF, _SC_SYNCHRONIZED_IO },
+#endif
+#if defined (_SC_TIMERS)
+ { "_POSIX_TIMERS", SYSCONF, _SC_TIMERS },
+#endif
+#if defined (_SC_THREADS)
+ { "_POSIX_THREADS", SYSCONF, _SC_THREADS },
+ { "_POSIX_THREAD_ATTR_STACKADDR", SYSCONF, _SC_THREAD_ATTR_STACKADDR },
+ { "_POSIX_THREAD_ATTR_STACKSIZE", SYSCONF, _SC_THREAD_ATTR_STACKSIZE },
+ { "_POSIX_THREAD_PRIORITY_SCHEDULING", SYSCONF, _SC_THREAD_PRIORITY_SCHEDULING },
+ { "_POSIX_THREAD_PRIO_INHERIT", SYSCONF, _SC_THREAD_PRIO_INHERIT },
+ { "_POSIX_THREAD_PRIO_PROTECT", SYSCONF, _SC_THREAD_PRIO_PROTECT },
+ { "_POSIX_THREAD_PROCESS_SHARED", SYSCONF, _SC_THREAD_PROCESS_SHARED },
+# if defined (_SC_THREAD_SAFE_FUNCTIONS)
+ { "_POSIX_THREAD_SAFE_FUNCTIONS", SYSCONF, _SC_THREAD_SAFE_FUNCTIONS },
+# endif
+#endif /* _SC_THREADS */
+
+ /* XPG 4.2 Configurable System Variables. */
+#if defined (_SC_ATEXIT_MAX)
+ { "ATEXIT_MAX", SYSCONF, _SC_ATEXIT_MAX },
+#endif
+#if defined (_SC_IOV_MAX)
+ { "IOV_MAX", SYSCONF, _SC_IOV_MAX },
+#endif
+#if defined (_SC_PAGESIZE)
+ { "PAGESIZE", SYSCONF, _SC_PAGESIZE },
+#endif
+#if defined (_SC_PAGE_SIZE)
+ { "PAGE_SIZE", SYSCONF, _SC_PAGE_SIZE },
+#endif
+
+#if defined (_SC_AIO_LISTIO_MAX)
+ { "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX },
+ { "AIO_MAX", SYSCONF, _SC_AIO_MAX },
+ { "AIO_PRIO_DELTA_MAX", SYSCONF, _SC_AIO_PRIO_DELTA_MAX },
+ { "DELAYTIMER_MAX", SYSCONF, _SC_DELAYTIMER_MAX },
+#if defined (_SC_GETGR_R_SIZE_MAX)
+ { "GETGR_R_SIZE_MAX", SYSCONF, _SC_GETGR_R_SIZE_MAX },
+#endif
+#if defined (_SC_GETPW_R_SIZE_MAX)
+ { "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX },
+#endif
+ { "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX },
+ { "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX },
+ { "RTSIG_MAX", SYSCONF, _SC_RTSIG_MAX },
+ { "SEM_NSEMS_MAX", SYSCONF, _SC_SEM_NSEMS_MAX },
+ { "SEM_VALUE_MAX", SYSCONF, _SC_SEM_VALUE_MAX },
+ { "SIGQUEUE_MAX", SYSCONF, _SC_SIGQUEUE_MAX },
+ { "TIMER_MAX", SYSCONF, _SC_TIMER_MAX },
+#endif /* _SC_AIO_LISTIO_MAX */
+#if defined (_SC_LOGIN_NAME_MAX)
+ { "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX },
+#endif
+#if defined (_SC_LOGNAME_MAX)
+ { "LOGNAME_MAX", SYSCONF, _SC_LOGNAME_MAX },
+#endif
+#if defined (_SC_TTY_NAME_MAX)
+ { "TTY_NAME_MAX", SYSCONF, _SC_TTY_NAME_MAX },
+#endif
+
+#if defined (_SC_PTHREAD_DESTRUCTOR_ITERATIONS)
+ { "PTHREAD_DESTRUCTOR_ITERATIONS", SYSCONF, _SC_THREAD_DESTRUCTOR_ITERATIONS },
+ { "PTHREAD_KEYS_MAX", SYSCONF, _SC_THREAD_KEYS_MAX },
+ { "PTHREAD_STACK_MIN", SYSCONF, _SC_THREAD_STACK_MIN },
+ { "PTHREAD_THREADS_MAX", SYSCONF, _SC_THREAD_THREADS_MAX },
+#endif /* _SC_PTHREAD_DESTRUCTOR_ITERATIONS */
+
+ /* XPG 4.2 Optional Facility Configuration Values */
+#if defined (_SC_XOPEN_UNIX)
+ { "_XOPEN_UNIX", SYSCONF, _SC_XOPEN_UNIX },
+ { "_XOPEN_CRYPT", SYSCONF, _SC_XOPEN_CRYPT },
+ { "_XOPEN_ENH_I18N", SYSCONF, _SC_XOPEN_ENH_I18N },
+ { "_XOPEN_SHM", SYSCONF, _SC_XOPEN_SHM },
+ { "_XOPEN_VERSION", SYSCONF, _SC_XOPEN_VERSION },
+# if defined (_SC_XOPEN_XCU_VERSION)
+ { "_XOPEN_XCU_VERSION", SYSCONF, _SC_XOPEN_XCU_VERSION },
+# endif
+#endif
+#if defined (_SC_XOPEN_REALTIME)
+ { "_XOPEN_REALTIME", SYSCONF, _SC_XOPEN_REALTIME },
+ { "_XOPEN_REALTIME_THREADS", SYSCONF, _SC_XOPEN_REALTIME_THREADS },
+#endif
+#if defined (_SC_XOPEN_LEGACY)
+ { "_XOPEN_LEGACY", SYSCONF, _SC_XOPEN_LEGACY },
+#endif /* _SC_XOPEN_LEGACY */
+
+ /* Single UNIX Specification version 2 Optional Facility Configuration Values */
+#if defined (_SC_XBS5_ILP32_OFF32)
+ { "_XBS5_ILP32_OFF32", SYSCONF, _SC_XBS5_ILP32_OFF32 },
+ { "_XBS5_ILP32_OFFBIG", SYSCONF, _SC_XBS5_ILP32_OFFBIG },
+ { "_XBS5_LP64_OFF64", SYSCONF, _SC_XBS5_LP64_OFF64 },
+ { "_XBS5_LPBIG_OFFBIG", SYSCONF, _SC_XBS5_LPBIG_OFFBIG },
+#endif /* _SC_XBS5_ILP32_OFF32 */
+
+ /* POSIX.1 Configurable Pathname Values */
{ "LINK_MAX", PATHCONF, _PC_LINK_MAX },
{ "MAX_CANON", PATHCONF, _PC_MAX_CANON },
{ "MAX_INPUT", PATHCONF, _PC_MAX_INPUT },
@@ -134,81 +321,93 @@ static const struct conf_variable conf_table[] =
{ "_POSIX_NO_TRUNC", PATHCONF, _PC_NO_TRUNC },
{ "_POSIX_VDISABLE", PATHCONF, _PC_VDISABLE },
+ /* XPG 4.2 Configurable Pathname Values */
+#if defined (_PC_FILESIZEBITS)
+ { "FILESIZEBITS", PATHCONF, _PC_FILESIZEBITS },
+#endif
+#if defined (_PC_ASYNC_IO)
+ { "_POSIX_ASYNC_IO", PATHCONF, _PC_ASYNC_IO },
+#endif
+#if defined (_PC_PRIO_IO)
+ { "_POSIX_PRIO_IO", PATHCONF, _PC_PRIO_IO },
+#endif
+#if defined (_PC_SYNC_IO)
+ { "_POSIX_SYNC_IO", PATHCONF, _PC_SYNC_IO },
+#endif
+
{ NULL }
};
+static int num_getconf_variables = sizeof(conf_table) / sizeof(struct conf_variable) - 1;
+
extern char *this_command_name;
extern char *xmalloc ();
extern char **make_builtin_argv ();
-static int getconf_main ();
+
+static void getconf_help ();
+static int getconf_print ();
+static int getconf_one ();
+static int getconf_all ();
int
getconf_builtin (list)
WORD_LIST *list;
{
- int c, r;
+ int c, r, opt, aflag;
char **v;
- WORD_LIST *l;
- v = make_builtin_argv (list, &c);
- r = getconf_main (c, v);
- free (v);
+ aflag = 0;
+ reset_internal_getopt();
+ while ((opt = internal_getopt (list, "ah")) != -1) {
+ switch (opt) {
+ case 'a':
+ aflag = 1;
+ break;
+ case 'h':
+ getconf_help();
+ return(EXECUTION_SUCCESS);
+ default:
+ builtin_usage();
+ return(EX_USAGE);
+ }
+ }
+
+ list = loptend;
+ if ((aflag == 0 && list == 0) || (aflag && list) || list_length(list) > 2) {
+ builtin_usage();
+ return(EX_USAGE);
+ }
+
+ r = aflag ? getconf_all() : getconf_one(list);
return r;
}
-static int
-getconf_main(argc, argv)
- int argc;
- char **argv;
+static void
+getconf_help()
{
- int ch;
const struct conf_variable *cp;
+ register int i, column;
- long val;
- size_t slen;
- char *sval;
-
- setlocale(LC_ALL, "");
-
- while ((ch = getopt(argc, argv, "")) != -1) {
- switch (ch) {
- case '?':
- default:
- builtin_usage();
- return(EX_USAGE);
- /* NOTREACHED */
- }
- }
- argc -= optind;
- argv += optind;
-
- if (argc < 1 || argc > 2) {
- builtin_usage();
- return(EX_USAGE);
- /* NOTREACHED */
- }
-
+ builtin_usage();
+ printf("Acceptable variable names are:\n");
for (cp = conf_table; cp->name != NULL; cp++) {
- if (strcmp(*argv, cp->name) == 0)
- break;
- }
- if (cp->name == NULL) {
- builtin_error ("%s: unknown variable", *argv);
- return (EXECUTION_FAILURE);
+ if (cp->type == PATHCONF)
+ printf("%s pathname\n", cp->name);
+ else
+ printf("%s\n", cp->name);
}
+}
- if (cp->type == PATHCONF) {
- if (argc != 2) {
- builtin_usage();
- return(EX_USAGE);
- }
- } else {
- if (argc != 1) {
- builtin_usage();
- return(EX_USAGE);
- }
- }
+static int
+getconf_print(cp, vpath, all)
+struct conf_variable *cp;
+char *vpath;
+int all;
+{
+ long val;
+ char *sval;
+ size_t slen;
switch (cp->type) {
case CONSTANT:
@@ -216,19 +415,33 @@ getconf_main(argc, argv)
break;
case CONFSTR:
+ errno = 0;
slen = confstr (cp->value, (char *) 0, (size_t) 0);
-
+ if (slen == 0) {
+ if (errno != 0) {
+ if (all)
+ printf ("getconf: %s\n", strerror(errno));
+ else
+ builtin_error ("%s", strerror(errno));
+ } else
+ printf ("undefined\n");
+ return (EXECUTION_FAILURE);
+ }
sval = xmalloc(slen);
confstr(cp->value, sval, slen);
printf("%s\n", sval);
+ free(sval);
break;
case SYSCONF:
errno = 0;
if ((val = sysconf(cp->value)) == -1) {
if (errno != 0) {
- builtin_error ("%s", strerror (errno));
+ if (all)
+ printf("getconf: %s\n", strerror (errno));
+ else
+ builtin_error ("%s", strerror (errno));
return (EXECUTION_FAILURE);
}
@@ -240,9 +453,12 @@ getconf_main(argc, argv)
case PATHCONF:
errno = 0;
- if ((val = pathconf(argv[1], cp->value)) == -1) {
+ if ((val = pathconf(vpath, cp->value)) == -1) {
if (errno != 0) {
- builtin_error ("%s: %s", argv[1], strerror (errno));
+ if (all)
+ printf("getconf: %s: %s\n", vpath, strerror (errno));
+ else
+ builtin_error ("%s: %s", vpath, strerror (errno));
return (EXECUTION_FAILURE);
}
@@ -256,6 +472,56 @@ getconf_main(argc, argv)
return (ferror(stdout) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
+static int
+getconf_all()
+{
+ const struct conf_variable *cp;
+ int ret;
+
+ ret = EXECUTION_SUCCESS;
+ for (cp = conf_table; cp->name != NULL; cp++) {
+ printf("%-35s", cp->name);
+ if (getconf_print(cp, ".", 1) == EXECUTION_FAILURE)
+ ret = EXECUTION_FAILURE;
+ }
+ return ret;
+}
+
+static int
+getconf_one(list)
+ WORD_LIST *list;
+{
+ const struct conf_variable *cp;
+ char *vname, *vpath;
+
+ vname = list->word->word;
+ vpath = (list->next && list->next->word) ? list->next->word->word
+ : (char *)NULL;
+
+ for (cp = conf_table; cp->name != NULL; cp++) {
+ if (strcmp(vname, cp->name) == 0)
+ break;
+ }
+ if (cp->name == NULL) {
+ builtin_error ("%s: unknown variable", vname);
+ return (EXECUTION_FAILURE);
+ }
+
+ if (cp->type == PATHCONF) {
+ if (list->next == 0) {
+ builtin_usage();
+ return(EX_USAGE);
+ }
+ } else {
+ if (list->next) {
+ builtin_usage();
+ return(EX_USAGE);
+ }
+ }
+
+ return (getconf_print(cp, vpath, 0));
+}
+
static char *getconf_doc[] = {
"getconf writes the current value of a configurable system limit or",
"option variable to the standard output.",
@@ -267,6 +533,6 @@ struct builtin getconf_struct = {
getconf_builtin,
BUILTIN_ENABLED,
getconf_doc,
- "getconf sysvar or getconf pathvar pathname",
+ "getconf -a or getconf -h or getconf sysvar or getconf pathvar pathname",
0
};
diff --git a/examples/loadables/print.c b/examples/loadables/print.c
index 8fea61cf..71449244 100644
--- a/examples/loadables/print.c
+++ b/examples/loadables/print.c
@@ -1,37 +1,4 @@
-/*
- * Copyright (c) 1989, 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. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. 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/types.h>
+#include "bashtypes.h"
#include <errno.h>
#include <limits.h>
@@ -47,44 +14,15 @@
extern int errno;
#endif
-#define PF(f, func) { \
- if (fieldwidth) \
- if (precision) \
- (void)fprintf(ofp, f, fieldwidth, precision, func); \
- else \
- (void)fprintf(ofp, f, fieldwidth, func); \
- else if (precision) \
- (void)fprintf(ofp, f, precision, func); \
- else \
- (void)fprintf(ofp, f, func); \
-}
-
-static int asciicode __P((void));
-static void escape __P((char *));
-static int getchr __P((void));
-static double getdouble __P((void));
-static int getint __P((int *));
-static int getlong __P((long *));
-static char *getstr __P((void));
-static char *mklong __P((char *, int));
-static void usage __P((void));
-
-static char **gargv;
-
int print_builtin ();
-static int printf_main ();
static int printargs ();
static FILE *ofp;
extern char *ansicstr ();
-extern char *single_quote ();
-extern char **make_builtin_argv ();
extern char *this_command_name;
-extern int optind;
-
static char *print_doc[] = {
"Output the arguments. The -f option means to use the argument as a",
"format string as would be supplied to printf(1). The rest of the",
@@ -168,12 +106,15 @@ opt_end:
if (pfmt)
{
- v = word_list_to_argv (list, 0, 2, &c);
- v[0] = this_command_name;
- v[1] = pfmt;
- r = printf_main (c, v);
- free (v);
- return r;
+ WORD_DESC *w;
+ WORD_LIST *nlist;
+
+ w = make_word (pfmt);
+ nlist = make_word_list (w, list);
+ r = printf_builtin (nlist);
+ nlist->next = (WORD_LIST *)NULL;
+ dispose_words (nlist);
+ return (r);
}
if (raw)
@@ -198,7 +139,8 @@ opt_end:
return 0;
}
-static int printargs (list, ofp)
+static int
+printargs (list, ofp)
WORD_LIST *list;
FILE *ofp;
{
@@ -208,7 +150,7 @@ static int printargs (list, ofp)
for (sawc = 0, l = list; l; l = l->next)
{
- ostr = ansicstr (l->word->word, strlen (l->word->word), &sawc);
+ ostr = ansicstr (l->word->word, strlen (l->word->word), &sawc, (int *)0);
fprintf (ofp, "%s", ostr);
free (ostr);
if (sawc)
@@ -219,335 +161,3 @@ static int printargs (list, ofp)
return (1);
}
-static int
-printf_main(argc, argv)
- int argc;
- char *argv[];
-{
- static char *skip1, *skip2;
- int ch, end, fieldwidth, precision;
- char convch, nextch, *format, *fmt, *start;
-
- while ((ch = getopt(argc, argv, "")) != EOF)
- switch (ch) {
- case '?':
- default:
- usage();
- return (1);
- }
- argc -= optind;
- argv += optind;
-
- if (argc < 1) {
- usage();
- return (1);
- }
-
- /*
- * Basic algorithm is to scan the format string for conversion
- * specifications -- once one is found, find out if the field
- * width or precision is a '*'; if it is, gather up value. Note,
- * format strings are reused as necessary to use up the provided
- * arguments, arguments of zero/null string are provided to use
- * up the format string.
- */
- skip1 = "#-+ 0";
- skip2 = "*0123456789";
-
- escape(fmt = format = *argv); /* backslash interpretation */
- gargv = ++argv;
- for (;;) {
- end = 0;
- /* find next format specification */
-next: for (start = fmt;; ++fmt) {
- if (!*fmt) {
- /* avoid infinite loop */
- if (end == 1) {
- warnx("missing format character",
- NULL, NULL);
- return (1);
- }
- end = 1;
- if (fmt > start)
- (void)printf("%s", start);
- if (!*gargv)
- return (0);
- fmt = format;
- goto next;
- }
- /* %% prints a % */
- if (*fmt == '%') {
- if (*++fmt != '%')
- break;
- *fmt++ = '\0';
- (void)printf("%s", start);
- goto next;
- }
- }
-
- /* skip to field width */
- for (; strchr(skip1, *fmt); ++fmt);
- if (*fmt == '*') {
- if (getint(&fieldwidth))
- return (1);
- } else
- fieldwidth = 0;
-
- /* skip to possible '.', get following precision */
- for (; strchr(skip2, *fmt); ++fmt);
- if (*fmt == '.')
- ++fmt;
- if (*fmt == '*') {
- if (getint(&precision))
- return (1);
- } else
- precision = 0;
-
- /* skip to conversion char */
- for (; strchr(skip2, *fmt); ++fmt);
- if (!*fmt) {
- warnx("missing format character", NULL, NULL);
- return (1);
- }
-
- convch = *fmt;
- nextch = *++fmt;
- *fmt = '\0';
- switch(convch) {
- case 'c': {
- char p;
-
- p = getchr();
- PF(start, p);
- break;
- }
- case 's': {
- char *p;
-
- p = getstr();
- PF(start, p);
- break;
- }
- case 'b': { /* expand escapes in argument */
- char *p;
-
- p = getstr();
- escape(p);
- PF(start, p);
- break;
- }
- case 'q': { /* print with shell single quoting */
- char *p, *p2;
-
- p = getstr();
- p2 = single_quote(p);
- PF(start, p2);
- free(p2);
- break;
- }
- case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
- long p;
- char *f;
-
- if ((f = mklong(start, convch)) == NULL)
- return (1);
- if (getlong(&p))
- return (1);
- PF(f, p);
- break;
- }
- case 'e': case 'E': case 'f': case 'g': case 'G': {
- double p;
-
- p = getdouble();
- PF(start, p);
- break;
- }
- default:
- warnx("illegal format character", NULL, NULL);
- return (1);
- }
- *fmt = nextch;
- }
- /* NOTREACHED */
-}
-
-static char *
-mklong(str, ch)
- char *str;
- int ch;
-{
- static char copy[64];
- int len;
-
- len = strlen(str) + 2;
- memmove(copy, str, len - 3);
- copy[len - 3] = 'l';
- copy[len - 2] = ch;
- copy[len - 1] = '\0';
- return (copy);
-}
-
-static void
-escape(fmt)
- register char *fmt;
-{
- register char *store;
- register int value, c;
-
- for (store = fmt; c = *fmt; ++fmt, ++store) {
- if (c != '\\') {
- *store = c;
- continue;
- }
- switch (*++fmt) {
- case '\0': /* EOS, user error */
- *store = '\\';
- *++store = '\0';
- return;
- case '\\': /* backslash */
- case '\'': /* single quote */
- *store = *fmt;
- break;
- case 'a': /* bell/alert */
- *store = '\7';
- break;
- case 'b': /* backspace */
- *store = '\b';
- break;
- case 'c':
- return;
- case 'e':
- case 'E':
- *store = '\033';
- break;
- case 'f': /* form-feed */
- *store = '\f';
- break;
- case 'n': /* newline */
- *store = '\n';
- break;
- case 'r': /* carriage-return */
- *store = '\r';
- break;
- case 't': /* horizontal tab */
- *store = '\t';
- break;
- case 'v': /* vertical tab */
- *store = '\13';
- break;
- /* octal constant */
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- for (c = 3, value = 0;
- c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
- value <<= 3;
- value += *fmt - '0';
- }
- --fmt;
- *store = value;
- break;
- default:
- *store = *fmt;
- break;
- }
- }
- *store = '\0';
-}
-
-static int
-getchr()
-{
- if (!*gargv)
- return ('\0');
- return ((int)**gargv++);
-}
-
-static char *
-getstr()
-{
- if (!*gargv)
- return ("");
- return (*gargv++);
-}
-
-static char *Number = "+-.0123456789";
-static int
-getint(ip)
- int *ip;
-{
- long val;
-
- if (getlong(&val))
- return (1);
- if (val > INT_MAX) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
- *ip = val;
- return (0);
-}
-
-static int
-getlong(lp)
- long *lp;
-{
- long val;
- char *ep;
-
- if (!*gargv) {
- *lp = 0;
- return (0);
- }
- if (strchr(Number, **gargv)) {
- errno = 0;
- val = strtol(*gargv, &ep, 0);
- if (*ep != '\0') {
- warnx("%s: illegal number", *gargv, NULL);
- return (1);
- }
- if (errno == ERANGE)
- if (val == LONG_MAX) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
- if (val == LONG_MIN) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
-
- *lp = val;
- ++gargv;
- return (0);
- }
- *lp = (long)asciicode();
- return (0);
-}
-
-static double
-getdouble()
-{
- if (!*gargv)
- return ((double)0);
- if (strchr(Number, **gargv))
- return (atof(*gargv++));
- return ((double)asciicode());
-}
-
-static int
-asciicode()
-{
- register int ch;
-
- ch = **gargv;
- if (ch == '\'' || ch == '"')
- ch = (*gargv)[1];
- ++gargv;
- return (ch);
-}
-
-static void
-usage()
-{
- (void)fprintf(stderr, "usage: print [-Rnprs] [-u unit] [-f format] [arg ...]\n");
-}
diff --git a/examples/loadables/printf.c b/examples/loadables/printf.c
deleted file mode 100644
index 33996c6e..00000000
--- a/examples/loadables/printf.c
+++ /dev/null
@@ -1,460 +0,0 @@
-/*
- * Copyright (c) 1989, 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. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. 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 !defined(BUILTIN) && !defined(SHELL)
-#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1989, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
-#endif /* not lint */
-#endif
-
-#ifndef lint
-static char sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93";
-#endif /* not lint */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-
-#include "bashansi.h"
-#include "shell.h"
-#include "builtins.h"
-#include "stdc.h"
-
-#if !defined (errno)
-extern int errno;
-#endif
-
-#define PF(f, func) { \
- if (fieldwidth) \
- if (precision) \
- (void)printf(f, fieldwidth, precision, func); \
- else \
- (void)printf(f, fieldwidth, func); \
- else if (precision) \
- (void)printf(f, precision, func); \
- else \
- (void)printf(f, func); \
-}
-
-static int asciicode __P((void));
-static void escape __P((char *));
-static int getchr __P((void));
-static double getdouble __P((void));
-static int getint __P((int *));
-static int getlong __P((long *));
-static char *getstr __P((void));
-static char *mklong __P((char *, int));
-static void usage __P((void));
-
-static char **gargv;
-
-int printf_builtin ();
-static int printf_main ();
-extern char *this_command_name;
-extern char *single_quote ();
-extern char **make_builtin_argv ();
-
-static char *printf_doc[] = {
- "printf formats and prints its arguments, after the first, under control",
- "of the format. The format is a character string which contains three",
- "types of objects: plain characters, which are simply copied to standard",
- "output, character escape sequences which are converted and copied to the",
- "standard output, and format specifications, each of which causes printing",
- "of the next successive argument. In addition to the standard printf(1)",
- "formats, %%b means to expand escapes in the corresponding argument, and",
- "%%q means to quote the argument in a way that can be reused as shell input.",
- (char *)NULL
-};
-
-struct builtin printf_struct = {
- "printf",
- printf_builtin,
- BUILTIN_ENABLED,
- printf_doc,
- "printf format [arguments]",
- (char *)0
-};
-
-int
-printf_builtin (list)
- WORD_LIST *list;
-{
- int c, r;
- char **v;
- WORD_LIST *l;
-
- v = make_builtin_argv (list, &c);
- r = printf_main (c, v);
- free (v);
- fflush(stdout);
-
- return r;
-}
-
-static int
-printf_main(argc, argv)
- int argc;
- char *argv[];
-{
- extern int optind;
- static char *skip1, *skip2;
- int ch, end, fieldwidth, precision;
- char convch, nextch, *format, *fmt, *start;
-
- while ((ch = getopt(argc, argv, "")) != EOF)
- switch (ch) {
- case '?':
- default:
- usage();
- return (1);
- }
- argc -= optind;
- argv += optind;
-
- if (argc < 1) {
- usage();
- return (1);
- }
-
- /*
- * Basic algorithm is to scan the format string for conversion
- * specifications -- once one is found, find out if the field
- * width or precision is a '*'; if it is, gather up value. Note,
- * format strings are reused as necessary to use up the provided
- * arguments, arguments of zero/null string are provided to use
- * up the format string.
- */
- skip1 = "#-+ 0";
- skip2 = "*0123456789";
-
- escape(fmt = format = *argv); /* backslash interpretation */
- gargv = ++argv;
- for (;;) {
- end = 0;
- /* find next format specification */
-next: for (start = fmt;; ++fmt) {
- if (!*fmt) {
- /* avoid infinite loop */
- if (end == 1) {
- warnx("missing format character",
- NULL, NULL);
- return (1);
- }
- end = 1;
- if (fmt > start)
- (void)printf("%s", start);
- if (!*gargv)
- return (0);
- fmt = format;
- goto next;
- }
- /* %% prints a % */
- if (*fmt == '%') {
- if (*++fmt != '%')
- break;
- *fmt++ = '\0';
- (void)printf("%s", start);
- goto next;
- }
- }
-
- /* skip to field width */
- for (; strchr(skip1, *fmt); ++fmt);
- if (*fmt == '*') {
- if (getint(&fieldwidth))
- return (1);
- } else
- fieldwidth = 0;
-
- /* skip to possible '.', get following precision */
- for (; strchr(skip2, *fmt); ++fmt);
- if (*fmt == '.')
- ++fmt;
- if (*fmt == '*') {
- if (getint(&precision))
- return (1);
- } else
- precision = 0;
-
- /* skip to conversion char */
- for (; strchr(skip2, *fmt); ++fmt);
- if (!*fmt) {
- warnx("missing format character", NULL, NULL);
- return (1);
- }
-
- convch = *fmt;
- nextch = *++fmt;
- *fmt = '\0';
- switch(convch) {
- case 'c': {
- char p;
-
- p = getchr();
- PF(start, p);
- break;
- }
- case 's': {
- char *p;
-
- p = getstr();
- PF(start, p);
- break;
- }
- case 'b': { /* expand escapes in argument */
- char *p;
-
- p = getstr();
- escape(p);
- PF("%s", p);
- break;
- }
- case 'q': { /* print with shell single quoting */
- char *p, *p2;
-
- p = getstr();
- p2 = single_quote(p);
- PF("%s", p2);
- free(p2);
- break;
- }
- case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
- long p;
- char *f;
-
- if ((f = mklong(start, convch)) == NULL)
- return (1);
- if (getlong(&p))
- return (1);
- PF(f, p);
- break;
- }
- case 'e': case 'E': case 'f': case 'g': case 'G': {
- double p;
-
- p = getdouble();
- PF(start, p);
- break;
- }
- default:
- warnx("illegal format character", NULL, NULL);
- return (1);
- }
- *fmt = nextch;
- }
- /* NOTREACHED */
-}
-
-static char *
-mklong(str, ch)
- char *str;
- int ch;
-{
- static char copy[64];
- int len;
-
- len = strlen(str) + 2;
- memmove(copy, str, len - 3);
- copy[len - 3] = 'l';
- copy[len - 2] = ch;
- copy[len - 1] = '\0';
- return (copy);
-}
-
-static void
-escape(fmt)
- register char *fmt;
-{
- register char *store;
- register int value, c;
-
- for (store = fmt; c = *fmt; ++fmt, ++store) {
- if (c != '\\') {
- *store = c;
- continue;
- }
- switch (*++fmt) {
- case '\0': /* EOS, user error */
- *store = '\\';
- *++store = '\0';
- return;
- case '\\': /* backslash */
- case '\'': /* single quote */
- *store = *fmt;
- break;
- case 'a': /* bell/alert */
- *store = '\7';
- break;
- case 'b': /* backspace */
- *store = '\b';
- break;
- case 'c':
- return;
- case 'e':
- case 'E':
- *store = '\033';
- break;
- case 'f': /* form-feed */
- *store = '\f';
- break;
- case 'n': /* newline */
- *store = '\n';
- break;
- case 'r': /* carriage-return */
- *store = '\r';
- break;
- case 't': /* horizontal tab */
- *store = '\t';
- break;
- case 'v': /* vertical tab */
- *store = '\13';
- break;
- /* octal constant */
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- for (c = 3, value = 0;
- c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
- value <<= 3;
- value += *fmt - '0';
- }
- --fmt;
- *store = value;
- break;
- default:
- *store = *fmt;
- break;
- }
- }
- *store = '\0';
-}
-
-static int
-getchr()
-{
- if (!*gargv)
- return ('\0');
- return ((int)**gargv++);
-}
-
-static char *
-getstr()
-{
- if (!*gargv)
- return ("");
- return (*gargv++);
-}
-
-static char *Number = "+-.0123456789";
-static int
-getint(ip)
- int *ip;
-{
- long val;
-
- if (getlong(&val))
- return (1);
- if (val > INT_MAX) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
- *ip = val;
- return (0);
-}
-
-static int
-getlong(lp)
- long *lp;
-{
- long val;
- char *ep;
-
- if (!*gargv) {
- *lp = 0;
- return (0);
- }
- if (strchr(Number, **gargv)) {
- errno = 0;
- val = strtol(*gargv, &ep, 0);
- if (*ep != '\0') {
- warnx("%s: illegal number", *gargv, NULL);
- return (1);
- }
- if (errno == ERANGE)
- if (val == LONG_MAX) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
- if (val == LONG_MIN) {
- warnx("%s: %s", *gargv, strerror(ERANGE));
- return (1);
- }
-
- *lp = val;
- ++gargv;
- return (0);
- }
- *lp = (long)asciicode();
- return (0);
-}
-
-static double
-getdouble()
-{
- if (!*gargv)
- return ((double)0);
- if (strchr(Number, **gargv))
- return (atof(*gargv++));
- return ((double)asciicode());
-}
-
-static int
-asciicode()
-{
- register int ch;
-
- ch = **gargv;
- if (ch == '\'' || ch == '"')
- ch = (*gargv)[1];
- ++gargv;
- return (ch);
-}
-
-static void
-usage()
-{
- (void)fprintf(stderr, "usage: printf format [arg ...]\n");
-}
diff --git a/examples/misc/alias-conv.bash b/examples/misc/aliasconv.bash
index cb92ee0a..cb92ee0a 100755
--- a/examples/misc/alias-conv.bash
+++ b/examples/misc/aliasconv.bash
diff --git a/examples/misc/alias-conv.sh b/examples/misc/aliasconv.sh
index 4cbebfb4..4cbebfb4 100755
--- a/examples/misc/alias-conv.sh
+++ b/examples/misc/aliasconv.sh
diff --git a/examples/scripts.v2/cal2day.bash b/examples/scripts.v2/cal2day.bash
new file mode 100644
index 00000000..f26128b6
--- /dev/null
+++ b/examples/scripts.v2/cal2day.bash
@@ -0,0 +1,49 @@
+#!/bin/bash
+# cal2day - "parse" appropriate calendar output to match date number
+# with day name.
+#
+# usage: cal2day month day [year]
+#
+# ORIGINAL *TAG:33239 3:Dec 9 1997:0755:sh.d/cal2day:
+#
+# Obtained from usenet
+#
+# Converted to bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
+
+#1 PARSE OPTIONS
+while getopts :dls _inst
+do case $_inst in
+ (d) format='%1d%.0s\n' ;; # 0, 1, ..., 7
+ (l) format='%0.s%-s\n' ;; # Sunday, Monday, ..., Saturday
+ (s) format='%0.s%-.3s\n' ;; # Sun, Mon, ..., Sat
+ esac
+done
+shift $((OPTIND-1))
+
+#2 PARAMETER VALUES
+((!$#)) && set -- $(date '+%m %d')
+: ${format:='%0.s%-.3s\n'}
+: ${1:?missing month parameter [1-12]}
+: ${2:?missing day parameter [1-31]}
+
+#3 CALCULATE DAY-OF-WEEK FROM DATE
+cal $1 ${3:-$(date +%Y)} | gawk -FX '
+BEGIN { day="Sunday Monday Tuesday WednesdayThursday Friday Saturday"
+ sub(/^0/, "", daynum)
+ dayre="(^| )" daynum "( |$)"
+ }
+#NR==2 { print length($0) }
+NR==1 || NR==2 \
+ { next }
+dayre { if (match($0, dayre))
+ { #print RSTART, RLENGTH, substr($0, RSTART, RLENGTH)
+ if (daynum<=9 || RSTART==1) RSTART-=1
+ exit
+ }
+ }
+END { # 20/21 char width assumed
+ printf format, RSTART/3, substr(day, RSTART*3+1, 9)
+ }
+' daynum=$2 format=$format -
+
+exit 0
diff --git a/examples/scripts/fixfiles.bash b/examples/scripts/fixfiles.bash
new file mode 100644
index 00000000..67311eed
--- /dev/null
+++ b/examples/scripts/fixfiles.bash
@@ -0,0 +1,92 @@
+#! /bin/bash
+#
+# From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
+# Newsgroups: comp.unix.shell,comp.os.linux.misc
+# Subject: GNU Bash Script to fix filenames
+# Date: 28 Mar 1996 14:54:43 -0800
+# Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
+#
+#This is a script which takes a list of directories, descends through each one
+#and ``corrects'' filenames that:
+#
+# - contain filename globbing characters: * ? [ ]
+# - quote characters: ' "
+# - control characters: 0-31 (127 is not dealt with---oops)
+# - - or + as the first character
+#
+# The GNU version of 'tr' is required. Also requires 'sed'.
+#
+# Script to process a given list of directories recursively
+# and rename each file to something that is reasonable.
+#
+# The rules are:
+#
+# 1. replace each space, [, ], *, ", and ' character in the name with a
+# period.
+# 2. replace each control character 1..31 with a printable character obtained
+# by adding 64 to the ascii value. ^A becomes A, ^B becomes B and so on.
+# 3. replace a - or + occuring at the beginning of the name with a #
+#
+# 4. if the resulting name has been changed in any way, then
+# 5. if a file of the new name already exists, then
+# 6. add a . to the new name and goto step 5.
+# 7. rename the old name to the new name
+#
+# written by Kaz Kylheku <kaz@cafe.net>
+# March 1996
+# Vancouver, Canada
+#
+# requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
+#
+# minimal conversion to bash v2 syntax done by Chet Ramey
+
+processfile()
+{
+ new_name="`echo -n $1 | tr '\173\175\052\077\042\047 ' '.......' |
+ tr '[\000-\037]' '[\100-\137]' |
+ sed -e 's/^-/#/' -e 's/+/#/'`"
+ if [ "$new_name" != "$1" ] ; then
+ while [ -e "$new_name" ] ; do
+ new_name="${new_name}."
+ done
+ echo changing \"$1\" to \"$new_name\" in `pwd`
+ mv -- "$1" "$new_name"
+ fi
+}
+
+processdir()
+{
+ set -f
+ local savepwd="$PWD"
+ if cd "$1" ; then
+ set +f
+ for file in * ; do
+ set -f
+ if [ "$file" != "." -a "$file" != ".." ] ; then
+ if [ -L "$file" ] ; then
+ echo "skipping symlink" $file in `pwd`
+ elif [ -d "$file" ] ; then
+ processdir "$file"
+ elif [ -f "$file" ] ; then
+ processfile "$file"
+ fi
+ fi
+ done
+ cd "$savepwd"
+ fi
+}
+
+shopt -s nullglob dotglob
+
+if [ $# = 0 ] ; then
+ echo "$0: must specify a list of directories" >&2
+ echo "$0: usage: $0 directory [directory ...]" >&2
+ exit 2
+fi
+
+while [ $# != 0 ] ; do
+ processdir "$1"
+ shift
+done
+
+exit 0
diff --git a/examples/scripts/hanoi.bash b/examples/scripts/hanoi.bash
new file mode 100644
index 00000000..c3081257
--- /dev/null
+++ b/examples/scripts/hanoi.bash
@@ -0,0 +1,21 @@
+# Towers of Hanoi in bash
+#
+# cribbed from the ksh93 book, example from exercises on page 85
+#
+# Chet Ramey
+# chet@po.cwru.edu
+
+hanoi() # n from to spare
+{
+ typeset -i nm1=$1-1
+ ((nm1>0)) && hanoi $nm1 $2 $4 $3
+ echo "Move disc $2 to $3"
+ ((nm1>0)) && hanoi $nm1 $4 $3 $2
+}
+
+case $1 in
+[1-9])
+ hanoi $1 1 2 3;;
+*) echo "${0##*/}: Argument must be from 1 to 9"
+ exit 1;;
+esac
diff --git a/examples/scripts/krand.bash b/examples/scripts/krand.bash
new file mode 100755
index 00000000..dfdfd323
--- /dev/null
+++ b/examples/scripts/krand.bash
@@ -0,0 +1,74 @@
+# Originally
+#
+# From: bsh20858@news.fhda.edu (Brian S Hiles)
+# Newsgroups: comp.unix.shell
+# Subject: Re: getting random numbers
+# Date: 23 Jan 1997 23:27:30 GMT
+# Message-ID: <5c8s52$eif@tiptoe.fhda.edu>
+
+# @(#) krand Produces a random number within integer limits
+# "krand" Korn shell script generates a random number in a
+# specified range with an optionally specified ``seed'' value.
+# Author: Peter Turnbull, May 1993
+# Modified by: Becca Thomas, January 1994
+
+# changed the optional third argument to a -s option, converted to
+# bash v2 syntax -- chet@po.cwru.edu
+
+PROGNAME=${0##*/}
+USAGE="usage: $PROGNAME [-s seed] lower-limit upper-limit"
+
+Seed=$$ # Initialize random-number seed value with PID
+
+usage()
+{
+ echo ${PROGNAME}: "$USAGE" >&2
+}
+
+errexit()
+{
+ echo ${PROGNAME}: "$@" >&2
+ exit 1
+}
+
+# Process command-line arguments:
+while getopts "s:" opt; do
+ case "$opt" in
+ s) Seed=$OPTARG ;;
+ *) usage ; exit 2;;
+ esac
+done
+
+shift $(($OPTIND - 1))
+
+case $# in
+ 2) Lower=$1; Upper=$2 ;;
+ *) usage ; exit 2;;
+esac
+
+# Check that specified values are integers:
+expr "$Lower" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "lower ($Lower) not an integer"; }
+expr "$Upper" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "upper ($Upper) not an integer"; }
+expr "$Seed" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "seed ($Seed) not an integer"; }
+
+# Check that values are in the correct range:
+if (( "$Lower" < 0 )) || [ ${#Lower} -gt 5 ]; then
+ errexit "lower limit ($Lower) less than zero"
+fi
+if (( "$Upper" > 32767 )) || [ ${#Upper} -gt 5 ]; then
+ errexit "upper limit ($Upper) greater than 32767"
+fi
+if (( "$Seed" < 0 )) || (( "$Seed" > 32767 )) || [ ${#Seed} -gt 5 ]; then
+ errexit "seed value ($Seed) out of range (0 to 32767)"
+fi
+(( "$Upper" <= "$Lower" )) && errexit "upper limit ($Upper) <= lower limit ($Lower)"
+
+# Seed the random-number generator:
+RANDOM=$Seed
+# Compute value, scaled within range:
+let rand="$RANDOM % ($Upper - $Lower + 1) + $Lower"
+# Report result:
+echo $rand
diff --git a/examples/scripts/randomcard.bash b/examples/scripts/randomcard.bash
new file mode 100644
index 00000000..9cb6b50c
--- /dev/null
+++ b/examples/scripts/randomcard.bash
@@ -0,0 +1,18 @@
+# The following prints a random card from a card deck.
+#
+# cribbed from the ksh93 book, example from page 70
+#
+# chet@po.cwru.edu
+#
+declare -i i=0
+
+# load the deck
+for suit in clubs diamonds hearts spades; do
+ for n in ace 2 3 4 5 6 7 8 9 10 jack queen king; do
+ card[i]="$n of $suit"
+ i=i+1 # let is not required with integer variables
+ done
+done
+
+# and print a random card
+echo ${card[RANDOM%52]}
diff --git a/examples/scripts/scrollbar b/examples/scripts/scrollbar
index c177179e..388bea8e 100755
--- a/examples/scripts/scrollbar
+++ b/examples/scripts/scrollbar
@@ -9,17 +9,17 @@
# converted from ksh syntax to bash v2 syntax by Chet Ramey
WIDTH=${COLUMNS:-80}
+WMINUS=$(( $WIDTH - 1 ))
[ $# -lt 1 ] && set -- TESTING
-# Posix.2 compatible printf command or bash loadable builtin
-# in examples/loadables/printf
+# use the bash-2.02 printf builtin
Text=$(printf "%-${WIDTH}s" "$*")
-Text=$(echo "$Text" | tr ' ' '_')
+Text=${Text// /_}
while :
do
printf "%-.${WIDTH}s\r" "$Text"
- LastC=$(expr "$Text" : '.*\(.\)$')
- Text=$(printf "%-.${WIDTH}s" "$LastC$Text")
+ LastC=${Text:${WMINUS}:1}
+ Text="$LastC""${Text%?}"
done
diff --git a/examples/scripts/scrollbar2 b/examples/scripts/scrollbar2
new file mode 100755
index 00000000..0e536341
--- /dev/null
+++ b/examples/scripts/scrollbar2
@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+# scrollbar - display scrolling text
+#
+# usage: scrollbar args
+#
+# A cute hack originally from Heiner Steven <hs@bintec.de>
+#
+# converted from ksh syntax to bash v2 syntax by Chet Ramey
+
+WIDTH=${COLUMNS:-80}
+WMINUS=$(( $WIDTH - 1 ))
+
+[ $# -lt 1 ] && set -- TESTING
+
+# use the bash-2.02 printf builtin
+Text=$(printf "%-${WIDTH}s" "$*")
+
+while :
+do
+ printf "%-.${WIDTH}s\r" "$Text"
+ LastC=${Text:${WMINUS}:1}
+ Text="$LastC""${Text%?}"
+done
diff --git a/examples/scripts/showperm.bash b/examples/scripts/showperm.bash
new file mode 100644
index 00000000..2e06c0bc
--- /dev/null
+++ b/examples/scripts/showperm.bash
@@ -0,0 +1,53 @@
+#Newsgroups: comp.unix.shell
+#From: gwc@root.co.uk (Geoff Clare)
+#Subject: Re: Determining permissions on a file
+#Message-ID: <Dr79nw.DtL@root.co.uk>
+#Date: Fri, 10 May 1996 17:23:56 GMT
+
+#Here's a bit of Korn shell that converts the symbolic permissions produced
+#by "ls -l" into octal, using only shell builtins. How to create a script
+#combining this with an "ls -l" is left as an exercise...
+#
+#
+# Converted to Bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
+#
+# usage: showperm modestring
+#
+# example: showperm '-rwsr-x--x'
+#
+
+[ -z "$1" ] && {
+ echo "showperm: usage: showperm modestring" >&2
+ exit 2
+}
+
+tmode="$1"
+
+typeset -i omode sbits
+typeset pmode
+
+# check for set-uid, etc. bits
+sbits=0
+case $tmode in
+???[sS]*) (( sbits += 8#4000 )) ;; # set-uid
+??????[sSl]*) (( sbits += 8#2000 )) ;; # set-gid or mand. lock
+?????????[tT]*) (( sbits += 8#1000 )) ;; # sticky
+esac
+
+omode=0
+while :
+do
+ tmode=${tmode#?}
+ case $tmode in
+ "") break ;;
+ [-STl]*) (( omode *= 2 )) ;;
+ [rwxst]*) (( omode = omode*2 + 1 )) ;;
+ *) echo "$0: first letter of \"$tmode\" is unrecognized" >&2
+ (( omode *= 2 ))
+ ;;
+ esac
+done
+
+(( omode += sbits ))
+
+printf "0%o\n" $omode
diff --git a/examples/scripts/timeout b/examples/scripts/timeout
new file mode 100644
index 00000000..ac8d88f4
--- /dev/null
+++ b/examples/scripts/timeout
@@ -0,0 +1,53 @@
+#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
+#From: gwc@root.co.uk (Geoff Clare)
+#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?)
+#Message-ID: <EoBxrs.223@root.co.uk>
+#Date: Fri, 13 Feb 1998 18:23:52 GMT
+
+#
+# Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu
+# UNTESTED
+#
+
+prog=${0##*/}
+usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"
+
+SIG=-TERM # default signal sent to the process when the timer expires
+timeout=60 # default timeout
+interval=15 # default interval between checks if the process is still alive
+delay=2 # default delay between posting the given signal and
+ # destroying the process (kill -KILL)
+
+while :
+do
+ case $1 in
+ --) shift; break ;;
+ -*) SIG=$1 ;;
+ [0-9]*) timeout=$1 ;;
+ :*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
+ +*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
+ *) break ;;
+ esac
+ shift
+done
+
+case $# in
+0) echo "$prog: $usage" >&2 ; exit 2 ;;
+esac
+
+(
+ for t in $timeout $delay
+ do
+ while (( $t > $interval ))
+ do
+ sleep $interval
+ kill -0 $$ || exit
+ t=$(( $t - $interval ))
+ done
+ sleep $t
+ kill $SIG $$ && kill -0 $$ || exit
+ SIG=-KILL
+ done
+) 2> /dev/null &
+
+exec "$@"
diff --git a/examples/startup-files/Bashrc b/examples/startup-files/Bashrc.bfox
index efe7d88b..efe7d88b 100644
--- a/examples/startup-files/Bashrc
+++ b/examples/startup-files/Bashrc.bfox