r10563: add module keyword to def-function
[uffi.git] / tests / getenv.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          getenv.lisp
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-2003 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi-tests)
17
18
19 (uffi:def-function ("getenv" c-getenv) 
20     ((name :cstring))
21   :module "c"
22   :returning :cstring)
23
24 (uffi:def-function ("setenv" c-setenv) 
25     ((name :cstring)
26      (value :cstring)
27      (overwrite :int))
28   :module "c"
29   :returning :int)
30
31 (uffi:def-function ("unsetenv" c-unsetenv)
32     ((name :cstring))
33   :module "c"
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