(sb-posix:chdir ) 和 (load)

(sb-posix:chdir ) and (load)

为什么 sb-posix:chdir 似乎没有改变 (truename ".")(load ...) 的 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}>.

sb上有点广告-posix repository:

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

那么让我们来看看getcwd:

C 来自 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.

如果我们在 slime 中或从 repository 中获得此功能的帮助:

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

这里的问题是SBCL当前目录在特殊变量中是固定的:

*default-pathname-defaults*

这就是为什么当你调用 truename 和 "." 时的原因。 SBCL 向您显示该变量内的当前目录,该目录不受真正改变 sb-posix 环境或 posix 进程的调用 sb-posix:chdir 的影响。如果你还想更改 SBCL 目录(在 slime 中你可以使用 , cd 然后键入新目录),你可以按照以下步骤进行但要小心,因为你正在修改一个包含路径名的特殊变量,例如它不会与粘液一起使用(我已经展示的命令):

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

2 directories, 1 file

然后让我们开始粘液

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

请注意转换路径名中的字符串并添加“/”,我不确定为什么 SBCL 采用这种工作方式。将 sb-posix 与 sbcl 路径

分开