4

Why is it that sb-posix:chdir doesn't seem to change (truename ".") nor (load ...)'s idea of cwd?

CL-USER> (sb-posix:getcwd)
"directory-B"
CL-USER> (sb-posix:chdir "directory-A")
0
CL-USER> (sb-posix:getcwd)
"directory-A"
CL-USER> (truename ".")
#P"directory-B"
CL-USER> (sb-posix:chdir "/tmp")
0
CL-USER> (truename ".")
#P"directory-B"
CL-USER> (load "some-file-under-dirA.asd")
; Evaluation aborted on #<SB-INT:SIMPLE-FILE-ERROR "~@<Couldn't load ~S: file does not exist.~@:>" {CE86631}>.
ealfonso
  • 6,622
  • 5
  • 39
  • 67

1 Answers1

5

There is a little advertisment on the sb-posix repository:

A few functions in sb-posix don't correspond directly to their C
counterparts.

So let's take a look on getcwd:

in C from here

The getcwd() function shall place an absolute pathname of the current working directory in the array pointed to by buf, and return buf. The pathname copied to the array shall contain no components that are symbolic links. The size argument is the size in bytes of the character array pointed to by the buf argument. If buf is a null pointer, the behavior of getcwd() is unspecified.

and if we get the help of this function in slime or from the repository :

"Returns the process's current working directory as a string."

The problem here is that SBCL current directoy is fixed in the special variable:

*default-pathname-defaults*

This is the reason why when you call truename and "." the SBCL shows you the current directory inside that variable which is not getting affected by the call sb-posix:chdir that really changes the sb-posix environment or the posix process. If you want to change also the SBCL directory (in slime you can use , cd and then type the new directory), you can go as follow but be careful because you are modifying a special variable that contains a pathname and for example it will not work well with slime (the command that I allready showed):

╭─anquegi@toshiba-debian  ~/learn/lisp/StackOverFlow/testDirs ‹ruby-2.2.1@laguna› 
╰─$ tree                                                                                                                                        148 ↵
.
├── dirA
└── dirB
    └── factorial.lisp

2 directories, 1 file

Then let's got to slime

; SLIME 2015-06-01
CL-USER> (sb-posix:getcwd)
"/home/anquegi/learn/lisp/StackOverFlow/testDirs"
CL-USER> *default-pathname-defaults*
#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/"
CL-USER> (sb-posix:chdir "dirB")
0
CL-USER> (sb-posix:getcwd)
"/home/anquegi/learn/lisp/StackOverFlow/testDirs/dirB"
CL-USER> *default-pathname-defaults*
#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/"
CL-USER> (setf *default-pathname-defaults* (sb-ext:native-pathname (format nil "~A~A" (sb-posix:getcwd) "/"))) 
#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/dirB/"
CL-USER> (load "factorial")
T
CL-USER> (factorial 3)
6

Please take care of transforming the string in a pathname and add the "/", I'm not sure why SBCL took this way of working. separating sb-posix from sbcl paths

anquegi
  • 11,125
  • 4
  • 51
  • 67