System Functions

In this chapter we are going to learn about the system functions

  • System()

  • SysGet()

  • SysSet()

  • SysUnset()

  • IsMSDOS()

  • IsWindows()

  • IsWindows64()

  • IsUnix()

  • IsMacOSX()

  • IsLinux()

  • IsFreeBSD()

  • IsAndroid()

  • GetArch()

  • Windowsnl()

  • Get Command Line Arguments

  • Get Active Source File Name

  • CurrentDir()

  • ExeFileName()

  • ChDir()

  • ExeFolder()

  • Version()

  • Shutdown()

  • NofProcessors()

System() Function

We can execute system commands using the system() function

Syntax:

System(cCommand)

Example:

System("myapp.exe")     # Run myapp.exe
System("ls")            # print list of files

SysGet() Function

We can get environment variables using the Get() function

Syntax:

SysGet(cVariable)

Example:

see sysget("path")              # print system path information

SysSet() Function

We can set environment variables using the SysSet() function

SysSet(cVariable, cValue) ---> Returns 1 for success and return 0 for failure

SysUnset() Function

We can delete an environment variables using the SysUnset() function

SysUnset(cVariable) ---> Returns 1 for success and return 0 for failure

IsMSDOS() Function

We can check if the operating system is MSDOS or not using the IsMSDOS() function

Syntax:

IsMSDOS() ---> Returns 1 if the operating system is MS-DOS, Returns 0 if it's not

IsWindows() Function

We can check if the operating system is Windows or not using the IsWindows() function

Syntax:

IsWindows() ---> Returns 1 if the operating system is Windows, Returns 0 if it's not

IsWindows64() Function

We can check if the operating system is Windows 64bit or not using the IsWindows64() function

Syntax:

IsWindows64() ---> Returns 1 if the operating system is Windows64, Returns 0 if it's not

IsUnix() Function

We can check if the operating system is Unix or not using the IsUnix() function

Syntax:

IsUnix() ---> Returns 1 if the operating system is Unix, Returns 0 if it's not

IsMacOSX() Function

We can check if the operating system is macOS or not using the IsMacOSX() function

Syntax:

IsMacOSX() ---> Returns 1 if the operating system is Mac OS X, Returns 0 if it's not

IsLinux() Function

We can check if the operating system is Linux or not using the IsLinux() function

Syntax:

IsLinux() ---> Returns 1 if the operating system is Linux, Returns 0 if it's not

IsFreeBSD() Function

We can check if the operating system is FreeBSD or not using the IsFreeBSD() function

Syntax:

IsFreeBSD() ---> Returns 1 if the operating system is FreeBSD, Returns 0 if it's not

IsAndroid() Function

We can check if the operating system is Android or not using the IsAndroid() function

Syntax:

IsAndroid() ---> Returns 1 if the operating system is Android, Returns 0 if it's not

Example

see "IsMSDOS()     --> " + ismsdos()     + nl
see "IsWindows()   --> " + iswindows()   + nl
see "IsWindows64() --> " + iswindows64() + nl
see "IsUnix()      --> " + isunix()      + nl
see "IsMacOSX()    --> " + ismacosx()    + nl
see "IsLinux()     --> " + islinux()     + nl
see "IsFreeBSD()   --> " + isfreebsd()   + nl
see "IsAndroid()   --> " + isandroid()   + nl

Output:

IsMSDOS()     --> 0
IsWindows()   --> 1
IsWindows64() --> 0
IsUnix()      --> 0
IsMacOSX()    --> 0
IsLinux()     --> 0
IsFreeBSD()   --> 0
IsAndroid()   --> 0

GetArch() Function

We can detect the architecture of the Ring executable using the GetArch() function

Syntax:

GetArch() ---> cString (The name of the architecture of the Ring executable)

Possible values are:

x86
x64
arm64
arm
unknown

Example:

switch getarch()
on "x86"
        ? "x86 32bit architecture"
on "x64"
        ? "x64 64bit architecture"
on "arm64"
        ? "ARM64 64bit architecture"
on "arm"
        ? "ARM 32bit architecture"
other
        ? "Unknown architecture"
off

Windowsnl() Function

We can get the windows new line string using the Windowsnl() function.

Syntax:

WindowsNL() ---> Returns a string contains CR+LF = CHAR(13) + CHAR(10)

Example:

cStr = read("input.txt")

if iswindows()
        cStr = substr(cStr,windowsnl(),nl)
ok

aList = str2list(cStr)
# to do - list items processing using "for in"
cStr = list2str(aList)

if iswindows()
        cStr = substr(cStr,nl,windowsnl())
ok

write("output.txt",cStr)

Get Command Line Arguments

We can get the command line arguments passed to the ring script using the sysargv variable.

The sysargv variable is a list contains the command line parameters.

Example

see copy("=",30) + nl
see "Command Line Parameters" + nl
see "Size : " + len(sysargv) + nl
see sysargv
see copy("=",30) + nl
if len(sysargv) < 4 return ok
nStart = sysargv[3]
nEnd = sysargv[4]
for x = nStart to nEnd
        see x + nl
next

Output

b:\mahmoud\apps\ring>ring tests\syspara.ring 1 10
==============================
Command Line Parameters
Size : 4
ring
tests\syspara.ring
1
10
==============================
1
2
3
4
5
6
7
8
9
10

Get Active Source File Name

We can get the active source file name (*.ring) using the filename() function

Syntax:

filename() ---> String contains the active source file name.

Example:

see "Active Source File Name : " + filename() + nl

Output:

Active Source File Name : tests\filename.ring

Example:

if sysargv[2] = filename()
        see "I'm the main program file!" + nl
        # we can run tests here!
else
        see "I'm a sub file in a program" + nl
ok

PrevFileName() Function

Using the PrevFileName() function we can get the previous active source file name.

The previous file would be the file of the caller function, Or the file of the function that we called before calling PrevFileName().

Syntax:

prevfilename() ---> String contains the previous source file name.

Example:

The next function in stdlib.ring uses the PrevFileName() to know if the file of the caller function is the main source file of the program or not.

Func IsMainSourceFile
        if PrevFileName() = sysargv[2]
                return true
        ok
        return false

CurrentDir() Function

Return the path of the current directory

Syntax:

CurrentDir() ---> String contains the path of the current directory

ExeFileName() Function

Return the Ring executable file name

Syntax:

exefilename() ---> String contains the Ring executable file name

ChDir() Function

Change the current directory

Syntax:

ChDir(cNewPath)

ExeFolder() Function

Return the Ring executable file path

Syntax:

exefolder() ---> String contains the Ring executable path

Version() Function

Return the Ring version

Syntax:

version() ---> String contains the Ring version

Output:

1.20

Shutdown() Function

Close the application

Syntax:

shutdown(nStatus) ---> Close the application

NofProcessors() Function

We can detect the number of processors using the NofProcessors() Function

Syntax:

NofProcessors() ---> nProcessors

Example:

? NofProcessors()