IsRoot - check whether path results in root
From NSIS Wiki
Jump to navigationJump to search
Notes
This script has not yet seen peer review and may contain errors. Due to the intended purpose of this script, peer review and comments is desired.
Description
An NSIS script function that is really a collection of multiple functions, used to sanity-check a given path.
This can be used to check paths to make sure you're not poking around at the drive's root folder.
Function posted in response to post in: http://forums.winamp.com/showthread.php?threadid=230599
Usage
Push <path> Call isRoot Pop <result:"true"|"false"> # "true" = root | "false" = not root
Examples
Push "" Call isRoot Pop $0 # $0 = "true" Push " " Call isRoot Pop $0 # $0 = "true" Push "c:" Call isRoot Pop $0 # $0 = "true" Push "c:\" Call isRoot Pop $0 # $0 = "true" Push "c:\test" Call isRoot Pop $0 # $0 = "false" Push "c:\test\" Call isRoot Pop $0 # $0 = "false"
The Script
Function isRoot Exch $0 # get rid of leading/trailing spaces Push $1 _loop_left: StrCpy $1 "$0" 1 StrCmp "$1" " " _left goto _loop_right _left: StrCpy $0 "$0" "" 1 goto _loop_left _loop_right: StrCpy $1 "$0" 1 -1 StrCmp "$1" " " _right goto _loop_end _right: StrCpy $0 "$0" -1 goto _loop_right _loop_end: Pop $1 # check whether the path is (now) an empty string StrCmp $0 "" _root # get rid of any trailing backslashes. If the path were "c:\", it'll now be "c:" Push $0 Exch $EXEDIR Exch $EXEDIR Pop $0 # check whether the last char is a colon. If it is, then we're looking at a root dir. StrCpy $0 $0 1 -1 StrCmp $0 ":" _root _not_root _root: StrCpy $0 "true" goto _end _not_root: StrCpy $0 "false" goto _end _end: Exch $0 FunctionEnd