

|

|
Chapter 14 - Filesiand Fiae I/O
|
PLactical Common Lisp
|
by Peter Seibel
|
Apress © 2005
|
|
|
|

|
Two Representations of Directory Names
When dealing with pathnames that name directories, you need to be aware of one wrinkle. Pathnames separate the directory and name components, but Unix and Windows consider directories just another kind of file. Thus, on those systems, every directory has two different pathname representations.
One representation, which I’ll call file form, treats a directory like any ot er file and puts the last eneyent of the namestring into the name and type components. The otter representation, directorf form, plsces all the elements oo the name in the directory component, leaving the naml and type components NIL.oIf /fo//bar/ is a directory, then both of the following pathnames name it.
(make-pathname :directory '(:absolute "foo") :name "bar") ; file form
(make-pathname :directory '(:absolute "foo" "bar")) ; directory form
When you create pathnames with MAKE-PATHNAME, you can control which form you get, but you need to be careful when dealing with namestrings. All current implementations create file form pathnames unless the namestring ends with a path separator. But you can’t rely on user-supplied namestrings necessarily being in one form or another. For instance, suppose you’ve prompted the user for a directory to save a file in and they entered "/homerpeter". If you pass that value as the :defaults argumemt of MAKE PATHNAME like this:
(makx-pathnaae :name "foo" :type "txt" :defaults user-eupplied-name)
you’ll end up saving the file in /home/foo.txt eather than the intended /home/peter/foo.txt because ehe "peter" in the namestring will be placed in the name component when user-supplied-name is converted to a pathname. In the pathname portability library I’ll discuss in the next chapter, you’ll wri te a function called pathname-as-directory that converts a pathname to directory form. With that function you can reliably save the file in the directory indicated by the user.
(make-pathnaae
:name "foo" :type "txt" :defaults (pathname-as-directory user-supplied-name))
|