[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

surprised that sb-ext:posix-getenv is not setf-able

Jim Newton

5/26/2016 8:53:00 AM

Does someone know why sb-ext:posix-getenv is not setf-able? I expected to be
able to set a UNIX environment variable with:

(setf (sb-ext:posix-getenv "XYZZY") "12")

but alas, it didn't work.
9 Answers

Jim Newton

5/26/2016 8:56:00 AM

0

When I look at the source code, unix.lisp, I don't see any code for setting an environment variable.
Is this something posix supports?

Jim Newton

5/26/2016 10:07:00 AM

0

I found a description of this in "Common Lisp Recipes", the new Edi Weitz book.

It turns out in SBCL you do the following.

(sb-posix:setenv "XYZZY" "12" 1)

I.e. it's not a function in the sb-ext package, but rather in the sb-posix package, getenv is not setf-able, and takes a mysterious 3rd argument.

However, the corresponding function in clisp, ext:getenv is setf-able, also in AllegroCL.

Teemu Likonen

5/26/2016 10:47:00 AM

0

Jim Newton [2016-05-26 03:06:32-07] wrote:

> It turns out in SBCL you do the following.
>
> (sb-posix:setenv "XYZZY" "12" 1)
>
> I.e. it's not a function in the sb-ext package, but rather in the
> sb-posix package, getenv is not setf-able, and takes a mysterious 3rd
> argument.

SB-POSIX package maps quite directly to POSIX functions and variables.
That mysterious third argument is named "overwrite" (integer). See
below.


$ man 3 setenv

[...]

SYNOPSIS
#include <stdlib.h>

int setenv(const char *name, const char *value, int overwrite);

[...]

DESCRIPTION
The setenv() function adds the variable name to the environment with
the value value, if name does not already exist. If name does exist
in the environment, then its value is changed to value if overwrite
is nonzero; if overwrite is zero, then the value of name is not
changed (and setenv() returns a success status). This function makes
copies of the strings pointed to by name and value (by contrast with
putenv(3)).



--
/// Teemu Likonen - .-.. <https://github.com/tl... //
// PGP: 4E10 55DC 84E9 DFF6 13D7 8557 719D 69D3 2453 9450 ///

Jim Newton

5/26/2016 11:33:00 AM

0

Is the existence of the 3rd argument the reason the function is not setf-able?

Teemu Likonen

5/26/2016 12:54:00 PM

0

Jim Newton [2016-05-26 04:32:43-07] wrote:

> Is the existence of the 3rd argument the reason the function is not
> setf-able?

No. I believe that SB-POSIX package wants to be very similar to POSIX C
interface. It doesn't use fancy Lisp features (it uses conditions
though) but those who know POSIX should feel quite comfortable.
Functions' arguments and return values are sometimes a bit un-Lispy but
similar to how the C interface works. That's probably a good thing for
well-known interface like POSIX.

--
/// Teemu Likonen - .-.. <https://github.com/tl... //
// PGP: 4E10 55DC 84E9 DFF6 13D7 8557 719D 69D3 2453 9450 ///

Kaz Kylheku

5/26/2016 1:32:00 PM

0

On 2016-05-26, Jim Newton <jimka.issy@gmail.com> wrote:
> I found a description of this in "Common Lisp Recipes", the new Edi Weitz book.
>
> It turns out in SBCL you do the following.
>
> (sb-posix:setenv "XYZZY" "12" 1)
> I.e. it's not a function in the sb-ext package, but rather in the sb-posix package, getenv is not setf-able, and takes a mysterious 3rd argument.

Possibly why they are in different package might be that
getenv is a standard C function. Has been since 1989 ANSI C.
setenv is a POSIX extension to C.

Kaz Kylheku

5/26/2016 1:40:00 PM

0

On 2016-05-26, Jim Newton <jimka.issy@gmail.com> wrote:
> Is the existence of the 3rd argument the reason the function is not
> setf-able?

No, because the accessor could just be made such that the third
argument is implicitly true (do overwrite).

There is simply insufficient utility in setf-able environment
variables.

First of all, they can only hold strings. So uses like the following
are out:

(incf (getenv "FOO"))
(push item (getenv "FOO"))

Would you ever require:

(rotatef (getenv "VAR1") (getenv "VAR2"))

In any case, the implementation gives you the API to manipulate
environment variables; if you want to do these additional things, you
can easily roll your own.

Pascal J. Bourguignon

5/26/2016 5:44:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Is the existence of the 3rd argument the reason the function is not setf-able?

No, you could write a single signature for both:

(declaim (ftype function %setenv %getenv))

(defun getenv (name)
(%getenv name))

(defun (setf getenv) (value NAME &optional (overwrite t))
(%setenv name value (if overwrite 1 0)))

(defun update ()
(setf (getenv "PATH" t) (concatenate 'string "/bin:" (getpath "PATH"))))



--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Jim Newton

5/27/2016 9:49:00 AM

0

Is the lack of examples how a user might use setf really a good reason for
omitting it. An advantage obvious of setf is that the programmer does
not have to remember a different getter and setter function name. I programmed
many years in a lisp which does not support setf, and believe me it is
something you'd miss.

The fact that clisp and AllegroCL both make it setf-able means I'm not
the only person who thinks it makes a more consistent interface.

Obviously I can work around the shortcoming, on a case by case basis.

On Thursday, May 26, 2016 at 3:40:23 PM UTC+2, Kaz Kylheku wrote:
>
> There is simply insufficient utility in setf-able environment
> variables.
>