(sb-posix:chdir ) 和 (加载)

eal*_*nso 4 sbcl common-lisp

为什么它似乎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}>.
Run Code Online (Sandbox Code Playgroud)

anq*_*egi 5

sb-posix 上有一个小广告sb-posix存储库

\n\n
A few functions in sb-posix don\'t correspond directly to their C\ncounterparts.\n
Run Code Online (Sandbox Code Playgroud)\n\n

那么让我们看一下 getcwd:

\n\n

在 C 中从从这里

\n\n
\n

getcwd() 函数将当前工作目录的绝对路径名放入 buf 指向的数组中,并返回 buf。复制到数组的路径名不应包含符号链接的组件。size 参数是 buf 参数指向的\n 字符数组的大小(以字节为单位)。如果 buf 是空指针,则 getcwd() 的行为未指定。

\n
\n\n

如果我们在 slime 中或从存储库中获得此​​功能的帮助

\n\n
\n

“以字符串形式返回进程的当前工作目录。”

\n
\n\n

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

\n\n
*default-pathname-defaults*\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
\xe2\x95\xad\xe2\x94\x80anquegi@toshiba-debian  ~/learn/lisp/StackOverFlow/testDirs \xe2\x80\xb9ruby-2.2.1@laguna\xe2\x80\xba \n\xe2\x95\xb0\xe2\x94\x80$ tree                                                                                                                                        148 \xe2\x86\xb5\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dirA\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dirB\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 factorial.lisp\n\n2 directories, 1 file\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后我们就开始粘液

\n\n
; SLIME 2015-06-01\nCL-USER> (sb-posix:getcwd)\n"/home/anquegi/learn/lisp/StackOverFlow/testDirs"\nCL-USER> *default-pathname-defaults*\n#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/"\nCL-USER> (sb-posix:chdir "dirB")\n0\nCL-USER> (sb-posix:getcwd)\n"/home/anquegi/learn/lisp/StackOverFlow/testDirs/dirB"\nCL-USER> *default-pathname-defaults*\n#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/"\nCL-USER> (setf *default-pathname-defaults* (sb-ext:native-pathname (format nil "~A~A" (sb-posix:getcwd) "/"))) \n#P"/home/anquegi/learn/lisp/StackOverFlow/testDirs/dirB/"\nCL-USER> (load "factorial")\nT\nCL-USER> (factorial 3)\n6\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n