SysLib — System Library

System-level functions for interacting with the operating system, file system, and environment. Designed for cross-platform applications running on Windows, macOS, Linux, iOS, and Android.

CategoryCountDescription
Command Line2paramcount, paramstr$
File Operations2fileexists, kill
Directory Operations4chdir, mkdir, rmdir, forcedirectories
Path Manipulation4extractfilename$, extractfilepath$, extractfileext$, changefileext$
System Paths7homepath$, documentspath$, temppath$, cachepath$, publicpath$, librarypath$, shareddocumentspath$
Media Paths10picturespath$, musicpath$, moviespath$, downloadspath$, camerapath$ (+ shared variants)
Mobile Paths4alarmspath$, ringtonespath$ (+ shared variants)
File Name Generation3randomfilename$, guidfilename$, tempfilename$
Path Separators3dirseparator$, altseparator$, pathseparator$
Environment1environ$
Colors3color, alphacolor, colortostr$

Command-Line Functions

FunctionSignatureDescription
paramcount()paramcount@Number of command-line arguments (excluding program name)
paramstr$(index)paramstr$@nGet argument at index (0 = program path, 1+ = args)
╯ plan9basic
' If called: myapp.exe file.txt -verbose
println "Program: "; paramstr$(0)
println "Arg 1: "; paramstr$(1)      ' "file.txt"
println "Arg 2: "; paramstr$(2)      ' "-verbose"
println "Total args: "; paramcount()

File Operations

FunctionSignatureDescription
fileexists(file$, followLinks)fileexists@$nCheck if file exists (1=yes, 0=no)
kill(file$)kill@$Delete a file (1=success, 0=fail)
╯ plan9basic
if fileexists("config.txt", 0) = 1 then
    println "Config file found"
endif

' Follow symbolic links
if fileexists("link_to_file", 1) = 1 then
    println "Target exists"
endif

' Delete a file
if kill("temp.dat") = 1 then
    println "File deleted"
else
    println "Delete failed"
endif
⚠ Warning: kill() permanently deletes the file. It is not sent to the Recycle Bin / Trash.

Directory Operations

FunctionSignatureDescription
chdir(path$)chdir@$Change current working directory
mkdir(path$)mkdir@$Create a new directory (parent must exist)
rmdir(path$)rmdir@$Remove an empty directory
forcedirectories(path$)forcedirectories@$Create full directory path including parents (1=success, 0=fail)
╯ plan9basic
' Change to documents folder
chdir(documentspath$())

' Create a single directory
mkdir("NewFolder")

' Create nested directories (all at once)
path$ = documentspath$() + dirseparator$() + "Projects" + dirseparator$() + "MyApp" + dirseparator$() + "Data"
if forcedirectories(path$) = 1 then
    println "Directory structure created"
endif

' Remove an empty directory
rmdir("EmptyFolder")
ⓘ Note: mkdir() requires parent directories to already exist. Use forcedirectories() to create the entire path at once.

Path Manipulation

FunctionSignatureDescription
extractfilename$(path$)extractfilename$@$File name with extension from full path
extractfilepath$(path$)extractfilepath$@$Directory portion from full path
extractfileext$(file$)extractfileext$@$Extension including the dot
changefileext$(file$, ext$)changefileext$@$$Change file extension
╯ plan9basic
path$ = "/home/user/documents/report.pdf"

println extractfilename$(path$)    ' report.pdf
println extractfilepath$(path$)    ' /home/user/documents/
println extractfileext$(path$)     ' .pdf

println changefileext$(path$, ".bak")  ' /home/user/documents/report.bak

println extractfileext$("README")  ' (empty string)

System Path Functions

Platform-specific standard directories. Returns appropriate paths on each OS.

FunctionSignatureDescription
homepath$()homepath$@User home directory
documentspath$()documentspath$@User documents folder
shareddocumentspath$()shareddocumentspath$@Shared documents folder
temppath$()temppath$@Temporary files directory
cachepath$()cachepath$@Application cache directory
publicpath$()publicpath$@Public/shared storage
librarypath$()librarypath$@Application library/support path
FunctionWindowsmacOSLinux
homepath$()C:\Users\Name/Users/Name/home/name
documentspath$()...\Documents.../Documents.../Documents
temppath$()%TEMP%/tmp/tmp
cachepath$()App-specificApp-specificApp-specific
librarypath$()App-specific~/LibraryApp-specific
╯ plan9basic
println "Home:      "; homepath$()
println "Documents: "; documentspath$()
println "Temp:      "; temppath$()
println "Cache:     "; cachepath$()
println "Library:   "; librarypath$()

Media Paths

Each media path has a user-specific and a shared variant.

