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.
This commit is contained in:
apb 2012-12-17 18:17:27 +00:00
parent 7dd382bcbb
commit 532fa0d9ae
2 changed files with 46 additions and 2 deletions

View File

@ -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.

View File

@ -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 ,