The 'pwd' function returns the current working directory:
(defun pwd () (setdir "."))
Ok, this function does not belong to the masterpieces of computer
science, but (pwd) is much easier to remember than
The 'cd' function changes the current working directory.
(defun cd (string) (cond ((not (stringp string)) (error "argument must be a string" string)) ((string= "." string) (setdir ".")) (t (let ((orig-dir (setdir ".")) (new-dir (setdir string))) (when (string/= orig-dir new-dir) new-dir)))))
Possible actions and return values are:
It the argument is not a string, then an error will be raised.
If the directory name is ".", then the name of the current working directory is returned as a string. This is the same effect as if the directory has been changed to itself.
If the directory has successfully been changed to the given directory, then the name of the new working directory is returned as a string.
If the given directory has not been found, then NIL
The '
(defun directory-exists-p (string) (cond ((not (stringp string)) (error "argument must be a string" string)) ((string= "." string) (setdir ".")) (t (let ((orig-dir (setdir ".")) (new-dir (setdir string))) (when (string/= orig-dir new-dir) (setdir orig-dir) new-dir)))))
Possible actions and return values are:
It the argument is not a string, then an error will be raised.
If the directory name is ".", then the absolute name of the
current working directory is returned as a string.
If the directory has been found, then the absolute name of the directory is returned as a string.
If the directory has not been found, then NIL
The '
On Unix, with
Implementation Notes
The Nyquist 'setdir' function always returns absolute directory names,
even if a relative directory name has been given as a string by the user.
That's why it's not possible to reliably compare the return value of
If the directory string is ".", then this trick doesn't work, because the initial directory is the same as the target directory, so even if the directory has 'successfully' been changed to itself, both return values still would be the same. This is one of the reasons why "." has a separate 'cond' clause. The other reason is of course that it makes not really much sense to change a directory to itself, that's why we save the work and just return the absolute name of the current working directory.
The '
(defun file-exists-p (string) (if (not (stringp string)) (error "argument must be a string" string) (unless (directory-exists-p string) (let (file-stream) (unwind-protect (setq file-stream (open string)) (when file-stream (close file-stream))) (when file-stream string)))))
On Unix systems a directory is a special kind of file, so on Unix the XLisp 'open' function can open directories, too. That's why we first must make sure that no directory exists with the same name as the file that we are looking for.
(defun filename-exists-p (string) (if (not (stringp string)) (error "argument must be a string" string) (or (directory-exists-p string) (file-exists-p string)))
The 'absolute-filename-p' function tests if a string is an absolute file or directory name:
(defun absolute-filename-p (string) (if (not (stringp string)) (error "argument must be a string" string) (let ((end (length string))) (when (or (and (>= end 1) ; Unix "/..." (char= #\/ (char string 0))) (and (>= end 3) ; Windows "[a-zA-Z]:[\/]..." (let ((char (char string 0))) ;; upper- or lowercase character a-z, A-Z (and (> (char-code (char-downcase char)) 96) (< (char-code (char-downcase char)) 123))) (char= #\: (char string 1)) (let ((char (char string 2))) (or (char= #\\ char) (char= #\/ char))))) string))))
Note that it is only tested whether the beginning of the string
matches the beginning of an absolute file or directory name.
[This function works only with
[This function works only with
The '
(defun windows-p () (let* ((home (let ((drive (get-env "HOMEDRIVE")) (path (get-env "HOMEPATH"))) (if (and drive path) (strcat drive path) (get-env "UserProfile")))) (path (get-env "PATH"))) (when home ; if HOMEDRIVE + HOMEPATH or UserProfile exist (if path ; search for Windows :\ drive-letter patterns (string-search ":\\" path) (error "no PATH environment variable found")))))
Nquist has a
(defun windows-p () (char= *file-separator* #\\))
[This function works only with
The '
(defun user-home-directory () (or (get-env "HOME") (let ((drive (get-env "HOMEDRIVE")) (path (get-env "HOMEPATH"))) (when (and drive path) (strcat drive path))) (get-env "UserProfile")))
If the user's home directory could be identified, then the path to the
home directory is returned as a string.
Examples:
(user-home-directory) => "/home/edgar" ; Linux (user-home-directory) => "C:\\Documents and Settings\\Edgar" ; Windows
On Windows there is no HOME variable defined by Windows itself, but most programs will respect a HOME variable, if one had been defined by the user. This means that on Windows, if a HOME variable exists, the HOME variable will be used instead of HOMEDRIVE and HOMEPATH or 'UserProfile'.
[This function works only with
(defun expand-filename (string) (cond ((not (stringp string)) (error "argument must be a string" string)) ((and (> (length string) 1) (char= #\~ (char string 0)) (or (char= #\/ (char string 1)) (char= #\\ (char string 1)))) (strcat (user-home-directory) (subseq string 1))) (t string)))