FunctionSignatureDescription
picturespath$()picturespath$@User pictures folder
sharedpicturespath$()sharedpicturespath$@Shared pictures folder
musicpath$()musicpath$@User music folder
sharedmusicpath$()sharedmusicpath$@Shared music folder
moviespath$()moviespath$@User movies/videos folder
sharedmoviespath$()sharedmoviespath$@Shared movies folder
downloadspath$()downloadspath$@User downloads folder
shareddownloadspath$()shareddownloadspath$@Shared downloads folder
camerapath$()camerapath$@Camera roll / photos folder
sharedcamerapath$()sharedcamerapath$@Shared camera folder
╯ plan9basic
println "Pictures:  "; picturespath$()
println "Music:     "; musicpath$()
println "Movies:    "; moviespath$()
println "Downloads: "; downloadspath$()
println "Camera:    "; camerapath$()
ⓘ Note: camerapath$() and sharedcamerapath$() are primarily useful on mobile platforms (iOS / Android).

Mobile-Specific Paths

FunctionSignatureDescription
alarmspath$()alarmspath$@Alarms sound folder
sharedalarmspath$()sharedalarmspath$@Shared alarms folder
ringtonespath$()ringtonespath$@Ringtones folder
sharedringtonespath$()sharedringtonespath$@Shared ringtones folder
ⓘ Note: These paths are primarily relevant on Android. On desktop platforms they may return empty or app-specific paths.

File Name Generation

FunctionSignatureDescription
randomfilename$()randomfilename$@Random file name (e.g. tmpA1B2C.tmp)
guidfilename$(useSep)guidfilename$@nGUID-based unique name (1=with hyphens, 0=without)
tempfilename$()tempfilename$@Create temp file and return its full path
╯ plan9basic
println randomfilename$()     ' e.g. "tmpA1B2C.tmp"

println guidfilename$(1)      ' {A1B2C3D4-E5F6-7890-...}
println guidfilename$(0)      ' {A1B2C3D4E5F67890...}

' tempfilename$ actually creates the file!
tempFile$ = tempfilename$()
println "Temp file: "; tempFile$

' Clean up when done
kill(tempFile$)
⚠ Warning: tempfilename$() actually creates the file on disk. Remember to delete it with kill() when done.

Path Separator Functions

FunctionSignatureDescription
dirseparator$()dirseparator$@Directory separator (\ on Windows, / on others)
altseparator$()altseparator$@Alternate separator (/ on Windows, same as dirseparator$ elsewhere)
pathseparator$()pathseparator$@PATH list separator (; on Windows, : on others)
FunctionWindowsmacOS / Linux
dirseparator$()\/
altseparator$()//
pathseparator$();:
╯ plan9basic
' Always build paths with dirseparator$()
path$ = documentspath$() + dirseparator$() + "MyApp" + dirseparator$() + "data.txt"
println path$
ⓘ Note: Always use dirseparator$() when building paths. Never hardcode \ or / if you want cross-platform compatibility.

Environment Variables

FunctionSignatureDescription
environ$(name$)environ$@$Get environment variable value (empty string if not found)
VariablePlatformDescription
USERNAMEWindowsCurrent user name
USERLinux / macOSCurrent user name
HOMELinux / macOSHome directory
USERPROFILEWindowsUser profile directory
PATHAllExecutable search paths
TEMP / TMPWindowsTemporary directory
TMPDIRmacOS / LinuxTemporary directory
╯ plan9basic
' Cross-platform user name
user$ = environ$("USERNAME")
if len(user$) = 0 then
    user$ = environ$("USER")
endif
println "User: "; user$

' Cross-platform home
home$ = environ$("HOME")
if len(home$) = 0 then
    home$ = environ$("USERPROFILE")
endif
println "Home: "; home$

Color Functions

FunctionSignatureDescription
color(name$)color@$Color name or hex string to numeric value
alphacolor(name$)alphacolor@$Color with alpha channel to numeric value
colortostr$(n)colortostr$@nNumeric color value back to string
╯ plan9basic
' Named colors
c = color("Red")
c = color("clBlue")
c = color("$FF0000")     ' Hex format

' With alpha
c = alphacolor("Red")
c = alphacolor("#80FF0000")  ' Semi-transparent red

' Convert back to string
c = color("Navy")
println "Navy: "; c
println "Name: "; colortostr$(c)
ⓘ Note: Supported color names include: Black, Maroon, Green, Olive, Navy, Purple, Teal, Gray, Silver, Red, Lime, Yellow, Blue, Fuchsia, Aqua, White, and many more. Hex formats $RRGGBB and #RRGGBB are also accepted.

Complete Examples

Cross-Platform Path Builder

╯ pathbuilder.bas
' Build paths that work on any platform
function buildPath$(base$, parts$) local result$, sep$
    sep$ = dirseparator$()
    result$ = base$
    if right$(result$) = sep$ then
        result$ = left$(result$, len(result$) - 1)
    endif
    result$ = result$ + sep$ + parts$
    return result$
endfunction

