r4727: *** empty log message ***
[uffi.git] / tests / 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: getenv.lisp,v 1.3 2003/05/01 23:31:40 kevin Exp $
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 :uffi-tests)
20
21
22 (uffi:def-function ("getenv" c-getenv) 
23     ((name :cstring))
24   :returning :cstring)
25
26 (uffi:def-function ("setenv" c-setenv) 
27     ((name :cstring)
28      (value :cstring)
29      (overwrite :int))
30   :returning :int)
31
32 (uffi:def-function ("unsetenv" c-unsetenv)
33     ((name :cstring))
34   :returning :void)
35
36 (defun my-getenv (key)
37   "Returns an environment variable, or NIL if it does not exist"
38   (check-type key string)
39   (uffi:with-cstring (key-native key)
40     (uffi:convert-from-cstring (c-getenv key-native))))
41
42 (defun my-setenv (key name &optional (overwrite t))
43   "Returns an environment variable, or NIL if it does not exist"
44   (check-type key string)
45   (check-type name string)
46   (setq overwrite (if overwrite 1 0))
47   (uffi:with-cstrings ((key-native key)
48                        (name-native name))
49     (c-setenv key-native name-native (if overwrite 1 0))))
50
51 (defun my-unsetenv (key)
52   "Returns an environment variable, or NIL if it does not exist"
53   (check-type key string)
54   (uffi:with-cstrings ((key-native key))
55     (c-unsetenv key-native)))
56
57 (deftest getenv.1 (progn
58                     (my-unsetenv "__UFFI_FOO1__")
59                     (my-getenv "__UFFI_FOO1__"))
60   nil)
61 (deftest getenv.2 (progn
62                     (my-setenv "__UFFI_FOO1__" "UFFI-TEST")
63                     (my-getenv "__UFFI_FOO1__"))
64   "UFFI-TEST")
65
66
67