043e7fca72ed6b93d0704d344f0c0dff05ec1e5f
[uffi.git] / examples / getenv.cl
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:  Mar 2002
9 ;;;;
10 ;;;; Copyright (c) 2002 Kevin M. Rosenberg
11 ;;;;
12 ;;;; $Id: getenv.cl,v 1.6 2002/03/10 17:42:35 kevin Exp $
13 ;;;;
14 ;;;; This file is part of UFFI. 
15 ;;;;
16 ;;;; UFFI is free software; you can redistribute it and/or modify
17 ;;;; it under the terms of the GNU General Public License (version 2) as
18 ;;;; published by the Free Software Foundation.
19 ;;;;
20 ;;;; UFFI is distributed in the hope that it will be useful,
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;;;; GNU General Public License for more details.
24 ;;;;
25 ;;;; You should have received a copy of the GNU General Public License
26 ;;;; along with UFFI; if not, write to the Free Software Foundation, Inc.,
27 ;;;; 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 ;;;; *************************************************************************
29
30 (in-package :cl-user)
31
32
33 (uffi:def-function ("getenv" c-getenv) 
34     ((name :cstring))
35   :returning :cstring)
36
37 (defun my-getenv (key)
38   "Returns an environment variable, or NIL if it does not exist"
39   (check-type key string)
40   (uffi:with-cstring (key-native key)
41     (uffi:convert-from-cstring (c-getenv key-native))))
42     
43 #+test-uffi
44 (progn
45   (flet ((print-results (str)
46            (format t "~&(getenv ~S) => ~S" str (my-getenv str))))
47     (print-results "USER")
48     (print-results "_FOO_")))
49