getting absolute pathname from pathname

Started by cormullion, March 21, 2006, 04:33:17 AM

Previous topic - Next topic

Lutz

#15

newLISP v.8.8.3 on OSX UTF-8, execute 'newlisp -h' for more info.

> (realpath)
"/Users/lutz/newlisp-8.8.3"
> (realpath "~/newlisp")
nil
> (realpath "./newlisp")
"/Users/lutz/newlisp-8.8.3/newlisp"
> (realpath "../newlisp")
"/Users/lutz/newlisp"
> (realpath "newlisp.c")
"/Users/lutz/newlisp-8.8.3/newlisp.c"
> (realpath ".")
"/Users/lutz/newlisp-8.8.3"
> (realpath "..")
"/Users/lutz"
>


realpath history:



FreeBSD: 4.3

Linux: libc 4.5.4

The  libc4 and libc5 implementation contains a buffer overflow (fixed in libc-5.4.13).

Solaris: at least since 1999



Lutz

cormullion

#16
Yes! I think that's great. Even easier to use than getwd/current-dir, and very useful for me.

statik

#17
Any idea when (cwd) is gonna make into newlisp? Next release? Release after that? Eventually?



Also, PWD is an environtment variable, and you can clearly see that by doing the following at the command line:



$ set


Is there a reason why (env) does not utilize ALL available environment variables?
-statik

cormullion

#18
Doesn't real-path do this (cwd) for you?

Lutz

#19
Yes, look into 'real-path in 8.8.3, its like a 'cwd' on steroids because it also translates relative paths (based on UNIX realpath). A 'cwd' would be (real-path) or (real-path "."), which is the same.
Quote
is there a reason why (env) does not utilize ALL available environment variables?


When Unix starts a process, i.e. newLISP, it supplies it only with a subset of environment variables. What you see using 'env' is all you have avaiable in your process space, but you could use the following method to get all environment variablers in the shell you are running:



(map (fn (e) (parse e "=")) (exec "set"))


This gives you an association list:



(
...
("BASH_VERSION" "'2.05b.0(1)-release'")
("DIRSTACK" "()")
("EUID" "501")
("GROUPS" "()")
("HOME" "/Users/lutz")
...
)


and you could use 'lookup' on it to get a certain value:



> (set 'my-env (map (fn (e) (parse e "=")) (exec "set")))

> (lookup "PPID" my-env)
"29039"
>


Lutz

statik

#20
I didn't realize you had implemented realpath into the latest dev... I'll check it out. Thanks.
-statik

statik

#21
Does anyone know how I can get the cwd of the file being executed, or a way to get the cwd of any given process?



My problem is that realpath shows the working directory from where newlisp was started.



Example:



$ pwd
/home/statik/
$ cat code/test.lsp
(println (real-path))
(exit)
$ newlisp code/test.lsp
/home/statik/
$


I'd like a way to find the current working directory of the script being executed. Any ideas?
-statik

Lutz

#22
The location of the script is always the second member in (main-args):



~> pwd
/Users/lutz
~> cat /usr/bin/mytest
#!/usr/bin/newlisp

(println "real-path -> " (real-path))
(println "script caller -> " (main-args 0))
(println "script -> " (main-args 1))

(exit)

~> mytest

real-path -> /Users/lutz
script caller -> /usr/bin/newlisp
script -> /usr/bin/mytest
~>


Lutz

statik

#23
That's fine and all, but how does one handle files that are loaded via (load)? How do those file determine where they are?
-statik