Module Name: src Committed By: apb Date: Mon Dec 17 18:17:27 UTC 2012
Modified Files: src/etc: rc.subr src/share/man/man8: rc.subr.8 Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.91 -r1.92 src/etc/rc.subr cvs rdiff -u -r1.29 -r1.30 src/share/man/man8/rc.subr.8 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/etc/rc.subr diff -u src/etc/rc.subr:1.91 src/etc/rc.subr:1.92 --- src/etc/rc.subr:1.91 Sat Apr 7 17:28:44 2012 +++ src/etc/rc.subr Mon Dec 17 18:17:27 2012 @@ -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 @@ collapse_backslash_newline() 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. Index: src/share/man/man8/rc.subr.8 diff -u src/share/man/man8/rc.subr.8:1.29 src/share/man/man8/rc.subr.8:1.30 --- src/share/man/man8/rc.subr.8:1.29 Thu Aug 11 22:52:46 2011 +++ src/share/man/man8/rc.subr.8 Mon Dec 17 18:17:27 2012 @@ -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 @@ otherwise 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 @@ is handled as per 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 ,