println "Home: "; homepath$()
println "Documents: "; documentspath$()
println "Temp: "; temppath$()

appData$ = buildPath$(documentspath$(), "MyApp")
println "App data: "; appData$

configFile$ = buildPath$(appData$, "config.txt")
println "Config: "; configFile$

Command-Line Argument Processor

╯ cmdline.bas
' Process command-line arguments
println "Program: "; paramstr$(0)
println "Args: "; paramcount()

if paramcount() > 0 then
    for i = 1 to paramcount()
        arg$ = paramstr$(i)
        print "  ["; i; "] "; arg$
        if left$(arg$) = "-" then
            println " (flag)"
        else
            println ""
        endif
    next
else
    println "No arguments provided."
    println "Usage: program [options] [files...]"
endif

File Manager Operations

╯ filemgr.bas
' File management utilities
function createAppFolder$() local path$
    path$ = documentspath$() + dirseparator$() + "Plan9BasicApp"
    if forcedirectories(path$) = 1 then
        return path$
    else
        return ""
    endif
endfunction

function fileInfo$(filename$) local info$
    info$ = "File: " + extractfilename$(filename$) + chr$(10)
    info$ = info$ + "Path: " + extractfilepath$(filename$) + chr$(10)
    info$ = info$ + "Ext:  " + extractfileext$(filename$) + chr$(10)
    info$ = info$ + "Exists: " + str$(fileexists(filename$, 0))
    return info$
endfunction

appFolder$ = createAppFolder$()
println "App folder: "; appFolder$

testPath$ = documentspath$() + dirseparator$() + "test.txt"
println fileInfo$(testPath$)
println "As .bak: "; changefileext$(testPath$, ".bak")

Temporary File Handling

╯ tempfiles.bas
' Working with temporary files
println "Temp dir: "; temppath$()
println ""

println "Random names:"
for i = 1 to 3
    println "  "; randomfilename$()
next
println ""

println "GUID names:"
println "  With sep: "; guidfilename$(1)
println "  No sep:   "; guidfilename$(0)
println ""

tempFile$ = tempfilename$()
println "Created: "; tempFile$

if fileexists(tempFile$, 0) = 1 then
    println "Exists: Yes"
    if kill(tempFile$) = 1 then
        println "Deleted OK"
    endif
endif

Environment Information

╯ envinfo.bas
' System environment overview
user$ = environ$("USERNAME")
if len(user$) = 0 then
    user$ = environ$("USER")
endif
println "User: "; user$
println "Home (func): "; homepath$()
println ""

println "Separator: ["; dirseparator$(); "]"
println "Path sep:  ["; pathseparator$(); "]"
println ""

println "System Paths:"
println "  Documents:  "; documentspath$()
println "  Pictures:   "; picturespath$()
println "  Music:      "; musicpath$()
println "  Downloads:  "; downloadspath$()
println "  Temp:       "; temppath$()
println "  Cache:      "; cachepath$()

Platform Notes

PlatformDir SeparatorPath SeparatorUser Env VarNotes
Windows\;USERNAMEPaths under C:\Users\Name\
macOS/:USERLibrary at ~/Library/
Linux/:USERFollows XDG Base Directory Spec
iOS/Sandboxed app containers
Android/Private storage; shared paths need permissions

Cross-Platform Tips

  1. Always use dirseparator$() when building paths
  2. Use system path functions instead of hardcoded paths
  3. Check fileexists() before file operations
  4. Use forcedirectories() to create nested folders
  5. Clean up temporary files created with tempfilename$()

Quick Reference

FunctionSignatureDescription
paramcount() / paramstr$(n)*@[n]Command-line arguments
fileexists(f$, follow)fileexists@$nCheck file exists (1/0)
kill(f$)kill@$Delete file (1=success)
chdir(p$) / mkdir(p$) / rmdir(p$)*@$Directory operations
forcedirectories(p$)forcedirectories@$Create full path (1=success)
extractfilename$(p$)extractfilename$@$Filename from path
extractfilepath$(p$)extractfilepath$@$Directory from path
extractfileext$(f$)extractfileext$@$Extension (with dot)
changefileext$(f$, e$)changefileext$@$$Change extension
homepath$() / documentspath$()*$@Core system paths
temppath$() / cachepath$()*$@Temp and cache paths
picturespath$() / musicpath$()*$@Media paths
moviespath$() / downloadspath$()*$@Media paths
camerapath$() / alarmspath$() / ringtonespath$()*$@Mobile paths
randomfilename$()randomfilename$@Random file name
guidfilename$(sep)guidfilename$@nGUID-based name
tempfilename$()tempfilename$@Create temp file, return path
dirseparator$() / pathseparator$()*$@Platform separators
environ$(name$)environ$@$Environment variable
color(s$) / alphacolor(s$)*@$Color name/hex to number
colortostr$(n)colortostr$@nNumber to color string

43 functions across 11 categories.