Skip to content

Add a command to obtain the current directory #263

Description

@rschmitt

Is your feature request related to a problem? Please describe.

I'm not aware of a command that displays the current subdirectory for a given unit. I have to infer it from the output of DIR, or discover that I'm in the wrong directory when a MOUNT or LOAD command fails.

Describe the solution you'd like

One obvious idea is a GETCWD command that takes an optional unit number and returns a path:

GETCWD U12
/roms

Outside of direct mode, things are trickier. A built-in function like

PRINT CWD$(12)
/roms

would only be useful for displaying full paths to the user; since there's no existing concept of a "path," this function would compose poorly with the existing APIs. Every call site would have to write string parsing code in order to implement typical functionality like iterating over path segments, getting the name of the parent directory, or obtaining the depth of the current directory.

One possibility is to expose the current directories through read-only built-in arrays:

CWD%(unit)        : REM Number of path components
CWD$(unit, index) : REM Path components

If unit 12's current directory is /roms, then:

CWD%(12) = 1
CWD$(12, 0) = "roms"
CWD$(12, 1) = ""

Some examples:

10 REM Print the current directory name
20 N%=CWD%(12)
30 IF N%=0 THEN PRINT "/" ELSE PRINT CWD$(12,N%-1)
10 REM Check whether the current directory is root
20 IF CWD%(12)=0 THEN PRINT "IN ROOT" : REM Equivalent to CWD$(12,0)=""
10 REM Ancestor search
20 FOR I%=CWD%(12)-1 TO 0 STEP -1
30   IF CWD$(12,I%)="PROJECTS" THEN 100
40 NEXT I%
50 PRINT "PROJECTS NOT FOUND":END
100 PRINT "FOUND AT LEVEL";I%
10 REM Navigate to `/` on the SD card
20 DO WHILE CWD%(12)>0
30   CHDIR "..",U12
40 LOOP

Describe alternatives you've considered

Instead of a built-in array, a built-in function could be provided that populates a caller-allocated array with path segments.

A direct mode LISTCWD command to show the current directory for each unit might be handy, for example when preparing to COPY something.

Support for path strings or path segment arrays could be ret-conned into existing commands (which is a much bigger change).

Additional context

According to the documentation, the getcwd hypervisor trap is not implemented.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions