Remove old CVS $Id$ keyword
[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 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package :cl-user)
15
16
17 (uffi:def-function ("getenv" c-getenv)
18     ((name :cstring))
19   :returning :cstring)
20
21 (defun my-getenv (key)
22   "Returns an environment variable, or NIL if it does not exist"
23   (check-type key string)
24   (uffi:with-cstring (key-native key)
25     (uffi:convert-from-cstring (c-getenv key-native))))
26
27 #+examples-uffi
28 (progn
29   (flet ((print-results (str)
30            (format t "~&(getenv ~S) => ~S" str (my-getenv str))))
31     (print-results "USER")
32     (print-results "_FOO_")))
33
34
35 #+test-uffi
36 (progn
37   (util.test:test (my-getenv "_FOO_") nil :fail-info "Error retrieving non-existent getenv")
38   (util.test:test (and (stringp (my-getenv "USER"))
39                        (< 0 (length (my-getenv "USER"))))
40                   t :fail-info "Error retrieving getenv")
41 )
42