72201e240b8ec710d2e3646af582e3eb4750023b
[uffi.git] / examples / getenv.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          getenv.cl
6 ;;;; Purpose:       UFFI Example file to get environment variable
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package :cl-user)
20
21
22 (uffi:def-function ("getenv" c-getenv) 
23     ((name :cstring))
24   :returning :cstring)
25
26 (defun my-getenv (key)
27   "Returns an environment variable, or NIL if it does not exist"
28   (check-type key string)
29   (uffi:with-cstring (key-native key)
30     (uffi:convert-from-cstring (c-getenv key-native))))
31     
32 #+examples-uffi
33 (progn
34   (flet ((print-results (str)
35            (format t "~&(getenv ~S) => ~S" str (my-getenv str))))
36     (print-results "USER")
37     (print-results "_FOO_")))
38
39
40 #+test-uffi
41 (progn
42   (util.test:test (my-getenv "_FOO_") nil :fail-info "Error retrieving non-existent getenv")
43   (util.test:test (and (stringp (my-getenv "USER"))
44                        (< 0 (length (my-getenv "USER"))))
45                   t :fail-info "Error retrieving getenv")
46 )
47