Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add shell implementations of basename and dirname to rc.subr...



details:   https://anonhg.NetBSD.org/src/rev/85bfee60d3dd
branches:  trunk
changeset: 783376:85bfee60d3dd
user:      apb <apb%NetBSD.org@localhost>
date:      Mon Dec 17 18:17:27 2012 +0000

description:
Add shell implementations of basename and dirname to rc.subr.  They
are supposed to mimic basename(1) and dirname(1), except that they
are usable before /usr/bin is mounted.

diffstat:

 etc/rc.subr              |  30 +++++++++++++++++++++++++++++-
 share/man/man8/rc.subr.8 |  18 +++++++++++++++++-
 2 files changed, 46 insertions(+), 2 deletions(-)

diffs (97 lines):

diff -r ca93d46017a1 -r 85bfee60d3dd etc/rc.subr
--- a/etc/rc.subr       Mon Dec 17 17:46:27 2012 +0000
+++ b/etc/rc.subr       Mon Dec 17 18:17:27 2012 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: rc.subr,v 1.91 2012/04/07 17:28:44 christos Exp $
+# $NetBSD: rc.subr,v 1.92 2012/12/17 18:17:27 apb Exp $
 #
 # Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -1256,6 +1256,34 @@
        done
 }
 
+# Shell implementations of basename and dirname, usable before
+# the /usr file system is mounted.
+#
+basename()
+{
+       local file="$1"
+       local suffix="$2"
+       local base
+
+       base="${file##*/}"              # remove up to and including last '/'
+       base="${base%${suffix}}"        # remove suffix, if any
+       command printf "%s\n" "${base}"
+}
+
+dirname()
+{
+       local file="$1"
+       local dir
+
+       case "$file" in
+       /*/*)   dir="${file%/*}" ;;     # common case: absolute path
+       /*)     dir="/" ;;              # special case: name in root dir
+       */*)    dir="${file%/*}" ;;     # common case: relative path with '/'
+       *)      dir="." ;;              # special case: name without '/'
+       esac
+       command printf "%s\n" "${dir}"
+}
+
 # Override the normal "echo" and "printf" commands, so that
 # partial lines printed by rc.d scripts appear immediately,
 # instead of being buffered by rc(8)'s post-processor.
diff -r ca93d46017a1 -r 85bfee60d3dd share/man/man8/rc.subr.8
--- a/share/man/man8/rc.subr.8  Mon Dec 17 17:46:27 2012 +0000
+++ b/share/man/man8/rc.subr.8  Mon Dec 17 18:17:27 2012 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: rc.subr.8,v 1.29 2011/08/11 22:52:46 apb Exp $
+.\"    $NetBSD: rc.subr.8,v 1.30 2012/12/17 18:17:27 apb Exp $
 .\"
 .\" Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -40,6 +40,8 @@
 .It
 .Ic backup_file Ar action Ar file Ar current Ar backup
 .It
+.Ic basename Ar file Op Ar suffix
+.It
 .Ic checkyesno Ar var
 .It
 .Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter
@@ -48,6 +50,8 @@
 .It
 .Ic collapse_backslash_newline
 .It
+.Ic dirname Ar file
+.It
 .Ic err Ar exitval Ar message
 .It
 .Ic load_rc_config Ar command
@@ -157,6 +161,12 @@
 is moved to
 .Ar backup .
 .El
+.Ic basename Ar file Op Ar suffix
+Just like
+.Xr basename 1 ,
+except implemented using shell built-in commands, and usable before the 
+.Pa /usr/bin
+direcory is available.
 .It Ic checkyesno Ar var
 Return 0 if
 .Ar var
@@ -214,6 +224,12 @@
 Copy input to output, collapsing
 .Ao backslash Ac Ns Ao newline Ac
 to nothing, but leaving other backslashes alone.
+.Ic dirname Ar file
+Just like
+.Xr dirname 1 ,
+except implemented using shell built-in commands, and usable before the 
+.Pa /usr/bin
+direcory is available.
 .It Ic err Ar exitval Ar message
 Display an error message to
 .Em stderr ,



Home | Main Index | Thread Index | Old Index