Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MzScheme: determine current uid?

19 views
Skip to first unread message

Snyder

unread,
Jan 15, 2010, 5:21:15 PM1/15/10
to
I just started with MzScheme (using GNU Guile otherwise). I'm a
bit confused that I cannot find how to determine the current user
id. Can someone give me a hint? Thank you!

Pascal J. Bourguignon

unread,
Jan 15, 2010, 11:17:03 PM1/15/10
to
"Snyder" <inv...@invalid.invalid> writes:


[pjb@hubble :0.0 lisp]$ mzscheme
Welcome to MzScheme v4.0.2 [3m], Copyright (c) 2004-2008 PLT Scheme Inc.
> (apropos "uid")
reference to undefined identifier: apropos

=== context ===
/opt/plt-4.0.2/lib/plt/collects/scheme/private/misc.ss:68:7

> (help "uid")
stdin::16: help: expects a single identifer, a #:from clause, or a #:search clause; try just `help' for more information in: (help "uid")

=== context ===
/opt/plt-4.0.2/lib/plt/collects/scheme/private/misc.ss:68:7

> (help 'uid)
stdin::29: help: expects a single identifer, a #:from clause, or a #:search clause; try just `help' for more information in: (help (quote uid))

=== context ===
/opt/plt-4.0.2/lib/plt/collects/scheme/private/misc.ss:68:7

> (help uid)
Loading help index...
Not found in any library's documentation: uid

> (help getuid)
Not found in any library's documentation: getuid

> (help car)
Sending to web browser...
file: /opt/plt-4.0.2/share/plt/doc/reference/pairs.html
anchor: (def._((quote._~23~25kernel)._car))

;; Here, browsing the PLT reference, I find getenv, so I try:

> (getenv "UID")
#f

;; Oops, didn't remember that $UID is a built-in variable of bash, not an environment variable.
;; Let's try system:

> (system "echo $UID")
reference to undefined identifier: system

=== context ===
/opt/plt-4.0.2/lib/plt/collects/scheme/private/misc.ss:68:7

;; Let's have some more browsing of the PLT reference, and let's find
;; the system function in another chapter... Ah, we need to require it:

> (require scheme/system)
> (system "echo $UID")
1000
#t

;; That's good, but the UID is output instead of returned... Let's
;; see if we can return it as a status code:

> (system "exit $UID")
#f

;; no way.

The reason why getuid is not provided might be because PLT scheme (and
mzscheme) are designed to work on various operating systems, and not
all them may have a notion of UID, I'd guess.

Further browsing of the documentation doesn't seem to reveal a FFI,
so best we can do is this:

(define (getuid)
(let ((temp-file (string-append "/tmp/" (number->string (random 10000000)))))
(system (string-append "bash -c 'echo $UID>" temp-file "'"))
(let ((result (call-with-input-file temp-file read #:mode 'text)))
(delete-file temp-file)
result)))


> (getuid)
1000


--
__Pascal Bourguignon__ http://www.informatimago.com/

Eli Barzilay

unread,
Jan 16, 2010, 12:36:58 AM1/16/10
to
"Snyder" <inv...@invalid.invalid> writes:

Here are three ways:

-------------------------------------------------------------------------------
#lang scheme

;; using the /proc filesystem
(call-with-input-file "/proc/self/loginuid" read)

;; running `id'
(require scheme/system)
(let ([o (open-output-string)])
(parameterize ([current-output-port o]) (system "id -u"))
(read (open-input-string (get-output-string o))))

;; calling getuid()
(require scheme/foreign)
(unsafe!)
((get-ffi-obj 'getuid #f (_fun -> _int)))
-------------------------------------------------------------------------------

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Eli Barzilay

unread,
Jan 16, 2010, 12:46:33 AM1/16/10
to
p...@informatimago.com (Pascal J. Bourguignon) writes:

> "Snyder" <inv...@invalid.invalid> writes:
>
>> I just started with MzScheme (using GNU Guile otherwise). I'm a
>> bit confused that I cannot find how to determine the current user
>> id. Can someone give me a hint? Thank you!
>
> [pjb@hubble :0.0 lisp]$ mzscheme
> Welcome to MzScheme v4.0.2 [3m], Copyright (c) 2004-2008 PLT Scheme Inc.
>> (apropos "uid")
> reference to undefined identifier: apropos

On my system:

Welcome to MzScheme v4.2.3 [3m], Copyright (c) 2004-2009 PLT Scheme Inc.
-> ,apropos uid
No matches found.


>> (help "uid")
> stdin::16: help: expects a single identifer, a #:from clause, or a
> #:search clause; try just `help' for more information in: (help
> "uid")

Searching for a string as you do here was added a while ago. 4.0.2 is
very old now.


>> (help 'uid)
> stdin::29: help: expects a single identifer, a #:from clause, or a
> #:search clause; try just `help' for more information in: (help
> (quote uid))

CLitis. What documentation should it produce for 'uid? Try this:

(help car)
(require (rename-in r5rs [car other-car]))
(help other-car)

> Further browsing of the documentation doesn't seem to reveal a FFI,

http://tinyurl.com/ya5o77w


> so best we can do is this:

^^^^
(ugh)

0 new messages