X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=tests%2Fgetenv.lisp;h=fd784488aeeabcf22c5cf513b3ecf5e5718fa738;hb=HEAD;hp=17d2758be1fe504343b059da0a7c2f346f717f0c;hpb=a95b9a217335917d96b8c0cced4f49c3e4846115;p=uffi.git diff --git a/tests/getenv.lisp b/tests/getenv.lisp index 17d2758..fd78448 100644 --- a/tests/getenv.lisp +++ b/tests/getenv.lisp @@ -2,46 +2,61 @@ ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; -;;;; Name: getenv.cl +;;;; Name: getenv.lisp ;;;; Purpose: UFFI Example file to get environment variable ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id: getenv.lisp,v 1.1 2002/09/30 10:02:36 kevin Exp $ +;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg ;;;; -;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg -;;;; -;;;; UFFI users are granted the rights to distribute and use this software -;;;; as governed by the terms of the Lisp Lesser GNU Public License -;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL. ;;;; ************************************************************************* -(in-package :cl-user) +(in-package #:uffi-tests) -(uffi:def-function ("getenv" c-getenv) +(uffi:def-function ("getenv" c-getenv) ((name :cstring)) :returning :cstring) +(uffi:def-function ("setenv" c-setenv) + ((name :cstring) + (value :cstring) + (overwrite :int)) + :returning :int) + +(uffi:def-function ("unsetenv" c-unsetenv) + ((name :cstring)) + :returning :void) + (defun my-getenv (key) "Returns an environment variable, or NIL if it does not exist" (check-type key string) (uffi:with-cstring (key-native key) (uffi:convert-from-cstring (c-getenv key-native)))) - -#+examples-uffi -(progn - (flet ((print-results (str) - (format t "~&(getenv ~S) => ~S" str (my-getenv str)))) - (print-results "USER") - (print-results "_FOO_"))) - - -#+test-uffi -(progn - (util.test:test (my-getenv "_FOO_") nil :fail-info "Error retrieving non-existent getenv") - (util.test:test (and (stringp (my-getenv "USER")) - (< 0 (length (my-getenv "USER")))) - t :fail-info "Error retrieving getenv") -) + +(defun my-setenv (key name &optional (overwrite t)) + "Returns an environment variable, or NIL if it does not exist" + (check-type key string) + (check-type name string) + (setq overwrite (if overwrite 1 0)) + (uffi:with-cstrings ((key-native key) + (name-native name)) + (c-setenv key-native name-native (if overwrite 1 0)))) + +(defun my-unsetenv (key) + "Returns an environment variable, or NIL if it does not exist" + (check-type key string) + (uffi:with-cstrings ((key-native key)) + (c-unsetenv key-native))) + +(deftest :getenv.1 (progn + (my-unsetenv "__UFFI_FOO1__") + (my-getenv "__UFFI_FOO1__")) + nil) +(deftest :getenv.2 (progn + (my-setenv "__UFFI_FOO1__" "UFFI-TEST") + (my-getenv "__UFFI_FOO1__")) + "UFFI-TEST") + +