Remove old CVS $Id$ keyword
[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 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package #:uffi-tests)
15
16
17 (uffi:def-function ("getenv" c-getenv)
18     ((name :cstring))
19   :returning :cstring)
20
21 (uffi:def-function ("setenv" c-setenv)
22     ((name :cstring)
23      (value :cstring)
24      (overwrite :int))
25   :returning :int)
26
27 (uffi:def-function ("unsetenv" c-unsetenv)
28     ((name :cstring))
29   :returning :void)
30
31 (defun my-getenv (key)
32   "Returns an environment variable, or NIL if it does not exist"
33   (check-type key string)
34   (uffi:with-cstring (key-native key)
35     (uffi:convert-from-cstring (c-getenv key-native))))
36
37 (defun my-setenv (key name &optional (overwrite t))
38   "Returns an environment variable, or NIL if it does not exist"
39   (check-type key string)
40   (check-type name string)
41   (setq overwrite (if overwrite 1 0))
42   (uffi:with-cstrings ((key-native key)
43                        (name-native name))
44     (c-setenv key-native name-native (if overwrite 1 0))))
45
46 (defun my-unsetenv (key)
47   "Returns an environment variable, or NIL if it does not exist"
48   (check-type key string)
49   (uffi:with-cstrings ((key-native key))
50     (c-unsetenv key-native)))
51
52 (deftest :getenv.1 (progn
53                     (my-unsetenv "__UFFI_FOO1__")
54                     (my-getenv "__UFFI_FOO1__"))
55   nil)
56 (deftest :getenv.2 (progn
57                     (my-setenv "__UFFI_FOO1__" "UFFI-TEST")
58                     (my-getenv "__UFFI_FOO1__"))
59   "UFFI-TEST")
60
61
62